Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 59 additions & 63 deletions languages/ruby/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"and"
"end"
"in"
"not"
"or"
] @keyword

Expand Down Expand Up @@ -95,37 +96,14 @@
(identifier) @variable.parameter
(optional_parameter
name: (identifier) @variable.parameter)
(keyword_parameter
[
name: (identifier)
":"
] @variable.parameter.keyword)
])

(block_parameters
(identifier) @variable.parameter)

; ERB strict locals are injected as Ruby, but their parameter list is not a
; valid standalone Ruby program. Match the parser's recovery shape so the
; first required local and the remaining keyword locals are highlighted alike.
((call
method: (identifier) @_locals
arguments: (argument_list
(parenthesized_statements
(call
method: (identifier) @variable.parameter.keyword))))
(#eq? @_locals "locals"))

((call
method: (identifier) @_locals
arguments: (argument_list
(parenthesized_statements
(call
arguments: (argument_list
(pair
key: (hash_key_symbol) @variable.parameter.keyword))))))
(#eq? @_locals "locals")
(#not-eq? @variable.parameter.keyword ""))
(keyword_parameter
name: (identifier) @variable.parameter.keyword
":" @variable.parameter.keyword)

; Identifiers
((identifier) @constant.builtin
Expand All @@ -138,7 +116,7 @@
(encoding) @constant.builtin

(hash_splat_nil
"**" @operator) @constant.builtin
"nil" @constant.builtin)

(constant) @type

Expand Down Expand Up @@ -189,6 +167,14 @@
(bare_symbol)
] @string.special.symbol

(pair
key: (_) @string.special.symbol
":" @string.special.symbol)

(keyword_pattern
key: (_) @string.special.symbol
":" @string.special.symbol)

(regex) @string.regex

(escape_sequence) @string.escape
Expand All @@ -209,59 +195,66 @@

(nil) @constant.builtin

; ERB strict locals are injected as Ruby, but their parameter list is not a
; valid standalone Ruby program. Match the parser's recovery shape so the
; first required local and the remaining keyword locals are highlighted alike.
((call
method: (identifier) @_locals
arguments: (argument_list
(parenthesized_statements
(call
method: (identifier) @variable.parameter.keyword))))
(#eq? @_locals "locals"))

((call
method: (identifier) @_locals
arguments: (argument_list
(parenthesized_statements
(call
arguments: (argument_list
(pair
key: (_) @variable.parameter.keyword
":" @variable.parameter.keyword))))))
(#eq? @_locals "locals"))

; Regular comments (exclude RBS inline comments)
((comment) @comment
(#not-match? @comment "^\\s*#[:|]")
(#not-match? @comment "^\\s*#\\s*(@rbs|\\|)"))

; Operators

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thought: Not sure that just removing operators here is a good idea. For example, removing the bare - capture loses highlighting during parser recovery:

def maximum = 5
def consumed = 3
def remaining = maximum &.- consumed

TS places the operator call in an ERROR recovery node (and this is the upstream issue). That node has no usable operator: field for -.

I'd rather scope down operators first with overrides. We can check what Helix does.

@AlternateRT AlternateRT Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has this been reported to upstream? I don't think we should concern ourself with what is a parsing error of the grammar.

After all, that example you gave should be valid code.

We can check what Helix does.

Helix seems to avoid bare captures for operators as much as possible too: https://github.com/helix-editor/helix/blob/master/runtime/queries/ruby/highlights.scm#L1-L21

[
"!"
"~"
"+"
"-"
"**"
"*"
"/"
"%"
"<<"
">>"
"**"
"&"
"|"
"^"
">"
"<"
"<="
">="
"=="
"==="
"!="
"=~"
"!~"
"<=>"
"||"
"&&"
".."
"..."
"="
"**="
"*="
"/="
"%="
"+="
"-="
"<<="
">>="
"&&="
"&="
"||="
"|="
"^="
"=>"
"->"
(operator)
] @operator

(lambda
"->" @operator)

(singleton_class
"<<" @operator)

(superclass
"<" @operator)

(alternative_pattern
"|" @operator)

(conditional
"?" @operator
":" @operator)

(_
operator: _ @operator
(#not-any-of? @operator "and" "or" "not" "defined?" "." "::"))

Comment thread
AlternateRT marked this conversation as resolved.
[
","
";"
Expand All @@ -280,6 +273,9 @@
"%i("
] @punctuation.bracket

(block_parameters
"|" @punctuation.bracket)

Comment thread
AlternateRT marked this conversation as resolved.
(interpolation
"#{" @punctuation.special
"}" @punctuation.special) @embedded
Loading