-
Notifications
You must be signed in to change notification settings - Fork 9
/
helm-lsp.el
executable file
·330 lines (290 loc) · 13.1 KB
/
helm-lsp.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
;;; helm-lsp.el --- LSP helm integration -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Ivan Yonchovski
;; Copyright (C) 2024 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/>.
;; Author: Ivan Yonchovski <[email protected]>
;; Keywords: languages, debug
;; URL: https://github.com/emacs-lsp/helm-lsp
;; Package-Requires: ((emacs "27.1") (dash "2.14.1") (lsp-mode "5.0") (helm "2.0"))
;; Version: 0.3
;;; Commentary:
;; `helm' for lsp function.
;;; Code:
(require 'helm)
(require 'helm-imenu)
(require 'dash)
(require 'lsp-mode)
(defvar helm-lsp-symbols-request-id nil)
(defvar helm-lsp-symbols-result-p nil)
(defvar helm-lsp-symbols-result nil)
(defgroup helm-lsp nil
"`helm-lsp' group."
:group 'lsp-mode
:tag "Language Server")
(lsp-defun helm-lsp-workspace-symbol-action ((&SymbolInformation :location))
"Action for helm workspace symbol.
CANDIDATE is the selected item in the helm menu."
(lsp-goto-location location))
(defface helm-lsp-container-face
'((t :height 0.8 :inherit shadow))
"The face used for code lens overlays."
:group 'helm-lsp)
(defcustom helm-lsp-treemacs-icons t
"If non-nil, use `lsp-treemacs' icons."
:group 'helm-lsp
:type 'boolean)
(defun helm-lsp--extract-file-name (uri)
"Extract file name from URI."
(propertize
(if (string= "jdt" (-> uri url-unhex-string url-generic-parse-url url-type))
(cl-second (s-match ".*\(\\(.*\\)" uri))
(f-filename uri))
'face 'helm-lsp-container-face))
(defun helm-lsp--get-icon (kind)
"Get the icon by KIND."
(require 'lsp-treemacs)
(ht-get (treemacs-theme->gui-icons (treemacs--find-theme lsp-treemacs-theme))
(lsp-treemacs-symbol-kind->icon kind)))
(defun helm-lsp--workspace-symbol (workspaces name input)
"Search against WORKSPACES NAME with default INPUT."
(setq helm-lsp-symbols-result nil)
(if workspaces
(with-lsp-workspaces workspaces
(helm
:sources
(helm-build-sync-source
name
:candidates (lambda ()
(if helm-lsp-symbols-result-p
helm-lsp-symbols-result
(with-lsp-workspaces workspaces
(-let (((request &as &plist :id request-id) ))
(setq helm-lsp-symbols-request-id request-id)
(lsp-request-async
"workspace/symbol"
(list :query helm-pattern)
(lambda (candidates)
(setq helm-lsp-symbols-request-id nil)
(and helm-alive-p
(let ((helm-lsp-symbols-result-p t))
(setq helm-lsp-symbols-result candidates)
(helm-update))))
:mode 'detached
:cancel-token :workspace-symbols)
helm-lsp-symbols-result))))
:action 'helm-lsp-workspace-symbol-action
:volatile t
:fuzzy-match t
:match (-const t)
:keymap helm-map
:candidate-transformer
(lambda (candidates)
(-map
(-lambda ((candidate &as
&SymbolInformation :container-name? :name :kind :location (&Location :uri)))
(let ((type (or (alist-get kind lsp-symbol-kinds) "Unknown")))
(cons
(if (and (featurep 'lsp-treemacs)
helm-lsp-treemacs-icons)
(concat
(or (helm-lsp--get-icon kind)
(helm-lsp--get-icon 'fallback))
(if (s-blank? container-name?)
name
(concat name " " (propertize container-name? 'face 'helm-lsp-container-face)))
(propertize " · " 'face 'success)
(helm-lsp--extract-file-name uri))
(concat (if (s-blank? container-name?)
name
(concat name " " (propertize container-name? 'face 'helm-lsp-container-face) " -" ))
" "
(propertize (concat "(" type ")") 'face 'font-lock-type-face)
(propertize " · " 'face 'success)
(helm-lsp--extract-file-name uri)))
candidate)))
(-take helm-candidate-number-limit candidates)))
:candidate-number-limit nil
:requires-pattern 0)
:input input))
(user-error "No LSP workspace active")))
;;;###autoload
(defun helm-lsp-workspace-symbol (arg)
"`helm' for lsp workspace/symbol.
When called with prefix ARG the default selection will be symbol at point."
(interactive "P")
(helm-lsp--workspace-symbol (or (lsp-workspaces)
(gethash (lsp-workspace-root default-directory)
(lsp-session-folder->servers (lsp-session))))
"Workspace symbol"
(when arg (thing-at-point 'symbol))))
;;;###autoload
(defun helm-lsp-global-workspace-symbol (arg)
"`helm' for lsp workspace/symbol for all of the current workspaces.
When called with prefix ARG the default selection will be symbol at point."
(interactive "P")
(helm-lsp--workspace-symbol (-uniq (-flatten (ht-values (lsp-session-folder->servers (lsp-session)))))
"Global workspace symbols"
(when arg (thing-at-point 'symbol))))
;;;###autoload
(defun helm-lsp-code-actions()
"Show lsp code actions using helm."
(interactive)
(let ((actions (lsp-code-actions-at-point)))
(cond
((seq-empty-p actions) (signal 'lsp-no-code-actions nil))
((and (eq (seq-length actions) 1) lsp-auto-execute-action)
(lsp-execute-code-action (lsp-seq-first actions)))
(t (helm :sources
(helm-build-sync-source
"Code Actions"
:candidates actions
:candidate-transformer
(lambda (candidates)
(-map
(-lambda ((candidate &as
&CodeAction :title))
(list title :data candidate))
candidates))
:action '(("Execute code action" . (lambda(candidate)
(lsp-execute-code-action (plist-get candidate :data)))))))))))
;; helm projects
(with-eval-after-load 'helm-projectile
(defvar helm-lsp-source-projects
(helm-build-sync-source
"LSP projects"
:candidates (lambda () (lsp-session-folders (lsp-session)))
:fuzzy-match helm-projectile-fuzzy-match
:keymap helm-projectile-projects-map
:mode-line helm-read-file-name-mode-line-string
:action 'helm-source-projectile-projects-actions)
"Helm source for known LSP projects.")
(defun helm-lsp-switch-project (&optional arg)
"Use projectile with Helm for finding files in project
With a prefix ARG invalidates the cache first."
(interactive "P")
(let ((helm-ff-transformer-show-only-basename nil)
(helm-boring-file-regexp-list nil))
(helm :sources 'helm-lsp-source-projects
:buffer (concat "*helm projectile: " (projectile-project-name) "*")
:truncate-lines helm-projectile-truncate-lines
:prompt (projectile-prepend-project-name "Switch to LSP project: ")))))
;; helm diagnostics
(defconst helm-lsp--diag-mapping
`((,lsp/diagnostic-severity-error . error)
(,lsp/diagnostic-severity-warning . warning)
(,lsp/diagnostic-severity-information . info)
(,lsp/diagnostic-severity-hint . info)))
(lsp-defun helm-lsp--diag-matched
(file (&Diagnostic :message
:source? :severity?
:range (&Range :start
(&Position :line :character)))
tokens)
(-all? (lambda (token)
(cl-case (aref token 0)
(?# (s-matches? (substring token 1) file))
(?* (s-contains? (substring token 1)
(symbol-name (alist-get severity? helm-lsp--diag-mapping))))
(t (s-contains? token message))))
tokens))
(lsp-defun helm-lsp-jump-to-error ((file start))
"Go to selected symbol"
(find-file file)
(goto-char (lsp--position-to-point start)))
(lsp-defun helm-lsp-quick-fix ((file start))
"Go to selected symbol and fix the action."
(find-file file)
(goto-char (lsp--position-to-point start))
(call-interactively #'lsp-execute-code-action))
(defface helm-lsp-diag-error
'((t :inherit error))
"Face used for corresponding diag error level."
:group 'lsp-faces)
(defface helm-lsp-diag-info
'((t :inherit success))
"Face used for corresponding diag error level."
:group 'lsp-faces)
(defface helm-lsp-diag-warning
'((t :inherit warning))
"Face used for corresponding diag error level."
:group 'lsp-faces)
(defcustom helm-lsp-diag-face-map
`((,lsp/diagnostic-severity-error . helm-lsp-diag-error)
(,lsp/diagnostic-severity-warning . helm-lsp-diag-warning)
(,lsp/diagnostic-severity-information . helm-lsp-diag-info)
(,lsp/diagnostic-severity-hint . helm-lsp-diag-info))
"Alist diagnostics to face."
:type 'alist)
(defun helm-lsp--diagnostics-transform (candidates)
(let ((tokens (helm-mm-split-pattern helm-pattern)))
(->>
candidates
(-keep (-lambda ((full-path file (diag &as &Diagnostic :message
:source? :severity?
:range (&Range :start (start &as &Position :line :character)) )))
(when (helm-lsp--diag-matched full-path diag tokens)
(list (format
"%s%s %s %s %s%s"
(if (fboundp 'lsp-treemacs-get-icon)
(lsp-treemacs-get-icon (alist-get severity?
helm-lsp--diag-mapping))
(propertize
(format "[%s] " (alist-get severity? helm-lsp--diag-mapping))
'face
(alist-get severity? helm-lsp-diag-face-map)))
(propertize (format "[%s]" source?) 'face 'lsp-details-face)
source? message
(propertize file 'face 'lsp-details-face)
(propertize (format ":%s:%s" line character) 'face 'lsp-details-face))
full-path start))))
(-sort (-lambda ((full-path-1 _ (&Diagnostic :range
(&Range? :start (&Position? :line l1 :character c1))))
(full-path-2 _ (&Diagnostic :range
(&Range? :start (&Position? :line l2 :character c2)))))
(if (string= full-path-1 full-path-2)
(cond
((not l1) t)
((not l2) nil)
(t (if (eq l1 l2) (< c1 c2) (< l1 l2))))
(string< full-path-1 full-path-2)))))))
;;;###autoload
(defun helm-lsp-diagnostics (arg)
"Diagnostics using `helm'"
(interactive "P")
(if (get-buffer "*helm-lsp-diagnostics*")
(progn
(run-with-timer 0 nil #'helm-update)
(helm-resume "*helm-lsp-diagnostics*"))
(helm
:sources
(helm-build-sync-source "Diagnostics"
:mode-line (list "Diagnostics(s)")
:candidates (lambda ()
(->> (lsp-diagnostics)
(ht-map (lambda (file v)
(-map (-partial #'list
file
(if-let ((wks (lsp-workspace-root file)))
(f-relative file wks)
file))
v)))
(apply #'append)))
:action '(("Goto diagnostic" . helm-lsp-jump-to-error)
("Quick fix" . helm-lsp-quick-fix))
:persistent-action #'helm-lsp-jump-to-error
:match (-const t)
:volatile t
:candidate-transformer #'helm-lsp--diagnostics-transform)
:candidate-number-limit nil
:buffer "*helm-lsp-diagnostics*")))
(provide 'helm-lsp)
;;; helm-lsp.el ends here