Skip to content

Commit a229185

Browse files
authored
Use lsp-protocol and plists (#458)
* migrate lsp-ui.el * migrate lsp-ui-doc.el * migrate lsp-ui-peek.el * migrate lsp-ui-sideline.el * Fix sideline * Check type instead of checking field * Check type * Fix byte compile errors * Fix tests
1 parent d92cf83 commit a229185

File tree

7 files changed

+89
-73
lines changed

7 files changed

+89
-73
lines changed

lsp-ui-doc.el

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
;;; Code:
3232

33+
(require 'lsp-protocol)
3334
(require 'lsp-mode)
3435
(require 'dash)
3536
(require 'dash-functional)
@@ -261,9 +262,11 @@ Because some variables are buffer local.")
261262
(string-trim-right
262263
(let* ((string (if (stringp marked-string)
263264
marked-string
264-
(gethash "value" marked-string)))
265-
(with-lang (hash-table-p marked-string))
266-
(language (or (and with-lang (or (gethash "language" marked-string) (gethash "kind" marked-string)))
265+
(lsp:markup-content-value marked-string)))
266+
(with-lang (lsp-marked-string? marked-string))
267+
(language (or (and with-lang
268+
(or (lsp:marked-string-language marked-string)
269+
(lsp:markup-content-kind marked-string)))
267270
language)))
268271
(cond
269272
(lsp-ui-doc-use-webkit
@@ -273,8 +276,8 @@ Because some variables are buffer local.")
273276
(t (lsp--render-element (lsp-ui-doc--inline-formatted-string string)))))))
274277

275278
(defun lsp-ui-doc--filter-marked-string (list-marked-string)
276-
(let ((groups (--separate (and (hash-table-p it)
277-
(lsp-get-renderer (gethash "language" it)))
279+
(let ((groups (--separate (and (lsp-marked-string? it)
280+
(lsp-get-renderer (lsp:marked-string-language it)))
278281
(append list-marked-string nil))))
279282
(if lsp-ui-doc-include-signature
280283
list-marked-string
@@ -287,19 +290,19 @@ MarkedString | MarkedString[] | MarkupContent (as defined in the LSP).
287290
We don't extract the string that `lps-line' is already displaying."
288291
(cond
289292
((stringp contents) (lsp-ui-doc--extract-marked-string contents)) ;; MarkedString
290-
((sequencep contents) ;; MarkedString[]
293+
((vectorp contents) ;; MarkedString[]
291294
(mapconcat 'lsp-ui-doc--extract-marked-string
292295
(lsp-ui-doc--filter-marked-string (seq-filter #'identity contents))
293296
"\n\n"
294297
;; (propertize "\n\n" 'face '(:height 0.4))
295298
))
296299
;; when we get markdown contents, render using emacs gfm-view-mode / markdown-mode
297-
((string= (gethash "kind" contents) "markdown") ;; Markdown MarkupContent
298-
(lsp-ui-doc--extract-marked-string (gethash "value" contents) "markdown"))
299-
((gethash "kind" contents) (gethash "value" contents)) ;; Plaintext MarkupContent
300-
((gethash "language" contents) ;; MarkedString
301-
(lsp-ui-doc--extract-marked-string (gethash "value" contents)
302-
(gethash "language" contents)))))
300+
((string= (lsp-markup-content? contents) lsp/markup-kind-markdown) ;; Markdown MarkupContent
301+
(lsp-ui-doc--extract-marked-string (lsp:markup-content-value contents) lsp/markup-kind-markdown))
302+
((lsp-markup-content? contents) (lsp:markup-content-value contents)) ;; Plaintext MarkupContent
303+
((lsp-marked-string? contents) ;; MarkedString
304+
(lsp-ui-doc--extract-marked-string (lsp:marked-string-value contents)
305+
(lsp:marked-string-language contents)))))
303306

304307
(defun lsp-ui-doc--webkit-run-xwidget ()
305308
"Launch embedded WebKit instance."
@@ -700,7 +703,7 @@ HEIGHT is the documentation number of lines."
700703
:cancel-token :lsp-ui-doc-hover)))))))
701704
(lsp-ui-doc--hide-frame))))
702705

703-
(defun lsp-ui-doc--callback (hover bounds buffer)
706+
(lsp-defun lsp-ui-doc--callback ((hover &as &Hover? :contents) bounds buffer)
704707
"Process the received documentation.
705708
HOVER is the doc returned by the LS.
706709
BOUNDS are points of the symbol that have been requested.
@@ -712,7 +715,7 @@ BUFFER is the buffer where the request has been made."
712715
(setq lsp-ui-doc--bounds bounds)
713716
(lsp-ui-doc--display
714717
(thing-at-point 'symbol t)
715-
(-some->> (gethash "contents" hover)
718+
(-some->> contents
716719
lsp-ui-doc--extract
717720
(replace-regexp-in-string "\r" ""))))
718721
(lsp-ui-doc--hide-frame)))

lsp-ui-flycheck.el

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727

2828
;;; Code:
2929

30-
(require 'lsp-mode)
3130
(require 'flycheck)
3231
(require 'pcase)
3332
(require 'dash)
3433

34+
(require 'lsp-protocol)
35+
(require 'lsp-mode)
36+
3537
(defgroup lsp-ui-flycheck nil
3638
"The LSP extension to display syntax checking."
3739
:group 'tools
@@ -40,8 +42,6 @@
4042
:link '(custom-manual "(lsp-ui-flycheck) Top")
4143
:link '(info-link "(lsp-ui-flycheck) Customizing"))
4244

43-
44-
4545
(defcustom lsp-ui-flycheck-list-position 'bottom
4646
"Position where `lsp-ui-flycheck-list' will show diagnostics for the whole workspace."
4747
:type '(choice (const :tag "Bottom" bottom)
@@ -72,15 +72,17 @@ Use `lsp-diagnostics' to receive diagnostics from your LSP server."
7272
'face 'dired-directory)
7373
(propertize "\n" 'face '(:height 0.2)))))
7474
(dolist (diag diagnostic)
75-
(let* ((message (or (lsp-diagnostic-message diag) "???"))
76-
(severity (or (lsp-diagnostic-severity diag) 1))
77-
(line (1+ (lsp-diagnostic-line diag)))
78-
(face (cond ((= severity 1) 'error)
79-
((= severity 2) 'warning)
80-
(t 'success)))
81-
(text (concat (propertize (number-to-string line) 'face face)
82-
": "
83-
(car (split-string message "\n")))))
75+
(-let* (((&Diagnostic :message :severity? :source?
76+
:range (&Range :start (&Position :line start-line))) diag)
77+
(formatted-message (or (if source? (format "%s: %s" source? message) message) "???"))
78+
(severity (or severity? 1))
79+
(line (1+ start-line))
80+
(face (cond ((= severity 1) 'error)
81+
((= severity 2) 'warning)
82+
(t 'success)))
83+
(text (concat (propertize (number-to-string line) 'face face)
84+
": "
85+
(car (split-string formatted-message "\n")))))
8486
(add-text-properties 0 (length text) `(diag ,diag file ,file window ,window) text)
8587
(insert (concat text "\n")))))
8688
(lsp-diagnostics)))
@@ -98,7 +100,7 @@ Use `lsp-diagnostics' to receive diagnostics from your LSP server."
98100
(window (selected-window)))
99101
(with-current-buffer buffer
100102
(lsp-ui-flycheck-list--update window workspace))
101-
(add-hook 'lsp-after-diagnostics-hook 'lsp-ui-flycheck-list--refresh nil t)
103+
(add-hook 'lsp-diagnostics-updated-hook 'lsp-ui-flycheck-list--refresh nil t)
102104
(setq lsp-ui-flycheck-list--buffer buffer)
103105
(let ((win (display-buffer-in-side-window
104106
buffer `((side . ,lsp-ui-flycheck-list-position) (slot . 5) (window-width . 0.20)))))
@@ -118,19 +120,19 @@ Use `lsp-diagnostics' to receive diagnostics from your LSP server."
118120

119121
(defun lsp-ui-flycheck-list--open ()
120122
(-when-let* ((diag (get-text-property (point) 'diag))
123+
((&Diagnostic :range (&Range :start (&Position :line start-line
124+
:character start-column))) diag)
121125
(file (get-text-property (point) 'file))
122126
(window (get-text-property (point) 'window))
123-
(line (lsp-diagnostic-line diag))
124-
(column (lsp-diagnostic-column diag))
125127
(marker (with-current-buffer
126128
(or (get-file-buffer file)
127129
(find-file-noselect file))
128130
(save-restriction
129131
(widen)
130132
(save-excursion
131133
(goto-char 1)
132-
(forward-line line)
133-
(forward-char column)
134+
(forward-line start-line)
135+
(forward-char start-column)
134136
(point-marker))))))
135137
(set-window-buffer window (marker-buffer marker) t)
136138
(with-selected-window window

lsp-ui-peek.el

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
;;; Code:
3636

37+
(require 'lsp-protocol)
3738
(require 'lsp-mode)
3839
(require 'xref)
3940
(require 'dash)
@@ -566,7 +567,8 @@ XREFS is a list of references/definitions."
566567

567568
(defun lsp-ui-peek--find-xrefs (input method param)
568569
"Find INPUT references.
569-
KIND is ‘references’, ‘definitions’ or a custom kind."
570+
METHOD is ‘references’, ‘definitions’, `implementation` or a custom kind.
571+
PARAM is the request params."
570572
(setq lsp-ui-peek--method method)
571573
(let ((xrefs (lsp-ui-peek--get-references method param)))
572574
(unless xrefs
@@ -578,11 +580,12 @@ KIND is ‘references’, ‘definitions’ or a custom kind."
578580
(not (cdr xrefs))
579581
(= (length (plist-get (car xrefs) :xrefs)) 1))
580582
(let ((x (car (plist-get (car xrefs) :xrefs))))
581-
(-if-let (uri (gethash "uri" x))
582-
(-let (((&hash "start" (&hash "line" "character")) (gethash "range" x)))
583+
(-if-let (uri (lsp:location-uri x))
584+
(-let (((&Range :start (&Position :line :character)) (lsp:location-range x)))
583585
(lsp-ui-peek--goto-xref `(:file ,(lsp--uri-to-path uri) :line ,line :column ,character)))
584-
(-let (((&hash "start" (&hash "line" "character")) (or (gethash "targetSelectionRange" x) (gethash "targetRange" x))))
585-
(lsp-ui-peek--goto-xref `(:file ,(lsp--uri-to-path (gethash "targetUri" x)) :line ,line :column ,character)))))
586+
(-let (((&Range :start (&Position :line :character)) (or (lsp:location-link-target-selection-range x)
587+
(lsp:location-link-target-range x))))
588+
(lsp-ui-peek--goto-xref `(:file ,(lsp--uri-to-path (lsp:location-link-target-uri x)) :line ,line :column ,character)))))
586589
(lsp-ui-peek-mode)
587590
(lsp-ui-peek--show xrefs))))
588591

@@ -610,7 +613,7 @@ The symbols are found matching PATTERN."
610613
(interactive (list (read-string "workspace/symbol: "
611614
nil 'xref--read-pattern-history)))
612615
(lsp-ui-peek--find-xrefs pattern "workspace/symbol"
613-
(append extra (list :query pattern))))
616+
(append extra (lsp-make-workspace-symbol-params :query pattern))))
614617

615618
(defun lsp-ui-peek-find-custom (method &optional extra)
616619
"Find custom references.
@@ -639,21 +642,23 @@ START and END are delimiters."
639642
`(,line . ,(concat before line after))))))
640643

641644
(defun lsp-ui-peek--xref-make-item (filename loc)
642-
"Return an item from a LOCATION in FILENAME.
645+
"Return an item from FILENAME given a LOC.
643646
LOCATION can be either a LSP Location or SymbolInformation."
644647
;; TODO: Read more informations from SymbolInformation.
645648
;; For now, only the location is used.
646-
(-let* ((loc (or (gethash "location" loc) loc))
647-
(range (or (gethash "range" loc) (gethash "targetSelectionRange" loc) (gethash "targetRange" loc)))
648-
((&hash "start" pos-start "end" pos-end) range)
649-
((&hash "line" start-line "character" start-col) pos-start)
650-
((&hash "line" end-line "character" end-col) pos-end)
649+
(-let* ((loc (or (lsp:symbol-information-location loc) loc))
650+
(range (or (lsp:location-range loc)
651+
(lsp:location-link-target-selection-range loc)
652+
(lsp:location-link-target-range loc)))
653+
((&Range :start pos-start :end pos-end) range)
654+
((&Position :line start-line :character start-col) pos-start)
655+
((&Position :line end-line :character end-col) pos-end)
651656
((line . chunk) (lsp-ui-peek--extract-chunk-from-buffer pos-start start-col
652657
(when (= start-line end-line) end-col))))
653658
(list :summary (or line filename)
654659
:chunk (or chunk filename)
655660
:file filename
656-
:line (gethash "line" pos-start)
661+
:line start-line
657662
:column start-col)))
658663

659664
(defun lsp-ui-peek--fontify-buffer (filename)
@@ -697,19 +702,23 @@ references. The function returns a list of `ls-xref-item'."
697702
"Get all references/definitions for the symbol under point.
698703
Returns item(s)."
699704
(-when-let* ((locs (lsp-request method params))
700-
(locs (if (listp locs) locs (if (vectorp locs) (append locs nil) (list locs)))))
705+
(locs (if (listp locs)
706+
locs
707+
(if (vectorp locs)
708+
(append locs nil)
709+
(list locs)))))
701710
(-filter
702711
(-lambda ((&plist :file))
703712
(or (f-file? file)
704713
(ignore
705714
(lsp-log "The following file %s is missing, ignoring from the results."
706715
file))))
707716
(mapcar #'lsp-ui-peek--get-xrefs-list
708-
(if (gethash "uri" (car locs))
717+
(if (lsp:location-uri (car locs))
709718
;; Location[]
710-
(--group-by (lsp--uri-to-path (gethash "uri" it)) locs)
719+
(--group-by (lsp--uri-to-path (lsp:location-uri it)) locs)
711720
;; LocationLink[]
712-
(--group-by (lsp--uri-to-path (gethash "targetUri" it)) locs))))))
721+
(--group-by (lsp--uri-to-path (lsp:location-link-target-uri it)) locs))))))
713722

714723
(defvar lsp-ui-mode-map)
715724

lsp-ui-sideline.el

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
;;; Code:
3232

33+
(require 'lsp-protocol)
3334
(require 'lsp-mode)
3435
(require 'flycheck nil 'noerror)
3536
(require 'dash)
@@ -221,19 +222,19 @@ function signature)."
221222
(when contents
222223
(cond
223224
((stringp contents) contents)
224-
((sequencep contents) ;; MarkedString[]
225-
(seq-find (lambda (it) (and (hash-table-p it)
226-
(lsp-get-renderer (gethash "language" it))))
225+
((vectorp contents) ;; MarkedString[]
226+
(seq-find (lambda (it) (and (lsp-marked-string? it)
227+
(lsp-get-renderer (lsp:marked-string-language it))))
227228
contents))
228-
((gethash "kind" contents) contents) ;; MarkupContent
229-
((gethash "language" contents) contents))))
229+
((lsp-markup-content? contents) contents) ;; MarkupContent
230+
((lsp-marked-string? contents) contents))))
230231

231232
(defun lsp-ui-sideline--format-info (marked-string)
232233
"Format MARKED-STRING.
233234
If the string has a language, we fontify it with the function provided
234235
by `lsp-mode'.
235236
MARKED-STRING is the string returned by `lsp-ui-sideline--extract-info'."
236-
(when (and marked-string (hash-table-p marked-string))
237+
(when (and marked-string (lsp-marked-string? marked-string))
237238
(setq marked-string (lsp--render-element marked-string))
238239
(add-face-text-property 0 (length marked-string) 'lsp-ui-sideline-symbol-info nil marked-string)
239240
(add-face-text-property 0 (length marked-string) 'default t marked-string)
@@ -292,7 +293,7 @@ is set to t."
292293
(defun lsp-ui-sideline--push-info (symbol tag bounds info bol eol)
293294
(when (and (= tag (lsp-ui-sideline--calculate-tag))
294295
(not (lsp-ui-sideline--stop-p)))
295-
(let* ((info (concat (-some->> (gethash "contents" info)
296+
(let* ((info (concat (-some->> (lsp:hover-contents info)
296297
lsp-ui-sideline--extract-info
297298
lsp-ui-sideline--format-info
298299
(replace-regexp-in-string "\r" ""))))
@@ -385,9 +386,9 @@ Push sideline overlays on `lsp-ui-sideline--ovs'."
385386
(defun lsp-ui-sideline--code-actions (actions bol eol)
386387
"Show code ACTIONS."
387388
(when lsp-ui-sideline-actions-kind-regex
388-
(setq actions (seq-filter (-lambda ((&hash "kind"))
389-
(or (not kind)
390-
(s-match lsp-ui-sideline-actions-kind-regex kind)))
389+
(setq actions (seq-filter (-lambda ((&CodeAction :kind?))
390+
(or (not kind?)
391+
(s-match lsp-ui-sideline-actions-kind-regex kind?)))
391392
actions)))
392393
(setq lsp-ui-sideline--code-actions actions)
393394
(dolist (ov lsp-ui-sideline--ovs)
@@ -396,7 +397,7 @@ Push sideline overlays on `lsp-ui-sideline--ovs'."
396397
(delq (overlay-get ov 'position) lsp-ui-sideline--occupied-lines))
397398
(delete-overlay ov)))
398399
(seq-doseq (action actions)
399-
(-let* ((title (->> (gethash "title" action)
400+
(-let* ((title (->> (lsp:code-action-title action)
400401
(replace-regexp-in-string "[\n\t ]+" " ")
401402
(concat lsp-ui-sideline-code-actions-prefix)))
402403
(margin (lsp-ui-sideline--margin-width))
@@ -485,7 +486,8 @@ from the language server."
485486
(-let (((symbol tag bounds position) it))
486487
(plist-get (lsp-request-async
487488
"textDocument/hover"
488-
(list :textDocument doc-id :position position)
489+
(lsp-make-hover-params :text-document doc-id
490+
:position position)
489491
(lambda (info)
490492
(when info (lsp-ui-sideline--push-info symbol tag bounds info bol eol)))
491493
:mode 'alive)

lsp-ui.el

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@
3333

3434
;;; Code:
3535

36+
(require 'dash)
37+
(require 'lsp-protocol)
38+
39+
(require 'lsp-ui-sideline)
40+
(require 'lsp-ui-peek)
41+
(require 'lsp-ui-imenu)
42+
(require 'lsp-ui-doc)
43+
3644
(defgroup lsp-ui nil
3745
"‘lsp-ui’ contains a series of useful UI integrations for ‘lsp-mode’."
3846
:group 'tools
3947
:group 'convenience
4048
:link '(custom-manual "(lsp-ui) Top")
4149
:link '(info-link "(lsp-ui) Customizing"))
4250

43-
(require 'lsp-ui-sideline)
44-
(require 'lsp-ui-peek)
45-
(require 'lsp-ui-imenu)
46-
(require 'lsp-ui-doc)
47-
(require 'dash)
48-
4951
(with-eval-after-load 'flycheck
5052
(require 'lsp-ui-flycheck))
5153

@@ -119,15 +121,13 @@ Both should have the form (FILENAME LINE COLUMN)."
119121
(< (caddr x) (caddr y)))))
120122

121123
(defun lsp-ui--reference-triples (extra)
122-
"Return references as a list of (FILENAME LINE COLUMN) triples."
124+
"Return references as a list of (FILENAME LINE COLUMN) triples given EXTRA."
123125
(let ((refs (lsp-request "textDocument/references"
124126
(append (lsp--text-document-position-params) extra))))
125127
(sort
126128
(mapcar
127-
(lambda (ref)
128-
(-let* (((&hash "uri" uri "range" range) ref)
129-
((&hash "line" line "character" col) (gethash "start" range)))
130-
(list (lsp--uri-to-path uri) line col)))
129+
(-lambda ((&Location :uri :range (&Range :start (&Position :line :character))))
130+
(list (lsp--uri-to-path uri) line character))
131131
refs)
132132
#'lsp-ui--location<)))
133133

test/lsp-ui-flycheck-test.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
(buf (get-buffer-create "test-buffer"))
3434
(dir (lsp-ui-test-create-project-buffer buf string))
3535
(file (concat dir "src/main.rs"))
36-
(diagnostics "clippy: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false"))
36+
(diagnostics "this comparison involving the minimum or maximum element for this type contains a case that is always true or always false"))
3737
(sit-for 3)
3838
(with-current-buffer buf
3939
(flycheck-buffer)

0 commit comments

Comments
 (0)