2022-10-11 10:27:55 +02:00
|
|
|
;;; ts-mode.el --- tree sitter support for TypeScript -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;; Copyright (C) 2022 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
;; 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.
|
|
|
|
|
|
|
|
;; 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/>.
|
|
|
|
|
2022-10-11 23:49:04 -07:00
|
|
|
;;; Code:
|
|
|
|
|
2022-10-11 10:27:55 +02:00
|
|
|
(require 'treesit)
|
|
|
|
(require 'rx)
|
|
|
|
(require 'js)
|
|
|
|
|
|
|
|
(defcustom ts-mode-indent-offset 2
|
|
|
|
"Number of spaces for each indentation step in `ts-mode'."
|
|
|
|
:type 'integer
|
|
|
|
:safe 'integerp
|
|
|
|
:group 'typescript)
|
|
|
|
|
|
|
|
(defvar 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 ?` "\"" table)
|
|
|
|
(modify-syntax-entry ?\240 "." table)
|
|
|
|
table)
|
|
|
|
"Syntax table for `ts-mode'.")
|
|
|
|
|
|
|
|
(defvar ts-mode--indent-rules
|
|
|
|
`((tsx
|
2022-10-27 21:05:02 -07:00
|
|
|
((parent-is "program") parent-bol 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)
|
2022-10-28 13:36:42 -07:00
|
|
|
((parent-is "ternary_expression") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "member_expression") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "named_imports") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "statement_block") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "type_arguments") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "variable_declarator") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "arguments") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "array") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "formal_parameters") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "template_substitution") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "object_pattern") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "object") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "object_type") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "enum_body") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "arrow_function") parent-bol ,ts-mode-indent-offset)
|
|
|
|
((parent-is "parenthesized_expression") parent-bol ,ts-mode-indent-offset)
|
2022-10-11 10:27:55 +02:00
|
|
|
|
|
|
|
;; TSX
|
2022-10-28 13:36:42 -07:00
|
|
|
((parent-is "jsx_opening_element") parent ,ts-mode-indent-offset)
|
2022-10-11 10:27:55 +02:00
|
|
|
((node-is "jsx_closing_element") parent 0)
|
2022-10-28 13:36:42 -07:00
|
|
|
((parent-is "jsx_element") parent ,ts-mode-indent-offset)
|
2022-10-11 10:27:55 +02:00
|
|
|
((node-is "/") parent 0)
|
2022-10-28 13:36:42 -07:00
|
|
|
((parent-is "jsx_self_closing_element") parent ,ts-mode-indent-offset)
|
2022-10-11 23:49:04 -07:00
|
|
|
(no-node parent-bol 0)))
|
|
|
|
"Tree-sitter indent rules.")
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2022-10-17 12:49:19 +02:00
|
|
|
(defvar ts-mode--keywords
|
|
|
|
'("!" "abstract" "as" "async" "await" "break"
|
|
|
|
"case" "catch" "class" "const" "continue" "debugger"
|
|
|
|
"declare" "default" "delete" "do" "else" "enum"
|
|
|
|
"export" "extends" "finally" "for" "from" "function"
|
|
|
|
"get" "if" "implements" "import" "in" "instanceof" "interface"
|
|
|
|
"keyof" "let" "namespace" "new" "of" "private" "protected"
|
|
|
|
"public" "readonly" "return" "set" "static" "switch"
|
|
|
|
"target" "throw" "try" "type" "typeof" "var" "void"
|
|
|
|
"while" "with" "yield")
|
|
|
|
"TypeScript keywords for tree-sitter font-locking.")
|
|
|
|
|
|
|
|
(defvar ts-mode--font-lock-settings
|
2022-10-11 10:27:55 +02:00
|
|
|
(treesit-font-lock-rules
|
|
|
|
:language 'tsx
|
|
|
|
:override t
|
2022-10-28 21:23:19 +02:00
|
|
|
:feature 'comment
|
|
|
|
`((comment) @font-lock-comment-face)
|
|
|
|
:language 'tsx
|
|
|
|
:override t
|
|
|
|
:feature 'constant
|
|
|
|
`(((identifier) @font-lock-constant-face
|
2022-10-11 10:27:55 +02:00
|
|
|
(:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))
|
|
|
|
|
2022-10-17 12:49:19 +02:00
|
|
|
[(true) (false) (null)] @font-lock-constant-face
|
2022-10-28 21:23:19 +02:00
|
|
|
(number) @font-lock-constant-face)
|
|
|
|
:language 'tsx
|
|
|
|
:override t
|
|
|
|
:feature 'keyword
|
|
|
|
`([,@ts-mode--keywords] @font-lock-keyword-face
|
|
|
|
[(this) (super)] @font-lock-keyword-face)
|
|
|
|
:language 'tsx
|
|
|
|
:override t
|
|
|
|
:feature 'string
|
|
|
|
`((regex pattern: (regex_pattern)) @font-lock-string-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
|
2022-10-28 21:23:19 +02:00
|
|
|
(template_substitution ["${" "}"] @font-lock-builtin-face))
|
2022-10-17 12:49:19 +02:00
|
|
|
:language 'tsx
|
|
|
|
:override t
|
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)
|
|
|
|
|
|
|
|
(method_definition
|
|
|
|
name: (property_identifier) @font-lock-function-name-face)
|
|
|
|
|
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-10-28 21:23:19 +02:00
|
|
|
(arrow_function
|
|
|
|
parameter: (identifier) @font-lock-variable-name-face)
|
|
|
|
|
|
|
|
(variable_declarator
|
|
|
|
name: (identifier) @font-lock-function-name-face
|
|
|
|
value: [(function) (arrow_function)])
|
|
|
|
|
|
|
|
(variable_declarator
|
|
|
|
name: (array_pattern
|
|
|
|
(identifier)
|
|
|
|
(identifier) @font-lock-function-name-face)
|
|
|
|
value: (array (number) (function))))
|
|
|
|
:language 'tsx
|
|
|
|
:override t
|
|
|
|
: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)
|
|
|
|
|
|
|
|
(assignment_expression
|
|
|
|
left: [(identifier) @font-lock-variable-name-face
|
|
|
|
(member_expression
|
|
|
|
property: (property_identifier) @font-lock-variable-name-face)])
|
|
|
|
|
|
|
|
(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))
|
|
|
|
(_ (_ (_ (identifier) @font-lock-variable-name-face)))]))
|
2022-10-17 12:49:19 +02:00
|
|
|
:language 'tsx
|
|
|
|
:override t
|
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)]
|
|
|
|
right: [(function) (arrow_function)])
|
|
|
|
|
|
|
|
(call_expression
|
|
|
|
function:
|
|
|
|
[(identifier) @font-lock-function-name-face
|
|
|
|
(member_expression
|
2022-10-28 21:23:19 +02:00
|
|
|
property: (property_identifier) @font-lock-function-name-face)]))
|
|
|
|
:language 'tsx
|
|
|
|
:override t
|
|
|
|
:feature 'property
|
|
|
|
`((pair key: (property_identifier) @font-lock-variable-name-face)
|
2022-10-11 10:27:55 +02:00
|
|
|
|
|
|
|
(pair value: (identifier) @font-lock-variable-name-face)
|
|
|
|
|
|
|
|
(pair
|
|
|
|
key: (property_identifier) @font-lock-function-name-face
|
|
|
|
value: [(function) (arrow_function)])
|
|
|
|
|
|
|
|
(property_signature
|
|
|
|
name: (property_identifier) @font-lock-variable-name-face)
|
|
|
|
|
|
|
|
((shorthand_property_identifier) @font-lock-variable-name-face)
|
|
|
|
|
|
|
|
((shorthand_property_identifier_pattern)
|
2022-10-28 21:23:19 +02:00
|
|
|
@font-lock-variable-name-face))
|
|
|
|
:language 'tsx
|
|
|
|
:override t
|
|
|
|
:feature 'pattern
|
|
|
|
`((pair_pattern
|
|
|
|
key: (property_identifier) @font-lock-variable-name-face)
|
2022-10-11 10:27:55 +02:00
|
|
|
|
2022-10-28 21:23:19 +02:00
|
|
|
(array_pattern (identifier) @font-lock-variable-name-face))
|
|
|
|
:language 'tsx
|
|
|
|
:override t
|
|
|
|
:feature 'jsx
|
|
|
|
`((jsx_opening_element
|
2022-10-11 10:27:55 +02:00
|
|
|
[(nested_identifier (identifier)) (identifier)]
|
|
|
|
@font-lock-function-name-face)
|
|
|
|
|
|
|
|
(jsx_closing_element
|
|
|
|
[(nested_identifier (identifier)) (identifier)]
|
|
|
|
@font-lock-function-name-face)
|
|
|
|
|
|
|
|
(jsx_self_closing_element
|
|
|
|
[(nested_identifier (identifier)) (identifier)]
|
|
|
|
@font-lock-function-name-face)
|
|
|
|
|
2022-10-17 12:49:19 +02:00
|
|
|
(jsx_attribute (property_identifier) @font-lock-constant-face)))
|
2022-10-11 23:49:04 -07:00
|
|
|
"Tree-sitter font-lock settings.")
|
2022-10-11 10:27:55 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(add-to-list 'auto-mode-alist '("\\.ts\\'" . ts-mode))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(add-to-list 'auto-mode-alist '("\\.tsx\\'" . ts-mode))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-derived-mode ts-mode prog-mode "TypeScript"
|
|
|
|
"Major mode for editing TypeScript."
|
|
|
|
:group 'typescript
|
|
|
|
:syntax-table ts-mode--syntax-table
|
|
|
|
|
|
|
|
(cond
|
2022-10-27 21:05:02 -07:00
|
|
|
;; `ts-mode' requires tree-sitter to work, so we don't check if
|
|
|
|
;; user enables tree-sitter for it.
|
2022-10-25 13:54:12 -07:00
|
|
|
((treesit-ready-p nil 'tsx)
|
|
|
|
;; Tree-sitter.
|
2022-10-27 21:05:02 -07:00
|
|
|
(treesit-parser-create 'tsx)
|
2022-10-25 13:54:12 -07:00
|
|
|
;; Comments.
|
|
|
|
(setq-local comment-start "// ")
|
|
|
|
(setq-local comment-start-skip "\\(?://+\\|/\\*+\\)\\s *")
|
|
|
|
(setq-local comment-end "")
|
|
|
|
;; Indent.
|
|
|
|
(setq-local treesit-simple-indent-rules ts-mode--indent-rules)
|
|
|
|
;; Navigation.
|
|
|
|
(setq-local treesit-defun-type-regexp
|
|
|
|
(rx (or "class_declaration"
|
|
|
|
"method_definition"
|
|
|
|
"function_declaration"
|
|
|
|
"lexical_declaration")))
|
|
|
|
;; Font-lock.
|
|
|
|
(setq-local treesit-font-lock-settings ts-mode--font-lock-settings)
|
2022-10-28 21:23:19 +02:00
|
|
|
(setq-local treesit-font-lock-feature-list
|
|
|
|
'((comment declaration)
|
|
|
|
(string keyword identifier expression constant)
|
|
|
|
(property pattern jsx)))
|
2022-10-26 15:36:15 -07:00
|
|
|
;; Imenu.
|
|
|
|
(setq-local imenu-create-index-function #'js--treesit-imenu)
|
|
|
|
;; Which-func (use imenu).
|
|
|
|
(setq-local which-func-functions nil)
|
2022-10-25 13:54:12 -07:00
|
|
|
(treesit-major-mode-setup))
|
|
|
|
;; Elisp.
|
2022-10-11 10:27:55 +02:00
|
|
|
(t
|
2022-10-19 16:44:04 -07:00
|
|
|
(js-mode)
|
|
|
|
(message "Tree-sitter for TypeScript isn't available, falling back to `js-mode'"))))
|
2022-10-11 10:27:55 +02:00
|
|
|
|
|
|
|
(provide 'ts-mode)
|
|
|
|
|
|
|
|
;;; ts-mode.el ends here
|