Skip to content

Commit

Permalink
ref(rust): improve TS highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Iron-E committed May 4, 2023
1 parent cdcc7ac commit baf159c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 13 deletions.
54 changes: 44 additions & 10 deletions after/queries/rust/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,29 @@
)

; enum variants
(enum_variant name: (identifier) @constant !body)
(enum_variant
name: (identifier) @type
body: [(field_declaration_list) (ordered_field_declaration_list)]
(call_expression
function: (scoped_identifier
name: (identifier) @type (#lua-match? @type "^%u")
)
)

(enum_variant name: (identifier) @type)
((identifier) @type.builtin
(#any-of? @type.builtin "Some" "None" "Ok" "Err")
)

; HRTB closures
(ERROR "for" "<" @punctuation.bracket (lifetime) ">" @punctuation.bracket)

; macros
(macro_invocation
macro: (identifier) @_id @debug (#contains? @_id "dbg" "debug")
macro: (identifier) @_id @debug (#contains? @_id "assert" "dbg" "debug")
"!" @debug
(#set! "priority" 101)
)

(macro_invocation
macro: (identifier) @_id @include (#lua-match? @_id "^include")
macro: (identifier) @_id @include (#contains? @_id "include")
"!" @include
(#set! "priority" 101)
)
Expand All @@ -67,18 +72,47 @@
(match_pattern "_" @variable.builtin)

; raw identifiers
((field_identifier) @punctuation.special
(
[
(field_identifier) @punctuation.special
(identifier) @punctuation.special
]
(#lua-match? @punctuation.special "^r#")
(#offset-from-start! @punctuation.special 0 0 0 2)
)

((identifier) @punctuation.special
((raw_string_literal) @punctuation.special
(#lua-match? @punctuation.special "^r#")
(#offset-from-start! @punctuation.special 0 0 0 2)
)

; trait bounds
(removed_trait_bound "?" @punctuation.special)
((raw_string_literal) @punctuation.special
(#lua-match? @punctuation.special "^r#")
(#offset-from-end! @punctuation.special 0 -1 0 0)
)

((raw_string_literal) @punctuation.special
(#lua-match? @punctuation.special "^r##")
(#offset-from-start! @punctuation.special 0 0 0 3)
)

((raw_string_literal) @punctuation.special
(#lua-match? @punctuation.special "^r##")
(#offset-from-end! @punctuation.special 0 -2 0 0)
)

((raw_string_literal) @punctuation.special
(#lua-match? @punctuation.special "^r###")
(#offset-from-start! @punctuation.special 0 0 0 4)
)

((raw_string_literal) @punctuation.special
(#lua-match? @punctuation.special "^r###")
(#offset-from-end! @punctuation.special 0 -3 0 0)
)

; `try!`
(try_expression ("?" @exception))

; unsafe
"unsafe" @exception
4 changes: 3 additions & 1 deletion ftplugin/rust/highlite.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
if vim.api.nvim_get_var('colors_name'):find '^highlite' then
require('highlite.treesitter').add_directive.offset_from_start()
local Treesitter = require 'highlite.treesitter' --- @type highlite.Treesitter
Treesitter.add_directive.offset_from_end()
Treesitter.add_directive.offset_from_start()
end
4 changes: 2 additions & 2 deletions lua/highlite/groups/default.lua
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,13 @@ local function from_palette(palette, opts)
['@lsp.type.builtinType.rust'] = '@type.builtin.rust',
['@lsp.type.decorator.rust'] = '@preproc.rust',
['@lsp.type.derive.rust'] = '@macro.rust',
['@lsp.type.enumMember.rust'] = NONE,
['@lsp.type.enumMember.rust'] = '@type',
['@lsp.type.keyword.rust'] = NONE,
['@lsp.type.macro.rust'] = NONE,
['@lsp.type.operator.rust'] = NONE,
['@lsp.type.selfKeyword.rust'] = '@variable.builtin.rust',
['@lsp.type.selfTypeKeyword.rust'] = '@lsp.type.typeAlias.rust',
['@lsp.typemod.enumMember.injected.rust'] = '@lsp.type.enumMember',
['@lsp.typemod.enumMember.defaultLibrary.rust'] = '@type.builtin',
['@lsp.typemod.keyword.injected.rust'] = '@keyword.rust',
['@lsp.typemod.macro.injected.rust'] = '@macro.rust',
['@lsp.typemod.operator.injected.rust'] = '@operator.rust',
Expand Down

0 comments on commit baf159c

Please sign in to comment.