Skip to content

Commit 162ccf9

Browse files
committed
Prevent error when styling Lua with no content; separate out whitespace in trailing fluff
1 parent 3c078ec commit 162ccf9

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

lib/coderay/scanners/lua.rb

+14-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ class CodeRay::Scanners::Lua < CodeRay::Scanners::Scanner
2929
]
3030

3131
SCANNER = /
32-
(?<space>\s*) # eat leading whitespace, just to make iteration of fluff easier
3332
(?<fluff>(?m).*?) # eat content up until something we want
3433
(?:
3534
(?<funcname>
@@ -147,17 +146,19 @@ class CodeRay::Scanners::Lua < CodeRay::Scanners::Scanner
147146

148147
def scan_tokens(tokens, options)
149148
# We use the block form of gsub instead of the StringScanner capabilities because StringScanner does not support named captures in 1.9
150-
match = nil
149+
remainder_index = 0
150+
151+
boring_kinds = [:space,:content]
152+
add_boring = ->(fluff) do
153+
fluff.split(/(\S+)/).each.with_index do |text,i|
154+
tokens.text_token(text, boring_kinds[i%2]) unless text.empty?
155+
end
156+
end
157+
151158
string.gsub(SCANNER) do
152159
match = $~
153-
tokens.text_token( match[:space], :space ) unless match[:space].empty?
154-
unless match[:fluff].empty?
155-
space = false
156-
match[:fluff].split(/(\s+)/).each do |piece|
157-
tokens.text_token(piece, space ? :space : :content)
158-
space = !space
159-
end
160-
end
160+
remainder_index = match.offset(0).last
161+
add_boring[match[:fluff]]
161162
if match[:funcname] && !match[:funcname].empty?
162163
f,s,n = match[:funcname].split(/(\s+)/)
163164
tokens.text_token(f,:keyword)
@@ -177,7 +178,9 @@ def scan_tokens(tokens, options)
177178
end
178179
end
179180
end
180-
tokens.text_token(match.post_match,:content) unless match.post_match.empty?
181+
unless remainder_index >= string.length
182+
add_boring[string[remainder_index..-1]]
183+
end
181184
tokens
182185
end
183186

0 commit comments

Comments
 (0)