/
evil-textobj-tree-sitter.el
176 lines (160 loc) · 8.75 KB
/
evil-textobj-tree-sitter.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
;;; evil-textobj-tree-sitter.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") (evil "1.0.0") (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 'evil)
(require 'tree-sitter)
(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 '((sh-mode . "bash")
(shell-script-mode . "bash")
(c-mode . "c")
(csharp-mode . "csharp")
(c++-mode . "cpp")
(go-mode . "go")
(html-mode . "html")
(java-mode . "java")
(javascript-mode . "javascript")
(js-mode . "javascript")
(js2-mode . "javascript")
(js3-mode . "javascript")
(julia-mode . "julia")
(php-mode . "php")
(python-mode . "python")
(rjsx-mode . "javascript")
(ruby-mode . "ruby")
(rust-mode . "rust")
(rustic-mode . "rust")
(typescript-mode . "typescript"))))
(setf (map-elt evil-textobj-tree-sitter-major-mode-language-alist
major-mode) lang-symbol))
(defun evil-textobj-tree-sitter--nodes-within (nodes)
"NODES which contain the current point inside them ordered inside out."
(sort (cl-remove-if-not (lambda (x)
(and (<= (car (tsc-node-byte-range x)) (point))
(< (point) (cdr (tsc-node-byte-range x)))))
nodes)
(lambda (x y)
(< (+ (abs (- (point)
(car (tsc-node-byte-range x))))
(abs (- (point)
(cdr (tsc-node-byte-range x))))) (+ (abs (- (point)
(car (tsc-node-byte-range y))))
(abs (- (point)
(cdr (tsc-node-byte-range y)))))))))
(defun evil-textobj-tree-sitter--nodes-after (nodes)
"NODES which contain the current point before them ordered top to bottom."
(cl-remove-if-not (lambda (x)
(> (car (tsc-node-byte-range x)) (point)))
nodes))
(defun evil-textobj-tree-sitter--get-nodes (group count &optional 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. This will return n(`COUNT') items. If a `QUERY'
alist is provided, we make use of that instead of the builtin query
set."
;; TODO: handle missing language queries gracefully
(let* ((lang-file (alist-get major-mode evil-textobj-tree-sitter-major-mode-language-alist))
(query-filename (concat evil-textobj-tree-sitter--queries-dir
lang-file "/textobjects.scm"))
(debugging-query (if (eq query nil)
(with-temp-buffer
(insert-file-contents query-filename)
(buffer-string))
(alist-get major-mode query)))
(root-node (tsc-root-node tree-sitter-tree))
(query (tsc-make-query tree-sitter-language debugging-query))
(captures (tsc-query-captures query root-node #'tsc--buffer-substring-no-properties))
(filtered-captures (cl-remove-if-not (lambda (x)
(member (car x) group))
captures))
(nodes (seq-map #'cdr filtered-captures))
(nodes-nodupes (cl-remove-duplicates nodes
:test (lambda (x y)
(and (= (car (tsc-node-byte-range x)) (car (tsc-node-byte-range y)))
(= (cdr (tsc-node-byte-range x)) (cdr (tsc-node-byte-range y)))))))
(nodes-within (evil-textobj-tree-sitter--nodes-within nodes-nodupes))
(nodes-after (evil-textobj-tree-sitter--nodes-after nodes-nodupes))
(all-nodes (append nodes-within nodes-after)))
(if (> (length all-nodes) 0)
(cl-subseq all-nodes 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-nodes ts-group
count query)))
(if (not (eq nodes nil))
(let ((range-min (apply #'min
(seq-map (lambda (x)
(car (tsc-node-byte-range x)))
nodes)))
(range-max (apply #'max
(seq-map (lambda (x)
(cdr (tsc-node-byte-range 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` ot the positioning might be off for unicode chars
(cons (cl-callf byte-to-position range-min) (cl-callf byte-to-position range-max)))))))
;;;###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. 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))
(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
(count &rest _)
(let ((range (evil-textobj-tree-sitter--range count ',interned-groups
,query)))
(if (not (eq range nil))
(evil-range (car range)
(cdr range))
(message (concat "No '" ,group "' text object found")))))))
(provide 'evil-textobj-tree-sitter)
;;; evil-textobj-tree-sitter.el ends here