-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-tree-sitter-c.el
206 lines (159 loc) · 9.47 KB
/
helm-tree-sitter-c.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
;;; helm-tree-sitter-c.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:
;; Provides function for dealing with C code
;;; Code:
(require 'helm-tree-sitter-utils)
(defvar helm-tree-sitter-c-candidate-producer
'(("function_definition" . helm-tree-sitter-c-function-definition-fn)
("preproc_include" . helm-tree-sitter-c-preproc-include-fn)
("struct_specifier" . helm-tree-sitter-c-struct-specifier-fn)
("enum_specifier" . helm-tree-sitter-c-enum-specifier-fn)
("union_specifier" . helm-tree-sitter-c-union-specifier-fn)
;; We get very spammy output if we try to show every declaration,
;; so we'll just ignore them for now.
;; ("declaration" . helm-tree-sitter-c-declaration-fn)
))
(defun helm-tree-sitter-c-preproc-include-fn (elem)
"Helm-tree-sitter handler for proproc_include nodes in C 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)))
(system-lib (helm-tree-sitter-utils-get-node-text (alist-get 'system_lib_string children-alist)))
(string-literal (helm-tree-sitter-utils-get-node-text (alist-get 'string_literal children-alist)))
;; In case of preprocessor include
(identifier (helm-tree-sitter-utils-get-node-text (alist-get 'identifier children-alist))))
(concat
(propertize "Include: "
'face 'italic)
(concat identifier
system-lib
(replace-regexp-in-string "\"" "" string-literal)))))
(defun helm-tree-sitter-c-function-definition-fn (elem)
"Helm-tree-sitter handler for function_definition nodes in C 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)))
(storage-class-specifier (helm-tree-sitter-utils-get-node-text (alist-get 'storage_class_specifier children-alist)))
(primitive-type (helm-tree-sitter-utils-get-node-text (alist-get 'primitive_type children-alist)))
(type-identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier children-alist)))
(sized-type-specifier (helm-tree-sitter-utils-get-node-text (alist-get 'sized_type_specifier children-alist)))
(struct-specifier (helm-tree-sitter-utils-get-node-text (alist-get 'struct_specifier children-alist)))
(function-declarator (helm-tree-sitter-utils-get-node-text (alist-get 'function_declarator children-alist)))
(function-pointer-declarator (helm-tree-sitter-utils-get-node-text (alist-get 'pointer_declarator children-alist))))
(concat
(propertize "Function: "
'face 'italic)
(helm-tree-sitter-utils-strip-newlines-and-whitespaces
(concat
(helm-tree-sitter-utils-append-space-if-not-empty storage-class-specifier)
struct-specifier
sized-type-specifier
primitive-type
type-identifier
" "
(helm-tree-sitter-utils-strip-newlines function-declarator)
(helm-tree-sitter-utils-strip-newlines function-pointer-declarator))))))
(defun helm-tree-sitter-c-struct-specifier-fn (elem)
"Helm-tree-sitter handler for struct_specifier nodes in C 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)))
(type-identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier children-alist)))
(field-declaration-list-node (alist-get 'field_declaration_list children-alist)))
;; To prevent output from being too verbose, we'll only show structs that have
;; field declarations too.
(when (tsc-node-p field-declaration-list-node)
(if (not (string= "" type-identifier))
(concat
(propertize "Struct: "
'face 'italic)
type-identifier)
;; We are dealing with struct that has a field declaration list, but no type-identifier...
;; This must be a typedef case.
;; Let's check if our parent has a type_identifier:
(let* ((parent-node (tsc-get-parent (helm-tree-sitter-core-elem-node elem))))
(when parent-node
(let* ((parent-children-alist (helm-tree-sitter-utils-node-children-to-alist parent-node))
(parent-type-identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier parent-children-alist))))
(concat
(propertize "typedef Struct: "
'face 'italic)
parent-type-identifier))))))))
(defun helm-tree-sitter-c-enum-specifier-fn (elem)
"Helm-tree-sitter handler for enum_specifier nodes in C 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)))
(type-identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier children-alist)))
(enumerator-list-list-node (alist-get 'enumerator_list children-alist)))
;; To prevent output from being too verbose, we'll only show enums that have
;; field declarations too.
(when (tsc-node-p enumerator-list-list-node)
(if (not (string= "" type-identifier))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "Enum: "
'face 'italic)
type-identifier)
;; We are dealing with enum that has a field declaration list, but no type-identifier...
;; This must be a typedef case.
;; Let's check if our parent has a type_identifier:
(let* ((parent-node (tsc-get-parent (helm-tree-sitter-core-elem-node elem))))
(when parent-node
(let* ((parent-children-alist (helm-tree-sitter-utils-node-children-to-alist parent-node))
(parent-type-identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier parent-children-alist))))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "typedef Enum: "
'face 'italic)
parent-type-identifier))))))))
(defun helm-tree-sitter-c-union-specifier-fn (elem)
"Helm-tree-sitter handler for union_specifier nodes in C 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)))
(type-identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier children-alist)))
(field-declaration-list (alist-get 'field_declaration_list children-alist)))
;; To prevent output from being too verbose, we'll only show enums that have
;; field declarations too.
(when (tsc-node-p field-declaration-list)
(if (not (string= "" type-identifier))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "Union: "
'face 'italic)
type-identifier)
;; We are dealing with union that has a field declaration list, but no type-identifier...
;; This must be a typedef case.
;; Let's check if our parent has a type_identifier:
(let* ((parent-node (tsc-get-parent (helm-tree-sitter-core-elem-node elem))))
(when parent-node
(let* ((parent-children-alist (helm-tree-sitter-utils-node-children-to-alist parent-node))
(parent-type-identifier (helm-tree-sitter-utils-get-node-text (alist-get 'type_identifier parent-children-alist))))
(concat
(helm-tree-sitter-utils-prepend-depth-if-needed elem)
(propertize "typedef Union: "
'face 'italic)
parent-type-identifier))))))))
(provide 'helm-tree-sitter-c)
;;; helm-tree-sitter-c.el ends here