Skip to content

Commit ec11b2d

Browse files
Fix SyntaxHighlighter delimiter state (#15104)
The syntax highlighter needs to reset the lexer's previous `delimiter_state` after exiting a nested state.
1 parent 5aa1204 commit ec11b2d

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

spec/std/crystal/syntax_highlighter/html_spec.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,8 @@ describe Crystal::SyntaxHighlighter::HTML do
162162
it_highlights! "%w[foo"
163163
it_highlights! "%i[foo"
164164
end
165+
166+
# fix for https://forum.crystal-lang.org/t/question-about-the-crystal-syntax-highlighter/7283
167+
it_highlights %q(/#{l[""]}/
168+
"\\n"), %(<span class="s">/</span><span class="i">\#{</span>l[<span class="s">&quot;&quot;</span>]<span class="i">}</span><span class="s">/</span>\n <span class="s">&quot;\\\\n&quot;</span>)
165169
end

src/crystal/syntax_highlighter.cr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ abstract class Crystal::SyntaxHighlighter
8484
space_before = false
8585

8686
while true
87+
previous_delimiter_state = lexer.token.delimiter_state
88+
8789
token = lexer.next_token
8890

8991
case token.type
@@ -105,6 +107,7 @@ abstract class Crystal::SyntaxHighlighter
105107
highlight_token token, last_is_def
106108
else
107109
highlight_delimiter_state lexer, token
110+
token.delimiter_state = previous_delimiter_state
108111
end
109112
when .string_array_start?, .symbol_array_start?
110113
highlight_string_array lexer, token

0 commit comments

Comments
 (0)