summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Changes.textile1
-rw-r--r--lib/coderay/scanners/java_script.rb32
2 files changed, 29 insertions, 4 deletions
diff --git a/Changes.textile b/Changes.textile
index 8d0dc59..f621de0 100644
--- a/Changes.textile
+++ b/Changes.textile
@@ -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
diff --git a/lib/coderay/scanners/java_script.rb b/lib/coderay/scanners/java_script.rb
index 92e3dfa..9eb0a0a 100644
--- a/lib/coderay/scanners/java_script.rb
+++ b/lib/coderay/scanners/java_script.rb
@@ -54,10 +54,17 @@ module Scanners
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
@@ -72,9 +79,10 @@ module Scanners
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
@@ -176,19 +184,35 @@ module Scanners
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