2022-11-25 21:04:18 +01:00
|
|
|
;;; typescript-ts-mode.el --- tree sitter support for TypeScript -*- lexical-binding: t; -*-
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2023-01-01 05:31:12 -05:00
|
|
|
;; Copyright (C) 2022-2023 Free Software Foundation, Inc.
|
2022-10-11 10:27:55 +02:00
|
|
|
|
|
|
|
;; Author : Theodor Thornhill <theo@thornhill.no>
|
|
|
|
;; Maintainer : Theodor Thornhill <theo@thornhill.no>
|
|
|
|
;; Created : October 2022
|
|
|
|
;; Keywords : typescript tsx 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-10-11 10:27:55 +02: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-10-11 10:27:55 +02: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-29 21:39:38 +01:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
|
2022-10-11 23:49:04 -07:00
|
|
|
;;; Code:
|
|
|
|
|
2022-10-11 10:27:55 +02:00
|
|
|
(require 'treesit)
|
|
|
|
(require 'js)
|
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-10-11 10:27:55 +02:00
|
|
|
|
2023-09-16 14:26:46 +03:00
|
|
|
(declare-function treesit-node-start "treesit.c")
|
|
|
|
(declare-function treesit-node-end "treesit.c")
|
2022-11-24 21:24:29 +02:00
|
|
|
(declare-function treesit-parser-create "treesit.c")
|
2023-07-23 08:16:14 +03:00
|
|
|
(declare-function treesit-query-capture "treesit.c")
|
2023-09-16 14:26:46 +03:00
|
|
|
(declare-function treesit-query-compile "treesit.c")
|
2022-11-24 21:24:29 +02:00
|
|
|
|
2022-11-25 21:04:18 +01:00
|
|
|
(defcustom typescript-ts-mode-indent-offset 2
|
|
|
|
"Number of spaces for each indentation step in `typescript-ts-mode'."
|
2022-11-14 08:03:11 +01:00
|
|
|
:version "29.1"
|
2022-10-11 10:27:55 +02:00
|
|
|
:type 'integer
|
|
|
|
:safe 'integerp
|
|
|
|
:group 'typescript)
|
|
|
|
|
2023-03-24 16:45:15 -07:00
|
|
|
(defface typescript-ts-jsx-tag-face
|
|
|
|
'((t . (:inherit font-lock-function-call-face)))
|
|
|
|
"Face for HTML tags like <div> and <p> in JSX."
|
|
|
|
:group 'typescript)
|
|
|
|
|
|
|
|
(defface typescript-ts-jsx-attribute-face
|
|
|
|
'((t . (:inherit font-lock-constant-face)))
|
|
|
|
"Face for HTML attributes like name and id in JSX."
|
|
|
|
:group 'typescript)
|
|
|
|
|
2022-11-25 21:04:18 +01:00
|
|
|
(defvar typescript-ts-mode--syntax-table
|
2022-10-11 10:27:55 +02:00
|
|
|
(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)
|
2022-12-05 13:37:58 +01:00
|
|
|
(modify-syntax-entry ?\' "\"" table)
|
2022-10-11 10:27:55 +02:00
|
|
|
(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)
|
|
|
|
(modify-syntax-entry ?` "\"" table)
|
2022-10-11 10:27:55 +02:00
|
|
|
table)
|
2022-11-25 21:04:18 +01:00
|
|
|
"Syntax table for `typescript-ts-mode'.")
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2023-07-22 13:37:54 +02:00
|
|
|
(defun tsx-ts-mode--indent-compatibility-b893426 ()
|
|
|
|
"Indent rules helper, to handle different releases of tree-sitter-tsx.
|
|
|
|
Check if a node type is available, then return the right indent rules."
|
|
|
|
;; handle commit b893426
|
|
|
|
(condition-case nil
|
|
|
|
(progn (treesit-query-capture 'tsx '((jsx_fragment) @capture))
|
|
|
|
`(((match "<" "jsx_fragment") parent 0)
|
|
|
|
((parent-is "jsx_fragment") parent typescript-ts-mode-indent-offset)))
|
2023-09-16 23:03:26 +03:00
|
|
|
(treesit-query-error
|
2023-07-22 13:37:54 +02:00
|
|
|
`(((match "<" "jsx_text") parent 0)
|
|
|
|
((parent-is "jsx_text") parent typescript-ts-mode-indent-offset)))))
|
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
(defun typescript-ts-mode--indent-rules (language)
|
|
|
|
"Rules used for indentation.
|
|
|
|
Argument LANGUAGE is either `typescript' or `tsx'."
|
|
|
|
`((,language
|
2023-03-04 01:09:00 -08:00
|
|
|
((parent-is "program") column-0 0)
|
2022-10-11 10:27:55 +02:00
|
|
|
((node-is "}") parent-bol 0)
|
|
|
|
((node-is ")") parent-bol 0)
|
|
|
|
((node-is "]") parent-bol 0)
|
|
|
|
((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-11-25 21:04:18 +01:00
|
|
|
((parent-is "ternary_expression") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "member_expression") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "named_imports") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "statement_block") parent-bol typescript-ts-mode-indent-offset)
|
2023-11-27 08:55:23 -08:00
|
|
|
((or (node-is "case")
|
|
|
|
(node-is "default"))
|
|
|
|
parent-bol typescript-ts-mode-indent-offset)
|
2023-02-20 13:38:55 +01:00
|
|
|
((parent-is "switch_case") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "switch_default") parent-bol typescript-ts-mode-indent-offset)
|
2022-11-25 21:04:18 +01:00
|
|
|
((parent-is "type_arguments") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "variable_declarator") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "arguments") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "array") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "formal_parameters") parent-bol typescript-ts-mode-indent-offset)
|
2023-02-14 20:53:55 +01:00
|
|
|
((parent-is "template_string") no-indent) ; Don't indent the string contents.
|
2022-11-25 21:04:18 +01:00
|
|
|
((parent-is "template_substitution") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "object_pattern") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "object") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "object_type") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "enum_body") parent-bol typescript-ts-mode-indent-offset)
|
2022-11-29 16:12:18 +01:00
|
|
|
((parent-is "class_body") parent-bol typescript-ts-mode-indent-offset)
|
2022-11-25 21:04:18 +01:00
|
|
|
((parent-is "arrow_function") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "parenthesized_expression") parent-bol typescript-ts-mode-indent-offset)
|
2022-12-14 21:23:33 +01:00
|
|
|
((parent-is "binary_expression") parent-bol typescript-ts-mode-indent-offset)
|
2023-11-21 15:59:48 +02:00
|
|
|
((match "while" "do_statement") parent-bol 0)
|
|
|
|
((match "else" "if_statement") parent-bol 0)
|
|
|
|
((parent-is ,(rx (or (seq (or "if" "for" "for_in" "while" "do") "_statement")
|
|
|
|
"else_clause")))
|
|
|
|
parent-bol typescript-ts-mode-indent-offset)
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
,@(when (eq language 'tsx)
|
2023-07-22 13:37:54 +02:00
|
|
|
(append (tsx-ts-mode--indent-compatibility-b893426)
|
|
|
|
`(((node-is "jsx_closing_element") parent 0)
|
|
|
|
((match "jsx_element" "statement") parent typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "jsx_element") parent typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "jsx_text") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "jsx_opening_element") parent typescript-ts-mode-indent-offset)
|
|
|
|
((parent-is "jsx_expression") parent-bol typescript-ts-mode-indent-offset)
|
|
|
|
((match "/" "jsx_self_closing_element") parent 0)
|
|
|
|
((parent-is "jsx_self_closing_element") parent typescript-ts-mode-indent-offset))))
|
2023-02-18 23:10:13 +01:00
|
|
|
;; FIXME(Theo): This no-node catch-all should be removed. When is it needed?
|
2022-11-29 21:39:38 +01:00
|
|
|
(no-node parent-bol 0))))
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2022-11-25 21:04:18 +01:00
|
|
|
(defvar typescript-ts-mode--keywords
|
2022-10-17 12:49:19 +02:00
|
|
|
'("!" "abstract" "as" "async" "await" "break"
|
|
|
|
"case" "catch" "class" "const" "continue" "debugger"
|
|
|
|
"declare" "default" "delete" "do" "else" "enum"
|
|
|
|
"export" "extends" "finally" "for" "from" "function"
|
2023-06-06 06:26:43 +02:00
|
|
|
"get" "if" "implements" "import" "in" "instanceof" "interface" "is" "infer"
|
2022-10-17 12:49:19 +02:00
|
|
|
"keyof" "let" "namespace" "new" "of" "private" "protected"
|
2023-08-03 11:31:41 +03:00
|
|
|
"public" "readonly" "return" "satisfies" "set" "static" "switch"
|
2022-10-17 12:49:19 +02:00
|
|
|
"target" "throw" "try" "type" "typeof" "var" "void"
|
|
|
|
"while" "with" "yield")
|
|
|
|
"TypeScript keywords for tree-sitter font-locking.")
|
|
|
|
|
2022-11-25 21:04:18 +01:00
|
|
|
(defvar typescript-ts-mode--operators
|
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
|
|
|
'("=" "+=" "-=" "*=" "/=" "%=" "**=" "<<=" ">>=" ">>>=" "&=" "^="
|
|
|
|
"|=" "&&=" "||=" "??=" "==" "!=" "===" "!==" ">" ">=" "<" "<=" "+"
|
|
|
|
"-" "*" "/" "%" "++" "--" "**" "&" "|" "^" "~" "<<" ">>" ">>>"
|
|
|
|
"&&" "||" "!" "?.")
|
|
|
|
"TypeScript operators for tree-sitter font-locking.")
|
|
|
|
|
2023-07-24 16:17:34 +02:00
|
|
|
(defun tsx-ts-mode--font-lock-compatibility-bb1f97b (language)
|
2023-07-22 13:37:54 +02:00
|
|
|
"Font lock rules helper, to handle different releases of tree-sitter-tsx.
|
2023-07-24 16:17:34 +02:00
|
|
|
Check if a node type is available, then return the right font lock rules.
|
|
|
|
Argument LANGUAGE is either `typescript' or `tsx'."
|
2023-07-22 13:37:54 +02:00
|
|
|
;; handle commit bb1f97b
|
|
|
|
;; Warning: treesitter-query-capture says both node types are valid,
|
|
|
|
;; but then raises an error if the wrong node type is used. So it is
|
|
|
|
;; important to check with the new node type (member_expression)
|
|
|
|
(condition-case nil
|
2023-10-21 15:05:45 +03:00
|
|
|
(progn (treesit-query-capture language '((jsx_opening_element (member_expression) @capture)))
|
2023-07-22 13:37:54 +02:00
|
|
|
'((jsx_opening_element
|
|
|
|
[(member_expression (identifier)) (identifier)]
|
|
|
|
@typescript-ts-jsx-tag-face)
|
|
|
|
|
|
|
|
(jsx_closing_element
|
|
|
|
[(member_expression (identifier)) (identifier)]
|
|
|
|
@typescript-ts-jsx-tag-face)
|
|
|
|
|
|
|
|
(jsx_self_closing_element
|
|
|
|
[(member_expression (identifier)) (identifier)]
|
|
|
|
@typescript-ts-jsx-tag-face)))
|
2023-09-16 23:03:26 +03:00
|
|
|
(treesit-query-error
|
|
|
|
'((jsx_opening_element
|
2023-07-22 13:37:54 +02:00
|
|
|
[(nested_identifier (identifier)) (identifier)]
|
|
|
|
@typescript-ts-jsx-tag-face)
|
|
|
|
|
|
|
|
(jsx_closing_element
|
|
|
|
[(nested_identifier (identifier)) (identifier)]
|
|
|
|
@typescript-ts-jsx-tag-face)
|
|
|
|
|
|
|
|
(jsx_self_closing_element
|
|
|
|
[(nested_identifier (identifier)) (identifier)]
|
|
|
|
@typescript-ts-jsx-tag-face)))))
|
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
(defun typescript-ts-mode--font-lock-settings (language)
|
|
|
|
"Tree-sitter font-lock settings.
|
|
|
|
Argument LANGUAGE is either `typescript' or `tsx'."
|
2022-10-11 10:27:55 +02:00
|
|
|
(treesit-font-lock-rules
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'comment
|
2023-12-09 11:35:44 -08:00
|
|
|
`([(comment) (hash_bang_line)] @font-lock-comment-face)
|
2022-11-03 11:53:49 -07:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-10-28 21:23:19 +02:00
|
|
|
: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) (null)] @font-lock-constant-face)
|
2022-11-03 11:53:49 -07:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'keyword
|
2022-11-25 21:04:18 +01:00
|
|
|
`([,@typescript-ts-mode--keywords] @font-lock-keyword-face
|
2022-10-28 21:23:19 +02:00
|
|
|
[(this) (super)] @font-lock-keyword-face)
|
2022-11-03 11:53:49 -07:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'string
|
2023-01-06 19:56:20 +02:00
|
|
|
`((regex pattern: (regex_pattern)) @font-lock-regexp-face
|
2022-10-17 12:49:19 +02:00
|
|
|
(string) @font-lock-string-face
|
2022-10-19 16:44:04 -07:00
|
|
|
(template_string) @js--fontify-template-string
|
2023-01-06 16:17:50 +02:00
|
|
|
(template_substitution ["${" "}"] @font-lock-misc-punctuation-face))
|
2022-11-03 11:53:49 -07:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2023-01-25 10:38:09 +01:00
|
|
|
:override t ;; for functions assigned to variables
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'declaration
|
|
|
|
`((function
|
2022-10-11 10:27:55 +02:00
|
|
|
name: (identifier) @font-lock-function-name-face)
|
|
|
|
(function_declaration
|
|
|
|
name: (identifier) @font-lock-function-name-face)
|
2023-06-03 15:00:41 +03:00
|
|
|
(function_signature
|
|
|
|
name: (identifier) @font-lock-function-name-face)
|
2022-10-11 10:27:55 +02:00
|
|
|
|
|
|
|
(method_definition
|
|
|
|
name: (property_identifier) @font-lock-function-name-face)
|
2023-01-02 20:56:41 +01:00
|
|
|
(method_signature
|
|
|
|
name: (property_identifier) @font-lock-function-name-face)
|
2022-11-28 16:05:27 +01:00
|
|
|
(required_parameter (identifier) @font-lock-variable-name-face)
|
|
|
|
(optional_parameter (identifier) @font-lock-variable-name-face)
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2023-01-25 10:38:09 +01:00
|
|
|
(variable_declarator
|
|
|
|
name: (identifier) @font-lock-function-name-face
|
|
|
|
value: [(function) (arrow_function)])
|
|
|
|
|
2022-10-17 12:49:19 +02:00
|
|
|
(variable_declarator
|
|
|
|
name: (identifier) @font-lock-variable-name-face)
|
|
|
|
|
|
|
|
(enum_declaration (identifier) @font-lock-type-face)
|
|
|
|
|
2022-11-28 16:05:27 +01:00
|
|
|
(extends_clause value: (identifier) @font-lock-type-face)
|
|
|
|
;; extends React.Component<T>
|
|
|
|
(extends_clause value: (member_expression
|
|
|
|
object: (identifier) @font-lock-type-face
|
|
|
|
property: (property_identifier) @font-lock-type-face))
|
|
|
|
|
2022-10-28 21:23:19 +02:00
|
|
|
(arrow_function
|
|
|
|
parameter: (identifier) @font-lock-variable-name-face)
|
|
|
|
|
|
|
|
(variable_declarator
|
|
|
|
name: (array_pattern
|
|
|
|
(identifier)
|
|
|
|
(identifier) @font-lock-function-name-face)
|
2023-01-04 09:13:23 +01:00
|
|
|
value: (array (number) (function)))
|
|
|
|
|
|
|
|
(catch_clause
|
2023-01-09 11:17:53 +01:00
|
|
|
parameter: (identifier) @font-lock-variable-name-face)
|
|
|
|
|
2023-01-26 19:54:27 +01:00
|
|
|
;; full module imports
|
2023-01-09 11:17:53 +01:00
|
|
|
(import_clause (identifier) @font-lock-variable-name-face)
|
2023-01-26 19:54:27 +01:00
|
|
|
;; named imports with aliasing
|
|
|
|
(import_clause (named_imports (import_specifier
|
|
|
|
alias: (identifier) @font-lock-variable-name-face)))
|
|
|
|
;; named imports without aliasing
|
|
|
|
(import_clause (named_imports (import_specifier
|
|
|
|
!alias
|
|
|
|
name: (identifier) @font-lock-variable-name-face)))
|
|
|
|
|
|
|
|
;; full namespace import (* as alias)
|
|
|
|
(import_clause (namespace_import (identifier) @font-lock-variable-name-face)))
|
2022-11-03 11:53:49 -07:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'identifier
|
|
|
|
`((nested_type_identifier
|
|
|
|
module: (identifier) @font-lock-type-face)
|
|
|
|
|
|
|
|
(type_identifier) @font-lock-type-face
|
|
|
|
|
|
|
|
(predefined_type) @font-lock-type-face
|
|
|
|
|
|
|
|
(new_expression
|
|
|
|
constructor: (identifier) @font-lock-type-face)
|
|
|
|
|
2022-10-17 12:49:19 +02:00
|
|
|
(enum_body (property_identifier) @font-lock-type-face)
|
|
|
|
|
|
|
|
(enum_assignment name: (property_identifier) @font-lock-type-face)
|
|
|
|
|
2023-01-02 20:56:41 +01:00
|
|
|
(variable_declarator
|
|
|
|
name: (identifier) @font-lock-variable-name-face)
|
2022-10-17 12:49:19 +02:00
|
|
|
|
|
|
|
(for_in_statement
|
|
|
|
left: (identifier) @font-lock-variable-name-face)
|
|
|
|
|
|
|
|
(arrow_function
|
2022-10-28 21:23:19 +02:00
|
|
|
parameters:
|
|
|
|
[(_ (identifier) @font-lock-variable-name-face)
|
|
|
|
(_ (_ (identifier) @font-lock-variable-name-face))
|
2023-01-04 09:13:23 +01:00
|
|
|
(_ (_ (_ (identifier) @font-lock-variable-name-face)))]))
|
2022-12-07 09:27:42 +01:00
|
|
|
|
|
|
|
:language language
|
|
|
|
:feature 'property
|
|
|
|
`((property_signature
|
2023-02-25 03:15:46 +02:00
|
|
|
name: (property_identifier) @font-lock-property-name-face)
|
2022-12-07 09:27:42 +01:00
|
|
|
(public_field_definition
|
2023-02-25 03:15:46 +02:00
|
|
|
name: (property_identifier) @font-lock-property-name-face)
|
2022-12-07 09:27:42 +01:00
|
|
|
|
2023-02-28 04:07:55 +02:00
|
|
|
(pair key: (property_identifier) @font-lock-property-use-face)
|
2022-12-07 09:27:42 +01:00
|
|
|
|
2023-02-28 04:07:55 +02:00
|
|
|
((shorthand_property_identifier) @font-lock-property-use-face))
|
2022-11-03 11:53:49 -07:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'expression
|
|
|
|
'((assignment_expression
|
2022-10-11 10:27:55 +02:00
|
|
|
left: [(identifier) @font-lock-function-name-face
|
|
|
|
(member_expression
|
|
|
|
property: (property_identifier) @font-lock-function-name-face)]
|
2023-01-02 20:56:41 +01:00
|
|
|
right: [(function) (arrow_function)]))
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2023-01-02 20:56:41 +01:00
|
|
|
:language language
|
|
|
|
:feature 'function
|
|
|
|
'((call_expression
|
2022-10-11 10:27:55 +02:00
|
|
|
function:
|
2023-02-25 03:15:46 +02:00
|
|
|
[(identifier) @font-lock-function-call-face
|
2022-10-11 10:27:55 +02:00
|
|
|
(member_expression
|
2023-02-25 03:15:46 +02:00
|
|
|
property: (property_identifier) @font-lock-function-call-face)]))
|
2022-11-03 11:53:49 -07:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'pattern
|
|
|
|
`((pair_pattern
|
2023-02-28 04:07:55 +02:00
|
|
|
key: (property_identifier) @font-lock-property-use-face
|
2023-02-25 03:54:31 +02:00
|
|
|
value: [(identifier) @font-lock-variable-name-face
|
|
|
|
(assignment_pattern left: (identifier) @font-lock-variable-name-face)])
|
2023-02-25 03:15:46 +02:00
|
|
|
|
|
|
|
(array_pattern (identifier) @font-lock-variable-name-face)
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2023-02-25 03:15:46 +02:00
|
|
|
((shorthand_property_identifier_pattern) @font-lock-variable-name-face))
|
2022-11-03 11:53:49 -07:00
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'jsx
|
2023-07-24 16:17:34 +02:00
|
|
|
(append (tsx-ts-mode--font-lock-compatibility-bb1f97b language)
|
2023-07-22 13:37:54 +02:00
|
|
|
`((jsx_attribute (property_identifier) @typescript-ts-jsx-attribute-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
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
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
|
|
|
:feature 'number
|
|
|
|
`((number) @font-lock-number-face
|
|
|
|
((identifier) @font-lock-number-face
|
2023-06-13 13:53:31 +01:00
|
|
|
(:match "\\`\\(?:NaN\\|Infinity\\)\\'" @font-lock-number-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
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
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
|
|
|
:feature 'operator
|
2022-11-25 21:04:18 +01:00
|
|
|
`([,@typescript-ts-mode--operators] @font-lock-operator-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
|
|
|
(ternary_expression ["?" ":"] @font-lock-operator-face))
|
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-11-21 13:12:03 +01:00
|
|
|
:feature 'bracket
|
|
|
|
'((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
|
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
2022-11-21 13:12:03 +01:00
|
|
|
:feature 'delimiter
|
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
|
|
|
'((["," "." ";" ":"]) @font-lock-delimiter-face)
|
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
:language language
|
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
|
|
|
:feature 'escape-sequence
|
|
|
|
:override t
|
2022-12-17 20:07:59 +01:00
|
|
|
'((escape_sequence) @font-lock-escape-face)))
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2023-01-16 08:36:49 +01:00
|
|
|
(defvar typescript-ts-mode--sentence-nodes
|
2023-01-15 22:14:51 +01:00
|
|
|
'("import_statement"
|
|
|
|
"debugger_statement"
|
|
|
|
"expression_statement"
|
|
|
|
"if_statement"
|
|
|
|
"switch_statement"
|
|
|
|
"for_statement"
|
|
|
|
"for_in_statement"
|
|
|
|
"while_statement"
|
|
|
|
"do_statement"
|
|
|
|
"try_statement"
|
|
|
|
"with_statement"
|
|
|
|
"break_statement"
|
|
|
|
"continue_statement"
|
|
|
|
"return_statement"
|
|
|
|
"throw_statement"
|
|
|
|
"empty_statement"
|
|
|
|
"labeled_statement"
|
|
|
|
"variable_declaration"
|
|
|
|
"lexical_declaration"
|
|
|
|
"property_signature")
|
|
|
|
"Nodes that designate sentences in TypeScript.
|
2023-09-01 17:14:44 -07:00
|
|
|
See `treesit-thing-settings' for more information.")
|
2023-01-15 22:14:51 +01:00
|
|
|
|
2023-01-21 14:47:34 +01:00
|
|
|
(defvar typescript-ts-mode--sexp-nodes
|
|
|
|
'("expression"
|
|
|
|
"pattern"
|
|
|
|
"array"
|
|
|
|
"function"
|
|
|
|
"string"
|
|
|
|
"escape"
|
|
|
|
"template"
|
|
|
|
"regex"
|
|
|
|
"number"
|
|
|
|
"identifier"
|
|
|
|
"this"
|
|
|
|
"super"
|
|
|
|
"true"
|
|
|
|
"false"
|
|
|
|
"null"
|
|
|
|
"undefined"
|
|
|
|
"arguments"
|
|
|
|
"pair")
|
|
|
|
"Nodes that designate sexps in TypeScript.
|
2023-09-01 17:14:44 -07:00
|
|
|
See `treesit-thing-settings' for more information.")
|
2023-01-21 14:47:34 +01:00
|
|
|
|
2022-10-11 10:27:55 +02:00
|
|
|
;;;###autoload
|
2022-11-29 21:39:38 +01:00
|
|
|
(define-derived-mode typescript-ts-base-mode prog-mode "TypeScript"
|
2023-09-16 23:03:26 +03:00
|
|
|
"Generic major mode for editing TypeScript.
|
|
|
|
|
|
|
|
This mode is intended to be inherited by concrete major modes."
|
2022-11-29 21:39:38 +01:00
|
|
|
:group 'typescript
|
|
|
|
:syntax-table typescript-ts-mode--syntax-table
|
|
|
|
|
|
|
|
;; Comments.
|
2023-01-21 12:24:55 +01:00
|
|
|
(c-ts-common-comment-setup)
|
2022-12-28 21:40:59 +01:00
|
|
|
(setq-local treesit-defun-prefer-top-level t)
|
2022-11-29 21:39:38 +01:00
|
|
|
|
|
|
|
;; Electric
|
|
|
|
(setq-local electric-indent-chars
|
2023-02-18 23:10:13 +01:00
|
|
|
(append "{}():;,<>/" electric-indent-chars))
|
|
|
|
(setq-local electric-layout-rules
|
|
|
|
'((?\; . after) (?\{ . after) (?\} . before)))
|
2022-11-29 21:39:38 +01:00
|
|
|
;; Navigation.
|
|
|
|
(setq-local treesit-defun-type-regexp
|
|
|
|
(regexp-opt '("class_declaration"
|
|
|
|
"method_definition"
|
|
|
|
"function_declaration"
|
|
|
|
"lexical_declaration")))
|
2022-12-27 20:57:12 -08:00
|
|
|
(setq-local treesit-defun-name-function #'js--treesit-defun-name)
|
|
|
|
|
2023-09-01 17:14:44 -07:00
|
|
|
(setq-local treesit-thing-settings
|
|
|
|
`((typescript
|
|
|
|
(sexp ,(regexp-opt typescript-ts-mode--sexp-nodes))
|
|
|
|
(sentence ,(regexp-opt
|
|
|
|
typescript-ts-mode--sentence-nodes))
|
|
|
|
(text ,(regexp-opt '("comment"
|
|
|
|
"template_string"))))))
|
2023-01-21 14:47:34 +01:00
|
|
|
|
2022-12-27 20:57:12 -08:00
|
|
|
;; Imenu (same as in `js-ts-mode').
|
|
|
|
(setq-local treesit-simple-imenu-settings
|
|
|
|
`(("Function" "\\`function_declaration\\'" nil nil)
|
|
|
|
("Variable" "\\`lexical_declaration\\'"
|
|
|
|
js--treesit-valid-imenu-entry nil)
|
|
|
|
("Class" ,(rx bos (or "class_declaration"
|
|
|
|
"method_definition")
|
|
|
|
eos)
|
|
|
|
nil nil))))
|
2022-11-29 21:39:38 +01:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode typescript-ts-mode typescript-ts-base-mode "TypeScript"
|
|
|
|
"Major mode for editing TypeScript."
|
|
|
|
:group 'typescript
|
|
|
|
:syntax-table typescript-ts-mode--syntax-table
|
|
|
|
|
|
|
|
(when (treesit-ready-p 'typescript)
|
|
|
|
(treesit-parser-create 'typescript)
|
|
|
|
|
|
|
|
;; Indent.
|
|
|
|
(setq-local treesit-simple-indent-rules
|
|
|
|
(typescript-ts-mode--indent-rules 'typescript))
|
|
|
|
|
|
|
|
;; Font-lock.
|
|
|
|
(setq-local treesit-font-lock-settings
|
|
|
|
(typescript-ts-mode--font-lock-settings 'typescript))
|
|
|
|
(setq-local treesit-font-lock-feature-list
|
2023-01-02 20:42:52 +02:00
|
|
|
'((comment declaration)
|
|
|
|
(keyword string escape-sequence)
|
2022-11-29 21:39:38 +01:00
|
|
|
(constant expression identifier number pattern property)
|
2023-11-24 07:18:26 -08:00
|
|
|
(operator function bracket delimiter)))
|
2023-09-16 22:55:17 +03:00
|
|
|
(setq-local syntax-propertize-function #'typescript-ts--syntax-propertize)
|
2022-11-29 21:39:38 +01:00
|
|
|
|
|
|
|
(treesit-major-mode-setup)))
|
|
|
|
|
2023-01-20 10:28:26 +02:00
|
|
|
(if (treesit-ready-p 'typescript)
|
|
|
|
(add-to-list 'auto-mode-alist '("\\.ts\\'" . typescript-ts-mode)))
|
|
|
|
|
2022-11-29 21:39:38 +01:00
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode tsx-ts-mode typescript-ts-base-mode "TypeScript[TSX]"
|
2023-03-24 16:45:15 -07:00
|
|
|
"Major mode for editing TSX and JSX documents.
|
|
|
|
|
|
|
|
This major mode defines two additional JSX-specific faces:
|
|
|
|
`typescript-ts-jsx-attribute-face' and
|
|
|
|
`typescript-ts-jsx-attribute-face' that are used for HTML tags
|
2023-04-02 15:09:41 -07:00
|
|
|
and attributes, respectively.
|
|
|
|
|
|
|
|
The JSX-specific faces are used when `treesit-font-lock-level' is
|
|
|
|
at least 3 (which is the default value)."
|
2022-10-11 10:27:55 +02:00
|
|
|
:group 'typescript
|
2022-11-25 21:04:18 +01:00
|
|
|
:syntax-table typescript-ts-mode--syntax-table
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2022-11-26 15:32:57 -08:00
|
|
|
(when (treesit-ready-p 'tsx)
|
2022-10-27 21:05:02 -07:00
|
|
|
(treesit-parser-create 'tsx)
|
2022-11-14 08:03:11 +01:00
|
|
|
|
2022-10-25 13:54:12 -07:00
|
|
|
;; Comments.
|
|
|
|
(setq-local comment-start "// ")
|
|
|
|
(setq-local comment-end "")
|
2022-12-02 17:03:21 -08:00
|
|
|
(setq-local comment-start-skip (rx (or (seq "/" (+ "/"))
|
|
|
|
(seq "/" (+ "*")))
|
|
|
|
(* (syntax whitespace))))
|
2022-11-27 14:15:57 -08:00
|
|
|
(setq-local comment-end-skip
|
|
|
|
(rx (* (syntax whitespace))
|
|
|
|
(group (or (syntax comment-end)
|
|
|
|
(seq (+ "*") "/")))))
|
2022-11-14 08:03:11 +01:00
|
|
|
|
2022-10-25 13:54:12 -07:00
|
|
|
;; Indent.
|
2022-11-29 21:39:38 +01:00
|
|
|
(setq-local treesit-simple-indent-rules
|
|
|
|
(typescript-ts-mode--indent-rules 'tsx))
|
2022-11-14 08:03:11 +01:00
|
|
|
|
2023-09-01 17:14:44 -07:00
|
|
|
(setq-local treesit-thing-settings
|
|
|
|
`((tsx
|
|
|
|
(sexp ,(regexp-opt
|
|
|
|
(append typescript-ts-mode--sexp-nodes
|
|
|
|
'("jsx"))))
|
|
|
|
(sentence ,(regexp-opt
|
|
|
|
(append typescript-ts-mode--sentence-nodes
|
|
|
|
'("jsx_element"
|
|
|
|
"jsx_self_closing_element")))))))
|
2023-01-21 14:47:34 +01:00
|
|
|
|
2022-10-25 13:54:12 -07:00
|
|
|
;; Font-lock.
|
2022-11-29 21:39:38 +01:00
|
|
|
(setq-local treesit-font-lock-settings
|
|
|
|
(typescript-ts-mode--font-lock-settings 'tsx))
|
2022-10-28 21:23:19 +02:00
|
|
|
(setq-local treesit-font-lock-feature-list
|
2023-01-02 20:42:52 +02:00
|
|
|
'((comment declaration)
|
|
|
|
(keyword string escape-sequence)
|
2022-11-29 21:39:38 +01:00
|
|
|
(constant expression identifier jsx number pattern property)
|
2023-01-02 20:56:41 +01:00
|
|
|
(function bracket delimiter)))
|
2023-09-05 21:29:27 +02:00
|
|
|
(setq-local syntax-propertize-function #'tsx-ts--syntax-propertize)
|
2022-11-14 08:03:11 +01:00
|
|
|
|
2022-11-26 15:32:57 -08:00
|
|
|
(treesit-major-mode-setup)))
|
2022-11-14 08:03:11 +01:00
|
|
|
|
2023-09-16 22:55:17 +03:00
|
|
|
(defvar typescript-ts--s-p-query
|
2023-09-05 21:29:27 +02:00
|
|
|
(when (treesit-available-p)
|
|
|
|
(treesit-query-compile 'typescript
|
|
|
|
'(((regex pattern: (regex_pattern) @regexp))))))
|
|
|
|
|
|
|
|
(defvar tsx-ts--s-p-query
|
|
|
|
(when (treesit-available-p)
|
|
|
|
(treesit-query-compile 'tsx
|
|
|
|
'(((regex pattern: (regex_pattern) @regexp))
|
|
|
|
((variable_declarator value: (jsx_element) @jsx))
|
|
|
|
((assignment_expression right: (jsx_element) @jsx))
|
|
|
|
((arguments (jsx_element) @jsx))
|
|
|
|
((parenthesized_expression (jsx_element) @jsx))
|
|
|
|
((return_statement (jsx_element) @jsx))))))
|
|
|
|
|
2023-09-16 22:55:17 +03:00
|
|
|
(defun typescript-ts--syntax-propertize (beg end)
|
|
|
|
(let ((captures (treesit-query-capture 'typescript typescript-ts--s-p-query beg end)))
|
|
|
|
(tsx-ts--syntax-propertize-captures captures)))
|
2023-09-05 21:29:27 +02:00
|
|
|
|
|
|
|
(defun tsx-ts--syntax-propertize (beg end)
|
|
|
|
(let ((captures (treesit-query-capture 'tsx tsx-ts--s-p-query beg end)))
|
2023-09-16 22:55:17 +03:00
|
|
|
(tsx-ts--syntax-propertize-captures captures)))
|
2023-09-05 21:29:27 +02:00
|
|
|
|
2023-09-16 22:55:17 +03:00
|
|
|
(defun tsx-ts--syntax-propertize-captures (captures)
|
2023-09-05 21:29:27 +02:00
|
|
|
(pcase-dolist (`(,name . ,node) captures)
|
|
|
|
(let* ((ns (treesit-node-start node))
|
|
|
|
(ne (treesit-node-end node))
|
|
|
|
(syntax (pcase-exhaustive name
|
|
|
|
('regexp
|
|
|
|
(cl-decf ns)
|
|
|
|
(cl-incf ne)
|
|
|
|
(string-to-syntax "\"/"))
|
|
|
|
('jsx
|
|
|
|
(string-to-syntax "|")))))
|
|
|
|
(put-text-property ns (1+ ns) 'syntax-table syntax)
|
|
|
|
(put-text-property (1- ne) ne 'syntax-table syntax))))
|
|
|
|
|
2023-01-20 10:28:26 +02:00
|
|
|
(if (treesit-ready-p 'tsx)
|
|
|
|
(add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode)))
|
|
|
|
|
2022-11-25 21:04:18 +01:00
|
|
|
(provide 'typescript-ts-mode)
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2022-11-25 21:04:18 +01:00
|
|
|
;;; typescript-ts-mode.el ends here
|