forked from emacs-lsp/lsp-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-headerline.el
484 lines (426 loc) · 21.1 KB
/
lsp-headerline.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
;;; lsp-headerline.el --- LSP headerline features -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2020 emacs-lsp maintainers
;;
;; 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:
;;
;; LSP headerline features
;;
;;; Code:
(require 'lsp-icons)
(require 'lsp-mode)
(defgroup lsp-headerline nil
"LSP support for headerline"
:prefix "lsp-headerline-"
:group 'lsp-mode
:tag "LSP Headerline")
(defcustom lsp-headerline-breadcrumb-segments '(path-up-to-project file symbols)
"Segments used in breadcrumb text on headerline."
:type '(repeat
(choice (const :tag "Include the project name." project)
(const :tag "Include the open file name." file)
(const :tag "Include the directories up to project." path-up-to-project)
(const :tag "Include document symbols if server supports it." symbols)))
:group 'lsp-headerline)
(defcustom lsp-headerline-breadcrumb-enable-symbol-numbers nil
"Whether to label symbols with numbers on the breadcrumb."
:type 'boolean
:group 'lsp-headerline)
(defcustom lsp-headerline-breadcrumb-enable-diagnostics t
"If non-nil, apply different face on the breadcrumb based on the errors."
:type 'boolean
:group 'lsp-headerline
:package-version '(lsp-mode . "8.0.0"))
(defface lsp-headerline-breadcrumb-separator-face '((t :inherit shadow :height 0.8))
"Face used for breadcrumb separator on headerline."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-path-face '((t :inherit font-lock-string-face))
"Face used for breadcrumb paths on headerline."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-path-error-face
'((t :underline (:style wave :color "Red1")
:inherit lsp-headerline-breadcrumb-path-face))
"Face used for breadcrumb paths on headerline when there is an error under that path"
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-path-warning-face
'((t :underline (:style wave :color "Yellow")
:inherit lsp-headerline-breadcrumb-path-face))
"Face used for breadcrumb paths on headerline when there is an warning under that path"
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-path-info-face
'((t :underline (:style wave :color "Green")
:inherit lsp-headerline-breadcrumb-path-face))
"Face used for breadcrumb paths on headerline when there is an info under that path"
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-path-hint-face
'((t :underline (:style wave :color "Green")
:inherit lsp-headerline-breadcrumb-path-face))
"Face used for breadcrumb paths on headerline when there is an hint under that path"
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-project-prefix-face
'((t :inherit font-lock-string-face :weight bold))
"Face used for breadcrumb prefix on headerline.
Only if `lsp-headerline-breadcrumb-prefix` is `project-name-only`."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-unknown-project-prefix-face
'((t :inherit shadow :weight bold))
"Face used for breadcrumb prefix on headerline.
Only if `lsp-headerline-breadcrumb-prefix` is `project-name-only`."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-symbols-face
'((t :inherit font-lock-doc-face :weight bold))
"Face used for breadcrumb symbols text on headerline."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-symbols-error-face
'((t :inherit lsp-headerline-breadcrumb-symbols-face
:underline (:style wave :color "Red1")))
"Face used for breadcrumb symbols text on headerline when there
is an error in symbols range."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-symbols-warning-face
'((t :inherit lsp-headerline-breadcrumb-symbols-face
:underline (:style wave :color "Yellow")))
"Face used for breadcrumb symbols text on headerline when there
is an warning in symbols range."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-symbols-info-face
'((t :inherit lsp-headerline-breadcrumb-symbols-face
:underline (:style wave :color "Green")))
"Face used for breadcrumb symbols text on headerline when there
is an info in symbols range."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-symbols-hint-face
'((t :inherit lsp-headerline-breadcrumb-symbols-face
:underline (:style wave :color "Green")))
"Face used for breadcrumb symbols text on headerline when there
is an hints in symbols range."
:group 'lsp-headerline)
(defface lsp-headerline-breadcrumb-deprecated-face
'((t :inherit lsp-headerline-breadcrumb-symbols-face
:strike-through t))
"Face used on breadcrumb deprecated text on modeline."
:group 'lsp-headerline)
(defvar-local lsp-headerline--string nil
"Holds the current breadcrumb string on headerline.")
(defvar lsp-headerline-arrow nil
"Holds the current breadcrumb string on headerline.")
(defvar-local lsp-headerline--path-up-to-project-segments nil
"Holds the current breadcrumb path-up-to-project segments for
caching purposes.")
;; Redefine local vars of `all-the-icons' to avoid bytecode compilation errors.
(defvar all-the-icons-default-adjust)
(defvar all-the-icons-scale-factor)
(defun lsp-headerline--arrow-icon ()
"Build the arrow icon for headerline breadcrumb."
(or
lsp-headerline-arrow
(setq lsp-headerline-arrow (let ((all-the-icons-scale-factor 1.0)
(all-the-icons-default-adjust 0))
(lsp-icons-all-the-icons-material-icon
"chevron_right"
'lsp-headerline-breadcrumb-separator-face
">"
'headerline-breadcrumb)))))
(lsp-defun lsp-headerline--symbol-icon ((&DocumentSymbol :kind))
"Build the SYMBOL icon for headerline breadcrumb."
(concat (lsp-icons-get-by-symbol-kind kind 'headerline-breadcrumb)
" "))
(lsp-defun lsp-headerline--go-to-symbol ((&DocumentSymbol
:selection-range (&RangeToPoint :start selection-start)
:range (&RangeToPoint :start narrowing-start
:end narrowing-end)))
"Go to breadcrumb symbol.
If the buffer is narrowed and the target symbol lies before the
minimum reachable point in the narrowed buffer, then widen and
narrow to the outer symbol."
(when (buffer-narrowed-p)
(narrow-to-region
(min (point-min) narrowing-start)
(max (point-max) narrowing-end)))
(goto-char selection-start))
(lsp-defun lsp-headerline--narrow-to-symbol ((&DocumentSymbol :range (&RangeToPoint :start :end)))
"Narrow to breadcrumb symbol range."
(narrow-to-region start end))
(defun lsp-headerline--with-action (local-map help-echo-string display-string)
"Assign LOCAL-MAP and HELP-ECHO-STRING to the region around the DISPLAY-STRING."
(propertize display-string
'mouse-face 'header-line-highlight
'help-echo help-echo-string
'local-map local-map))
(defmacro lsp-headerline--make-mouse-handler (&rest body)
"Making mouse event handler.
Switch to current mouse interacting window before doing BODY."
(declare (debug t) (indent 0))
`(lambda (event)
(interactive "e")
(select-window (posn-window (elt event 1)))
,@body))
(defun lsp-headerline--directory-with-action (full-path directory-display-string)
"Build action for FULL-PATH and DIRECTORY-DISPLAY-STRING."
(lsp-headerline--with-action (let ((map (make-sparse-keymap)))
(define-key map [header-line mouse-1]
(lsp-headerline--make-mouse-handler
(dired full-path)))
(define-key map [header-line mouse-2]
(lsp-headerline--make-mouse-handler
(dired-other-window full-path)))
map)
(format "mouse-1: browse '%s' with Dired\nmouse-2: browse '%s' with Dired in other window"
directory-display-string
directory-display-string)
(propertize directory-display-string
'lsp-full-path full-path)))
(declare-function evil-set-jump "ext:evil-jumps")
(lsp-defun lsp-headerline--symbol-with-action ((symbol &as &DocumentSymbol :name) symbol-display-string)
"Build action for SYMBOL and SYMBOL-STRING."
(lsp-headerline--with-action (let ((map (make-sparse-keymap)))
(define-key map [header-line mouse-1]
(lsp-headerline--make-mouse-handler
(when (bound-and-true-p evil-mode)
(evil-set-jump))
(lsp-headerline--go-to-symbol symbol)))
(define-key map [header-line mouse-2]
(lsp-headerline--make-mouse-handler
(-let (((&DocumentSymbol :range (&RangeToPoint :start :end)) symbol))
(if (and (eq (point-min) start) (eq (point-max) end))
(widen)
(lsp-headerline--narrow-to-symbol symbol)))))
map)
(format "mouse-1: go to '%s' symbol\nmouse-2: %s"
name
(-let (((&DocumentSymbol :range (&RangeToPoint :start :end)) symbol))
(if (and (eq (point-min) start) (eq (point-max) end))
"widen"
(format "narrow to '%s' range" name))))
symbol-display-string))
(defun lsp-headerline--path-up-to-project-root (root-path path)
"Find recursively the folders until the project ROOT-PATH.
PATH is the current folder to be checked."
(let ((current-path path)
headerline-path-components)
(while (not (lsp-f-same? root-path current-path))
(push (lsp-headerline--directory-with-action current-path
(f-filename current-path))
headerline-path-components)
(setq current-path (lsp-f-parent current-path)))
headerline-path-components))
(defun lsp-headerline--build-project-string ()
"Build the project-segment string for the breadcrumb."
(-if-let (root (lsp-workspace-root))
(propertize (lsp-headerline--directory-with-action
root
(f-filename root))
'font-lock-face
'lsp-headerline-breadcrumb-project-prefix-face)
(propertize "<unknown>"
'font-lock-face
'lsp-headerline-breadcrumb-unknown-project-prefix-face)))
(defun lsp-headerline--build-file-string ()
"Build the file-segment string for the breadcrumb."
(let* ((file-path (or (buffer-file-name) ""))
(filename (f-filename file-path)))
(if-let ((file-ext (f-ext file-path)))
(concat (lsp-icons-get-by-file-ext file-ext 'headerline-breadcrumb)
" "
(propertize filename
'font-lock-face
(lsp-headerline--face-for-path file-path)))
filename)))
(defun lsp-headerline--face-for-path (dir)
"Calculate the face for DIR."
(if-let ((diags (lsp-diagnostics-stats-for (directory-file-name dir))))
(cl-labels ((check-severity
(severity)
(not (zerop (aref diags severity)))))
(cond
((not lsp-headerline-breadcrumb-enable-diagnostics)
'lsp-headerline-breadcrumb-path-face)
((check-severity lsp/diagnostic-severity-error)
'lsp-headerline-breadcrumb-path-error-face)
((check-severity lsp/diagnostic-severity-warning)
'lsp-headerline-breadcrumb-path-warning-face)
((check-severity lsp/diagnostic-severity-information)
'lsp-headerline-breadcrumb-path-info-face)
((check-severity lsp/diagnostic-severity-hint)
'lsp-headerline-breadcrumb-path-hint-face)
(t 'lsp-headerline-breadcrumb-path-face)))
'lsp-headerline-breadcrumb-path-face))
(defun lsp-headerline--severity-level-for-range (range)
"Get the severiy level for RANGE."
(let ((range-severity 10))
(mapc (-lambda ((&Diagnostic :range (&Range :start) :severity?))
(when (lsp-point-in-range? start range)
(setq range-severity (min range-severity severity?))))
(lsp--get-buffer-diagnostics))
range-severity))
(defun lsp-headerline--build-path-up-to-project-string ()
"Build the path-up-to-project segment for the breadcrumb."
(if-let ((root (lsp-workspace-root)))
(let ((segments (or
lsp-headerline--path-up-to-project-segments
(setq lsp-headerline--path-up-to-project-segments
(lsp-headerline--path-up-to-project-root
root
(lsp-f-parent (buffer-file-name)))))))
(mapconcat (lambda (next-dir)
(propertize next-dir
'font-lock-face
(lsp-headerline--face-for-path
(get-text-property
0 'lsp-full-path next-dir))))
segments
(concat " " (lsp-headerline--arrow-icon) " ")))
""))
(lsp-defun lsp-headerline--face-for-symbol ((&DocumentSymbol :deprecated?
:range))
"Get the face for SYMBOL."
(let ((range-severity (lsp-headerline--severity-level-for-range range)))
(cond
(deprecated? 'lsp-headerline-breadcrumb-deprecated-face)
((not lsp-headerline-breadcrumb-enable-diagnostics)
'lsp-headerline-breadcrumb-symbols-face)
((= range-severity lsp/diagnostic-severity-error)
'lsp-headerline-breadcrumb-symbols-error-face)
((= range-severity lsp/diagnostic-severity-warning)
'lsp-headerline-breadcrumb-symbols-warning-face)
((= range-severity lsp/diagnostic-severity-information)
'lsp-headerline-breadcrumb-symbols-info-face)
((= range-severity lsp/diagnostic-severity-hint)
'lsp-headerline-breadcrumb-symbols-hint-face)
(t 'lsp-headerline-breadcrumb-symbols-face))))
(defun lsp-headerline--build-symbol-string ()
"Build the symbol segment for the breadcrumb."
(if (lsp-feature? "textDocument/documentSymbol")
(-if-let* ((lsp--document-symbols-request-async t)
(symbols (lsp--get-document-symbols))
(symbols-hierarchy (lsp--symbols->document-symbols-hierarchy symbols))
(enumerated-symbols-hierarchy
(-map-indexed (lambda (index elt)
(cons elt (1+ index)))
symbols-hierarchy)))
(mapconcat
(-lambda (((symbol &as &DocumentSymbol :name)
. index))
(let* ((symbol2-name
(propertize name
'font-lock-face
(lsp-headerline--face-for-symbol symbol)))
(symbol2-icon (lsp-headerline--symbol-icon symbol))
(full-symbol-2
(concat
(if lsp-headerline-breadcrumb-enable-symbol-numbers
(concat
(propertize (number-to-string index)
'face
'lsp-headerline-breadcrumb-symbols-face)
" ")
"")
(if symbol2-icon
(concat symbol2-icon symbol2-name)
symbol2-name))))
(lsp-headerline--symbol-with-action symbol full-symbol-2)))
enumerated-symbols-hierarchy
(concat " " (lsp-headerline--arrow-icon) " "))
"")
""))
(defun lsp-headerline--build-string ()
"Build the header-line string."
(string-trim-right
(mapconcat
(lambda (segment)
(let ((segment-string
(pcase segment
('project (lsp-headerline--build-project-string))
('file (lsp-headerline--build-file-string))
('path-up-to-project (lsp-headerline--build-path-up-to-project-string))
('symbols (lsp-headerline--build-symbol-string))
(_ (lsp-log "'%s' is not a valid entry for `lsp-headerline-breadcrumb-segments'"
(symbol-name segment))
""))))
(if (eq segment-string "")
""
(concat (lsp-headerline--arrow-icon)
" "
segment-string
" "))))
lsp-headerline-breadcrumb-segments
"")))
(defun lsp-headerline--check-breadcrumb (&rest _)
"Request for document symbols to build the breadcrumb."
(setq lsp-headerline--string (lsp-headerline--build-string))
(force-mode-line-update))
(defun lsp-headerline--enable-breadcrumb ()
"Enable headerline breadcrumb mode."
(when (and lsp-headerline-breadcrumb-enable
(lsp-feature? "textDocument/documentSymbol"))
(lsp-headerline-breadcrumb-mode 1)))
(defun lsp-headerline--disable-breadcrumb ()
"Disable headerline breadcrumb mode."
(lsp-headerline-breadcrumb-mode -1))
;;;###autoload
(define-minor-mode lsp-headerline-breadcrumb-mode
"Toggle breadcrumb on headerline."
:group 'lsp-headerline
:global nil
(cond
(lsp-headerline-breadcrumb-mode
;; make sure header-line-format, if non-nil, is a list. as
;; mode-line-format says: "The value may be nil, a string, a
;; symbol or a list."
(unless (listp header-line-format)
(setq header-line-format (list header-line-format)))
(add-to-list 'header-line-format '(t (:eval lsp-headerline--string)))
(add-hook 'xref-after-jump-hook #'lsp-headerline--check-breadcrumb nil t)
(add-hook 'lsp-on-idle-hook #'lsp-headerline--check-breadcrumb nil t)
(add-hook 'lsp-configure-hook #'lsp-headerline--enable-breadcrumb nil t)
(add-hook 'lsp-unconfigure-hook #'lsp-headerline--disable-breadcrumb nil t))
(t
(remove-hook 'lsp-on-idle-hook #'lsp-headerline--check-breadcrumb t)
(remove-hook 'lsp-configure-hook #'lsp-headerline--enable-breadcrumb t)
(remove-hook 'lsp-unconfigure-hook #'lsp-headerline--disable-breadcrumb t)
(remove-hook 'xref-after-jump-hook #'lsp-headerline--check-breadcrumb t)
(setq lsp-headerline--path-up-to-project-segments nil)
(setq header-line-format (remove '(t (:eval lsp-headerline--string)) header-line-format)))))
;;;###autoload
(defun lsp-breadcrumb-go-to-symbol (symbol-position)
"Go to the symbol on breadcrumb at SYMBOL-POSITION."
(interactive "P")
(if (numberp symbol-position)
(if (lsp-feature? "textDocument/documentSymbol")
(-if-let* ((lsp--document-symbols-request-async t)
(symbols (lsp--get-document-symbols))
(symbols-hierarchy (lsp--symbols->document-symbols-hierarchy symbols)))
(lsp-headerline--go-to-symbol (nth (1- symbol-position) symbols-hierarchy))
(lsp--info "Symbol not found for position %s" symbol-position))
(lsp--info "Server does not support breadcrumb."))
(lsp--info "Call this function with a number representing the symbol position on breadcrumb")))
(declare-function evil-set-command-property "ext:evil-common")
(with-eval-after-load 'evil
(evil-set-command-property 'lsp-breadcrumb-go-to-symbol :jump t))
;;;###autoload
(defun lsp-breadcrumb-narrow-to-symbol (symbol-position)
"Narrow to the symbol range on breadcrumb at SYMBOL-POSITION."
(interactive "P")
(if (numberp symbol-position)
(if (lsp-feature? "textDocument/documentSymbol")
(-if-let* ((lsp--document-symbols-request-async t)
(symbols (lsp--get-document-symbols))
(symbols-hierarchy (lsp--symbols->document-symbols-hierarchy symbols)))
(lsp-headerline--narrow-to-symbol (nth (1- symbol-position) symbols-hierarchy))
(lsp--info "Symbol not found for position %s" symbol-position))
(lsp--info "Server does not support breadcrumb."))
(lsp--info "Call this function with a number representing the symbol position on breadcrumb")))
(lsp-consistency-check lsp-headerline)
(provide 'lsp-headerline)
;;; lsp-headerline.el ends here