forked from emacs-lsp/emacs-ccls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccls-semantic-highlight.el
317 lines (272 loc) · 11.7 KB
/
ccls-semantic-highlight.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
;;; -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Tobias Pisani
;; Copyright (C) 2018-2020 Fangrui Song
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and-or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
;; SOFTWARE.
;;; Code:
(require 'ccls-common)
;; ---------------------------------------------------------------------
;; Customization
;; ---------------------------------------------------------------------
(defgroup ccls-sem nil
"ccls semantic highlight."
:group 'tools
:group 'ccls)
(defface ccls-skipped-range-face
'((t :inherit shadow))
"The face used to mark skipped ranges."
:group 'ccls-sem)
(defvar ccls-sem-face-function 'ccls-sem--default-face
"Function used to determine the face of a symbol in semantic highlight.")
(defface ccls-sem-global-variable-face
'((t :weight extra-bold))
"The additional face for global variables."
:group 'ccls-sem)
(defface ccls-sem-local-face
'((t :weight normal))
"The additional face for local entities."
:group 'ccls-sem)
(defface ccls-sem-local-function-face
'((t :inherit ccls-sem-local-face))
"The additional face for local functions."
:group 'ccls-sem)
(defface ccls-sem-member-face
'((t :slant italic))
"The extra face applied to member functions/variables."
:group 'ccls-sem)
(defface ccls-sem-static-face
'((t :weight bold))
"The additional face for variables with static storage."
:group 'ccls-sem)
(defface ccls-sem-static-field-face
'((t :inherit ccls-sem-static-face))
"The additional face for static member variables."
:group 'ccls-sem)
(defface ccls-sem-static-method-face
'((t :inherit ccls-sem-static-face))
"The additional face for static member functions."
:group 'ccls-sem)
(defcustom ccls-sem-function-faces [font-lock-function-name-face]
"Faces for functions."
:type '(repeat face)
:group 'ccls-sem)
(defcustom ccls-sem-macro-faces [font-lock-variable-name-face]
"Faces for macros."
:type '(repeat face)
:group 'ccls-sem)
(defcustom ccls-sem-namespace-faces [font-lock-constant-face]
"Faces for namespaces."
:type '(repeat face)
:group 'ccls-sem)
(defcustom ccls-sem-parameter-faces [font-lock-variable-name-face]
"Faces for parameters."
:type '(repeat face)
:group 'ccls-sem)
(defcustom ccls-sem-type-faces [font-lock-type-face]
"Faces used to mark types."
:type '(repeat face)
:group 'ccls-sem)
(defcustom ccls-sem-variable-faces [font-lock-variable-name-face]
"Faces for variables."
:type '(repeat face)
:group 'ccls-sem)
;; Default colors used by `ccls-use-default-rainbow-sem-highlight'
(defcustom ccls-sem-function-colors
'("#e5b124" "#927754" "#eb992c" "#e2bf8f" "#d67c17"
"#88651e" "#e4b953" "#a36526" "#b28927" "#d69855")
"Default colors for `ccls-sem-function-faces'."
:type '(repeat color)
:group 'ccls-sem)
(defcustom ccls-sem-macro-colors
'("#e79528" "#c5373d" "#e8a272" "#d84f2b" "#a67245"
"#e27a33" "#9b4a31" "#b66a1e" "#e27a71" "#cf6d49")
"Default colors for `ccls-sem-macro-faces'."
:type '(repeat color)
:group 'ccls-sem)
(defcustom ccls-sem-namespace-colors
'("#429921" "#58c1a4" "#5ec648" "#36815b" "#83c65d"
"#417b2f" "#43cc71" "#7eb769" "#58bf89" "#3e9f4a")
"Default colors for `ccls-sem-namespace-faces'."
:type '(repeat color)
:group 'ccls-sem)
(defcustom ccls-sem-parameter-colors
'("#429921" "#58c1a4" "#5ec648" "#36815b" "#83c65d"
"#417b2f" "#43cc71" "#7eb769" "#58bf89" "#3e9f4a")
"Default colors for `ccls-sem-parameter-faces'."
:type '(repeat color)
:group 'ccls-sem)
(defcustom ccls-sem-type-colors
'("#e1afc3" "#d533bb" "#9b677f" "#e350b6" "#a04360"
"#dd82bc" "#de3864" "#ad3f87" "#dd7a90" "#e0438a")
"Default colors for `ccls-sem-type-faces'."
:type '(repeat color)
:group 'ccls-sem)
(defcustom ccls-sem-variable-colors
ccls-sem-parameter-colors
"Default colors for `ccls-sem-variable-faces'."
:type '(repeat color)
:group 'ccls-sem)
(defcustom ccls-enable-skipped-ranges
t
"Enable skipped ranges.
Regions that are disabled by preprocessors will be displayed in shadow."
:group 'ccls-sem
:type 'boolean)
(defcustom ccls-sem-highlight-method
nil
"The method used to draw semantic highlight.
overlays are more accurate than font-lock, but slower.
If nil, disable semantic highlight."
:group 'ccls-sem
:type '(radio
(const nil)
(const :tag "overlay" overlay)
(const :tag "font-lock" font-lock)))
;; ---------------------------------------------------------------------
;; Semantic highlight
;; ---------------------------------------------------------------------
(defvar-local ccls--skipped-ranges-overlays nil "Skipped ranges overlays.")
(defvar-local ccls--sem-overlays nil "Semantic highlight overlays.")
(eval-and-compile
(lsp-interface
(CclsLR (:L :R) nil)
(CclsSemanticHighlight (:uri :symbols) nil)
(CclsSkippedRanges (:uri :skippedRanges) nil))
(lsp-interface
(CclsSemanticHighlightSymbol (:id :parentKind :kind :storage :ranges) nil)))
(defun ccls--clear-sem-highlights ()
"."
(pcase ccls-sem-highlight-method
('overlay
(while ccls--sem-overlays
(delete-overlay (pop ccls--sem-overlays))))
('font-lock
(font-lock-ensure))))
(defun ccls-sem--default-face (symbol)
"Get semantic highlight face of SYMBOL."
;; https://github.com/ccls-project/ccls/blob/master/src/symbol.h
(-let* (((&CclsSemanticHighlightSymbol :id :parent-kind :kind :storage) symbol)
(fn0 (lambda (faces lo0 hi0)
(let* ((n (length faces))
(lo (/ (* lo0 n) 1000))
(hi (/ (* hi0 n) 1000))
(idx (max 0 (if (= lo hi) (1- hi) (+ lo (% id (- hi lo)))))))
(elt faces idx))))
(fn (lambda (faces) (elt faces (% id (length faces))))))
;; ccls/src/indexer.h ClangSymbolKind
;; clang/Index/IndexSymbol.h clang::index::SymbolKind
(pcase kind
;; Functions
(6 `(,(funcall fn0 ccls-sem-function-faces 0 800)
ccls-sem-member-face)) ; Method
(9 `(,(funcall fn0 ccls-sem-function-faces 800 1000)
ccls-sem-member-face)) ; Constructor
(12 `(,(funcall fn0 ccls-sem-function-faces 0 1000)
,@(when (= storage 2)
'(ccls-sem-local-function-face)))) ; Function
(254 `(,(funcall fn0 ccls-sem-function-faces 0 1000)
ccls-sem-static-method-face)) ; StaticMethod
;; Types
(3 (funcall fn0 ccls-sem-namespace-faces 0 1000)) ; Namespace
((or 5 23) (funcall fn0 ccls-sem-type-faces 0 800)) ; Struct, Class
(10 (funcall fn0 ccls-sem-type-faces 800 1000)) ; Enum
(26 (funcall fn0 ccls-sem-type-faces 0 1000)) ; TypeParameter
(252 `(,(funcall fn0 ccls-sem-type-faces 0 1000))) ; TypeAlias
;; Variables
(13 `(,(funcall fn0 ccls-sem-variable-faces 0 1000)
,@(when (member parent-kind '(1 3))
'(ccls-sem-global-variable-face))
,@(when (= storage 2)
'(ccls-sem-static-face)))) ; Variable
(253 (funcall fn0 ccls-sem-parameter-faces 0 1000)) ; Parameter
(255 (funcall fn0 ccls-sem-macro-faces 0 1000)) ; Macro
(8 `(,(funcall fn0 ccls-sem-variable-faces 0 1000)
,(if (= storage 2)
'ccls-sem-static-field-face
'ccls-sem-member-face
))) ; Field
(22 `(,(funcall fn0 ccls-sem-variable-faces 0 1000)
ccls-sem-member-face)) ; EnumMember
(_ (funcall fn ccls-sem-variable-faces)))))
(defun ccls--publish-semantic-highlight (_workspace params)
"Publish semantic highlight information according to PARAMS."
(when ccls-sem-highlight-method
(-when-let* (((&CclsSemanticHighlight :uri :symbols) params)
(file (lsp--uri-to-path uri))
(buffer (find-buffer-visiting file)))
(with-current-buffer buffer
(with-silent-modifications
(ccls--clear-sem-highlights)
(let (overlays)
(seq-doseq (symbol symbols)
(-when-let* ((face (funcall ccls-sem-face-function symbol)))
(seq-doseq (range (lsp:ccls-semantic-highlight-symbol-ranges symbol))
(-let (((&CclsLR :l :r) range))
(push (list (1+ l) (1+ r) face) overlays)))))
;; The server guarantees the ranges are non-overlapping.
(setq overlays (sort overlays (lambda (x y) (< (car x) (car y)))))
(pcase ccls-sem-highlight-method
('font-lock
(seq-doseq (x overlays)
(set-text-properties (car x) (cadr x)
`(fontified t face ,(caddr x) font-lock-face ,(caddr x)))))
('overlay
(seq-doseq (x overlays)
(let ((ov (make-overlay (car x) (cadr x))))
(overlay-put ov 'face (caddr x))
(overlay-put ov 'ccls-sem-highlight t)
(push ov ccls--sem-overlays)))))))))))
(defmacro ccls-use-default-rainbow-sem-highlight ()
"Use default rainbow semantic highlight theme."
(require 'dash)
`(progn
,@(cl-loop
for kind in '("function" "macro" "namespace" "parameter" "type" "variable") append
(let ((colors (intern (format "ccls-sem-%s-colors" kind))))
(append
(--map-indexed
`(defface ,(intern (format "ccls-sem-%s-face-%S" kind it-index))
'((t :foreground ,it)) "." :group 'ccls-sem)
(symbol-value colors))
(list
`(setq ,(intern (format "ccls-sem-%s-faces" kind))
(apply #'vector
(cl-loop for i below (length ,colors) collect
(intern (format "ccls-sem-%s-face-%S" ,kind i)))))))))))
;; ---------------------------------------------------------------------
;; Skipped ranges
;; ---------------------------------------------------------------------
(defun ccls--clear-skipped-ranges ()
"Clean up overlays."
(while ccls--skipped-ranges-overlays
(delete-overlay (pop ccls--skipped-ranges-overlays))))
(defun ccls--publish-skipped-ranges (_workspace params)
"Put overlays on (preprocessed) inactive regions according to PARAMS."
(-let* (((&CclsSkippedRanges :uri :skipped-ranges) params)
(file (lsp--uri-to-path uri)))
(-when-let (buffer (find-buffer-visiting file))
(with-current-buffer buffer
(with-silent-modifications
(ccls--clear-skipped-ranges)
(when ccls-enable-skipped-ranges
(overlay-recenter (point-max))
(seq-doseq (range skipped-ranges)
(let ((ov (make-overlay (lsp--position-to-point (lsp:range-start range))
(lsp--position-to-point (lsp:range-end range)) buffer)))
(overlay-put ov 'face 'ccls-skipped-range-face)
(overlay-put ov 'ccls-inactive t)
(push ov ccls--skipped-ranges-overlays)))))))))
(provide 'ccls-semantic-highlight)