diff options
author | Eric Dallo <ericdallo06@hotmail.com> | 2020-06-16 13:23:57 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-16 13:23:57 -0300 |
commit | a229185dcba6467e992ed0f1b2f7376e1bb8543a (patch) | |
tree | 757d3f7b9c774f6d4b1610bf31c4db525ba2e79c | |
parent | d92cf83d95c9ca177b735500ead88cf68dc2e2db (diff) |
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
-rw-r--r-- | lsp-ui-doc.el | 31 | ||||
-rw-r--r-- | lsp-ui-flycheck.el | 36 | ||||
-rw-r--r-- | lsp-ui-peek.el | 43 | ||||
-rw-r--r-- | lsp-ui-sideline.el | 26 | ||||
-rw-r--r-- | lsp-ui.el | 22 | ||||
-rw-r--r-- | test/lsp-ui-flycheck-test.el | 2 | ||||
-rw-r--r-- | test/lsp-ui-sideline-test.el | 2 |
7 files changed, 89 insertions, 73 deletions
diff --git a/lsp-ui-doc.el b/lsp-ui-doc.el index c7a58cf..f20fde6 100644 --- a/lsp-ui-doc.el +++ b/lsp-ui-doc.el @@ -30,6 +30,7 @@ ;;; Code: +(require 'lsp-protocol) (require 'lsp-mode) (require 'dash) (require 'dash-functional) @@ -261,9 +262,11 @@ Because some variables are buffer local.") (string-trim-right (let* ((string (if (stringp marked-string) marked-string - (gethash "value" marked-string))) - (with-lang (hash-table-p marked-string)) - (language (or (and with-lang (or (gethash "language" marked-string) (gethash "kind" marked-string))) + (lsp:markup-content-value marked-string))) + (with-lang (lsp-marked-string? marked-string)) + (language (or (and with-lang + (or (lsp:marked-string-language marked-string) + (lsp:markup-content-kind marked-string))) language))) (cond (lsp-ui-doc-use-webkit @@ -273,8 +276,8 @@ Because some variables are buffer local.") (t (lsp--render-element (lsp-ui-doc--inline-formatted-string string))))))) (defun lsp-ui-doc--filter-marked-string (list-marked-string) - (let ((groups (--separate (and (hash-table-p it) - (lsp-get-renderer (gethash "language" it))) + (let ((groups (--separate (and (lsp-marked-string? it) + (lsp-get-renderer (lsp:marked-string-language it))) (append list-marked-string nil)))) (if lsp-ui-doc-include-signature list-marked-string @@ -287,19 +290,19 @@ MarkedString | MarkedString[] | MarkupContent (as defined in the LSP). We don't extract the string that `lps-line' is already displaying." (cond ((stringp contents) (lsp-ui-doc--extract-marked-string contents)) ;; MarkedString - ((sequencep contents) ;; MarkedString[] + ((vectorp contents) ;; MarkedString[] (mapconcat 'lsp-ui-doc--extract-marked-string (lsp-ui-doc--filter-marked-string (seq-filter #'identity contents)) "\n\n" ;; (propertize "\n\n" 'face '(:height 0.4)) )) ;; when we get markdown contents, render using emacs gfm-view-mode / markdown-mode - ((string= (gethash "kind" contents) "markdown") ;; Markdown MarkupContent - (lsp-ui-doc--extract-marked-string (gethash "value" contents) "markdown")) - ((gethash "kind" contents) (gethash "value" contents)) ;; Plaintext MarkupContent - ((gethash "language" contents) ;; MarkedString - (lsp-ui-doc--extract-marked-string (gethash "value" contents) - (gethash "language" contents))))) + ((string= (lsp-markup-content? contents) lsp/markup-kind-markdown) ;; Markdown MarkupContent + (lsp-ui-doc--extract-marked-string (lsp:markup-content-value contents) lsp/markup-kind-markdown)) + ((lsp-markup-content? contents) (lsp:markup-content-value contents)) ;; Plaintext MarkupContent + ((lsp-marked-string? contents) ;; MarkedString + (lsp-ui-doc--extract-marked-string (lsp:marked-string-value contents) + (lsp:marked-string-language contents))))) (defun lsp-ui-doc--webkit-run-xwidget () "Launch embedded WebKit instance." @@ -700,7 +703,7 @@ HEIGHT is the documentation number of lines." :cancel-token :lsp-ui-doc-hover))))))) (lsp-ui-doc--hide-frame)))) -(defun lsp-ui-doc--callback (hover bounds buffer) +(lsp-defun lsp-ui-doc--callback ((hover &as &Hover? :contents) bounds buffer) "Process the received documentation. HOVER is the doc returned by the LS. BOUNDS are points of the symbol that have been requested. @@ -712,7 +715,7 @@ BUFFER is the buffer where the request has been made." (setq lsp-ui-doc--bounds bounds) (lsp-ui-doc--display (thing-at-point 'symbol t) - (-some->> (gethash "contents" hover) + (-some->> contents lsp-ui-doc--extract (replace-regexp-in-string "\r" "")))) (lsp-ui-doc--hide-frame))) diff --git a/lsp-ui-flycheck.el b/lsp-ui-flycheck.el index e2bd9a1..375ff68 100644 --- a/lsp-ui-flycheck.el +++ b/lsp-ui-flycheck.el @@ -27,11 +27,13 @@ ;;; Code: -(require 'lsp-mode) (require 'flycheck) (require 'pcase) (require 'dash) +(require 'lsp-protocol) +(require 'lsp-mode) + (defgroup lsp-ui-flycheck nil "The LSP extension to display syntax checking." :group 'tools @@ -40,8 +42,6 @@ :link '(custom-manual "(lsp-ui-flycheck) Top") :link '(info-link "(lsp-ui-flycheck) Customizing")) - - (defcustom lsp-ui-flycheck-list-position 'bottom "Position where `lsp-ui-flycheck-list' will show diagnostics for the whole workspace." :type '(choice (const :tag "Bottom" bottom) @@ -72,15 +72,17 @@ Use `lsp-diagnostics' to receive diagnostics from your LSP server." 'face 'dired-directory) (propertize "\n" 'face '(:height 0.2))))) (dolist (diag diagnostic) - (let* ((message (or (lsp-diagnostic-message diag) "???")) - (severity (or (lsp-diagnostic-severity diag) 1)) - (line (1+ (lsp-diagnostic-line diag))) - (face (cond ((= severity 1) 'error) - ((= severity 2) 'warning) - (t 'success))) - (text (concat (propertize (number-to-string line) 'face face) - ": " - (car (split-string message "\n"))))) + (-let* (((&Diagnostic :message :severity? :source? + :range (&Range :start (&Position :line start-line))) diag) + (formatted-message (or (if source? (format "%s: %s" source? message) message) "???")) + (severity (or severity? 1)) + (line (1+ start-line)) + (face (cond ((= severity 1) 'error) + ((= severity 2) 'warning) + (t 'success))) + (text (concat (propertize (number-to-string line) 'face face) + ": " + (car (split-string formatted-message "\n"))))) (add-text-properties 0 (length text) `(diag ,diag file ,file window ,window) text) (insert (concat text "\n"))))) (lsp-diagnostics))) @@ -98,7 +100,7 @@ Use `lsp-diagnostics' to receive diagnostics from your LSP server." (window (selected-window))) (with-current-buffer buffer (lsp-ui-flycheck-list--update window workspace)) - (add-hook 'lsp-after-diagnostics-hook 'lsp-ui-flycheck-list--refresh nil t) + (add-hook 'lsp-diagnostics-updated-hook 'lsp-ui-flycheck-list--refresh nil t) (setq lsp-ui-flycheck-list--buffer buffer) (let ((win (display-buffer-in-side-window buffer `((side . ,lsp-ui-flycheck-list-position) (slot . 5) (window-width . 0.20))))) @@ -118,10 +120,10 @@ Use `lsp-diagnostics' to receive diagnostics from your LSP server." (defun lsp-ui-flycheck-list--open () (-when-let* ((diag (get-text-property (point) 'diag)) + ((&Diagnostic :range (&Range :start (&Position :line start-line + :character start-column))) diag) (file (get-text-property (point) 'file)) (window (get-text-property (point) 'window)) - (line (lsp-diagnostic-line diag)) - (column (lsp-diagnostic-column diag)) (marker (with-current-buffer (or (get-file-buffer file) (find-file-noselect file)) @@ -129,8 +131,8 @@ Use `lsp-diagnostics' to receive diagnostics from your LSP server." (widen) (save-excursion (goto-char 1) - (forward-line line) - (forward-char column) + (forward-line start-line) + (forward-char start-column) (point-marker)))))) (set-window-buffer window (marker-buffer marker) t) (with-selected-window window diff --git a/lsp-ui-peek.el b/lsp-ui-peek.el index 3593aac..288aa7c 100644 --- a/lsp-ui-peek.el +++ b/lsp-ui-peek.el @@ -34,6 +34,7 @@ ;;; Code: +(require 'lsp-protocol) (require 'lsp-mode) (require 'xref) (require 'dash) @@ -566,7 +567,8 @@ XREFS is a list of references/definitions." (defun lsp-ui-peek--find-xrefs (input method param) "Find INPUT references. -KIND is ‘references’, ‘definitions’ or a custom kind." +METHOD is ‘references’, ‘definitions’, `implementation` or a custom kind. +PARAM is the request params." (setq lsp-ui-peek--method method) (let ((xrefs (lsp-ui-peek--get-references method param))) (unless xrefs @@ -578,11 +580,12 @@ KIND is ‘references’, ‘definitions’ or a custom kind." (not (cdr xrefs)) (= (length (plist-get (car xrefs) :xrefs)) 1)) (let ((x (car (plist-get (car xrefs) :xrefs)))) - (-if-let (uri (gethash "uri" x)) - (-let (((&hash "start" (&hash "line" "character")) (gethash "range" x))) + (-if-let (uri (lsp:location-uri x)) + (-let (((&Range :start (&Position :line :character)) (lsp:location-range x))) (lsp-ui-peek--goto-xref `(:file ,(lsp--uri-to-path uri) :line ,line :column ,character))) - (-let (((&hash "start" (&hash "line" "character")) (or (gethash "targetSelectionRange" x) (gethash "targetRange" x)))) - (lsp-ui-peek--goto-xref `(:file ,(lsp--uri-to-path (gethash "targetUri" x)) :line ,line :column ,character))))) + (-let (((&Range :start (&Position :line :character)) (or (lsp:location-link-target-selection-range x) + (lsp:location-link-target-range x)))) + (lsp-ui-peek--goto-xref `(:file ,(lsp--uri-to-path (lsp:location-link-target-uri x)) :line ,line :column ,character))))) (lsp-ui-peek-mode) (lsp-ui-peek--show xrefs)))) @@ -610,7 +613,7 @@ The symbols are found matching PATTERN." (interactive (list (read-string "workspace/symbol: " nil 'xref--read-pattern-history))) (lsp-ui-peek--find-xrefs pattern "workspace/symbol" - (append extra (list :query pattern)))) + (append extra (lsp-make-workspace-symbol-params :query pattern)))) (defun lsp-ui-peek-find-custom (method &optional extra) "Find custom references. @@ -639,21 +642,23 @@ START and END are delimiters." `(,line . ,(concat before line after)))))) (defun lsp-ui-peek--xref-make-item (filename loc) - "Return an item from a LOCATION in FILENAME. + "Return an item from FILENAME given a LOC. LOCATION can be either a LSP Location or SymbolInformation." ;; TODO: Read more informations from SymbolInformation. ;; For now, only the location is used. - (-let* ((loc (or (gethash "location" loc) loc)) - (range (or (gethash "range" loc) (gethash "targetSelectionRange" loc) (gethash "targetRange" loc))) - ((&hash "start" pos-start "end" pos-end) range) - ((&hash "line" start-line "character" start-col) pos-start) - ((&hash "line" end-line "character" end-col) pos-end) + (-let* ((loc (or (lsp:symbol-information-location loc) loc)) + (range (or (lsp:location-range loc) + (lsp:location-link-target-selection-range loc) + (lsp:location-link-target-range loc))) + ((&Range :start pos-start :end pos-end) range) + ((&Position :line start-line :character start-col) pos-start) + ((&Position :line end-line :character end-col) pos-end) ((line . chunk) (lsp-ui-peek--extract-chunk-from-buffer pos-start start-col (when (= start-line end-line) end-col)))) (list :summary (or line filename) :chunk (or chunk filename) :file filename - :line (gethash "line" pos-start) + :line start-line :column start-col))) (defun lsp-ui-peek--fontify-buffer (filename) @@ -697,7 +702,11 @@ references. The function returns a list of `ls-xref-item'." "Get all references/definitions for the symbol under point. Returns item(s)." (-when-let* ((locs (lsp-request method params)) - (locs (if (listp locs) locs (if (vectorp locs) (append locs nil) (list locs))))) + (locs (if (listp locs) + locs + (if (vectorp locs) + (append locs nil) + (list locs))))) (-filter (-lambda ((&plist :file)) (or (f-file? file) @@ -705,11 +714,11 @@ Returns item(s)." (lsp-log "The following file %s is missing, ignoring from the results." file)))) (mapcar #'lsp-ui-peek--get-xrefs-list - (if (gethash "uri" (car locs)) + (if (lsp:location-uri (car locs)) ;; Location[] - (--group-by (lsp--uri-to-path (gethash "uri" it)) locs) + (--group-by (lsp--uri-to-path (lsp:location-uri it)) locs) ;; LocationLink[] - (--group-by (lsp--uri-to-path (gethash "targetUri" it)) locs)))))) + (--group-by (lsp--uri-to-path (lsp:location-link-target-uri it)) locs)))))) (defvar lsp-ui-mode-map) diff --git a/lsp-ui-sideline.el b/lsp-ui-sideline.el index 400712e..84ddf8e 100644 --- a/lsp-ui-sideline.el +++ b/lsp-ui-sideline.el @@ -30,6 +30,7 @@ ;;; Code: +(require 'lsp-protocol) (require 'lsp-mode) (require 'flycheck nil 'noerror) (require 'dash) @@ -221,19 +222,19 @@ function signature)." (when contents (cond ((stringp contents) contents) - ((sequencep contents) ;; MarkedString[] - (seq-find (lambda (it) (and (hash-table-p it) - (lsp-get-renderer (gethash "language" it)))) + ((vectorp contents) ;; MarkedString[] + (seq-find (lambda (it) (and (lsp-marked-string? it) + (lsp-get-renderer (lsp:marked-string-language it)))) contents)) - ((gethash "kind" contents) contents) ;; MarkupContent - ((gethash "language" contents) contents)))) + ((lsp-markup-content? contents) contents) ;; MarkupContent + ((lsp-marked-string? contents) contents)))) (defun lsp-ui-sideline--format-info (marked-string) "Format MARKED-STRING. If the string has a language, we fontify it with the function provided by `lsp-mode'. MARKED-STRING is the string returned by `lsp-ui-sideline--extract-info'." - (when (and marked-string (hash-table-p marked-string)) + (when (and marked-string (lsp-marked-string? marked-string)) (setq marked-string (lsp--render-element marked-string)) (add-face-text-property 0 (length marked-string) 'lsp-ui-sideline-symbol-info nil marked-string) (add-face-text-property 0 (length marked-string) 'default t marked-string) @@ -292,7 +293,7 @@ is set to t." (defun lsp-ui-sideline--push-info (symbol tag bounds info bol eol) (when (and (= tag (lsp-ui-sideline--calculate-tag)) (not (lsp-ui-sideline--stop-p))) - (let* ((info (concat (-some->> (gethash "contents" info) + (let* ((info (concat (-some->> (lsp:hover-contents info) lsp-ui-sideline--extract-info lsp-ui-sideline--format-info (replace-regexp-in-string "\r" "")))) @@ -385,9 +386,9 @@ Push sideline overlays on `lsp-ui-sideline--ovs'." (defun lsp-ui-sideline--code-actions (actions bol eol) "Show code ACTIONS." (when lsp-ui-sideline-actions-kind-regex - (setq actions (seq-filter (-lambda ((&hash "kind")) - (or (not kind) - (s-match lsp-ui-sideline-actions-kind-regex kind))) + (setq actions (seq-filter (-lambda ((&CodeAction :kind?)) + (or (not kind?) + (s-match lsp-ui-sideline-actions-kind-regex kind?))) actions))) (setq lsp-ui-sideline--code-actions actions) (dolist (ov lsp-ui-sideline--ovs) @@ -396,7 +397,7 @@ Push sideline overlays on `lsp-ui-sideline--ovs'." (delq (overlay-get ov 'position) lsp-ui-sideline--occupied-lines)) (delete-overlay ov))) (seq-doseq (action actions) - (-let* ((title (->> (gethash "title" action) + (-let* ((title (->> (lsp:code-action-title action) (replace-regexp-in-string "[\n\t ]+" " ") (concat lsp-ui-sideline-code-actions-prefix))) (margin (lsp-ui-sideline--margin-width)) @@ -485,7 +486,8 @@ from the language server." (-let (((symbol tag bounds position) it)) (plist-get (lsp-request-async "textDocument/hover" - (list :textDocument doc-id :position position) + (lsp-make-hover-params :text-document doc-id + :position position) (lambda (info) (when info (lsp-ui-sideline--push-info symbol tag bounds info bol eol))) :mode 'alive) @@ -33,6 +33,14 @@ ;;; Code: +(require 'dash) +(require 'lsp-protocol) + +(require 'lsp-ui-sideline) +(require 'lsp-ui-peek) +(require 'lsp-ui-imenu) +(require 'lsp-ui-doc) + (defgroup lsp-ui nil "‘lsp-ui’ contains a series of useful UI integrations for ‘lsp-mode’." :group 'tools @@ -40,12 +48,6 @@ :link '(custom-manual "(lsp-ui) Top") :link '(info-link "(lsp-ui) Customizing")) -(require 'lsp-ui-sideline) -(require 'lsp-ui-peek) -(require 'lsp-ui-imenu) -(require 'lsp-ui-doc) -(require 'dash) - (with-eval-after-load 'flycheck (require 'lsp-ui-flycheck)) @@ -119,15 +121,13 @@ Both should have the form (FILENAME LINE COLUMN)." (< (caddr x) (caddr y))))) (defun lsp-ui--reference-triples (extra) - "Return references as a list of (FILENAME LINE COLUMN) triples." + "Return references as a list of (FILENAME LINE COLUMN) triples given EXTRA." (let ((refs (lsp-request "textDocument/references" (append (lsp--text-document-position-params) extra)))) (sort (mapcar - (lambda (ref) - (-let* (((&hash "uri" uri "range" range) ref) - ((&hash "line" line "character" col) (gethash "start" range))) - (list (lsp--uri-to-path uri) line col))) + (-lambda ((&Location :uri :range (&Range :start (&Position :line :character)))) + (list (lsp--uri-to-path uri) line character)) refs) #'lsp-ui--location<))) diff --git a/test/lsp-ui-flycheck-test.el b/test/lsp-ui-flycheck-test.el index deef4bf..d2ba62b 100644 --- a/test/lsp-ui-flycheck-test.el +++ b/test/lsp-ui-flycheck-test.el @@ -33,7 +33,7 @@ (buf (get-buffer-create "test-buffer")) (dir (lsp-ui-test-create-project-buffer buf string)) (file (concat dir "src/main.rs")) - (diagnostics "clippy: this comparison involving the minimum or maximum element for this type contains a case that is always true or always false")) + (diagnostics "this comparison involving the minimum or maximum element for this type contains a case that is always true or always false")) (sit-for 3) (with-current-buffer buf (flycheck-buffer) diff --git a/test/lsp-ui-sideline-test.el b/test/lsp-ui-sideline-test.el index d70a126..c8f45ed 100644 --- a/test/lsp-ui-sideline-test.el +++ b/test/lsp-ui-sideline-test.el @@ -25,7 +25,7 @@ (let* ((ov1 (nth 0 lsp-ui-sideline--ovs)) (ov2 (nth 1 lsp-ui-sideline--ovs)) (msg1 " `#[warn(unused_variables)]` on by default [unused_variables]") - (msg2 " rustc: unused variable: `bar`")) + (msg2 " unused variable: `bar`")) (should (string= (substring-no-properties (overlay-get ov1 'after-string)) msg1)) (should (string= (substring-no-properties (overlay-get ov2 'after-string)) msg2)) |