-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm-tree-sitter-rust.el
132 lines (97 loc) · 5.4 KB
/
helm-tree-sitter-rust.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
;;; helm-tree-sitter-rust.el --- Helm interface for tree-sitter -*- lexical-binding: t -*-
;; Copyright (C) 2021 ~ 2022 Giedrius Jonikas <[email protected]>
;; Author: Giedrius Jonikas <[email protected]>
;; Version: 0.1.0
;; URL: https://gitlab.com/giedriusj1/helm-tree-sitter
;; Package-Requires: ((emacs "25.1") (helm "3.6.2") (tree-sitter "0.16.1"))
;; 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:
;; Simple helm interface to tree-sitter.
;;; Commentary:
;; Provides function for dealing with Rust code
;;; Code:
(require 'helm-tree-sitter-utils)
(defvar helm-tree-sitter-rust-candidate-producer
'(("use_declaration" . helm-tree-sitter-rust-use-declaration-fn)
("struct_item" . helm-tree-sitter-rust-struct-item-fn)
("function_item" . helm-tree-sitter-rust-function-definition-fn)
("impl_item" . helm-tree-sitter-rust-impl-item-fn)
("macro_definition" . helm-tree-sitter-rust-macro-definition-fn)))
(defun helm-tree-sitter-rust-use-declaration-fn (elem)
"Helm-tree-sitter handler for use_declaration nodes in Rust mode.
Argument ELEM is `helm-tree-sitter-core-elem' representing the node."
(unless (helm-tree-sitter-core-elem-p elem)
(signal 'wrong-type-argument (list 'helm-tree-sitter-core-elem-p elem)))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "Use: "
'face 'italic)
(helm-tree-sitter-utils-get-node-text (helm-tree-sitter-core-elem-node elem))))
(defun helm-tree-sitter-rust-function-definition-fn (elem)
"Helm-tree-sitter handler for function_definition nodes in Rust mode.
Argument ELEM is `helm-tree-sitter-core-elem' representing the node."
(unless (helm-tree-sitter-core-elem-p elem)
(signal 'wrong-type-argument (list 'helm-tree-sitter-core-elem-p elem)))
(let* ((children-alist (helm-tree-sitter-utils-node-children-to-alist (helm-tree-sitter-core-elem-node elem)))
(visibility-modifier (helm-tree-sitter-utils-get-node-text (alist-get 'visibility_modifier children-alist)))
(identifier (helm-tree-sitter-utils-get-node-text (alist-get 'identifier children-alist)))
(type-identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier children-alist)))
(parameters (helm-tree-sitter-utils-get-node-text (alist-get 'parameters children-alist))))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "Fn: "
'face 'italic)
(helm-tree-sitter-utils-strip-newlines-and-whitespaces
(concat
(helm-tree-sitter-utils-append-space-if-not-empty visibility-modifier)
(helm-tree-sitter-utils-append-space-if-not-empty type-identifier)
identifier
parameters
(helm-tree-sitter-utils-prepend-if-not-empty type-identifier " -> "))))))
(defun helm-tree-sitter-rust-struct-item-fn (elem)
"Helm-tree-sitter handler for struct_item nodes in Rust mode.
Argument ELEM is `helm-tree-sitter-core-elem' representing the node."
(unless (helm-tree-sitter-core-elem-p elem)
(signal 'wrong-type-argument (list 'helm-tree-sitter-core-elem-p elem)))
(let* ((children-alist (helm-tree-sitter-utils-node-children-to-alist (helm-tree-sitter-core-elem-node elem)))
(identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier children-alist))))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "Struct: "
'face 'italic)
identifier)))
(defun helm-tree-sitter-rust-impl-item-fn (elem)
"Helm-tree-sitter handler for impl_item nodes in Rust mode.
Argument ELEM is `helm-tree-sitter-core-elem' representing the node."
(unless (helm-tree-sitter-core-elem-p elem)
(signal 'wrong-type-argument (list 'helm-tree-sitter-core-elem-p elem)))
(let* ((children-alist (helm-tree-sitter-utils-node-children-to-alist (helm-tree-sitter-core-elem-node elem)))
(identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier children-alist))))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "Impl: "
'face 'italic)
identifier)))
(defun helm-tree-sitter-rust-macro-definition-fn (elem)
"Helm-tree-sitter handler for macro_definition nodes in Rust mode.
Argument ELEM is `helm-tree-sitter-core-elem' representing the node."
(unless (helm-tree-sitter-core-elem-p elem)
(signal 'wrong-type-argument (list 'helm-tree-sitter-core-elem-p elem)))
(let* ((children-alist (helm-tree-sitter-utils-node-children-to-alist (helm-tree-sitter-core-elem-node elem)))
(identifier (helm-tree-sitter-utils-get-node-text (alist-get 'identifier children-alist))))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "Macro: "
'face 'italic)
identifier)))
(provide 'helm-tree-sitter-rust)
;;; helm-tree-sitter-rust.el ends here