2022-11-12 14:33:14 -08:00
|
|
|
;;; java-ts-mode.el --- tree-sitter support for Java -*- lexical-binding: t; -*-
|
2022-11-10 17:15:49 +01:00
|
|
|
|
2023-01-01 05:31:12 -05:00
|
|
|
;; Copyright (C) 2022-2023 Free Software Foundation, Inc.
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;; 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.
|
|
|
|
|
2022-12-08 23:56:24 +01:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2022-11-10 17:15:49 +01:00
|
|
|
;; 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.
|
|
|
|
|
2022-12-08 23:56:24 +01:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2022-11-10 17:15:49 +01:00
|
|
|
;; 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
|
2022-12-08 23:56:24 +01:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'treesit)
|
2022-11-27 14:15:57 -08:00
|
|
|
(eval-when-compile (require 'rx))
|
2023-01-21 12:24:55 +01:00
|
|
|
(require 'c-ts-common) ; For comment indent and filling.
|
2022-11-10 17:15:49 +01:00
|
|
|
|
2022-11-21 19:08:25 +02:00
|
|
|
(declare-function treesit-parser-create "treesit.c")
|
|
|
|
(declare-function treesit-induce-sparse-tree "treesit.c")
|
|
|
|
(declare-function treesit-node-start "treesit.c")
|
2022-12-28 16:41:58 +02:00
|
|
|
(declare-function treesit-node-type "treesit.c")
|
2022-11-21 19:08:25 +02:00
|
|
|
(declare-function treesit-node-child-by-field-name "treesit.c")
|
2023-03-09 12:23:14 +02:00
|
|
|
(declare-function treesit-node-child-by-field-name "treesit.c")
|
|
|
|
(declare-function treesit-query-capture "treesit.c")
|
2022-11-21 19:08:25 +02:00
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
(defcustom java-ts-mode-indent-offset 4
|
|
|
|
"Number of spaces for each indentation step in `java-ts-mode'."
|
2022-11-14 08:03:11 +01:00
|
|
|
:version "29.1"
|
2022-11-10 17:15:49 +01:00
|
|
|
: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)
|
2022-12-05 13:37:58 +01:00
|
|
|
(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)
|
2022-11-10 17:15:49 +01:00
|
|
|
table)
|
|
|
|
"Syntax table for `java-ts-mode'.")
|
|
|
|
|
|
|
|
(defvar java-ts-mode--indent-rules
|
|
|
|
`((java
|
2023-03-04 01:09:00 -08:00
|
|
|
((parent-is "program") column-0 0)
|
2023-02-05 08:49:08 +01:00
|
|
|
((match "}" "element_value_array_initializer")
|
|
|
|
parent-bol 0)
|
2023-03-04 01:09:00 -08:00
|
|
|
((node-is "}") column-0 c-ts-common-statement-offset)
|
2022-11-10 17:15:49 +01:00
|
|
|
((node-is ")") parent-bol 0)
|
2023-02-05 08:49:08 +01:00
|
|
|
((node-is "else") parent-bol 0)
|
2022-11-10 17:15:49 +01:00
|
|
|
((node-is "]") parent-bol 0)
|
2023-01-21 12:24:55 +01:00
|
|
|
((and (parent-is "comment") c-ts-common-looking-at-star)
|
|
|
|
c-ts-common-comment-start-after-first-star -1)
|
2022-12-25 11:21:50 -08:00
|
|
|
((parent-is "comment") prev-adaptive-prefix 0)
|
2022-12-09 22:36:03 +01:00
|
|
|
((parent-is "text_block") no-indent)
|
2023-03-04 01:09:00 -08:00
|
|
|
((parent-is "class_body") column-0 c-ts-common-statement-offset)
|
2023-02-05 08:24:14 +01:00
|
|
|
((parent-is "array_initializer") parent-bol java-ts-mode-indent-offset)
|
2023-03-04 01:09:00 -08:00
|
|
|
((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") column-0 c-ts-common-statement-offset)
|
2023-01-25 13:00:33 +01:00
|
|
|
((parent-is "enum_body_declarations") parent-bol 0)
|
2023-03-04 01:09:00 -08:00
|
|
|
((parent-is "enum_body") column-0 c-ts-common-statement-offset)
|
|
|
|
((parent-is "switch_block") column-0 c-ts-common-statement-offset)
|
|
|
|
((parent-is "record_declaration_body") column-0 c-ts-common-statement-offset)
|
2022-11-10 17:15:49 +01:00
|
|
|
((query "(method_declaration (block _ @indent))") parent-bol java-ts-mode-indent-offset)
|
|
|
|
((query "(method_declaration (block (_) @indent))") parent-bol java-ts-mode-indent-offset)
|
2023-01-13 11:33:58 +01:00
|
|
|
((parent-is "local_variable_declaration") parent-bol java-ts-mode-indent-offset)
|
|
|
|
((parent-is "expression_statement") parent-bol java-ts-mode-indent-offset)
|
2023-01-28 19:51:08 +01:00
|
|
|
((match "type_identifier" "field_declaration") parent-bol 0)
|
2023-01-13 11:33:58 +01:00
|
|
|
((parent-is "field_declaration") parent-bol java-ts-mode-indent-offset)
|
|
|
|
((parent-is "return_statement") parent-bol java-ts-mode-indent-offset)
|
2022-11-10 17:15:49 +01:00
|
|
|
((parent-is "variable_declarator") parent-bol java-ts-mode-indent-offset)
|
2023-09-17 08:18:54 +02:00
|
|
|
((match ">" "type_arguments") parent-bol 0)
|
|
|
|
((parent-is "type_arguments") parent-bol java-ts-mode-indent-offset)
|
2022-11-10 17:15:49 +01:00
|
|
|
((parent-is "method_invocation") parent-bol java-ts-mode-indent-offset)
|
|
|
|
((parent-is "switch_rule") parent-bol java-ts-mode-indent-offset)
|
2023-01-25 13:00:33 +01:00
|
|
|
((parent-is "switch_label") parent-bol java-ts-mode-indent-offset)
|
2022-11-10 17:15:49 +01:00
|
|
|
((parent-is "ternary_expression") parent-bol java-ts-mode-indent-offset)
|
2022-12-15 14:54:17 +01:00
|
|
|
((parent-is "lambda_expression") parent-bol java-ts-mode-indent-offset)
|
2022-11-10 17:15:49 +01:00
|
|
|
((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)
|
2023-03-04 01:09:00 -08:00
|
|
|
((parent-is "block") column-0 c-ts-common-statement-offset)))
|
2022-11-10 17:15:49 +01:00
|
|
|
"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"
|
2023-01-11 21:49:04 +01:00
|
|
|
"try" "uses" "volatile" "while" "with" "record"
|
|
|
|
"@interface")
|
2022-12-08 17:49:55 +02:00
|
|
|
"Java keywords for tree-sitter font-locking.")
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
(defvar java-ts-mode--operators
|
2022-11-21 13:12:03 +01:00
|
|
|
'("+" ":" "++" "-" "--" "&" "&&" "|" "||" "="
|
|
|
|
"!=" "==" "*" "/" "%" "<" "<=" ">" ">="
|
|
|
|
"-=" "+=" "*=" "/=" "%=" "->" "^" "^="
|
|
|
|
"|=" "~" ">>" ">>>" "<<" "::" "?" "&=")
|
2022-12-08 16:51:59 +01:00
|
|
|
"Java operators for tree-sitter font-locking.")
|
2022-11-10 17:15:49 +01:00
|
|
|
|
2023-03-06 12:24:34 +01:00
|
|
|
(defun java-ts-mode--string-highlight-helper ()
|
|
|
|
"Returns, 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))))
|
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
(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
|
2023-06-13 13:53:31 +01:00
|
|
|
(:match "\\`[A-Z_][0-9A-Z_]*\\'" @font-lock-constant-face))
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
[(true) (false)] @font-lock-constant-face)
|
2022-11-10 17:15:49 +01:00
|
|
|
:language 'java
|
|
|
|
:override t
|
|
|
|
:feature 'keyword
|
2022-12-15 11:48:32 +01:00
|
|
|
`([,@java-ts-mode--keywords
|
2023-02-15 18:54:46 +01:00
|
|
|
(this)
|
|
|
|
(super)] @font-lock-keyword-face
|
2022-12-15 11:48:32 +01:00
|
|
|
(labeled_statement
|
|
|
|
(identifier) @font-lock-keyword-face))
|
2022-11-10 17:15:49 +01:00
|
|
|
:language 'java
|
|
|
|
:override t
|
|
|
|
:feature 'operator
|
2022-11-21 13:12:03 +01:00
|
|
|
`([,@java-ts-mode--operators] @font-lock-operator-face
|
|
|
|
"@" @font-lock-constant-face)
|
2022-11-10 17:15:49 +01:00
|
|
|
: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
|
2023-03-06 12:24:34 +01:00
|
|
|
(java-ts-mode--string-highlight-helper)
|
2022-11-10 17:15:49 +01:00
|
|
|
:language 'java
|
|
|
|
:override t
|
|
|
|
:feature 'literal
|
|
|
|
`((null_literal) @font-lock-constant-face
|
Utilize new font-lock faces for more tree-sitter modes (Bug#59397)
* lisp/progmodes/java-ts-mode.el (java-ts-mode--font-lock-settings):
Use font-lock-number-face.
(java-ts-mode): Alphabetize features.
* lisp/progmodes/js.el (js--treesit-operators): Define operators.
(js--treesit-font-lock-settings): Use bracket, delimiter,
escape-sequence, property, number, and operator font-lock faces.
(js-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Use bracket, delimiter, escape-sequence, and number faces. Remove
unused features.
(json-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/sh-script.el (sh-mode--treesit-settings): Use
bracket, delimiter, number, misc-punctuation, and operator font-lock
faces.
(sh-mode--treesit-operators): Remove ; and ;; from list.
(bash-ts-mode): Add them to the feature list and alphabetize.
* lisp/progmodes/ts-mode.el (ts-mode--operators): Define operators.
(ts-mode--font-lock-settings): Use escape-sequence, number, and
operator font-lock faces.
(ts-mode): Add them to the feature list and alphabetize.
2022-11-19 22:30:13 -05:00
|
|
|
(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)
|
2022-11-10 17:15:49 +01:00
|
|
|
:language 'java
|
|
|
|
:override t
|
|
|
|
:feature 'type
|
2023-01-11 21:49:04 +01:00
|
|
|
'((annotation_type_declaration
|
|
|
|
name: (identifier) @font-lock-type-face)
|
|
|
|
|
|
|
|
(interface_declaration
|
2022-11-10 17:15:49 +01:00
|
|
|
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)
|
|
|
|
|
2023-05-30 15:29:38 +02:00
|
|
|
(compact_constructor_declaration
|
|
|
|
name: (identifier) @font-lock-type-face)
|
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
(field_access
|
|
|
|
object: (identifier) @font-lock-type-face)
|
|
|
|
|
|
|
|
(method_reference (identifier) @font-lock-type-face)
|
|
|
|
|
2023-02-25 03:15:46 +02:00
|
|
|
(scoped_identifier (identifier) @font-lock-constant-face)
|
2022-11-21 13:12:03 +01:00
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
((scoped_identifier name: (identifier) @font-lock-type-face)
|
2023-06-13 13:45:08 +01:00
|
|
|
(:match "\\`[A-Z]" @font-lock-type-face))
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
(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
|
2023-01-14 15:18:40 +01:00
|
|
|
`((annotation_type_element_declaration
|
|
|
|
name: (identifier) @font-lock-function-name-face)
|
|
|
|
|
|
|
|
(method_declaration
|
2022-11-10 17:15:49 +01:00
|
|
|
name: (identifier) @font-lock-function-name-face)
|
|
|
|
|
2022-11-21 13:12:03 +01:00
|
|
|
(variable_declarator
|
|
|
|
name: (identifier) @font-lock-variable-name-face)
|
|
|
|
|
|
|
|
(element_value_pair
|
2023-02-28 04:07:55 +02:00
|
|
|
key: (identifier) @font-lock-property-use-face)
|
2022-11-21 13:12:03 +01:00
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
(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
|
2023-02-28 04:07:55 +02:00
|
|
|
object: (identifier) @font-lock-variable-use-face)
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
(method_invocation
|
2023-02-25 03:15:46 +02:00
|
|
|
name: (identifier) @font-lock-function-call-face)
|
2022-11-10 17:15:49 +01:00
|
|
|
|
2023-01-02 13:46:25 +01:00
|
|
|
(argument_list (identifier) @font-lock-variable-name-face)
|
|
|
|
|
2023-02-28 04:07:55 +02:00
|
|
|
(expression_statement (identifier) @font-lock-variable-use-face))
|
2022-11-21 13:12:03 +01:00
|
|
|
|
|
|
|
:language 'java
|
|
|
|
:feature 'bracket
|
|
|
|
'((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
|
|
|
|
|
|
|
|
:language 'java
|
|
|
|
:feature 'delimiter
|
|
|
|
'((["," ":" ";"]) @font-lock-delimiter-face))
|
2022-12-08 16:51:59 +01:00
|
|
|
"Tree-sitter font-lock settings for `java-ts-mode'.")
|
2022-11-10 17:15:49 +01:00
|
|
|
|
2022-12-24 18:24:01 -08:00
|
|
|
(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))))
|
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode java-ts-mode prog-mode "Java"
|
2022-11-12 14:33:14 -08:00
|
|
|
"Major mode for editing Java, powered by tree-sitter."
|
2022-11-14 08:03:11 +01:00
|
|
|
:group 'java
|
2022-11-10 17:15:49 +01:00
|
|
|
:syntax-table java-ts-mode--syntax-table
|
|
|
|
|
2022-11-19 18:58:12 -08:00
|
|
|
(unless (treesit-ready-p 'java)
|
2022-11-10 17:15:49 +01:00
|
|
|
(error "Tree-sitter for Java isn't available"))
|
|
|
|
|
|
|
|
(treesit-parser-create 'java)
|
|
|
|
|
|
|
|
;; Comments.
|
2023-01-21 12:24:55 +01:00
|
|
|
(c-ts-common-comment-setup)
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;; Indent.
|
2023-02-06 18:48:04 -08:00
|
|
|
(setq-local c-ts-common-indent-type-regexp-alist
|
|
|
|
`((block . ,(rx (or "class_body"
|
|
|
|
"array_initializer"
|
|
|
|
"constructor_body"
|
|
|
|
"annotation_type_body"
|
|
|
|
"interface_body"
|
2023-02-12 18:57:40 +01:00
|
|
|
"lambda_expression"
|
2023-02-06 18:48:04 -08:00
|
|
|
"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")))
|
2023-02-05 08:49:08 +01:00
|
|
|
(setq-local c-ts-common-indent-offset 'java-ts-mode-indent-offset)
|
2022-11-10 17:15:49 +01:00
|
|
|
(setq-local treesit-simple-indent-rules java-ts-mode--indent-rules)
|
|
|
|
|
2022-11-14 08:03:11 +01:00
|
|
|
;; Electric
|
|
|
|
(setq-local electric-indent-chars
|
|
|
|
(append "{}():;," electric-indent-chars))
|
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
;; Navigation.
|
2022-12-07 10:07:37 +01:00
|
|
|
(setq-local treesit-defun-type-regexp
|
|
|
|
(regexp-opt '("method_declaration"
|
|
|
|
"class_declaration"
|
|
|
|
"record_declaration"
|
|
|
|
"interface_declaration"
|
|
|
|
"enum_declaration"
|
|
|
|
"import_declaration"
|
|
|
|
"package_declaration"
|
2023-01-14 22:39:08 +01:00
|
|
|
"module_declaration"
|
|
|
|
"constructor_declaration")))
|
2022-12-24 18:24:01 -08:00
|
|
|
(setq-local treesit-defun-name-function #'java-ts-mode--defun-name)
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;; Font-lock.
|
|
|
|
(setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings)
|
|
|
|
(setq-local treesit-font-lock-feature-list
|
2022-11-26 15:05:57 -08:00
|
|
|
'(( comment definition )
|
|
|
|
( constant keyword string type)
|
|
|
|
( annotation expression literal)
|
|
|
|
( bracket delimiter operator)))
|
2022-11-10 17:15:49 +01:00
|
|
|
|
|
|
|
;; Imenu.
|
2022-12-27 20:57:12 -08:00
|
|
|
(setq-local treesit-simple-imenu-settings
|
|
|
|
'(("Class" "\\`class_declaration\\'" nil nil)
|
2022-12-28 00:32:37 -08:00
|
|
|
("Interface" "\\`interface_declaration\\'" nil nil)
|
2022-12-27 20:57:12 -08:00
|
|
|
("Enum" "\\`record_declaration\\'" nil nil)
|
|
|
|
("Method" "\\`method_declaration\\'" nil nil)))
|
2022-11-10 17:15:49 +01:00
|
|
|
(treesit-major-mode-setup))
|
|
|
|
|
2023-01-20 10:28:26 +02:00
|
|
|
(if (treesit-ready-p 'java)
|
|
|
|
(add-to-list 'auto-mode-alist '("\\.java\\'" . java-ts-mode)))
|
|
|
|
|
2022-11-10 17:15:49 +01:00
|
|
|
(provide 'java-ts-mode)
|
|
|
|
|
|
|
|
;;; java-ts-mode.el ends here
|