-
Notifications
You must be signed in to change notification settings - Fork 12
/
evil-textobj-tree-sitter-core.el
319 lines (294 loc) · 16.4 KB
/
evil-textobj-tree-sitter-core.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
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
;;; evil-textobj-tree-sitter-core.el --- Provides evil textobjects using tree-sitter -*- lexical-binding: t; -*-
;; URL: https://github.com/meain/evil-textobj-tree-sitter
;; Keywords: evil, tree-sitter, text-object, convenience
;; SPDX-License-Identifier: Apache-2.0
;; Package-Requires: ((emacs "25.1") (tree-sitter "0.15.0"))
;; Version: 0.1
;;; Commentary:
;; This package is a port of nvim-treesitter/nvim-treesitter-textobjects.
;; This package will let you create evil textobjects using the power
;; of tree-sitter grammars. You can easily create
;; function,class,comment etc textobjects in multiple languages.
;;
;; You can do a sample map like below to create a function textobj.
;; (define-key evil-outer-text-objects-map "f"
;; (evil-textobj-tree-sitter-get-textobj "function.outer"))
;; `evil-textobj-tree-sitter-get-textobj' will return you a function
;; that you can use in a define-key map. You can pass in any of the
;; supported queries as an arg of that function. You can also pass in
;; multiple queries as a list and we will match on all of them, ranked
;; on which ones comes up first in the file.
;; You can find more info in the README.md file at
;; https://github.com/meain/evil-textobj-tree-sitter
;;; Code:
(require 'cl-lib)
(require 'tree-sitter)
(defvar evil-textobj-tree-sitter--evil-available (require 'evil nil t)
"Specifies if we have `evil-mode' available to use in package.")
(defgroup evil-textobj-tree-sitter nil "Text objects based on tree-sitter for Evil."
:group 'evil)
(defconst evil-textobj-tree-sitter--dir (file-name-directory (locate-library "evil-textobj-tree-sitter.el"))
"The directory where the library `tree-sitter-langs' is located.")
(defconst evil-textobj-tree-sitter--queries-dir (file-name-as-directory (concat evil-textobj-tree-sitter--dir "queries")))
(defcustom evil-textobj-tree-sitter-major-mode-language-alist nil
"Alist that maps major modes to tree-sitter language names."
:group 'evil-textobj-tree-sitter
:type '(alist :key-type symbol
:value-type string))
(pcase-dolist (`(,major-mode . ,lang-symbol)
(reverse '((c++-mode . "cpp")
(c-mode . "c")
(csharp-mode . "csharp")
(elixir-mode . "elixir")
(elm-mode . "elm")
(ess-r-mode . "r")
(go-mode . "go")
(haskell-mode . "haskell")
(html-mode . "html")
(java-mode . "java")
(javascript-mode . "javascript")
(js-mode . "javascript")
(js2-mode . "javascript")
(js3-mode . "javascript")
(julia-mode . "julia")
(php-mode . "php")
(prisma-mode . "prisma")
(python-mode . "python")
(rjsx-mode . "javascript")
(ruby-mode . "ruby")
(rust-mode . "rust")
(rustic-mode . "rust")
(sh-mode . "bash")
(shell-script-mode . "bash")
(typescript-mode . "typescript")
(verilog-mode . "verilog")
(zig-mode . "zig"))))
(setf (map-elt evil-textobj-tree-sitter-major-mode-language-alist
major-mode) lang-symbol))
(defun evil-textobj-tree-sitter--nodes-filter-before (nodes)
"NODES which contain the current after them."
(sort (cl-remove-if-not (lambda (x)
(< (byte-to-position (car (last x))) (point)))
nodes)
(lambda (x y) (< (car (last x)) (car (last y))))))
(defun evil-textobj-tree-sitter--nodes-filter-within (nodes)
"NODES which contain the current point inside them ordered inside out."
(let ((byte-pos (position-bytes (point))))
(sort (cl-remove-if-not (lambda (x)
(and (<= (nth 1 x) byte-pos)
(< byte-pos (car (last x)))))
nodes)
(lambda (x y)
(< (+ (abs (- byte-pos
(nth 1 x)))
(abs (- byte-pos
(car (last x))))) (+ (abs (- byte-pos
(nth 1 y)))
(abs (- byte-pos
(car (last x))))))))))
(defun evil-textobj-tree-sitter--nodes-filter-after (nodes)
"NODES which contain the current point before them ordered top to bottom."
(sort (cl-remove-if-not (lambda (x)
(> (byte-to-position (nth 1 x)) (point)))
nodes)
(lambda (x y) (< (nth 1 x) (nth 1 y)))))
(defun evil-textobj-tree-sitter--get-query (language top-level)
"Get tree sitter query for LANGUAGE.
TOP-LEVEL is used to mention if we should load optional inherits.
https://github.com/nvim-treesitter/nvim-treesitter/pull/564"
(with-temp-buffer
(let ((filename (concat evil-textobj-tree-sitter--queries-dir
language "/textobjects.scm")))
(if (file-exists-p filename)
(progn
(insert-file-contents filename)
(goto-char (point-min))
(let* ((first-line (thing-at-point 'line t))
(first-line-matches (save-match-data (when (string-match "^; *inherits *:? *\\([a-z_,()]+\\) *$"
first-line)
(match-string 1 first-line)))))
(if first-line-matches
(insert (string-join (mapcar (lambda (x)
(if (string-prefix-p "(" x)
(if top-level
(evil-textobj-tree-sitter--get-query (substring x 1 -1)
nil))
(evil-textobj-tree-sitter--get-query x nil)))
(split-string first-line-matches ","))
"\n"))))
(buffer-string))))))
(defun evil-textobj-tree-sitter--get-nodes (group query)
"Get a list of viable nodes based on `GROUP' value.
They will be order with captures with point inside them first then the
ones that follow. If a `QUERY' alist is provided and
it contains a current `major-mode',
we make use of that instead of the builtin query set."
(let* ((lang-name (alist-get major-mode evil-textobj-tree-sitter-major-mode-language-alist))
(user-query (alist-get major-mode query))
(debugging-query (if (eq user-query nil)
(evil-textobj-tree-sitter--get-query lang-name t)
user-query))
(root-node (tsc-root-node tree-sitter-tree))
(query (tsc-make-query tree-sitter-language debugging-query))
(matches (tsc-query-matches query root-node #'tsc--buffer-substring-no-properties))
(all-captures '())
(captures (tsc-query-captures query root-node #'tsc--buffer-substring-no-properties)))
(progn
(seq-map (lambda (x)
(let* ((match-captures (cdr x))
(capture-start nil)
(capture-end nil))
(seq-map (lambda (y)
(let ((csplits (split-string (symbol-name (car y)) "\\.")))
(pcase (car (last csplits))
("_end" (setq capture-end y))
("_start" (setq capture-start y))
(_ (push (list (car y)
(car (tsc-node-byte-range (cdr y)))
(cdr (tsc-node-byte-range (cdr y))))
all-captures)))))
match-captures)
(if (and capture-start capture-end)
(push (list (intern (string-join (butlast (split-string (symbol-name (car capture-start)) "\\.") 1) "."))
(car (tsc-node-byte-range (cdr capture-start)))
(cdr (tsc-node-byte-range (cdr capture-end))))
all-captures))))
matches)
(cl-remove-duplicates (cl-remove-if-not (lambda (x)
(member (car x) group))
all-captures)
:test (lambda (x y)
(and (eq (car x) (car y)) ;; names are equal
(= (nth 1 x) (nth 1 y))
(= (car (last x)) (car (last y)))))))))
(defun evil-textobj-tree-sitter--get-within-and-after (group count query)
"Given a `GROUP' `QUERY' find `COUNT' number of nodes within in and after current point."
(let* ((nodes (evil-textobj-tree-sitter--get-nodes group query))
(nodes-within (evil-textobj-tree-sitter--nodes-filter-within nodes))
(nodes-after (evil-textobj-tree-sitter--nodes-filter-after nodes))
(filtered-nodes (append nodes-within nodes-after)))
(if (> (length filtered-nodes) 0)
(cl-subseq filtered-nodes 0 count))))
(defun evil-textobj-tree-sitter--get-within (group count query)
"Given a `GROUP' `QUERY' find `COUNT' number of nodes within in and after current point."
(let* ((nodes (evil-textobj-tree-sitter--get-nodes group query))
(nodes-within (evil-textobj-tree-sitter--nodes-filter-within nodes)))
(if (> (length nodes-within) 0)
(cl-subseq nodes-within 0 count))))
(defun evil-textobj-tree-sitter--range (count ts-group &optional query)
"Get the range of the closeset item of type `TS-GROUP'.
`COUNT' is supported even thought it does not actually make sense in
most cases as if we do 3-in-func the selections will not be continues,
but we can only provide the start and end as of now which is what we
are doing. If a `QUERY' alist is provided, we make use of that
instead of the builtin query set."
(if (equal tree-sitter-mode nil)
(message "tree-sitter-mode not enabled for buffer")
(let ((nodes (evil-textobj-tree-sitter--get-within-and-after ts-group count query)))
(if (not (eq nodes nil))
(let ((range-min (apply #'min
(seq-map (lambda (x)
(nth 1 x))
nodes)))
(range-max (apply #'max
(seq-map (lambda (x)
(car (last x)))
nodes))))
;; Have to compute min and max like this as we might have nested functions
;; We have to use `cl-callf byte-to-position` of the positioning might be off for unicode chars
(cons (cl-callf byte-to-position range-min) (cl-callf byte-to-position range-max)))))))
(defun evil-textobj-tree-sitter--message-not-found (groups)
"Log a message that `GROUPS' are not found."
(let ((not-found (mapconcat (lambda (g)
(concat "'" g "'"))
groups
" or ")))
(message (concat "No " not-found " text object found"))))
;;;###autoload
(defmacro evil-textobj-tree-sitter-get-textobj (group &optional query)
"Macro to create a textobj function from `GROUP'.
You can pass in multiple groups as a list and in that case as long as
any one of them is available, it will be picked.
You can optionally pass in a alist mapping `major-mode' to their
respective tree-sitter query in `QUERY' with named captures to use
that instead of the default query list. If `QUERY' does not contain
current `major-mode', then the default queries are used.
Check the README file in the repo to see how to use it.
Check this url for builtin objects
https://github.com/nvim-treesitter/nvim-treesitter-textobjects#built-in-textobjects"
(declare (debug t) (indent defun))
(if evil-textobj-tree-sitter--evil-available
(let* ((groups (if (eq (type-of group) 'string)
(list group)
group))
(funsymbol (intern (concat "evil-textobj-tree-sitter-function--"
(mapconcat 'identity groups "-"))))
(interned-groups (mapcar #'intern groups)))
`(evil-define-text-object ,funsymbol
;; rest argument is named because of compiler warning `argument _ not left unused`
(count &rest unused)
(let ((range (evil-textobj-tree-sitter--range count ',interned-groups ,query)))
(if (not (eq range nil))
(evil-range (car range)
(cdr range))
(evil-textobj-tree-sitter--message-not-found ',groups)))))
(error "Cannot use `evil-textobj-tree-sitter-goto-textobj' without `evil-mode'")))
(defun evil-textobj-tree-sitter--get-goto-location (groups previous end query)
"Get the start/end of the textobj of type `GROUPS'.
By default it goes to the start of the textobj, but pass in `END' if
you want to go to the end of the textobj instead. You can pass in
`PREVIOUS' if you want to search backwards. `QUERY' for custom queries."
(let* ((nodes (evil-textobj-tree-sitter--get-nodes groups query))
(usable-nodes (if previous
(if end
(cl-remove-if-not (lambda (x)
(< (byte-to-position (car (last x))) (point)))
nodes)
(cl-remove-if-not (lambda (x)
(< (byte-to-position (nth 1 x)) (point)))
nodes))
(if end
(cl-remove-if-not (lambda (x)
;; -1 is needed as evil cannot select till end
(> (- (byte-to-position (car (last x))) 1) (point)))
nodes)
(cl-remove-if-not (lambda (x)
(> (byte-to-position (nth 1 x)) (point)))
nodes))))
(node (car (sort usable-nodes
(lambda (x y)
(if previous
(if end
(> (car(last x)) (car (last y)))
(> (nth 1 x) (nth 1 y)))
(if end
(< (car(last x)) (car (last y)))
(< (nth 1 x) (nth 1 y)))))))))
(if node
(let ((actual-position (cl-callf byte-to-position
(if end
(car (last node))
(nth 1 node)))))
(if end
;; tree sitter count + 1 kinda (probably have to look in other places as well)
;; This is a mess that evil creates (not really an issue in Emacs mode)
(- actual-position 1)
actual-position)))))
;;;###autoload
(defun evil-textobj-tree-sitter-goto-textobj (group &optional previous end query)
"Got to the start/end of the textobj of type `GROUP'.
By default it goes to the start of the textobj, but pass in `END' if
you want to go to the end of the textobj instead. You can pass in
`PREVIOUS' if you want to search backwards. Optionally pass in
`QUERY' if you want to define a custom query."
(let* ((groups (if (eq (type-of group) 'string)
(list group)
group))
(interned-groups (mapcar #'intern groups))
(goto-position (evil-textobj-tree-sitter--get-goto-location
interned-groups previous end query)))
(if goto-position
(goto-char goto-position)
(evil-textobj-tree-sitter--message-not-found groups))))
(provide 'evil-textobj-tree-sitter-core)
;;; evil-textobj-tree-sitter-core.el ends here