Skip to content

Commit 327071f

Browse files
authored
Fix highlighting comparable method definition (#2149)
Previously, when trying to highlight the definition of the comparable method (aka the spaceship, `<=>`), the `<` would be highlighted as `Name::Function` but the `=>` would be `Operator`. This happens because of the ordering of potential matches in the `funcname` regular expression: ```ruby %r( # snip <<? | >>? | <=>? # snip ) ``` The method name is always caught by the single `<` match, and so never makes it to the `<=>` possibility. This commit fixes the issue by trying to match `<=>?` before `<<?`. This ensures it _can_ be matched, and since none of the following potential matches are a superset of its (`<=>?`) matches it won't cause any issue with subsequent possibilities.
1 parent 3b461b1 commit 327071f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/rouge/lexers/ruby.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ def self.detect?(text)
297297
(
298298
[\p{L}_]\p{Word}*[!?]? |
299299
\*\*? | [-+]@? | [/%&\|^`~] | \[\]=? |
300-
<<? | >>? | <=>? | >= | ===?
300+
<=>? | <<? | >>? | >= | ===?
301301
)
302302
)x do |m|
303303
puts "matches: #{[m[0], m[1], m[2], m[3]].inspect}" if @debug

spec/lexers/ruby_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,20 @@
8989
['Literal.Number.Float', '20.4e+8']
9090
end
9191
end
92+
93+
describe 'method definition' do
94+
it 'identifies comparable method' do
95+
assert_tokens_equal "def <=>(o); end",
96+
["Keyword", "def"],
97+
["Text", " "],
98+
["Name.Function", "<=>"],
99+
["Punctuation", "("],
100+
["Name", "o"],
101+
["Punctuation", ");"],
102+
["Text", " "],
103+
["Keyword", "end"]
104+
end
105+
end
92106
end
93107

94108
describe 'guessing' do

0 commit comments

Comments
 (0)