emacs/lisp/progmodes/java-ts-mode.el
Stefan Kangas dce6791e99 Merge from origin/emacs-29
db96b1282f * lisp/help.el: Use 'C-h C-q' to toggle 'help-quick' wind...
489865c21e ; Improve markup of long key sequences
d42c2668cf ; * etc/NEWS: Fix wording of a recently edited entry.
7a0eaee198 * lisp/isearch.el: Small fixes.
b69bffeec0 * lisp/vc/diff-mode.el (diff-minor-mode-prefix): Replace ...
9263847ab7 ; * etc/NEWS: Move the paragraph with 'C-u RET' closer to...
62fb2dc37d * doc/emacs/display.texi (Text Scale): Improve section ab...
70480d3b6b * lisp/repeat.el (repeat-echo-function): Suggest 'add-fun...
fd48201ffe * lisp/tab-line.el (tab-line-cache-key-default): More cac...
b164660260 * etc/package-keyring.gpg: Update with new key
c0be51389e ; Yet another declare-function to avoid treesit-related w...
8676bec51d ; * lisp/treesit.el (treesit--simple-imenu-1): Doc fix; w...
2ddc480f44 Warn of absent networks module in ERC
19d00fab9a Avoid "already compiled" warning in erc-compat
2d8f7b66bc ; Fix one more treesit byte-compilation warning.
2d0a921486 ; Avoid treesit-related byte-compiler warnings
8503b370be (python--treesit-settings): Remove duplicate matcher
b464e6c490 Make last change of w32 GUI dialogs conditional and rever...
eedc9d79ae Fix tree-sitter typos
248c13dcfe Update tree-sitter major modes to use the new Imenu facility
b39dc7ab27 Add tree-sitter helper functions for Imenu
ba1ddea9da Fix treesit--things-around (bug#60355)
7512b9025a ; * lisp/treesit.el (treesit-traverse-parent): Remove alias.
5326b04198 Improve treesit-node-top-level and treesit-parent-until
637f5b164f ; Add "src" to the heuristic sub-directory heuristic
8ab6df0c9f ; * lisp/epa-ks.el (epa-ks-do-key-to-fetch): Fix 'when' u...
2b55a48d3e * src/w32menu.c (simple_dialog_show): Use MB_YESNOCANCEL ...
8b8b791567 ; Improve documentation of TAB/SPC indentation
624e382211 ; Improve doc strings of some new faces
41f12e1019 ; * lisp/elide-head.el (elide-head): Doc fix to silence c...
e3b4cd0ac1 ; * lisp/htmlfontify.el (hfy-text-p): Fix whitespace.
1b4dc4691c Fix htmlfontify.el command injection vulnerability.
1fe4b98b4d Improve support for Scheme R6RS and R7RS libraries (bug#5...
2347f37f67 ; * test/src/treesit-tests.el: remove dead store (bytecom...
a6d961ae2f Add a new tree-sitter query predicate 'pred'
835a80dcc4 ; Fix tree-sitter defun tests
a14821d615 Improve gnutls-min-prime-bits docstring
b14bbd108e Improve handling of tab-bar height.
669160d47b ; * nt/INSTALL.W64: More fixes and updates.
26b2ec7cb8 Simplify last change (bug#60311)
082fc6e308 Fix 'json-available-p' on MS-Windows
6c86faec29 loaddefs-gen: Group results by absolute file name
d90d7d15f2 ; Fix vindexes in parsing.texi
eb26872837 Fix imenu for c-ts-mode (bug#60296)
8f68b6497e Clean up python-ts-mode font-lock features
28f26b11a1 Add comment indent and filling to other tree-sitter major...
c6b0282645 ; Remove unused function in c-ts-mode
6e52a9fcad ; * doc/lispref/modes.texi (Parser-based Font Lock): Mino...
2bcd1e9a99 ; * doc/lispref/parsing.texi (Retrieving Nodes): Add notice.
7c7950fe00 Add maintainer stub for tree-sitter files
cf32776622 ; * doc/lispref/parsing.texi (Using Parser): Remove delet...

# Conflicts:
#	etc/NEWS
#	lisp/progmodes/c-ts-mode.el
#	lisp/progmodes/typescript-ts-mode.el
#	lisp/treesit.el
2022-12-28 21:40:59 +01:00

326 lines
12 KiB
EmacsLisp

;;; 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.
;; 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:
;;
;;; Code:
(require 'treesit)
(eval-when-compile (require 'rx))
(require 'c-ts-mode) ; 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")
(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)
(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") parent-bol 0)
((node-is "}") (and parent parent-bol) 0)
((node-is ")") parent-bol 0)
((node-is "]") parent-bol 0)
((and (parent-is "comment") c-ts-mode--looking-at-star)
c-ts-mode--comment-start-after-first-star -1)
((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "text_block") no-indent)
((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 "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") (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")
"Java keywords for tree-sitter font-locking.")
(defvar java-ts-mode--operators
'("+" ":" "++" "-" "--" "&" "&&" "|" "||" "="
"!=" "==" "*" "/" "%" "<" "<=" ">" ">="
"-=" "+=" "*=" "/=" "%=" "->" "^" "^="
"|=" "~" ">>" ">>>" "<<" "::" "?" "&=")
"Java operators for tree-sitter font-locking.")
(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_][A-Z_\\d]*$" @font-lock-constant-face))
[(true) (false)] @font-lock-constant-face)
:language 'java
:override t
:feature 'keyword
`([,@java-ts-mode--keywords
(this)] @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
`((string_literal) @font-lock-string-face
(text_block) @font-lock-string-face)
: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
'((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 (identifier) @font-lock-variable-name-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)
(variable_declarator
name: (identifier) @font-lock-variable-name-face)
(element_value_pair
key: (identifier) @font-lock-property-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))
: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))))
;;;###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"))
(treesit-parser-create 'java)
;; Comments.
(c-ts-mode-comment-setup)
(setq-local treesit-text-type-regexp
(regexp-opt '("line_comment"
"block_comment"
"text_block")))
;; 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
(regexp-opt '("method_declaration"
"class_declaration"
"record_declaration"
"interface_declaration"
"enum_declaration"
"import_declaration"
"package_declaration"
"module_declaration")))
(setq-local treesit-defun-name-function #'java-ts-mode--defun-name)
;; Font-lock.
(setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings)
(setq-local treesit-font-lock-feature-list
'(( comment definition )
( constant keyword string type)
( annotation expression literal)
( bracket delimiter operator)))
;; 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))
(provide 'java-ts-mode)
;;; java-ts-mode.el ends here