Skip to content

Commit 2084954

Browse files
authored
Add highlight.scm for Clojure (#206)
* Add highlight.scm for Clojure * Comment invalid
1 parent e7d34c3 commit 2084954

File tree

1 file changed

+340
-0
lines changed

1 file changed

+340
-0
lines changed

queries/clojure/highlights.scm

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
;; >> Explanation
2+
;; Parsers for lisps are a bit weird in that they just return the raw forms.
3+
;; This means we have to do a bit of extra work in the queries to get things
4+
;; highlighted as they should be.
5+
;;
6+
;; For the most part this means that some things have to be assigned multiple
7+
;; groups.
8+
;; By doing this we can add a basic capture and then later refine it with more
9+
;; specialied captures.
10+
;; This can mean that sometimes things are highlighted weirdly because they
11+
;; have multiple highlight groups applied to them.
12+
13+
14+
;; >> Literals
15+
16+
(kwd_lit) @symbol
17+
(str_lit) @string
18+
(num_lit) @number
19+
(char_lit) @character
20+
(bool_lit) @boolean
21+
(nil_lit) @constant.builtin
22+
(comment) @comment
23+
(regex_lit) @string.regex
24+
25+
["'" "`"] @string.escape
26+
27+
["~" "~@" "#"] @punctuation.special
28+
29+
["{" "}" "[" "]" "(" ")"] @punctuation.bracket
30+
31+
32+
33+
;; >> Symbols
34+
35+
; General symbol highlighting
36+
(sym_lit) @variable
37+
38+
; General function calls
39+
(list_lit
40+
.
41+
(sym_lit) @function.call)
42+
(anon_fn_lit
43+
.
44+
(sym_lit) @function.call)
45+
46+
; Quoted symbols
47+
(quoting_lit
48+
(sym_lit) @symbol)
49+
(syn_quoting_lit
50+
(sym_lit) @symbol)
51+
52+
; Used in destructure pattern
53+
((sym_lit) @parameter
54+
(#match? @parameter "^[&]"))
55+
56+
; Inline function variables
57+
((sym_lit) @variable.builtin
58+
(#match? @variable.builtin "^[%]"))
59+
60+
; Constructor
61+
;;((sym_lit) @constructor
62+
;; (#match? @constructor "^-\\>[^\\>].*"))
63+
64+
; Dynamic variables
65+
((sym_lit) @variable.builtin
66+
(#match? @variable.builtin "^[*].+[*]$"))
67+
68+
; Gensym
69+
;; Might not be needed
70+
((sym_lit) @variable
71+
(#match? @variable "^.*#$"))
72+
73+
; Types
74+
;; TODO: improve?
75+
((sym_lit) @type
76+
(#match? @type "^[A-Z][^/]*$"))
77+
;; Symbols with `.` but not `/`
78+
((sym_lit) @type
79+
(#match? @type "^[^/]+[.][^/]*$"))
80+
81+
; Interop
82+
((sym_lit) @method
83+
(#match? @method "^\\.[^-]"))
84+
((sym_lit) @field
85+
(#match? @field "^\\.-"))
86+
((sym_lit) @field
87+
(#match? @field "^[A-Z].*/.+"))
88+
(list_lit
89+
.
90+
(sym_lit) @method
91+
(#match? @method "^[A-Z].*/.+"))
92+
;; TODO: Special casing for the `.` macro
93+
94+
; Operators
95+
((sym_lit) @operator
96+
(#any-of? @operator
97+
"*" "*'" "+" "+'" "-" "-'" "/"
98+
"<" "<=" ">" ">=" "=" "=="))
99+
((sym_lit) @keyword.operator
100+
(#any-of? @keyword.operator
101+
"not" "not=" "and" "or"))
102+
103+
; Definition functions
104+
((sym_lit) @keyword
105+
(#any-of? @keyword
106+
"def" "defonce" "defrecord" "defmacro" "definline"
107+
"defmulti" "defmethod" "defstruct" "defprotocol"
108+
"deftype"))
109+
((sym_lit) @keyword
110+
(#eq? @keyword "declare"))
111+
((sym_lit) @keyword.function
112+
(#match? @keyword.function "^(defn|defn-|fn|fn[*])$"))
113+
114+
; Comment
115+
((sym_lit) @comment
116+
(#any-of? @comment "comment"))
117+
118+
; Conditionals
119+
((sym_lit) @conditional
120+
(#any-of? @conditional
121+
"case" "cond" "cond->" "cond->>" "condp"))
122+
((sym_lit) @conditional
123+
(#match? @conditional "^if(\\-.*)?$"))
124+
((sym_lit) @conditional
125+
(#match? @conditional "^when(\\-.*)?$"))
126+
127+
; Repeats
128+
((sym_lit) @repeat
129+
(#any-of? @repeat
130+
"doseq" "dotimes" "for" "loop" "recur" "while"))
131+
132+
; Exception
133+
((sym_lit) @exception
134+
(#any-of? @exception
135+
"throw" "try" "catch" "finally" "ex-info"))
136+
137+
; Includes
138+
((sym_lit) @include
139+
(#any-of? @include "ns" "import" "require" "use"))
140+
141+
; Builtin macros
142+
;; TODO: Do all these items belong here?
143+
((sym_lit) @function.macro
144+
(#any-of? @function.macro
145+
"." ".." "->" "->>" "amap" "areduce" "as->" "assert"
146+
"binding" "bound-fn" "delay" "do" "dosync"
147+
"doto" "extend-protocol" "extend-type" "future"
148+
"gen-class" "gen-interface" "io!" "lazy-cat"
149+
"lazy-seq" "let" "letfn" "locking" "memfn" "monitor-enter"
150+
"monitor-exit" "proxy" "proxy-super" "pvalues"
151+
"refer-clojure" "reify" "set!" "some->" "some->>" "sync"
152+
"time" "unquote" "unquote-splicing" "var" "vswap!"
153+
"ex-cause" "ex-data" "ex-message"))
154+
((sym_lit) @function.macro
155+
(#match? @function.macro "^with\\-.*$"))
156+
157+
; All builtin functions
158+
; (->> (ns-publics *ns*)
159+
; (keep (fn [[s v]] (when-not (:macro (meta v)) s)))
160+
; sort
161+
; cp/print))
162+
;; ...and then lots of manual filtering...
163+
((sym_lit) @function.builtin
164+
(#any-of? @function.builtin
165+
"->ArrayChunk" "->Eduction" "->Vec" "->VecNode" "->VecSeq"
166+
"-cache-protocol-fn" "-reset-methods" "PrintWriter-on"
167+
"StackTraceElement->vec" "Throwable->map" "accessor"
168+
"aclone" "add-classpath" "add-tap" "add-watch" "agent"
169+
"agent-error" "agent-errors" "aget" "alength" "alias"
170+
"all-ns" "alter" "alter-meta!" "alter-var-root" "ancestors"
171+
"any?" "apply" "array-map" "aset" "aset-boolean" "aset-byte"
172+
"aset-char" "aset-double" "aset-float" "aset-int"
173+
"aset-long" "aset-short" "assoc" "assoc!" "assoc-in"
174+
"associative?" "atom" "await" "await-for" "await1"
175+
"bases" "bean" "bigdec" "bigint" "biginteger" "bit-and"
176+
"bit-and-not" "bit-clear" "bit-flip" "bit-not" "bit-or"
177+
"bit-set" "bit-shift-left" "bit-shift-right" "bit-test"
178+
"bit-xor" "boolean" "boolean-array" "boolean?"
179+
"booleans" "bound-fn*" "bound?" "bounded-count"
180+
"butlast" "byte" "byte-array" "bytes" "bytes?"
181+
"cast" "cat" "char" "char-array" "char-escape-string"
182+
"char-name-string" "char?" "chars" "chunk" "chunk-append"
183+
"chunk-buffer" "chunk-cons" "chunk-first" "chunk-next"
184+
"chunk-rest" "chunked-seq?" "class" "class?"
185+
"clear-agent-errors" "clojure-version" "coll?"
186+
"commute" "comp" "comparator" "compare" "compare-and-set!"
187+
"compile" "complement" "completing" "concat" "conj"
188+
"conj!" "cons" "constantly" "construct-proxy" "contains?"
189+
"count" "counted?" "create-ns" "create-struct" "cycle"
190+
"dec" "dec'" "decimal?" "dedupe" "default-data-readers"
191+
"delay?" "deliver" "denominator" "deref" "derive"
192+
"descendants" "destructure" "disj" "disj!" "dissoc"
193+
"dissoc!" "distinct" "distinct?" "doall" "dorun" "double"
194+
"double-array" "eduction" "empty" "empty?" "ensure" "ensure-reduced"
195+
"enumeration-seq" "error-handler" "error-mode" "eval"
196+
"even?" "every-pred" "every?" "extend" "extenders" "extends?"
197+
"false?" "ffirst" "file-seq" "filter" "filterv" "find"
198+
"find-keyword" "find-ns" "find-protocol-impl"
199+
"find-protocol-method" "find-var" "first" "flatten"
200+
"float" "float-array" "float?" "floats" "flush" "fn?"
201+
"fnext" "fnil" "force" "format" "frequencies"
202+
"future-call" "future-cancel" "future-cancelled?"
203+
"future-done?" "future?" "gensym" "get" "get-in"
204+
"get-method" "get-proxy-class" "get-thread-bindings"
205+
"get-validator" "group-by" "halt-when" "hash"
206+
"hash-combine" "hash-map" "hash-ordered-coll" "hash-set"
207+
"hash-unordered-coll" "ident?" "identical?" "identity"
208+
"ifn?" "in-ns" "inc" "inc'" "indexed?" "init-proxy"
209+
"inst-ms" "inst-ms*" "inst?" "instance?" "int" "int-array"
210+
"int?" "integer?" "interleave" "intern" "interpose" "into"
211+
"into-array" "ints" "isa?" "iterate" "iterator-seq" "juxt"
212+
"keep" "keep-indexed" "key" "keys" "keyword" "keyword?"
213+
"last" "line-seq" "list" "list*" "list?" "load" "load-file"
214+
"load-reader" "load-string" "loaded-libs" "long" "long-array"
215+
"longs" "macroexpand" "macroexpand-1" "make-array" "make-hierarchy"
216+
"map" "map-entry?" "map-indexed" "map?" "mapcat" "mapv"
217+
"max" "max-key" "memoize" "merge" "merge-with" "meta"
218+
"method-sig" "methods" "min" "min-key" "mix-collection-hash"
219+
"mod" "munge" "name" "namespace" "namespace-munge" "nat-int?"
220+
"neg-int?" "neg?" "newline" "next" "nfirst" "nil?" "nnext"
221+
"not-any?" "not-empty" "not-every?" "ns-aliases"
222+
"ns-imports" "ns-interns" "ns-map" "ns-name" "ns-publics"
223+
"ns-refers" "ns-resolve" "ns-unalias" "ns-unmap" "nth"
224+
"nthnext" "nthrest" "num" "number?" "numerator" "object-array"
225+
"odd?" "parents" "partial" "partition" "partition-all"
226+
"partition-by" "pcalls" "peek" "persistent!" "pmap" "pop"
227+
"pop!" "pop-thread-bindings" "pos-int?" "pos?" "pr"
228+
"pr-str" "prefer-method" "prefers" "primitives-classnames"
229+
"print" "print-ctor" "print-dup" "print-method" "print-simple"
230+
"print-str" "printf" "println" "println-str" "prn" "prn-str"
231+
"promise" "proxy-call-with-super" "proxy-mappings" "proxy-name"
232+
"push-thread-bindings" "qualified-ident?" "qualified-keyword?"
233+
"qualified-symbol?" "quot" "rand" "rand-int" "rand-nth" "random-sample"
234+
"range" "ratio?" "rational?" "rationalize" "re-find" "re-groups"
235+
"re-matcher" "re-matches" "re-pattern" "re-seq" "read"
236+
"read+string" "read-line" "read-string" "reader-conditional"
237+
"reader-conditional?" "realized?" "record?" "reduce"
238+
"reduce-kv" "reduced" "reduced?" "reductions" "ref" "ref-history-count"
239+
"ref-max-history" "ref-min-history" "ref-set" "refer"
240+
"release-pending-sends" "rem" "remove" "remove-all-methods"
241+
"remove-method" "remove-ns" "remove-tap" "remove-watch"
242+
"repeat" "repeatedly" "replace" "replicate"
243+
"requiring-resolve" "reset!" "reset-meta!" "reset-vals!"
244+
"resolve" "rest" "restart-agent" "resultset-seq" "reverse"
245+
"reversible?" "rseq" "rsubseq" "run!" "satisfies?"
246+
"second" "select-keys" "send" "send-off" "send-via"
247+
"seq" "seq?" "seqable?" "seque" "sequence" "sequential?"
248+
"set" "set-agent-send-executor!" "set-agent-send-off-executor!"
249+
"set-error-handler!" "set-error-mode!" "set-validator!"
250+
"set?" "short" "short-array" "shorts" "shuffle"
251+
"shutdown-agents" "simple-ident?" "simple-keyword?"
252+
"simple-symbol?" "slurp" "some" "some-fn" "some?"
253+
"sort" "sort-by" "sorted-map" "sorted-map-by"
254+
"sorted-set" "sorted-set-by" "sorted?" "special-symbol?"
255+
"spit" "split-at" "split-with" "str" "string?"
256+
"struct" "struct-map" "subs" "subseq" "subvec" "supers"
257+
"swap!" "swap-vals!" "symbol" "symbol?" "tagged-literal"
258+
"tagged-literal?" "take" "take-last" "take-nth" "take-while"
259+
"tap>" "test" "the-ns" "thread-bound?" "to-array"
260+
"to-array-2d" "trampoline" "transduce" "transient"
261+
"tree-seq" "true?" "type" "unchecked-add" "unchecked-add-int"
262+
"unchecked-byte" "unchecked-char" "unchecked-dec"
263+
"unchecked-dec-int" "unchecked-divide-int" "unchecked-double"
264+
"unchecked-float" "unchecked-inc" "unchecked-inc-int"
265+
"unchecked-int" "unchecked-long" "unchecked-multiply"
266+
"unchecked-multiply-int" "unchecked-negate" "unchecked-negate-int"
267+
"unchecked-remainder-int" "unchecked-short" "unchecked-subtract"
268+
"unchecked-subtract-int" "underive" "unquote"
269+
"unquote-splicing" "unreduced" "unsigned-bit-shift-right"
270+
"update" "update-in" "update-proxy" "uri?" "uuid?"
271+
"val" "vals" "var-get" "var-set" "var?" "vary-meta" "vec"
272+
"vector" "vector-of" "vector?" "volatile!" "volatile?"
273+
"vreset!" "with-bindings*" "with-meta" "with-redefs-fn" "xml-seq"
274+
"zero?" "zipmap"))
275+
276+
277+
278+
;; >> Context based highlighting
279+
280+
;; def-likes
281+
;; Correctly highlight docstrings
282+
;(list_lit
283+
;.
284+
;(sym_lit) @_keyword ; Don't really want to highlight twice
285+
;(#any-of? @_keyword
286+
;"def" "defonce" "defrecord" "defmacro" "definline"
287+
;"defmulti" "defmethod" "defstruct" "defprotocol"
288+
;"deftype")
289+
;.
290+
;(sym_lit)
291+
;.
292+
;;; TODO: Add @comment highlight
293+
;(str_lit)?
294+
;.
295+
;(_))
296+
297+
; Function definitions
298+
(list_lit
299+
.
300+
(sym_lit) @_keyword.function
301+
(#any-of? @_keyword.function "fn" "fn*" "defn" "defn-")
302+
.
303+
(sym_lit)? @function
304+
.
305+
;; TODO: Add @comment highlight
306+
(str_lit)?)
307+
;; TODO: Fix parameter highlighting
308+
;; I think there's a bug here in nvim-treesitter
309+
;; TODO: Reproduce bug and file ticket
310+
;.
311+
;[(vec_lit
312+
; (sym_lit)* @parameter)
313+
; (list_lit
314+
; (vec_lit
315+
; (sym_lit)* @parameter))])
316+
317+
;[((list_lit
318+
; (vec_lit
319+
; (sym_lit) @parameter)
320+
; (_)
321+
; +
322+
; ((vec_lit
323+
; (sym_lit) @parameter)
324+
; (_)))
325+
326+
327+
; Meta punctuation
328+
;; NOTE: When the above `Function definitions` query captures the
329+
;; the @function it also captures the child meta_lit
330+
;; We capture the meta_lit symbol (^) after so that the later
331+
;; highlighting overrides the former
332+
"^" @punctuation.special
333+
334+
;; namespaces
335+
(list_lit
336+
.
337+
(sym_lit) @_include
338+
(#eq? @_include "ns")
339+
.
340+
(sym_lit) @namespace)

0 commit comments

Comments
 (0)