-
Notifications
You must be signed in to change notification settings - Fork 1
/
vue-ts-mode.el
294 lines (239 loc) · 9.67 KB
/
vue-ts-mode.el
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
;;; vue-ts-mode.el --- Major mode for editing Vue templates -*- lexical-binding: t; -*-
;; Copyright (C) 2023 8uff3r
;; Author: 8uff3r <8uff3r@gmail.com>
;; Homepage: https://github.com/8uff3r/vue-ts-mode
;; Version: 1.0.0
;; Package-Requires: ((emacs "29.1"))
;; Keywords: languages
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides a major mode with syntax highlighting for Vue
;; templates. It leverages Emacs' built-in tree-sitter support, as well as
;; ikatyang's tree-sitter grammar for Vue.
;; More info:
;; README: https://github.com/8uff3r/vue-ts-mode
;; tree-sitter-vue: https://github.com/ikatyang/tree-sitter-vue
;; Vue: https://vuejs.org//
;;; Code:
(require 'treesit)
(require 'typescript-ts-mode)
(require 'css-mode)
(defgroup vue ()
"Major mode for editing Vue templates."
:group 'languages)
;; Indent Rules
(defcustom vue-ts-mode-indent-offset 2
"Number of spaces for each indentation step in `vue-ts-mode'."
:type 'integer
:group 'vue
:package-version '(vue-ts-mode . "1.0.0"))
(defvar vue-ts-mode--indent-rules
`((vue
((node-is "/>") parent-bol 0)
((node-is ">") parent-bol 0)
((node-is "end_tag") parent-bol 0)
((parent-is "comment") prev-adaptive-prefix 0)
((parent-is "element") parent-bol vue-ts-mode-indent-offset)
((parent-is "script_element") parent-bol 0)
((parent-is "style_element") parent-bol 0)
((parent-is "template_element") parent-bol vue-ts-mode-indent-offset)
((parent-is "start_tag") parent-bol vue-ts-mode-indent-offset)
((parent-is "self_closing_tag") parent-bol vue-ts-mode-indent-offset))
(css . ,(append (alist-get 'css css--treesit-indent-rules)
'(((parent-is "stylesheet") parent-bol 0))))
(typescript . ,(alist-get 'typescript (typescript-ts-mode--indent-rules 'typescript))))
"Tree-sitter indentation rules for `vue-ts-mode'.")
;; font-lock rules
(defface vue-ts-mode-template-tag-bracket-face
'((t :foreground "#86e1fc"))
"Face for html tags angle brackets (<, > and />)."
:group 'vue-ts-mode-faces)
(defun vue-ts-mode--prefix-font-lock-features (prefix settings)
"Prefix with PREFIX the font lock features in SETTINGS."
(mapcar (lambda (setting)
(list (nth 0 setting)
(nth 1 setting)
(intern (format "%s-%s" prefix (nth 2 setting)))
(nth 3 setting)))
settings))
(defvar vue-font-lock-settings
(append
(vue-ts-mode--prefix-font-lock-features
"typescript"
(typescript-ts-mode--font-lock-settings 'typescript))
(vue-ts-mode--prefix-font-lock-features
"css" css--treesit-settings)
(treesit-font-lock-rules
:language 'vue
:override t
:feature 'vue-ref
'((element (_ (attribute
(attribute_name)
@font-lock-type-face
(:equal @font-lock-type-face "ref")
(quoted_attribute_value
(attribute_value)
@font-lock-variable-name-face)))))
:language 'vue
:override t
:feature 'vue-sp-dir
'((_ (_ (directive_attribute
(directive_name)
@font-lock-type-face
(:match "\\`\\(v-if\\|v-for\\|v-model\\|v-else\\|v-else-if\\)\\'"
@font-lock-type-face)))))
:language 'vue
:feature 'vue-attr
'((attribute_name) @font-lock-property-name-face)
:language 'vue
:override t
:feature 'vue-definition
'((tag_name) @font-lock-function-name-face)
:language 'vue
:override t
:feature 'vue-directive
'((_ (_
(directive_attribute
(directive_name) @font-lock-keyword-face
(directive_argument) @font-lock-type-face))))
:language 'vue
:override t
:feature 'vue-bracket
'(([ "{{" "}}" "<" ">" "</" "/>"]) @vue-ts-mode-template-tag-bracket-face)
:language 'vue
:feature 'vue-string
'((attribute (quoted_attribute_value) @font-lock-string-face))
:language 'typescript
:override t
:feature 'typescript-custom-property
'(((property_identifier) @font-lock-property-name-face))
:language 'typescript
:override t
:feature 'typescript-custom-variable
'(((identifier) @font-lock-variable-name-face))
:language 'typescript
:override 't
:feature 'typescript-custom-function
'((call_expression
function:
[(identifier) @font-lock-function-call-face
(member_expression
property: (property_identifier) @font-lock-function-call-face)])))))
(defvar vue-ts-mode--range-settings
(treesit-range-rules
:embed 'typescript
:host 'vue
'((script_element (raw_text) @capture))
:embed 'typescript
:host 'vue
:local 't
'((interpolation (raw_text) @capture))
:embed 'typescript
:host 'vue
:local 't
'((directive_attribute
(quoted_attribute_value (attribute_value) @capture)))
:embed 'css
:host 'vue
'((style_element (raw_text) @capture))))
(defun vue-ts-mode--advice-for-treesit-buffer-root-node (&optional lang)
"Return the current ranges for the LANG parser in the current buffer.
If LANG is omitted, return ranges for the first language in the parser list.
If `major-mode' is currently `vue-ts-mode', or if LANG is vue, this function
instead always returns t."
(if (or (eq lang 'vue) (not (eq major-mode 'vue-ts-mode)))
t
(treesit-parser-included-ranges
(treesit-parser-create
(or lang (treesit-parser-language (car (treesit-parser-list))))))))
(defun vue-ts-mode--advice-for-treesit--merge-ranges (_ new-ranges _ _)
"Return truthy if `major-mode' is `vue-ts-mode', and if NEW-RANGES is non-nil."
(and (eq major-mode 'vue-ts-mode) new-ranges))
(defun vue-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."
(when (equal (treesit-node-type node) "tag_name")
(treesit-node-text node t)))
(defun vue-ts-mode--treesit-language-at-point (point)
"Return the language at POINT."
(let* ((range nil)
(language-in-range
(cl-loop
for parser in (treesit-parser-list)
do (setq range
(cl-loop
for range in (treesit-parser-included-ranges parser)
if (and (>= point (car range)) (<= point (cdr range)))
return parser))
if range
return (treesit-parser-language parser))))
(or language-in-range 'vue)))
;;;###autoload
(define-derived-mode vue-ts-mode prog-mode "Vue-ts"
"Major mode for editing Vue templates, powered by tree-sitter."
:group 'vue
;; :syntax-table html-mode-syntax-table
(unless (treesit-ready-p 'vue)
(error "Tree-sitter grammar for Vue isn't available"))
(unless (treesit-ready-p 'css)
(error "Tree-sitter grammar for CSS isn't available"))
(unless (treesit-ready-p 'typescript)
(error "Tree-sitter grammar for Typescript/TYPESCRIPT isn't available"))
(when (treesit-ready-p 'typescript)
(treesit-parser-create 'vue)
;; Comments and text content
(setq-local treesit-text-type-regexp
(regexp-opt '("comment" "text")))
;; Indentation rules
(setq-local treesit-simple-indent-rules vue-ts-mode--indent-rules
css-indent-offset vue-ts-mode-indent-offset)
;; Font locking
(setq-local treesit-font-lock-settings vue-font-lock-settings)
(setq-local treesit-font-lock-feature-list
'((vue-attr vue-definition css-selector
css-comment css-query css-keyword
typescript-comment typescript-declaration)
(vue-ref vue-string vue-directive css-property css-constant
css-string
typescript-keyword
typescript-string typescript-escape-sequence)
(vue-sp-dir css-error css-variable css-function
css-operator
typescript-constant
typescript-expression typescript-identifier
typescript-number typescript-pattern
typescript-operator
typescript-property)
(vue-bracket css-bracket
typescript-function
typescript-bracket
typescript-delimiter
typescript-custom-function
typescript-custom-variable
typescript-custom-property)))
;; Embedded languages
(setq-local treesit-range-settings vue-ts-mode--range-settings)
(setq-local treesit-language-at-point-function
#'vue-ts-mode--treesit-language-at-point)
(treesit-major-mode-setup)))
(if (treesit-ready-p 'vue)
(add-to-list 'auto-mode-alist '("\\.vue\\'" . vue-ts-mode)))
(advice-add
#'treesit-buffer-root-node
:before-while
#'vue-ts-mode--advice-for-treesit-buffer-root-node)
(advice-add
#'treesit--merge-ranges
:before-while
#'vue-ts-mode--advice-for-treesit--merge-ranges)
(provide 'vue-ts-mode)
;;; vue-ts-mode.el ends here