mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
java-ts-mode.el
447 lines (386 loc) · 17.1 KB
/
java-ts-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
;;; java-ts-mode.el --- tree-sitter support for Java -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2024 Free Software Foundation, Inc.
;; Author : Theodor Thornhill <theo@thornhill.no>
;; Maintainer : Theodor Thornhill <theo@thornhill.no>
;; Created : November 2022
;; Keywords : java languages tree-sitter
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; If the tree-sitter doxygen grammar is available, then the comment
;; blocks can be highlighted according to this grammar.
;;; Code:
(require 'treesit)
(eval-when-compile (require 'rx))
(require 'c-ts-common) ; For comment indent and filling.
(declare-function treesit-parser-create "treesit.c")
(declare-function treesit-induce-sparse-tree "treesit.c")
(declare-function treesit-node-start "treesit.c")
(declare-function treesit-node-type "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(declare-function treesit-node-child-by-field-name "treesit.c")
(declare-function treesit-query-capture "treesit.c")
(defcustom java-ts-mode-indent-offset 4
"Number of spaces for each indentation step in `java-ts-mode'."
:version "29.1"
:type 'integer
:safe 'integerp
:group 'java)
(defcustom java-ts-mode-enable-doxygen nil
"Enable doxygen syntax highlighting.
If Non-nil, enable doxygen based font lock for comment blocks.
This needs to be set before enabling `java-ts-mode'; if you change
the value after enabling `java-ts-mode', toggle the mode off and on
again."
:version "31.1"
:type 'boolean
:safe 'booleanp
:group 'java)
(defvar java-ts-mode--syntax-table
(let ((table (make-syntax-table)))
;; Taken from the cc-langs version
(modify-syntax-entry ?_ "_" table)
(modify-syntax-entry ?\\ "\\" table)
(modify-syntax-entry ?+ "." table)
(modify-syntax-entry ?- "." table)
(modify-syntax-entry ?= "." table)
(modify-syntax-entry ?% "." table)
(modify-syntax-entry ?< "." table)
(modify-syntax-entry ?> "." table)
(modify-syntax-entry ?& "." table)
(modify-syntax-entry ?| "." table)
(modify-syntax-entry ?\' "\"" table)
(modify-syntax-entry ?\240 "." table)
(modify-syntax-entry ?/ ". 124b" table)
(modify-syntax-entry ?* ". 23" table)
(modify-syntax-entry ?\n "> b" table)
(modify-syntax-entry ?\^m "> b" table)
(modify-syntax-entry ?@ "'" table)
table)
"Syntax table for `java-ts-mode'.")
(defvar java-ts-mode--indent-rules
`((java
((parent-is "program") column-0 0)
((match "}" "element_value_array_initializer")
parent-bol 0)
((node-is
,(format "\\`%s\\'"
(regexp-opt '("constructor_body" "class_body" "interface_body"
"block" "switch_block" "array_initializer"))))
parent-bol 0)
((node-is "}") standalone-parent 0)
((node-is ")") parent-bol 0)
((node-is "else") parent-bol 0)
((node-is "]") parent-bol 0)
((and (parent-is "comment") c-ts-common-looking-at-star)
c-ts-common-comment-start-after-first-star -1)
((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "text_block") no-indent)
((parent-is "class_body") column-0 c-ts-common-statement-offset)
((parent-is "array_initializer") parent-bol java-ts-mode-indent-offset)
((parent-is "annotation_type_body") column-0 c-ts-common-statement-offset)
((parent-is "interface_body") column-0 c-ts-common-statement-offset)
((parent-is "constructor_body") standalone-parent java-ts-mode-indent-offset)
((parent-is "enum_body_declarations") parent-bol 0)
((parent-is "enum_body") column-0 c-ts-common-statement-offset)
((parent-is "switch_block") standalone-parent java-ts-mode-indent-offset)
((parent-is "record_declaration_body") column-0 c-ts-common-statement-offset)
((query "(method_declaration (block _ @indent))") parent-bol java-ts-mode-indent-offset)
((query "(method_declaration (block (_) @indent))") parent-bol java-ts-mode-indent-offset)
((parent-is "local_variable_declaration") parent-bol java-ts-mode-indent-offset)
((parent-is "expression_statement") parent-bol java-ts-mode-indent-offset)
((match "type_identifier" "field_declaration") parent-bol 0)
((parent-is "field_declaration") parent-bol java-ts-mode-indent-offset)
((parent-is "return_statement") parent-bol java-ts-mode-indent-offset)
((parent-is "variable_declarator") parent-bol java-ts-mode-indent-offset)
((match ">" "type_arguments") parent-bol 0)
((parent-is "type_arguments") parent-bol java-ts-mode-indent-offset)
((parent-is "method_invocation") parent-bol java-ts-mode-indent-offset)
((parent-is "switch_rule") parent-bol java-ts-mode-indent-offset)
((parent-is "switch_label") parent-bol java-ts-mode-indent-offset)
((parent-is "ternary_expression") parent-bol java-ts-mode-indent-offset)
((parent-is "lambda_expression") parent-bol java-ts-mode-indent-offset)
((parent-is "element_value_array_initializer") parent-bol java-ts-mode-indent-offset)
((parent-is "function_definition") parent-bol 0)
((parent-is "conditional_expression") first-sibling 0)
((parent-is "assignment_expression") parent-bol 2)
((parent-is "binary_expression") parent 0)
((parent-is "parenthesized_expression") first-sibling 1)
((parent-is "argument_list") parent-bol java-ts-mode-indent-offset)
((parent-is "annotation_argument_list") parent-bol java-ts-mode-indent-offset)
((parent-is "modifiers") parent-bol 0)
((parent-is "formal_parameters") parent-bol java-ts-mode-indent-offset)
((parent-is "formal_parameter") parent-bol 0)
((parent-is "init_declarator") parent-bol java-ts-mode-indent-offset)
((parent-is "if_statement") parent-bol java-ts-mode-indent-offset)
((parent-is "for_statement") parent-bol java-ts-mode-indent-offset)
((parent-is "while_statement") parent-bol java-ts-mode-indent-offset)
((parent-is "switch_statement") parent-bol java-ts-mode-indent-offset)
((parent-is "case_statement") parent-bol java-ts-mode-indent-offset)
((parent-is "labeled_statement") parent-bol java-ts-mode-indent-offset)
((parent-is "do_statement") parent-bol java-ts-mode-indent-offset)
((parent-is "block") standalone-parent java-ts-mode-indent-offset)))
"Tree-sitter indent rules.")
(defvar java-ts-mode--keywords
'("abstract" "assert" "break" "case" "catch"
"class" "continue" "default" "do" "else"
"enum" "exports" "extends" "final" "finally"
"for" "if" "implements" "import" "instanceof"
"interface" "module" "native" "new" "non-sealed"
"open" "opens" "package" "private" "protected"
"provides" "public" "requires" "return" "sealed"
"static" "strictfp" "switch" "synchronized"
"throw" "throws" "to" "transient" "transitive"
"try" "uses" "volatile" "while" "with" "record"
"@interface")
"Java keywords for tree-sitter font-locking.")
(defvar java-ts-mode--operators
'("+" ":" "++" "-" "--" "&" "&&" "|" "||" "="
"!=" "==" "*" "/" "%" "<" "<=" ">" ">="
"-=" "+=" "*=" "/=" "%=" "->" "^" "^="
"|=" "~" ">>" ">>>" "<<" "::" "?" "&=")
"Java operators for tree-sitter font-locking.")
(defun java-ts-mode--string-highlight-helper ()
"Return, for strings, a query based on what is supported by
the available version of Tree-sitter for Java."
(condition-case nil
(progn (treesit-query-capture 'java '((text_block) @font-lock-string-face))
`((string_literal) @font-lock-string-face
(text_block) @font-lock-string-face))
(error
`((string_literal) @font-lock-string-face))))
(defvar java-ts-mode--font-lock-settings
(treesit-font-lock-rules
:language 'java
:override t
:feature 'comment
`((line_comment) @font-lock-comment-face
(block_comment) @font-lock-comment-face)
:language 'java
:override t
:feature 'constant
`(((identifier) @font-lock-constant-face
(:match "\\`[A-Z_][0-9A-Z_]*\\'" @font-lock-constant-face))
[(true) (false)] @font-lock-constant-face)
:language 'java
:override t
:feature 'keyword
`([,@java-ts-mode--keywords
(this)
(super)] @font-lock-keyword-face
(labeled_statement
(identifier) @font-lock-keyword-face))
:language 'java
:override t
:feature 'operator
`([,@java-ts-mode--operators] @font-lock-operator-face
"@" @font-lock-constant-face)
:language 'java
:override t
:feature 'annotation
`((annotation
name: (identifier) @font-lock-constant-face)
(marker_annotation
name: (identifier) @font-lock-constant-face))
:language 'java
:override t
:feature 'string
(java-ts-mode--string-highlight-helper)
:language 'java
:override t
:feature 'literal
`((null_literal) @font-lock-constant-face
(binary_integer_literal) @font-lock-number-face
(decimal_integer_literal) @font-lock-number-face
(hex_integer_literal) @font-lock-number-face
(octal_integer_literal) @font-lock-number-face
(decimal_floating_point_literal) @font-lock-number-face
(hex_floating_point_literal) @font-lock-number-face)
:language 'java
:override t
:feature 'type
'((annotation_type_declaration
name: (identifier) @font-lock-type-face)
(interface_declaration
name: (identifier) @font-lock-type-face)
(class_declaration
name: (identifier) @font-lock-type-face)
(record_declaration
name: (identifier) @font-lock-type-face)
(enum_declaration
name: (identifier) @font-lock-type-face)
(constructor_declaration
name: (identifier) @font-lock-type-face)
(compact_constructor_declaration
name: (identifier) @font-lock-type-face)
(field_access
object: (identifier) @font-lock-type-face)
(method_reference (identifier) @font-lock-type-face)
(scoped_identifier (identifier) @font-lock-constant-face)
((scoped_identifier name: (identifier) @font-lock-type-face)
(:match "\\`[A-Z]" @font-lock-type-face))
(type_identifier) @font-lock-type-face
[(boolean_type)
(integral_type)
(floating_point_type)
(void_type)] @font-lock-type-face)
:language 'java
:override t
:feature 'definition
`((annotation_type_element_declaration
name: (identifier) @font-lock-function-name-face)
(method_declaration
name: (identifier) @font-lock-function-name-face)
(variable_declarator
name: (identifier) @font-lock-variable-name-face)
(element_value_pair
key: (identifier) @font-lock-property-use-face)
(formal_parameter
name: (identifier) @font-lock-variable-name-face)
(catch_formal_parameter
name: (identifier) @font-lock-variable-name-face))
:language 'java
:override t
:feature 'expression
'((method_invocation
object: (identifier) @font-lock-variable-use-face)
(method_invocation
name: (identifier) @font-lock-function-call-face)
(argument_list (identifier) @font-lock-variable-name-face)
(expression_statement (identifier) @font-lock-variable-use-face))
:language 'java
:feature 'bracket
'((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
:language 'java
:feature 'delimiter
'((["," ":" ";"]) @font-lock-delimiter-face))
"Tree-sitter font-lock settings for `java-ts-mode'.")
(defun java-ts-mode--defun-name (node)
"Return the defun name of NODE.
Return nil if there is no name or if NODE is not a defun node."
(pcase (treesit-node-type node)
((or "method_declaration"
"class_declaration"
"record_declaration"
"interface_declaration"
"enum_declaration"
"import_declaration"
"package_declaration"
"module_declaration")
(treesit-node-text
(treesit-node-child-by-field-name node "name")
t))))
(defvar java-ts-mode--feature-list
'(( comment document definition )
( constant keyword string type)
( annotation expression literal)
( bracket delimiter operator)))
;;;###autoload
(define-derived-mode java-ts-mode prog-mode "Java"
"Major mode for editing Java, powered by tree-sitter."
:group 'java
:syntax-table java-ts-mode--syntax-table
(unless (treesit-ready-p 'java)
(error "Tree-sitter for Java isn't available"))
(let ((primary-parser (treesit-parser-create 'java)))
;; Comments.
(c-ts-common-comment-setup)
;; Indent.
(setq-local c-ts-common-indent-type-regexp-alist
`((block . ,(rx (or "class_body"
"array_initializer"
"constructor_body"
"annotation_type_body"
"interface_body"
"lambda_expression"
"enum_body"
"switch_block"
"record_declaration_body"
"block")))
(close-bracket . "}")
(if . "if_statement")
(else . ("if_statement" . "alternative"))
(for . "for_statement")
(while . "while_statement")
(do . "do_statement")))
(setq-local c-ts-common-indent-offset 'java-ts-mode-indent-offset)
(setq-local treesit-simple-indent-rules java-ts-mode--indent-rules)
;; Electric
(setq-local electric-indent-chars
(append "{}():;," electric-indent-chars))
;; Navigation.
(setq-local treesit-defun-type-regexp
(regexp-opt '("method_declaration"
"class_declaration"
"record_declaration"
"interface_declaration"
"enum_declaration"
"import_declaration"
"package_declaration"
"module_declaration"
"constructor_declaration")))
(setq-local treesit-defun-name-function #'java-ts-mode--defun-name)
(setq-local treesit-thing-settings
`((java
(sexp ,(rx (or "annotation"
"parenthesized_expression"
"argument_list"
"identifier"
"modifiers"
"block"
"body"
"literal"
"access"
"reference"
"_type"
"true"
"false")))
(sentence ,(rx (or "statement"
"local_variable_declaration"
"field_declaration"
"module_declaration"
"package_declaration"
"import_declaration")))
(text ,(regexp-opt '("line_comment"
"block_comment"
"text_block"))))))
;; Font-lock.
(setq-local treesit-font-lock-settings
java-ts-mode--font-lock-settings)
;; Inject doxygen parser for comment.
(when (and java-ts-mode-enable-doxygen (treesit-ready-p 'doxygen t))
(setq-local treesit-primary-parser primary-parser)
(setq-local treesit-font-lock-settings
(append treesit-font-lock-settings
c-ts-mode-doxygen-comment-font-lock-settings))
(setq-local treesit-range-settings
(treesit-range-rules
:embed 'doxygen
:host 'java
:local t
`(((block_comment) @cap (:match "/\\*\\*" @cap)))))))
(setq-local treesit-font-lock-feature-list java-ts-mode--feature-list)
;; Imenu.
(setq-local treesit-simple-imenu-settings
'(("Class" "\\`class_declaration\\'" nil nil)
("Interface" "\\`interface_declaration\\'" nil nil)
("Enum" "\\`record_declaration\\'" nil nil)
("Method" "\\`method_declaration\\'" nil nil)))
(treesit-major-mode-setup))
(derived-mode-add-parents 'java-ts-mode '(java-mode))
(if (treesit-ready-p 'java)
(add-to-list 'auto-mode-alist '("\\.java\\'" . java-ts-mode)))
(when (and java-ts-mode-enable-doxygen (not (treesit-ready-p 'doxygen t)))
(message "Doxygen syntax highlighting can't be enabled, please install the language grammar."))
(provide 'java-ts-mode)
;;; java-ts-mode.el ends here