-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcitre-lang-c.el
282 lines (254 loc) · 10.6 KB
/
citre-lang-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
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
;;; citre-lang-c.el --- Language support for C -*- lexical-binding: t -*-
;; Copyright (C) 2021 Hao WANG
;; Author: Hao WANG <amaikinono@gmail.com>
;; Maintainer: Hao WANG <amaikinono@gmail.com>
;; Created: 28 Jan 2021
;; Keywords: convenience, tools
;; Homepage: https://github.com/universal-ctags/citre
;; Version: 0.4.1
;; Package-Requires: ((emacs "26.1"))
;; This file is NOT part of GNU Emacs.
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Tags backend language support for C. The features are:
;;
;; - When the symbol is after "->" or ".", tags of member kind are sorted above
;; others.
;; - When the symbol is before "(", tags of function/macro kinds are sorted
;; above others.
;; - When the cursor is on the header file in a "#include" directive, the
;; header file itself, and the references to that header file (if tagged) is
;; found as its definitions. References that uses paths can't be found.
;; Also, file names will be used for auto-completion.
;;; Code:
;; To see the outline of this file, run M-x outline-minor-mode and
;; then press C-c @ C-t. To also show the top-level functions and
;; variable declarations in each section, run M-x occur with the
;; following query: ^;;;;* \|^(
;;;; Libraries
(require 'citre-tags)
(require 'rx)
;;;; Get symbol at point
(defun citre-lang-c--get-header-at-point ()
"Get header at point."
(let* ((bounds (if (use-region-p)
(cons (region-beginning) (region-end))
(bounds-of-thing-at-point 'filename))))
(when (and bounds
(save-excursion
(goto-char (car bounds))
(looking-back
(rx "#" (* space) "include"
(* space) (or "<" "\""))
(line-beginning-position))))
(let* ((full-symbol (buffer-substring-no-properties
(car bounds) (cdr bounds)))
;; Path name can be used in #include directive, so the same header
;; can be referenced differently in different files. We only keep
;; the non-directory part. By doing so we can't match #includes
;; that uses paths, but we make use of binary search of readtags.
(symbol (file-name-nondirectory full-symbol))
;; When SYMBOL and FULL-SYMBOL are not equal, we know we are using
;; path, and we want to get it to filter the input field of file
;; tags.
(path (when (not (equal symbol full-symbol))
;; Remove upper components above ../
;; ../linux/foo.h => linux/foo.h
(if (string-match (rx (* "../") (group (* anything)))
full-symbol)
(match-string 1 full-symbol)
full-symbol))))
(citre-put-property symbol
'bounds (cons (- (cdr bounds) (length symbol))
(cdr bounds))
'syntax 'header
'literal-path path)))))
(defun citre-lang-c--get-normal-symbol ()
"Get non-header symbol at point."
(when-let* ((symbol (citre-tags-get-symbol-default)))
(let* ((bounds (citre-get-property 'bounds symbol))
syntax)
(save-excursion
(goto-char (car bounds))
(when (looking-back (rx (or "->" ".")) (- (point) 2))
(setq syntax 'member)))
(save-excursion
(goto-char (cdr bounds))
(when (looking-at (rx (* " ") "("))
(setq syntax (if (eq syntax 'member)
'callable-member
'function))))
(save-excursion
(goto-char (car bounds))
(when (looking-back (rx symbol-start
(group (or "struct" "union" "enum" "goto"))
(+ space))
(line-beginning-position))
(setq syntax (intern (match-string 1)))))
(citre-put-property symbol 'syntax syntax))))
(defun citre-lang-c-get-symbol ()
"Get symbol function for C."
(or (citre-lang-c--get-header-at-point)
(citre-lang-c--get-normal-symbol)))
;;;; Finding definitions
(defun citre-lang-c-definition-filter (symbol)
"Filter for finding definitions of SYMBOL in C."
(pcase (citre-get-property 'syntax symbol)
('header
`(or
;; The header.
,(citre-readtags-filter-kind "file")
;; The references to the header.
,(citre-readtags-filter-kind "header")))
(_ (citre-tags-definition-default-filter symbol))))
(defun citre-lang-c-definition-sorter (symbol)
"Sorter for finding definitions of SYMBOL in C."
`(<or>
,(citre-readtags-sorter
citre-tags-sorter-arg-put-references-below)
;; Sort on the kinds.
,(pcase (citre-get-property 'syntax symbol)
('header
(apply #'citre-readtags-sorter
;; Put the reference tags below.
`(filter ,(citre-readtags-filter-kind "header") -)
;; For definitions (i.e., the file tags of the header), put
;; those with the right directory above others.
(when-let* ((path (citre-get-property 'literal-path symbol)))
(list `(filter ,(citre-readtags-filter 'input path 'suffix)
+)))))
('member
(citre-readtags-sorter (citre-tags-sorter-arg-put-kinds-above
'("member"))))
('callable-member
(citre-readtags-sorter
(citre-tags-sorter-arg-put-kinds-above '("member"))
;; If a member is callable, its typeref field may include
;; "(*)" as substring.
;;
;; e.g. f in
;; ---
;; struct s {
;; int (*f) (void);
;; };
;; ---
;; is tagged like:
;;
;; f input.c 2;" ...typeref:typename:int (*)(void)
;; +-----------------------------------------^^^
;; `- This is the clue for finding callable members.
`(filter ,(citre-readtags-filter 'typeref "(*)" 'substr) +)))
('function
(citre-readtags-sorter
(citre-tags-sorter-arg-put-kinds-above '("function" "macro"))))
('goto
(citre-readtags-sorter (citre-tags-sorter-arg-put-kinds-above
'("label"))
;; Several languages defines "label" kind, and
;; we should put labels in C above others. We
;; do this for C++ too as ctags considers header
;; files to be C++ source files.
`(filter ,(citre-readtags-filter-lang "C") +)
`(filter ,(citre-readtags-filter-lang "C++")
+)))
((and (or 'struct 'union 'enum)
keyword)
;; If a symbol comes after keywords "struct", "union", or "enum",
;; the symbol must a name of struct tag, union tag or enum tag.
;; We utilize this fact to imporve the order of the list.
;;
;; e.g. input for ctags:
;; --
;; struct point { /* struct tag */
;; float x;
;; float y;
;; };
;;
;; struct game {
;; ...
;; score point; /* member */
;; ...
;; };
;; --
;;
;; Consider your cursor is at | in the following buffer content:
;; --
;; struct rectangle {
;; struct po|int a, b;
;; };
;; --
;; When a user presses M-., the user expects citre gives
;; the struct tag higher priority than the member in the xref
;; list though the struct tag and the member has the same name,
;; "point".
(citre-readtags-sorter
(citre-tags-sorter-arg-put-kinds-above (list (symbol-name keyword)))))
;; Don't sort for other syntax.
(_ 0))
,(citre-readtags-sorter 'input '(length name +) 'name
citre-tags-sorter-arg-size-order)))
;;;; Auto-completion
(defun citre-lang-c-completion-filter (symbol)
"Filter for auto-completing SYMBOL in C."
(pcase (citre-get-property 'syntax symbol)
('header
citre-tags-filter-file-tags)
(_
(citre-tags-completion-default-filter symbol))))
(defun citre-lang-c-completion-sorter (symbol)
"Sorter for auto-completing SYMBOL in C."
`(<or>
,(pcase (citre-get-property 'syntax symbol)
('member
(citre-readtags-sorter (citre-tags-sorter-arg-put-kinds-above
'("member"))))
('callable-member
(citre-readtags-sorter
(citre-tags-sorter-arg-put-kinds-above '("member"))
;; See the comment in `citre-lang-c-definition-sorter'.
`(filter ,(citre-readtags-filter 'typeref "(*)" 'substr) +)))
('function
(citre-readtags-sorter
(citre-tags-sorter-arg-put-kinds-above '("function" "member"))))
('goto
(citre-readtags-sorter (citre-tags-sorter-arg-put-kinds-above
'("label"))))
((and (or 'struct 'union 'enum)
keyword)
;; See the comment in `citre-lang-c-definition-sorter'.
(citre-readtags-sorter
(citre-tags-sorter-arg-put-kinds-above (list (symbol-name keyword)))))
(_ 0))
,citre-tags-completion-default-sorter))
;;;; Plugging into the language support framework
(defvar citre-lang-c-plist
`(:get-symbol
citre-lang-c-get-symbol
:definition-filter
citre-lang-c-definition-filter
:definition-sorter
citre-lang-c-definition-sorter
:completion-filter
citre-lang-c-completion-filter
:completion-sorter
citre-lang-c-completion-sorter)
"C language support for Citre.")
(citre-tags-register-language-support 'c-mode citre-lang-c-plist)
(provide 'citre-lang-c)
;; Local Variables:
;; indent-tabs-mode: nil
;; outline-regexp: ";;;;* "
;; fill-column: 79
;; emacs-lisp-docstring-fill-column: 65
;; sentence-end-double-space: t
;; End:
;;; citre-lang-c.el ends here