Skip to content

Commit

Permalink
Initial commit on top of lsp-layer-improvements PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Cormac Cannon committed Mar 20, 2018
1 parent 4c98a39 commit b8ae1bc
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
76 changes: 76 additions & 0 deletions layers/+tools/cquery/README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#+TITLE: cquery layer

* Table of Contents :TOC_4_gh:noexport:
- [[#description][Description]]
- [[#features][Features:]]
- [[#external-dependencies][External Dependencies]]
- [[#cquery-server][cquery server]]
- [[#configuration][Configuration]]
- [[#basic][Basic]]
- [[#setting-path-to-cquery-executable][Setting path to cquery executable]]
- [[#additional-configuration-options][Additional configuration options]]
- [[#keybindings][Keybindings]]
- [[#goto-find][goto (find)]]
- [[#hierarchy][hierarchy]]

* Description
This layer extends the [[../lsp/README.org][LSP layer]] to add [[https://github.com/cquery-project/cquery][cquery]] support.

** Features:
- Cross references (definitions, references, base/derived classes/methods, type instances, ...)
- Diagnostics
- Completion with =company-lsp=
- Semantic highlighting
- See more on [[https://github.com/cquery-project/cquery/wiki/Emacs]]
- Cross-platform - cquery functional on Windows, Linux and OSX.

* External Dependencies
** cquery server
Install the =cquery= server. [[https://github.com/cquery-project/cquery/wiki/Getting-started][Instructions]].


* Configuration
** Basic
Add =cquery= to the =dotspacemacs-configuration-layers= list in =~/.spacemacs=.

** Setting path to cquery executable
The basic configuration above should work if the cquery executable folder is present in your path. If not, you can set the path explicitly.
#+BEGIN_SRC emacs-lisp
(cquery :variables cquery-executable "/path/to/bin/cquery")
#+END_SRC
If you need to expand =~= in the path, you can use =file-truename= like
#+BEGIN_SRC emacs-lisp
(cquery :variables cquery-executable (file-truename "~/bin/cquery"))
#+END_SRC

** Additional configuration options
cquery needs to know where to store index cache (=cquery-cache-dir=).
There are other initialization options such as the number of indexer threads, cache serialization format.
They have good default values. Refer to [[https://github.com/cquery-project/cquery/wiki/Initialization-options][Initialization options]] and set =cquery-extra-init-params= for more customization.

* Keybindings
** goto (find)
In addition to the LSP layer defaults (see [[../lsp/README.org][LSP layer README]]), this layer defines the following bindings under the ~m g~:

| binding | function |
|---------+-------------------------------|
| ~m g &~ | find references (address) |
| ~m g R~ | find references (read) |
| ~m g W~ | find references (write) |
| ~m g c~ | find callers |
| ~m g t~ | text-document/type-definition |
| ~m g v~ | vars |

** hierarchy
The layer binds the following hierarchy-related functions under ~m h~:

| binding | function |
|---------+-------------------------------|
| ~m h b~ | base class(es) |
| ~m h d~ | derived class(es) |
| ~m h c~ | call hierarchy |
| ~m h C~ | call hierarchy (inv) |
| ~m h i~ | inheritance heirarchy |

This comment has been minimized.

Copy link
@Kypert

Kypert Apr 2, 2018

Spelling: heirarchy -> hierarchy

| ~m h I~ | inheritance hierarhcy (inv) |

This comment has been minimized.

Copy link
@Kypert

Kypert Apr 2, 2018

Spelling: hierarhcy -> hierarchy

| ~m h m~ | member hierarchy |
| ~m h M~ | member hierarchy (inv) |
18 changes: 18 additions & 0 deletions layers/+tools/cquery/config.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(defvar cquery-extra-init-params '(:cacheFormat "msgpack")
"Extra initialisation parameters to pass to cquery. See
https://github.com/cquery-project/cquery/blob/master/src/config.h for details.")

(defvar cquery-project-whitelist nil
"A list of project directory patterns for which cquery should be
initialized. This overrides `cquery-project-blacklist'.")

(defvar cquery-project-blacklist nil
"A list of project root patterns for which cquery shouldn't be
initialized. `cquery-project-whitelist' is checked first, then this,
if no pattern matches the project root, cquery will be initialized.")

(defvar cquery-sem-highlight-method 'font-lock
"TODO don't know what this does yet")

(defvar cquery-executable "cquery"
"Path to cquery executable (default value assumes it's in the path)")
65 changes: 65 additions & 0 deletions layers/+tools/cquery/funcs.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
;; >>> Lifted from MaskRay's cquery layer
(require 'cl-lib)
(require 'subr-x)

(defun cquery//enable ()
(condition-case nil
(lsp-cquery-enable)
(user-error nil)))


(defun cquery/customise-lsp-ui-peek ()
(defun text-document/type-definition () (interactive) (lsp-ui-peek-find-custom 'type "textDocument/typeDefinition"))
(defun cquery/base () (interactive) (lsp-ui-peek-find-custom 'base "$cquery/base"))
(defun cquery/callers () (interactive) (lsp-ui-peek-find-custom 'callers "$cquery/callers"))
(defun cquery/derived () (interactive) (lsp-ui-peek-find-custom 'derived "$cquery/derived"))
(defun cquery/vars () (interactive) (lsp-ui-peek-find-custom 'vars "$cquery/vars"))
(defun cquery/random () (interactive) (lsp-ui-peek-find-custom 'random "$cquery/random"))

(defun cquery/references-address ()
(interactive)
(lsp-ui-peek-find-custom
'address "textDocument/references"
(plist-put (lsp--text-document-position-params) :context
'(:role 128))))

(defun cquery/references-read ()
(interactive)
(lsp-ui-peek-find-custom
'read "textDocument/references"
(plist-put (lsp--text-document-position-params) :context
'(:role 8))))

(defun cquery/references-write ()
(interactive)
(lsp-ui-peek-find-custom
'write "textDocument/references"
(plist-put (lsp--text-document-position-params) :context
'(:role 16))))

)

(defun cquery/define-keys-for-mode (mode)
"Define key bindings for the specific MODE."

;; lsp layer uses =,l,t,r as prefixes for format, lsp, toggle and refactor -- extend these
(spacemacs/declare-prefix-for-mode mode "mh" "heirarchy")

This comment has been minimized.

Copy link
@Kypert

Kypert Apr 2, 2018

Spelling: heirarchy -> hierarchy

(spacemacs/set-leader-keys-for-major-mode mode
;; heirarchy

This comment has been minimized.

Copy link
@Kypert

Kypert Apr 2, 2018

Spelling: heirarchy -> hierarchy

"hb" #'cquery/base
"hd" #'cquery/derived
"hc" #'cquery-call-hierarchy
"hC" (lambda () (interactive) (cquery-call-hierarchy t))
"hi" #'cquery-inheritance-hierarchy
"hI" (lambda () (interactive) (cquery-inheritance-hierarchy t))
"hm" #'cquery-member-hierarchy
"hM" (lambda () (interactive) (cquery-member-hierarchy t))
;; lsp/peek
"g&" #'cquery/references-address
"gR" #'cquery/references-read
"gW" #'cquery/references-write
"gc" #'cquery/callers
"gt" #'text-document/type-definition
"gv" #'cquery/vars
)
)
1 change: 1 addition & 0 deletions layers/+tools/cquery/layers.el
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(configuration-layer/declare-layers '(lsp c-c++))
50 changes: 50 additions & 0 deletions layers/+tools/cquery/packages.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
(defconst cquery-packages
'(
cquery
company-lsp
projectile
))

;; See also https://github.com/cquery-project/cquery/wiki/Emacs
(defun cquery/init-cquery ()
(use-package cquery
:defer t
:commands lsp-cquery-enable
:init
(add-hook 'c-mode-common-hook #'cquery//enable)
:config
(dolist (mode c-c++-modes)
(spacemacs/lsp-append-jump-handlers mode)
(spacemacs/lsp-bind-keys-for-mode mode)
(cquery/define-keys-for-mode mode)
(spacemacs/set-leader-keys-for-major-mode mode
;; format
"=c" 'spacemacs/clang-format-region-or-buffer
"=f" 'spacemacs/clang-format-function
;; goto
"gf" 'find-file-at-point
"gF" 'ffap-other-window
)
)
;; overlay is slow
;; Use https://github.com/emacs-mirror/emacs/commits/feature/noverlay
(cquery-use-default-rainbow-sem-highlight)
(evil-set-initial-state 'cquery-tree-mode 'emacs)

(cquery/customise-lsp-ui-peek)
(setq-default flycheck-disabled-checkers '(c/c++-clang c/c++-gcc)) ;; in flycheck.el

;;evil-record-macro keybinding clobbers q in cquery-tree-mode-map for some reason?
(evil-make-overriding-map cquery-tree-mode-map)

))

(defun cquery/post-init-company-lsp ()
(spacemacs|add-company-backends :backends company-lsp :modes c-mode-common))

(defun cquery/pre-init-projectile ()
(spacemacs|use-package-add-hook
:post-init
(add-to-list 'projectile-globally-ignored-directories ".cquery_cached_index")
)
)

5 comments on commit b8ae1bc

@Kypert
Copy link

@Kypert Kypert commented on b8ae1bc Apr 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While testing this excellent commit, I found some tricky words.

@cormacc
Copy link
Owner

@cormacc cormacc commented on b8ae1bc Apr 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Kypert. Amended commit here:
f68507b

@dubnde
Copy link

@dubnde dubnde commented on b8ae1bc Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Currently trying this too. So far so good with an 80M compile_commands.json file. How does one hook it into ivy or helm?

@Kypert
Copy link

@Kypert Kypert commented on b8ae1bc Apr 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure exactly what you mean, I just added this layer to dotspacemacs-configuration-layers:
(cquery :variables cquery-executable "/path/to/cquery")

Then, in my project I also made sure that compile_commands.json is located in project root. In my case I just made a link compile_commands.json -> build/compile_commands.json.

On another topic, I get an lsp-cquery stack trace when trying to open up a project using tramp. Any of you tried that?

@Kypert
Copy link

@Kypert Kypert commented on b8ae1bc Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future references.

I get an lsp-cquery stack trace when trying to open up a project using tramp

emacs-lsp/lsp-mode#197

Please sign in to comment.