Skip to content

JavaScript scanner: Highlight multi-line comments in diff correctly #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changes.textile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ h2. Changes in 1.1

* New scanner: Sass [#93]
* Diff scanner: Highlight inline changes in multi-line changes [#99]
* JavaScript scanner: Highlight multi-line comments in diff correctly
* Remove double-click toggle handler from HTML table output
* Fixes to CSS scanner (floats, pseudoclasses)
* Plugin does not warn about fallback when default is defined
Expand Down
32 changes: 28 additions & 4 deletions lib/coderay/scanners/java_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,17 @@ class JavaScript < Scanner

protected

def setup
@state = :initial
end

def scan_tokens encoder, options

state = :initial
string_delimiter = nil
state, string_delimiter = options[:state] || @state
if string_delimiter
encoder.begin_group state
end

value_expected = true
key_expected = false
function_expected = false
Expand All @@ -72,9 +79,10 @@ def scan_tokens encoder, options
value_expected = true if !value_expected && match.index(?\n)
encoder.text_token match, :space

elsif match = scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
elsif match = scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .*() ) !mx)
value_expected = true
encoder.text_token match, :comment
state = :open_multi_line_comment if self[1]

elsif check(/\.?\d/)
key_expected = value_expected = false
Expand Down Expand Up @@ -176,19 +184,35 @@ def scan_tokens encoder, options
elsif match = scan(/ \\ | $ /x)
encoder.end_group state
encoder.text_token match, :error unless match.empty?
string_delimiter = nil
key_expected = value_expected = false
state = :initial
else
raise_inspect "else case #{string_delimiter} reached; %p not handled." % peek(1), encoder
end

when :open_multi_line_comment
if match = scan(%r! .*? \*/ !mx)
state = :initial
else
match = scan(%r! .+ !mx)
end
value_expected = true
encoder.text_token match, :comment if match

else
raise_inspect 'Unknown state', encoder
#:nocov:
raise_inspect 'Unknown state: %p' % [state], encoder
#:nocov:

end

end

if options[:keep_state]
@state = state, string_delimiter
end

if [:string, :regexp].include? state
encoder.end_group state
end
Expand Down