Skip to content

Commit 3aa890c

Browse files
committed
Merge pull request rubychan#117 from rubychan/javascript-keep-state
JavaScript scanner: Highlight multi-line comments in diff correctly
2 parents a69d5d4 + 33f1a73 commit 3aa890c

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

Changes.textile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ h2. Changes in 1.1
66

77
* New scanner: Sass [#93]
88
* Diff scanner: Highlight inline changes in multi-line changes [#99]
9+
* JavaScript scanner: Highlight multi-line comments in diff correctly
910
* Remove double-click toggle handler from HTML table output
1011
* Fixes to CSS scanner (floats, pseudoclasses)
1112
* Plugin does not warn about fallback when default is defined

lib/coderay/scanners/java_script.rb

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,17 @@ class JavaScript < Scanner
5454

5555
protected
5656

57+
def setup
58+
@state = :initial
59+
end
60+
5761
def scan_tokens encoder, options
5862

59-
state = :initial
60-
string_delimiter = nil
63+
state, string_delimiter = options[:state] || @state
64+
if string_delimiter
65+
encoder.begin_group state
66+
end
67+
6168
value_expected = true
6269
key_expected = false
6370
function_expected = false
@@ -72,9 +79,10 @@ def scan_tokens encoder, options
7279
value_expected = true if !value_expected && match.index(?\n)
7380
encoder.text_token match, :space
7481

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

7987
elsif check(/\.?\d/)
8088
key_expected = value_expected = false
@@ -176,19 +184,35 @@ def scan_tokens encoder, options
176184
elsif match = scan(/ \\ | $ /x)
177185
encoder.end_group state
178186
encoder.text_token match, :error unless match.empty?
187+
string_delimiter = nil
179188
key_expected = value_expected = false
180189
state = :initial
181190
else
182191
raise_inspect "else case #{string_delimiter} reached; %p not handled." % peek(1), encoder
183192
end
184193

194+
when :open_multi_line_comment
195+
if match = scan(%r! .*? \*/ !mx)
196+
state = :initial
197+
else
198+
match = scan(%r! .+ !mx)
199+
end
200+
value_expected = true
201+
encoder.text_token match, :comment if match
202+
185203
else
186-
raise_inspect 'Unknown state', encoder
204+
#:nocov:
205+
raise_inspect 'Unknown state: %p' % [state], encoder
206+
#:nocov:
187207

188208
end
189209

190210
end
191211

212+
if options[:keep_state]
213+
@state = state, string_delimiter
214+
end
215+
192216
if [:string, :regexp].include? state
193217
encoder.end_group state
194218
end

0 commit comments

Comments
 (0)