-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtree-sitter-ess-r.el
275 lines (224 loc) · 8.88 KB
/
tree-sitter-ess-r.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
;;; tree-sitter-ess-r.el --- R with tree-sitter -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2022 Shuguang Sun
;; Author: Shuguang Sun <[email protected]>
;; Created: 2021/11/30
;; Version: 1.1
;; URL: https://github.com/ShuguangSun/tree-sitter-ess-r
;; Package-Requires: ((emacs "26.1") (ess "18.10.1") (tree-sitter "0.12.1") (tree-sitter-langs "0.12.0"))
;; Keywords: tools
;; 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:
;; R with tree-sitter
;; (require 'tree-sitter-ess-r)
;; (add-hook 'ess-r-mode-hook 'tree-sitter-ess-r-mode-activate)
;; or
;; M-x tree-sitter-ess-r-using-r-faces
;; Make tree-sitter to support r
;; Linux
;; 1. git clone https://github.com/r-lib/tree-sitter-r.git
;; 2. gcc ./src/parser.c ./src/scanner.cc -lstdc++ -fPIC -I./ -I./src/ -I./src/tree_sitter --shared -o r.so
;; 3. cp ./r.so /path/to/tree-sitter-langs/langs/bin (/path/to/tree-sitter-langs/ is path of your tree-sitter-langs package)
;; 4. mkdir /path/to/tree-sitter-langs/queries/r
;; 5. cp ./queries/* /path/to/tree-sitter-langs/queries/r
;; Windows (MINGW64)
;; 1. git clone https://github.com/r-lib/tree-sitter-r.git
;; 2. gcc ./src/parser.c ./src/scanner.cc -lstdc++ -fPIC -I./ -I./src/ -I./src/tree_sitter --shared -o r.dll
;; 3. cp ./r.dll /path/to/tree-sitter-langs/langs/bin (/path/to/tree-sitter-langs/ is path of your tree-sitter-langs package)
;; 4. mkdir /path/to/tree-sitter-langs/queries/r
;; 5. cp ./queries/* /path/to/tree-sitter-langs/queries/r
;;; Code:
(require 'ess-r-mode)
(require 'tree-sitter)
(require 'tree-sitter-hl)
(require 'tree-sitter-langs)
(require 'tree-sitter-query)
(defgroup tree-sitter-ess-r ()
"R with tree-sitter support."
:group 'tree-sitter
:prefix "tree-sitter-ess-r-")
(defface tree-sitter-ess-r-matrix-face
'((default :inherit ess-matrix-face))
"Face for matrix."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-operator-face
'((default :inherit ess-operator-face))
"Face for operators."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-punctuation.bracket-face
'((default :inherit ess-paren-face))
"Face for brackets."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-repeat-face
'((default :inherit ess-keyword-face))
"Face for repeat keyword."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-assignment-face
'((default :inherit ess-assignment-face)) ;; ess-assignment-face
"Face for assignment."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-opspecial-face
'((default :inherit ess-%op%-face))
"Face for special `%...%'."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-operatorcomp-face
'((default :inherit ess-operator-face))
"Face for REL, OR and AND."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-operatorpipe-face
'((default :inherit ess-%op%-face))
"Face for `|>' operator."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-function-face
'((default :inherit ess-function-call-face))
"Face for function all."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-conditional-face
'((default :inherit r-control-flow-keyword-face))
"Face for control flow keyword."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-keyword.function-face
'((default :inherit ess-keyword-face))
"Face for keyword.function."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-float-face
'((default :inherit ess-numbers-face))
"Face for numbers."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-number-face
'((default :inherit ess-numbers-face))
"Face for numbers."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-modifier-face
'((default :inherit ess-modifiers-face))
"Face for modifiers."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-boolean-face
'((default :inherit ess-constant-face))
"Face for boolean."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-method-face
'((default :inherit tree-sitter-ess-r-function-face))
"Face for method declarations and definitions."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-namespace-face
'((default :inherit ess-function-call-face))
"Face for namespace."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-dollar-face
'((default :inherit ess-operator-face))
"Face for dollar."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-slot-face
'((default :inherit ess-operator-face))
"Face for slot."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-error-face
'((default :inherit font-lock-warning-face))
"Face for errors."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-comment-face
'((default :inherit font-lock-comment-face))
"Face for comments."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-string-face
'((default :inherit font-lock-string-face))
"Face for strings."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-doc-face
'((default :inherit font-lock-doc-face))
"Face for docs."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-operatorunary-face
'((default :inherit font-lock-warning-face))
"Face for some unary operator."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-parameter-face
'((default :inherit font-lock-builtin-face)) ;; :slant italic ;; :weight normal
"Face for parameters."
:group 'tree-sitter-ess-r)
(defface tree-sitter-ess-r-varname-face
'((default :inherit font-lock-variable-name-face)) ;; :slant italic
"Face for variable names."
:group 'tree-sitter-ess-r)
(add-to-list 'tree-sitter-major-mode-language-alist '(ess-r-mode . r))
;; Some additional patterns.
(tree-sitter-hl-add-patterns 'r
[(function_definition name: "function" @keyword.function)
(binary_operator
lhs: (identifier) @function
operator: "<-"
rhs: (function_definition))
(binary_operator
lhs: (identifier) @varname
operator: "<-"
rhs: (_))
(binary_operator operator: ["=" "<-" "<<-" "->" "->>"] @assignment)
(unary_operator operator: ["-" "+" "!" "~"] @operatorunary)
(binary_operator operator: [
"<"
">"
"<="
">="
"=="
"!="
"||"
"|"
"&&"
"&"
] @operatorcomp)
(binary_operator operator: ["|>"] @operatorpipe)
(binary_operator operator: [":" "~"] @opspecial)
(binary_operator operator: "special" @opspecial)
(extract_operator
lhs: (identifier) @varname
operator: "$" @dollar
rhs: (_) @parameter)
(extract_operator
lhs: (identifier) @varname
operator: "@" @slot
rhs: (_) @method)
([(return) (next) (break)] @repeat)
([(true) (false)
(null) (inf) (nan) (na)
(dots) (dot_dot_i)
] @boolean)
;; (parameter name: (identifier) @parameter)
;; (argument name: (identifier) @parameter)
(for_statement variable: (identifier) @varname)
(call function: (identifier) @modifier
(.match? @modifier "^(library|attach|detach|source|require|setwd|options|par|load|rm|message|warning|.Deprecated|signalCondition|withCallingHandlers)$"))
;; ((identifier) @modifier
;; ;; (.match? @modifier `,(concat "^(" (mapconcat 'identity ess-R-modifiers "|") ")")))
;; (.match? @modifier "^(library|attach|detach|source|require|setwd|options|par|load|rm|message|warning|.Deprecated|signalCondition|withCallingHandlers)$"))
])
(defun tree-sitter-ess-r-hl-face-from-ess-scope (capture-name)
"Return the default face used to highlight CAPTURE-NAME."
;; TODO: If a scope does not have a corresponding face, check its ancestors.
;; (print capture-name)
(pcase capture-name
("operator")
(_ (intern (format "tree-sitter-ess-r-%s-face" capture-name)))))
(defun tree-sitter-ess-r-mode-activate ()
"Hook to turn on `tree-sitter-hl-mode'."
(setq-local tree-sitter-hl-face-mapping-function
#'tree-sitter-ess-r-hl-face-from-ess-scope)
(if (tree-sitter-require 'r)
(tree-sitter-hl-mode 1)))
;;;###autoload
(defun tree-sitter-ess-r-using-r-faces ()
"Turn on `tree-sitter-hl-mode' with `ess-r-mode' faces."
(interactive)
(add-hook 'ess-r-mode-hook #'tree-sitter-ess-r-mode-activate)
(if (tree-sitter-require 'r)
(tree-sitter-hl-mode 1)))
(provide 'tree-sitter-ess-r)
;;; tree-sitter-ess-r.el ends here