Commit a4ad8276 authored by Fangrui Song's avatar Fangrui Song

Revamp codeLens & codeAction

parent 32efd2ad
......@@ -59,7 +59,10 @@
(defun ccls--make-code-lens-string (command)
"."
(let ((map (make-sparse-keymap)))
(define-key map [mouse-1] (lambda () (interactive) (ccls--execute-command (gethash "command" command) (gethash "arguments" command))))
(define-key map [mouse-1]
(lambda () (interactive)
(xref--show-xrefs (lsp--locations-to-xref-items
(lsp--execute-command command)) nil)))
(propertize (gethash "title" command)
'face 'ccls-code-lens-face
'mouse-face 'ccls-code-lens-mouse-face
......@@ -69,23 +72,12 @@
"."
(overlay-recenter (point-max))
(ccls-clear-code-lens)
(let (buffers)
(seq-do (lens result)
(let* ((range (ccls--read-range (gethash "range" lens)))
(root (gethash "command" lens))
;; (title (gethash "title" root))
;; (command (gethash "command" root))
(buffer (find-buffer-visiting (lsp--uri-to-path (gethash "uri" (gethash "arguments" root))))))
(when buffer
(with-current-buffer buffer
(save-excursion
(when (not (member buffer buffers))
(ccls-clear-code-lens)
(overlay-recenter (point-max))
(setq buffers (cons buffer buffers)))
(let ((ov (make-overlay (car range) (cdr range) buffer)))
(overlay-put ov 'ccls-code-lens t)
(overlay-put ov 'after-string (format " %s" (ccls--make-code-lens-string root)))))))))))
(seq-doseq (lens result)
(let* ((range (ccls--read-range (gethash "range" lens)))
(root (gethash "command" lens)))
(let ((ov (make-overlay (cdr range) (cdr range))))
(overlay-put ov 'ccls-code-lens t)
(overlay-put ov 'after-string (format " %s" (ccls--make-code-lens-string root)))))))
(defun ccls-request-code-lens ()
"Request code lens from ccls."
......
......@@ -103,20 +103,19 @@ lsp-workspace, and PARAMS is a hashmap of the params recieved with the notificat
;; Commands
;; ---------------------------------------------------------------------
(defun ccls--execute-command (command &optional arguments)
(defun ccls--execute-command (command arguments)
"Execute a ccls command."
(let ((uri (gethash "uri" arguments)))
(save-current-buffer
(find-file (lsp--uri-to-path uri))
(pcase command
;; Code actions
('"ccls._applyFixIt"
(seq-doseq (edit data)
(ccls--apply-textedit edit)))
('"ccls.showReferences" ;; Used by code lenses
(xref--show-xrefs (lsp--locations-to-xref-items (gethash "locations" arguments)) nil))
(_
(message "unknown command: %s" command))))))
(pcase command
;; Code actions
('"ccls._applyFixIt"
(find-file (lsp--uri-to-path (gethash "uri" args)))
(seq-doseq (edit data)
(ccls--apply-textedit edit)))
('"ccls.xref" ;; Used by code lenses
(xref--show-xrefs (lsp--locations-to-xref-items
(lsp--send-request (lsp--make-request "workspace/executeCommand") command arguments)) nil))
(_
(message "unknown command: %s" command))))
(defun ccls--apply-textedit (edit)
(let* ((range (gethash "range" edit))
......@@ -127,12 +126,4 @@ lsp-workspace, and PARAMS is a hashmap of the params recieved with the notificat
(goto-char start)
(insert newText)))
(defun ccls--execute-command-locally-advice (orig-func command args)
"ccls currently doesn't support `workspace/executeCommand', so execute those locally."
(if (ccls--is-ccls-buffer)
(ccls--execute-command command args)
(funcall orig-func args)))
(advice-add 'lsp--send-execute-command :around #'ccls--execute-command-locally-advice)
(provide 'ccls-common)
......@@ -4,7 +4,7 @@
;; Copyright (C) 2018 Fangrui Song
;; Author: Tobias Pisani, Fangrui Song
;; Package-Version: 20180925.1
;; Package-Version: 20180929.1
;; Version: 0.1
;; Homepage: https://github.com/MaskRay/emacs-ccls
;; Package-Requires: ((emacs "25.1") (lsp-mode "4.2") (dash "0.14") (projectile "1.0.0"))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment