Skip to content

Commit

Permalink
Add ccls-specific configuration
Browse files Browse the repository at this point in the history
Most of these changes come from MaskRay's private configuration found
here :
https://github.com/MaskRay/Config/blob/c078dfad340041673f2e056729a44c3df05a6248/home/.config/doom/modules/private/my-cc/config.el

- Limit ccls threads to be maximum half the available core count on
  Linux and MacOS
- Add wrappers to call ccls LSP extensions
- Enable ccls-code-lens-mode
- Set initial state to emacs when navigating ccls-tree
- Use 'font-lock for semantic highlighting, making the speed choice
  according to variable's docstring

- config tweaks to eglot
  InitializationOptions aren't handled this way. An example of handling
  ccls init options is provided on joaotavora/eglot#545, hoping that a fix
  gets merged upstream instead of having to handle ccls specifics in Doom
  only
  • Loading branch information
gagbo committed Oct 7, 2020
1 parent d807a75 commit ac62a7d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 6 deletions.
90 changes: 90 additions & 0 deletions modules/lang/cc/autoload.el
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,93 @@ header files."
(`c-mode 'ffap-c-path)
(`c++-mode 'ffap-c++-path))
(expand-file-name dir project-root)))))


;;
;; CCLS specific helpers

;; ccls/vars ccls/base ccls/derived ccls/members have a parameter while others are interactive.
;; (ccls/base 1) direct bases
;; (ccls/derived 1) direct derived
;; (ccls/member 2) => 2 (Type) => nested classes / types in a namespace
;; (ccls/member 3) => 3 (Func) => member functions / functions in a namespace
;; (ccls/member 0) => member variables / variables in a namespace
;; (ccls/vars 1) => field
;; (ccls/vars 2) => local variable
;; (ccls/vars 3) => field or local variable. 3 = 1 | 2
;; (ccls/vars 4) => parameter

;;;###autoload
(defun +ccls/callee ()
"Show callees of symbol under point."
(interactive)
(lsp-ui-peek-find-custom "$ccls/call" '(:callee t)))

;;;###autoload
(defun +ccls/caller ()
"Show callers of symbol under point."
(interactive)
(lsp-ui-peek-find-custom "$ccls/call"))

;;;###autoload
(defun ccls/vars (kind)
"Show variables of type KIND as symbol under point.
1 -> field
2 -> local variable
3 -> field or local variables. 3 = 1 | 2.
4 -> parameter"
(lsp-ui-peek-find-custom "$ccls/vars" `(:kind ,kind)))

;;;###autoload
(defun ccls/base (levels)
"Show bases of class under point up to LEVELS levels (1 for direct bases)."
(lsp-ui-peek-find-custom "$ccls/inheritance" `(:levels ,levels)))

;;;###autoload
(defun ccls/derived (levels)
"Show derived classes from class under point down to LEVELS levels (1 for direct derived)."
(lsp-ui-peek-find-custom "$ccls/inheritance" `(:levels ,levels :derived t)))

;;;###autoload
(defun ccls/member (kind)
"Show member elements of kind KIND for class/namespace under point.
0 -> member variables/ variables in a namespace
2 -> nested classes / types in a namespace
3 -> member functions / functions in a namespace"
(lsp-ui-peek-find-custom "$ccls/member" `(:kind ,kind)))

;; The meaning of :role corresponds to https://github.com/maskray/ccls/blob/master/src/symbol.h
;;;###autoload
(defun +ccls/references-address ()
"References w/ Role::Address bit (e.g. variables explicitly being taken addresses)"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :role 128)))

;;;###autoload
(defun +ccls/references-macro ()
"References w/ Role::Dynamic bit (macro expansions)"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :role 64)))

;;;###autoload
(defun +ccls/references-not-call ()
"References w/o Role::Call bit (e.g. where functions are taken addresses)"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :excludeRole 32)))

;;;###autoload
(defun +ccls/references-read ()
"References w/ Role::Read"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :role 8)))

;;;###autoload
(defun +ccls/references-write ()
"References w/ Role::Write"
(interactive)
(lsp-ui-peek-find-custom "textDocument/references"
(plist-put (lsp--text-document-position-params) :role 16)))
41 changes: 35 additions & 6 deletions modules/lang/cc/config.el
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,26 @@ If rtags or rdm aren't available, fail silently instead of throwing a breaking e
(add-hook! '(c-mode-local-vars-hook
c++-mode-local-vars-hook
objc-mode-local-vars-hook)
#'lsp!)
#'lsp!)
(map!
(:after ccls
:map (c-mode-map c++-mode-map)
:n "C-h" (λ! (ccls-navigate "U"))
:n "C-j" (λ! (ccls-navigate "R"))
:n "C-k" (λ! (ccls-navigate "L"))
:n "C-l" (λ! (ccls-navigate "D"))
(:localleader
:desc "Preprocess file" :n "lp" #'ccls-preprocess-file
:desc "Reset cache and reload CCLS" :n "lf" #'ccls-reload)
(:after lsp-ui-peek
(:localleader
:desc "Callers list" :n "c" #'+ccls/caller
:desc "Callees list" :n "C" #'+ccls/callee
:desc "References (address)" :n "a" #'+ccls/references-address
:desc "References (not call)" :n "f" #'+ccls/references-not-call
:desc "References (Macro)" :n "m" #'+ccls/references-macro
:desc "References (Read)" :n "r" #'+ccls/references-read
:desc "References (Write)" :n "w" #'+ccls/references-write))))

(when (featurep! :tools lsp +eglot)
;; Map eglot specific helper
Expand All @@ -249,7 +268,6 @@ If rtags or rdm aren't available, fail silently instead of throwing a breaking e
"-isystem/usr/local/include"]
:resourceDir (cdr (doom-call-process "clang" "-print-resource-dir"))))))))))))


(use-package! ccls
:when (featurep! +lsp)
:unless (featurep! :tools lsp +eglot)
Expand All @@ -260,9 +278,20 @@ If rtags or rdm aren't available, fail silently instead of throwing a breaking e
(add-to-list 'projectile-project-root-files-bottom-up ".ccls-root")
(add-to-list 'projectile-project-root-files-top-down-recurring "compile_commands.json"))
:config
(add-hook 'lsp-after-open-hook #'ccls-code-lens-mode)
(set-evil-initial-state! 'ccls-tree-mode 'emacs)
(setq ccls-sem-highlight-method 'font-lock)
(when (or IS-MAC IS-LINUX)
(let ((cpu-count-command (cond (IS-MAC '("sysctl -n hw.ncpu"))
(IS-LINUX '("nproc"))
(t (error "unreachable code")))))
(setq ccls-initialization-options
`(:index (:trackDependency 1
:threads ,(max 1 (/ (string-to-number (cdr (apply #'doom-call-process cpu-count-command))) 2)))))))
(when IS-MAC
(setq ccls-initialization-options
`(:clang ,(list :extraArgs ["-isystem/Library/Developer/CommandLineTools/usr/include/c++/v1"
"-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
"-isystem/usr/local/include"]
:resourceDir (cdr (doom-call-process "clang" "-print-resource-dir")))))))
(append ccls-initialization-options
`(:clang ,(list :extraArgs ["-isystem/Library/Developer/CommandLineTools/usr/include/c++/v1"
"-isystem/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include"
"-isystem/usr/local/include"]
:resourceDir (cdr (doom-call-process "clang -print-resource-dir"))))))))

0 comments on commit ac62a7d

Please sign in to comment.