all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 8aed2db9c6602980d9cec4fa33a05dae44210da1 10895 bytes (raw)
name: lisp/progmodes/java-ts-mode.el 	 # note: path name is non-authoritative(*)

  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
 
;;; java-ts-mode.el --- tree-sitter support for Java  -*- lexical-binding: t; -*-

;; Copyright (C) 2022 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.

;; This program 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.

;; This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.


;;; Commentary:
;;

;;; Code:

(require 'treesit)
(require 'rx)

(defcustom java-ts-mode-font-lock-feature-list
  '((basic comment keyword constant string operator)
    (type definition expression literal annotation)
    ())
  "Language constructs to font-lock.

See `treesit-font-lock-feature-list' for description of how to
use this variable.  Supported features are as defined in
`java-ts-mode--font-lock-settings'."
  :type 'list
  :safe 'listp
  :group 'java)

(defcustom java-ts-mode-indent-offset 4
  "Number of spaces for each indentation step in `java-ts-mode'."
  :type 'integer
  :safe 'integerp
  :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)
    table)
  "Syntax table for `java-ts-mode'.")

(defvar java-ts-mode--indent-rules
  `((java
     ((parent-is "program") parent-bol 0)
     ((node-is "}") (and parent parent-bol) 0)
     ((node-is ")") parent-bol 0)
     ((node-is "]") parent-bol 0)
     ((parent-is "class_body") parent-bol java-ts-mode-indent-offset)
     ((parent-is "interface_body") parent-bol java-ts-mode-indent-offset)
     ((parent-is "constructor_body") parent-bol java-ts-mode-indent-offset)
     ((parent-is "enum_body") parent-bol java-ts-mode-indent-offset)
     ((parent-is "switch_block") parent-bol java-ts-mode-indent-offset)
     ((parent-is "record_declaration_body") parent-bol java-ts-mode-indent-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 "variable_declarator") 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 "ternary_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") (and parent parent-bol) 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")
  "C keywords for tree-sitter font-locking.")

(defvar java-ts-mode--operators
  '("@" "+" ":" "++" "-" "--" "&" "&&" "|" "||"
    "!=" "==" "*" "/" "%" "<" "<=" ">" ">=" "="
    "-=" "+=" "*=" "/=" "%=" "->" "^" "^=" "&="
    "|=" "~" ">>" ">>>" "<<" "::" "?")
  "C operators for tree-sitter font-locking.")

(defvar java-ts-mode--font-lock-settings
  (treesit-font-lock-rules
   :language 'java
   :override t
   :feature 'basic
   '((identifier) @font-lock-variable-name-face)
   :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_][A-Z_\\d]*$" @font-lock-constant-face))
     (true) @font-lock-constant-face
     (false) @font-lock-constant-face)
   :language 'java
   :override t
   :feature 'keyword
   `([,@java-ts-mode--keywords] @font-lock-keyword-face
     (labeled_statement
      (identifier) @font-lock-keyword-face))
   :language 'java
   :override t
   :feature 'operator
   `([,@java-ts-mode--operators] @font-lock-builtin-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
   `((string_literal) @font-lock-string-face)
   :language 'java
   :override t
   :feature 'literal
   `((null_literal) @font-lock-constant-face
     (decimal_floating_point_literal)  @font-lock-constant-face
     (hex_floating_point_literal) @font-lock-constant-face)
   :language 'java
   :override t
   :feature 'type
   '((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)

     (field_access
      object: (identifier) @font-lock-type-face)

     (method_reference (identifier) @font-lock-type-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
   `((method_declaration
      name: (identifier) @font-lock-function-name-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-name-face)

     (method_invocation
      name: (identifier) @font-lock-function-name-face)

     (argument_list (identifier) @font-lock-variable-name-face)))
  "Tree-sitter font-lock settings.")

(defun java-ts-mode--imenu-1 (node)
  "Helper for `java-ts-mode--imenu'.
Find string representation for NODE and set marker, then recurse
the subtrees."
  (let* ((ts-node (car node))
         (subtrees (mapcan #'java-ts-mode--imenu-1 (cdr node)))
         (name (when ts-node
                 (or (treesit-node-text
                      (or (treesit-node-child-by-field-name
                           ts-node "name"))
                      t)
                     "Unnamed node")))
         (marker (when ts-node
                   (set-marker (make-marker)
                               (treesit-node-start ts-node)))))
    (cond
     ((null ts-node) subtrees)
     (subtrees
      `((,name ,(cons name marker) ,@subtrees)))
     (t
      `((,name . ,marker))))))

(defun java-ts-mode--imenu ()
  "Return Imenu alist for the current buffer."
  (let* ((node (treesit-buffer-root-node))
         (tree (treesit-induce-sparse-tree
                node (rx (or "class_declaration"
                             "interface_declaration"
                             "enum_declaration"
                             "record_declaration"
                             "method_declaration")))))
    (java-ts-mode--imenu-1 tree)))

;;;###autoload
(define-derived-mode java-ts-mode prog-mode "Java"
  "Major mode for editing Java, powered by tree-sitter."
  :group 'c
  :syntax-table java-ts-mode--syntax-table

  (unless (treesit-ready-p nil 'java)
    (error "Tree-sitter for Java isn't available"))

  (treesit-parser-create 'java)

  ;; Comments.
  (setq-local comment-start "// ")
  (setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
  (setq-local comment-end "")

  ;; Indent.
  (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
              (rx (or "declaration")))

  ;; Font-lock.
  (setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings)
  (setq-local treesit-font-lock-feature-list
              java-ts-mode-font-lock-feature-list)

  ;; Imenu.
  (setq-local imenu-create-index-function #'java-ts-mode--imenu)
  (setq-local which-func-functions nil) ;; Piggyback on imenu
  (treesit-major-mode-setup))

(provide 'java-ts-mode)

;;; java-ts-mode.el ends here

debug log:

solving 8aed2db9c6 ...
found 8aed2db9c6 in https://yhetil.org/emacs/87k03ya8no.fsf@thornhill.no/
found 5ec7da9f5c in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 5ec7da9f5c724de2421c4116a811d0cd38792d67	lisp/progmodes/java-ts-mode.el

applying [1/1] https://yhetil.org/emacs/87k03ya8no.fsf@thornhill.no/
diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index 5ec7da9f5c..8aed2db9c6 100644

Checking patch lisp/progmodes/java-ts-mode.el...
Applied patch lisp/progmodes/java-ts-mode.el cleanly.

index at:
100644 8aed2db9c6602980d9cec4fa33a05dae44210da1	lisp/progmodes/java-ts-mode.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.