Skip to content

Commit 0cbef16

Browse files
authored
Fix regex delimiter detection in syntax highlighter (#16394)
1 parent abbfbf7 commit 0cbef16

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

spec/std/crystal/syntax_highlighter/colorize_spec.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ describe Crystal::SyntaxHighlighter::Colorize do
127127

128128
it_highlights "Set{1, 2, 3}", %(\e[36mSet\e[39m{\e[35m1\e[39m, \e[35m2\e[39m, \e[35m3\e[39m})
129129

130+
it_highlights "foo(/Name: /)", %(foo(\e[93m/Name: /\e[39m))
131+
it_highlights "foo[/Name: /]", %(foo[\e[93m/Name: /\e[39m])
132+
it_highlights "Foo{/Name: /}", %(\e[36mFoo\e[39m{\e[93m/Name: /\e[39m})
133+
130134
it_highlights <<-CRYSTAL, <<-ANSI
131135
foo, bar = <<-FOO, <<-BAR
132136
foo

spec/std/crystal/syntax_highlighter/html_spec.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ describe Crystal::SyntaxHighlighter::HTML do
122122

123123
it_highlights "Set{1, 2, 3}", %(<span class="t">Set</span>{<span class="n">1</span>, <span class="n">2</span>, <span class="n">3</span>})
124124

125+
it_highlights "foo(/Name: /)", %(foo(<span class="s">/Name: /</span>))
126+
it_highlights "foo[/Name: /]", %(foo[<span class="s">/Name: /</span>])
127+
it_highlights "Foo{/Name: /}", %(<span class="t">Foo</span>{<span class="s">/Name: /</span>})
128+
125129
it_highlights <<-CRYSTAL, <<-HTML
126130
foo, bar = <<-FOO, <<-BAR
127131
foo

src/crystal/syntax_highlighter.cr

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,16 @@ abstract class Crystal::SyntaxHighlighter
7070
end
7171

7272
private def slash_is_not_regex(last_token_type type, space_before)
73-
return nil if type.nil?
74-
75-
type.number? || type.const? || type.instance_var? ||
76-
type.class_var? || type.op_rparen? ||
77-
type.op_rsquare? || type.op_rcurly? || !space_before
73+
case type
74+
when Nil
75+
false
76+
when .number?, .const?, .instance_var?, .class_var?, .op_rparen?, .op_rsquare?, .op_rcurly?
77+
true
78+
when .op_lparen?, .op_lsquare?, .op_lcurly?
79+
false
80+
else
81+
!space_before
82+
end
7883
end
7984

8085
private def highlight_normal_state(lexer, break_on_rcurly = false)

0 commit comments

Comments
 (0)