feat: use custom Nix highlight queries for richer syntax colors
- queries/nix-highlights.scm: Adapted from Helix editor queries. Properly identifies functions, properties, variable parameters, escape sequences, and string interpolation. Much richer than the bundled tree-sitter-nix query (99 lines vs 113 lines). - src/highlight.rs: Load custom Nix query via include_str! instead of using bundled HIGHLIGHTS_QUERY. - static/style.css: Fix variable (#e36209) and punctuation (#586069) colors to be distinct from text color. Results: hl-function now appears (6 occurrences), hl-property visible (17), hl-variable reduced from 43 to 17. Functions like mkShell now render in purple/blue instead of orange.
This commit is contained in:
133
queries/nix-highlights.scm
Normal file
133
queries/nix-highlights.scm
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
; Nix syntax highlighting queries (adapted from Helix editor)
|
||||||
|
; Maps to standard tree-sitter capture names for compatibility with tree-sitter-highlight
|
||||||
|
|
||||||
|
(comment) @comment
|
||||||
|
|
||||||
|
; Punctuation
|
||||||
|
[
|
||||||
|
";"
|
||||||
|
"."
|
||||||
|
","
|
||||||
|
"="
|
||||||
|
":"
|
||||||
|
(ellipses)
|
||||||
|
] @punctuation.delimiter
|
||||||
|
|
||||||
|
[
|
||||||
|
"("
|
||||||
|
")"
|
||||||
|
"["
|
||||||
|
"]"
|
||||||
|
"{"
|
||||||
|
"}"
|
||||||
|
] @punctuation.bracket
|
||||||
|
|
||||||
|
; Keywords
|
||||||
|
"assert" @keyword
|
||||||
|
"or" @keyword
|
||||||
|
"rec" @keyword
|
||||||
|
|
||||||
|
[
|
||||||
|
"if"
|
||||||
|
"then"
|
||||||
|
"else"
|
||||||
|
] @keyword
|
||||||
|
|
||||||
|
[
|
||||||
|
"let"
|
||||||
|
"inherit"
|
||||||
|
"in"
|
||||||
|
"with"
|
||||||
|
] @keyword
|
||||||
|
|
||||||
|
; Variables and identifiers
|
||||||
|
(variable_expression name: (identifier) @variable)
|
||||||
|
|
||||||
|
; Attribute access (properties)
|
||||||
|
(select_expression
|
||||||
|
attrpath: (attrpath attr: (identifier)) @property)
|
||||||
|
|
||||||
|
; Function calls
|
||||||
|
(apply_expression
|
||||||
|
function: [
|
||||||
|
(variable_expression name: (identifier) @function)
|
||||||
|
(select_expression
|
||||||
|
attrpath: (attrpath
|
||||||
|
attr: (identifier) @function .))])
|
||||||
|
|
||||||
|
; Builtin variables
|
||||||
|
((identifier) @variable.builtin
|
||||||
|
(#match? @variable.builtin "^(__currentSystem|__currentTime|__nixPath|__nixVersion|__storeDir|builtins)$")
|
||||||
|
(#is-not? local))
|
||||||
|
|
||||||
|
; Builtin functions
|
||||||
|
((identifier) @function.builtin
|
||||||
|
(#match? @function.builtin "^(__add|__addErrorContext|__all|__any|__appendContext|__attrNames|__attrValues|__bitAnd|__bitOr|__bitXor|__catAttrs|__compareVersions|__concatLists|__concatMap|__concatStringsSep|__deepSeq|__div|__elem|__elemAt|__fetchurl|__filter|__filterSource|__findFile|__foldl'|__fromJSON|__functionArgs|__genList|__genericClosure|__getAttr|__getContext|__getEnv|__hasAttr|__hasContext|__hashFile|__hashString|__head|__intersectAttrs|__isAttrs|__isBool|__isFloat|__isFunction|__isInt|__isList|__isPath|__isString|__langVersion|__length|__lessThan|__listToAttrs|__mapAttrs|__match|__mul|__parseDrvName|__partition|__path|__pathExists|__readDir|__readFile|__replaceStrings|__seq|__sort|__split|__splitVersion|__storePath|__stringLength|__sub|__substring|__tail|__toFile|__toJSON|__toPath|__toXML|__trace|__tryEval|__typeOf|__unsafeDiscardOutputDependency|__unsafeDiscardStringContext|__unsafeGetAttrPos|__valueSize|abort|baseNameOf|derivation|derivationStrict|dirOf|fetchGit|fetchMercurial|fetchTarball|fromTOML|import|isNull|map|placeholder|removeAttrs|scopedImport|throw|toString)$")
|
||||||
|
(#is-not? local))
|
||||||
|
|
||||||
|
; Strings
|
||||||
|
[
|
||||||
|
(string_expression)
|
||||||
|
(indented_string_expression)
|
||||||
|
] @string
|
||||||
|
|
||||||
|
; Special string types (paths, URIs)
|
||||||
|
[
|
||||||
|
(path_expression)
|
||||||
|
(hpath_expression)
|
||||||
|
(spath_expression)
|
||||||
|
] @string.special.path
|
||||||
|
|
||||||
|
(uri_expression) @string.special.uri
|
||||||
|
|
||||||
|
; Boolean constants
|
||||||
|
((identifier) @constant.builtin (#match? @constant.builtin "^(true|false)$"))
|
||||||
|
|
||||||
|
; Null constant
|
||||||
|
((identifier) @constant.builtin (#eq? @constant.builtin "null"))
|
||||||
|
|
||||||
|
; Numbers
|
||||||
|
(integer_expression) @number
|
||||||
|
(float_expression) @number
|
||||||
|
|
||||||
|
; Escape sequences
|
||||||
|
(escape_sequence) @escape
|
||||||
|
(dollar_escape) @escape
|
||||||
|
|
||||||
|
; Function parameters
|
||||||
|
(function_expression
|
||||||
|
"@"? @punctuation.delimiter
|
||||||
|
universal: (identifier) @variable.parameter
|
||||||
|
"@"? @punctuation.delimiter
|
||||||
|
)
|
||||||
|
|
||||||
|
(formal
|
||||||
|
name: (identifier) @variable.parameter
|
||||||
|
"?"? @punctuation.delimiter)
|
||||||
|
|
||||||
|
; String interpolation
|
||||||
|
(interpolation
|
||||||
|
"${" @punctuation.special
|
||||||
|
"}" @punctuation.special) @embedded
|
||||||
|
|
||||||
|
; Operators
|
||||||
|
(unary_expression
|
||||||
|
operator: _ @operator)
|
||||||
|
|
||||||
|
(binary_expression
|
||||||
|
operator: _ @operator)
|
||||||
|
|
||||||
|
; Attribute bindings
|
||||||
|
(binding
|
||||||
|
attrpath: (attrpath attr: (identifier)) @property)
|
||||||
|
|
||||||
|
; Inherit expressions
|
||||||
|
(inherit_from attrs: (inherited_attrs attr: (identifier) @property))
|
||||||
|
(inherited_attrs attr: (identifier) @variable)
|
||||||
|
|
||||||
|
; Has attribute expression
|
||||||
|
(has_attr_expression
|
||||||
|
expression: (_)
|
||||||
|
"?" @operator
|
||||||
|
attrpath: (attrpath
|
||||||
|
attr: (identifier) @property))
|
||||||
@@ -145,7 +145,7 @@ fn get_config(lang: Language) -> HighlightConfiguration {
|
|||||||
Language::Nix => (
|
Language::Nix => (
|
||||||
tree_sitter_nix::LANGUAGE.into(),
|
tree_sitter_nix::LANGUAGE.into(),
|
||||||
"nix",
|
"nix",
|
||||||
tree_sitter_nix::HIGHLIGHTS_QUERY,
|
include_str!("../queries/nix-highlights.scm"),
|
||||||
),
|
),
|
||||||
Language::Python => (
|
Language::Python => (
|
||||||
tree_sitter_python::LANGUAGE.into(),
|
tree_sitter_python::LANGUAGE.into(),
|
||||||
|
|||||||
@@ -278,10 +278,10 @@ article.post header {
|
|||||||
--hl-type: #005cc5;
|
--hl-type: #005cc5;
|
||||||
--hl-number: #005cc5;
|
--hl-number: #005cc5;
|
||||||
--hl-operator: #d73a49;
|
--hl-operator: #d73a49;
|
||||||
--hl-variable: #24292e;
|
--hl-variable: #e36209;
|
||||||
--hl-constant: #005cc5;
|
--hl-constant: #005cc5;
|
||||||
--hl-property: #005cc5;
|
--hl-property: #005cc5;
|
||||||
--hl-punctuation: #24292e;
|
--hl-punctuation: #586069;
|
||||||
--hl-punctuation-special: #d73a49;
|
--hl-punctuation-special: #d73a49;
|
||||||
--hl-attribute: #22863a;
|
--hl-attribute: #22863a;
|
||||||
--hl-escape: #005cc5;
|
--hl-escape: #005cc5;
|
||||||
@@ -298,10 +298,10 @@ article.post header {
|
|||||||
--hl-type: #79b8ff;
|
--hl-type: #79b8ff;
|
||||||
--hl-number: #79b8ff;
|
--hl-number: #79b8ff;
|
||||||
--hl-operator: #f97583;
|
--hl-operator: #f97583;
|
||||||
--hl-variable: #e1e4e8;
|
--hl-variable: #ffab70;
|
||||||
--hl-constant: #79b8ff;
|
--hl-constant: #79b8ff;
|
||||||
--hl-property: #79b8ff;
|
--hl-property: #79b8ff;
|
||||||
--hl-punctuation: #e1e4e8;
|
--hl-punctuation: #a0a0a0;
|
||||||
--hl-punctuation-special: #f97583;
|
--hl-punctuation-special: #f97583;
|
||||||
--hl-attribute: #85e89d;
|
--hl-attribute: #85e89d;
|
||||||
--hl-escape: #79b8ff;
|
--hl-escape: #79b8ff;
|
||||||
|
|||||||
Reference in New Issue
Block a user