all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob 4f6a3f970b53dae84e6a039088f6bc1e04b4ff01 10709 bytes (raw)
name: lisp/progmodes/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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
 
;;; 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/>.

;;; Code:

(require 'treesit)
(require 'rx)
(require 'js)

(defcustom ts-mode-font-lock-feature-list
  '((comment declaration)
    (string keyword identifier expression constant)
    (property pattern jsx))
  "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
`ts-mode--font-lock-settings'."
  :version "29.1"
  :type 'list
  :safe 'listp
  :group 'typescript)

(defcustom ts-mode-indent-offset 2
  "Number of spaces for each indentation step in `ts-mode'."
  :version "29.1"
  :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
     ((parent-is "program") parent-bol 0)
     ((node-is "}") parent-bol 0)
     ((node-is ")") parent-bol 0)
     ((node-is "]") parent-bol 0)
     ((node-is ">") parent-bol 0)
     ((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)

     ;; TSX
     ((parent-is "jsx_opening_element") parent ts-mode-indent-offset)
     ((node-is "jsx_closing_element") parent 0)
     ((parent-is "jsx_element") parent ts-mode-indent-offset)
     ((node-is "/") parent 0)
     ((parent-is "jsx_self_closing_element") parent ts-mode-indent-offset)
     (no-node parent-bol 0)))
  "Tree-sitter indent rules.")

(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
  (treesit-font-lock-rules
   :language 'tsx
   :override t
   :feature 'comment
   `((comment) @font-lock-comment-face
     (comment) @contextual)

   :language 'tsx
   :override t
   :feature 'constant
   `(((identifier) @font-lock-constant-face
      (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))

     [(true) (false) (null)] @font-lock-constant-face
     (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
     (string) @font-lock-string-face
     (string) @contextual
     (template_string) @js--fontify-template-string
     (template_string) @contextual
     (template_substitution ["${" "}"] @font-lock-builtin-face))

   :language 'tsx
   :override t
   :feature 'declaration
   `((function
      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)

     (variable_declarator
      name: (identifier) @font-lock-variable-name-face)

     (enum_declaration (identifier) @font-lock-type-face)

     (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)

     (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
      parameters:
      [(_ (identifier) @font-lock-variable-name-face)
       (_ (_ (identifier) @font-lock-variable-name-face))
       (_ (_ (_ (identifier) @font-lock-variable-name-face)))]))

   :language 'tsx
   :override t
   :feature 'expression
   '((assignment_expression
      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
        property: (property_identifier) @font-lock-function-name-face)]))

   :language 'tsx
   :override t
   :feature 'property
   `((pair key: (property_identifier) @font-lock-variable-name-face)

     (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)
      @font-lock-variable-name-face))

   :language 'tsx
   :override t
   :feature 'pattern
   `((pair_pattern
      key: (property_identifier) @font-lock-variable-name-face)

     (array_pattern (identifier) @font-lock-variable-name-face))

   :language 'tsx
   :override t
   :feature 'jsx
   `((jsx_opening_element
      [(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)

     (jsx_attribute (property_identifier) @font-lock-constant-face)))
  "Tree-sitter font-lock settings.")

;;;###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
   ;; `ts-mode' requires tree-sitter to work, so we don't check if
   ;; user enables tree-sitter for it.
   ((treesit-ready-p nil 'tsx)
    ;; Tree-sitter.
    (treesit-parser-create 'tsx)

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

    ;; Electric
    (setq-local electric-indent-chars
                (append "{}():;," electric-indent-chars))

    ;; 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)
    (setq-local treesit-font-lock-feature-list
                ts-mode-font-lock-feature-list)

    ;; Imenu.
    (setq-local imenu-create-index-function #'js--treesit-imenu)

    ;; Which-func (use imenu).
    (setq-local which-func-functions nil)

    (treesit-major-mode-setup))

   ;; Elisp.
   (t
    (js-mode)
    (message "Tree-sitter for TypeScript isn't available, falling back to `js-mode'"))))

(provide 'ts-mode)

;;; ts-mode.el ends here

debug log:

solving 4f6a3f970b ...
found 4f6a3f970b in https://yhetil.org/emacs/87cz9qa4ab.fsf@thornhill.no/ ||
	https://yhetil.org/emacs/87a64u31d0.fsf@thornhill.no/
found f4dfa2ae95 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 f4dfa2ae95f16f5734cc7ce801cff5945d53c485	lisp/progmodes/ts-mode.el

applying [1/1] https://yhetil.org/emacs/87cz9qa4ab.fsf@thornhill.no/
diff --git a/lisp/progmodes/ts-mode.el b/lisp/progmodes/ts-mode.el
index f4dfa2ae95..4f6a3f970b 100644

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

skipping https://yhetil.org/emacs/87a64u31d0.fsf@thornhill.no/ for 4f6a3f970b
index at:
100644 4f6a3f970b53dae84e6a039088f6bc1e04b4ff01	lisp/progmodes/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.