Commit caf5a334 authored by Fangrui Song's avatar Fangrui Song

Migrate from legacy lsp-mode.el to lsp.el

parent 07ad5539
......@@ -147,7 +147,7 @@
:lighter "Lens"
(cond
(ccls-code-lens-mode
(when lsp--cur-workspace
(when lsp--buffer-workspaces
(ccls-request-code-lens)
(add-hook 'lsp-after-diagnostics-hook 'ccls-code-lens--request-when-idle t t)))
(t
......
......@@ -24,58 +24,14 @@
;;; Code:
(require 'cc-mode)
(require 'lsp-mode)
(require 'lsp)
(require 'cl-lib)
(require 'seq)
(require 'subr-x)
(require 'dash)
;; ---------------------------------------------------------------------
;; Customization
;; ---------------------------------------------------------------------
(defgroup ccls nil
"Customization options for the ccls client"
:group 'tools)
;; ---------------------------------------------------------------------
;; Utility
;; ---------------------------------------------------------------------
(defun ccls--read-range (range)
(cons (lsp--position-to-point (gethash "start" range))
(lsp--position-to-point (gethash "end" range))))
(cl-defun ccls--get-root ()
"Return the root directory of a ccls project."
(-when-let (match (locate-dominating-file default-directory ".ccls-root"))
(expand-file-name match)))
(defun ccls--get-renderer ()
(thread-last lsp--cur-workspace
lsp--workspace-client
lsp--client-string-renderers
(assoc-string (thread-first lsp--cur-workspace
lsp--workspace-client
lsp--client-language-id
(funcall (current-buffer))))
cdr))
(defun ccls--render-string (str)
(funcall (ccls--get-renderer) str))
(defun ccls--render-type (str)
"Render a string as a type"
(string-remove-suffix " a;" (ccls--render-string (format "%s a;" str))))
;; ---------------------------------------------------------------------
;; Notification handlers
;; ---------------------------------------------------------------------
(defvar ccls--handlers nil
"List of cons-cells of (METHOD . HANDLER) pairs, where METHOD is the lsp method to handle,
and handler is a function invoked as (handler WORKSPACE PARAMS), where WORKSPACE is the current
lsp-workspace, and PARAMS is a hashmap of the params recieved with the notification.")
(provide 'ccls-common)
......@@ -98,7 +98,7 @@
(name (ccls-inheritance-hierarchy-node-name data)))
(if (string-equal name "[[Base]]")
(propertize "Bases" 'face 'ccls-inheritance-hierarchy-base-face)
(ccls--render-type name))))
name)))
(defun ccls-inheritance-hierarchy (derived)
(interactive "P")
......
......@@ -90,9 +90,9 @@
(defun ccls-member-hierarchy--make-string (node depth)
"Propertize the name of NODE with the correct properties"
(let ((data (ccls-tree-node-data node)))
(ccls--render-string (if (eq depth 0)
(ccls-member-hierarchy-node-name data)
(ccls-member-hierarchy-node-field-name data)))))
(if (= depth 0)
(ccls-member-hierarchy-node-name data)
(ccls-member-hierarchy-node-field-name data))))
(defun ccls-member-hierarchy ()
(interactive)
......
......@@ -284,14 +284,14 @@ If nil, disable semantic highlight."
(cl-loop for i below (length ,colors) collect
(intern (format "ccls-sem-%s-face-%S" ,kind i)))))))))))
;; Add handler
(push '("$ccls/publishSemanticHighlight" . (lambda (w p) (ccls--publish-semantic-highlight w p)))
ccls--handlers)
;; ---------------------------------------------------------------------
;; Skipped ranges
;; ---------------------------------------------------------------------
(defun ccls--read-range (range)
(cons (lsp--position-to-point (gethash "start" range))
(lsp--position-to-point (gethash "end" range))))
(defun ccls--clear-skipped-ranges ()
"Clean up overlays."
(while ccls--inactive-overlays
......@@ -313,8 +313,4 @@ If nil, disable semantic highlight."
(overlay-put ov 'ccls-inactive t)
(push ov ccls--inactive-overlays))))))))
;; Add handler
(push '("$ccls/publishSkippedRanges" . (lambda (w p) (ccls--publish-skipped-ranges w p)))
ccls--handlers)
(provide 'ccls-semantic-highlight)
......@@ -184,13 +184,13 @@
(defun ccls-tree--open (client)
"."
(let ((opoint (point))
(lsp-ws lsp--cur-workspace)
(lsp-ws lsp--buffer-workspaces)
(root-node-data (ccls-tree--request-init client))
(orig-buf (current-buffer))
(bufname (format "*ccls-tree %s*" (ccls-tree-client-name client))))
(with-current-buffer (get-buffer-create bufname)
(ccls-tree-mode)
(setq lsp--cur-workspace lsp-ws
(setq lsp--buffer-workspaces lsp-ws
ccls-tree--cur-client client
ccls-tree--origin-buffer orig-buf
ccls-tree--origin-win (get-buffer-window orig-buf)
......@@ -262,7 +262,7 @@
(defun ccls-tree-press (&optional split-fn)
"Jump to the location."
(interactive)
(-when-let* ((workspace lsp--cur-workspace)
(-when-let* ((workspaces lsp--buffer-workspaces)
(node (ccls-tree--node-at-point))
(unused (window-live-p ccls-tree--origin-win)))
(with-selected-window ccls-tree--origin-win
......@@ -270,11 +270,11 @@
(funcall split-fn))
(find-file (car (ccls-tree-node-location node)))
;; TODO Extract lsp-ui-peek.el lsp-ui-peek--goto-xref
(unless lsp--cur-workspace
(setq lsp--cur-workspace workspace))
(unless lsp-mode
(unless lsp--buffer-workspaces
(setq lsp--buffer-workspaces workspaces)
(lsp-mode 1)
(lsp-on-open))
(dolist (workspace cur-buffer-workspaces)
(lsp--open-in-workspace workspace)))
(goto-char (lsp--position-to-point (cdr (ccls-tree-node-location node))))
(recenter)
(run-hooks 'xref-after-jump-hook))))
......
......@@ -57,23 +57,15 @@
:type 'file
:group 'ccls)
(defcustom ccls-extra-args
(defcustom ccls-args
nil
"Additional command line options passed to the ccls executable."
:type '(repeat string)
:group 'ccls)
(defcustom ccls-cache-dir
".ccls-cache/"
"Directory in which ccls will store its index cache.
Relative to the project root directory."
:type 'directory
:group 'ccls)
(defcustom ccls-extra-init-params
(defcustom ccls-initialization-options
nil
"Additional initializationOptions passed to ccls."
:type '(repeat string)
"initializationOptions"
:group 'ccls)
;; ---------------------------------------------------------------------
......@@ -138,32 +130,23 @@ DIRECTION can be \"D\", \"L\", \"R\" or \"U\"."
;; Register lsp client
;; ---------------------------------------------------------------------
(defun ccls--make-renderer (mode)
`(lambda (str)
(with-temp-buffer
(delay-mode-hooks (,(intern (format "%s-mode" mode))))
(insert str)
(font-lock-ensure)
(buffer-string))))
(defun ccls--initialize-client (client)
(dolist (p ccls--handlers)
(lsp-client-on-notification client (car p) (cdr p)))
(lsp-provide-marked-string-renderer client "c" (ccls--make-renderer "c"))
(lsp-provide-marked-string-renderer client "cpp" (ccls--make-renderer "c++"))
(lsp-provide-marked-string-renderer client "objectivec" (ccls--make-renderer "objc")))
(defun ccls--get-init-params (workspace)
`(:cacheDirectory ,(file-name-as-directory
(expand-file-name ccls-cache-dir (lsp--workspace-root workspace)))
,@ccls-extra-init-params)) ; TODO: prog reports for modeline
;;;###autoload (autoload 'lsp-ccls-enable "ccls")
(lsp-define-stdio-client
lsp-ccls "cpp" #'ccls--get-root
`(,ccls-executable ,@ccls-extra-args)
:initialize #'ccls--initialize-client
:extra-init-params #'ccls--get-init-params)
(defun ccls--suggest-project-root ()
(and (memq major-mode '(c-mode c++-mode cuda-mode objc-mode))
(locate-dominating-file default-directory ".ccls-root")))
(advice-add 'lsp--suggest-project-root :before-until #'ccls--suggest-project-root)
(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection (lambda () (cons ccls-executable ccls-args)))
:major-modes '(c-mode c++-mode cuda-mode objc-mode)
:server-id 'ccls
:multi-root nil
:notification-handlers
(lsp-ht ("$ccls/publishSkippedRanges" #'ccls--publish-skipped-ranges)
("$ccls/publishSemanticHighlight" #'ccls--publish-semantic-highlight))
:initialization-options (lambda () ccls-initialization-options)
:library-folders-fn nil))
(provide 'ccls)
;;; ccls.el ends here
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