c-c++: refactor rtags support, key bindings, attempt ggtags fallback

- Move rtags key bindings under `SPC m g` prefix
- Document rtags support and key bindings in README.org
- attempt to implement gtags fallback using
  https://github.com/Andersbakken/rtags#fall-back-to-other-taggers
- make c-c++ layer dependend on gtags layer (gtags layer is automatically
  installed when the c-c++ layer is used)
- sort packages config in packages.el
- use a dolist instead of a function to define rtags key bindings
This commit is contained in:
syl20bnr 2018-01-06 02:44:33 -05:00
parent 7a74ae105a
commit 6932a337ee
5 changed files with 186 additions and 90 deletions

View File

@ -13,10 +13,12 @@
- [[#clang-format][clang-format]]
- [[#company-clang-and-flycheck][Company-clang and flycheck]]
- [[#cmake-configuration][CMake configuration]]
- [[#rtags-configuration][RTags configuration]]
- [[#enable-google-set-c-style][Enable google-set-c-style]]
- [[#key-bindings][Key Bindings]]
- [[#debugging-realgud][Debugging (realgud)]]
- [[#formatting-clang-format][Formatting (clang-format)]]
- [[#rtags][RTags]]
* Description
This layer adds configuration for C/C++ language as well support for [[https://cmake.org/][CMake]]
@ -37,6 +39,7 @@ scripts.
company-clang (when =c-c++-enable-clang-support= is turned on), or
company-ycmd (when =ycmd= layer is included).
- Support for [[https://github.com/realgud/realgud][realgud]] debugger.
- Support for [[https://github.com/Andersbakken/rtags][rtags]]
- Support for CMake configure/build (with limited support for other build systems),
automatic generation of =compile_commands.json= (compile flags), on-the-fly configuration
of flycheck, company-clang and RTags (if installed) with [[https://github.com/atilaneves/cmake-ide][cmake-ide]] .
@ -124,6 +127,15 @@ build your project with ~SPC c c~ key binding.
(helm-make-arguments . "-j7"))))
#+END_SRC
** RTags configuration
To enable support for =rtags=, set the layer variable
=c-c++-enable-rtags-support= to =t= in your dotfile.
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((c-c++ :variables c-c++-enable-rtags-support t)))
#+END_SRC
** Enable google-set-c-style
If you have clang enabled with =clang-format= as described earlier in this page
you may not have a lot of neeed for =google-set-c-style= if you are already
@ -194,3 +206,37 @@ set that up like this:
|-------------+---------------------------------|
| ~SPC m = =~ | format current region or buffer |
| ~SPC m = f~ | format current function |
** RTags
| Key Binding | Description |
|-------------+---------------------------------|
| ~SPC m g .~ | find symbol at point |
| ~SPC m g ,~ | find references at point |
| ~SPC m g ;~ | find file |
| ~SPC m g /~ | find all references at point |
| ~SPC m g [~ | location stack back |
| ~SPC m g ]~ | location stack forward |
| ~SPC m g >~ | c++ tags find symbol |
| ~SPC m g <~ | c++ tags find references |
| ~SPC m g B~ | show rtags buffer |
| ~SPC m g d~ | print dependencies |
| ~SPC m g D~ | diagnostics |
| ~SPC m g e~ | reparse file |
| ~SPC m g E~ | preprocess file |
| ~SPC m g F~ | fixit |
| ~SPC m g G~ | guess function at point |
| ~SPC m g h~ | print class hierarchy |
| ~SPC m g I~ | c++ tags imenu |
| ~SPC m g L~ | copy and print current location |
| ~SPC m g M~ | symbol info |
| ~SPC m g O~ | goto offset |
| ~SPC m g p~ | set current project |
| ~SPC m g R~ | rename symbol |
| ~SPC m g s~ | print source arguments |
| ~SPC m g S~ | display summary |
| ~SPC m g T~ | taglist |
| ~SPC m g v~ | find virtuals at point |
| ~SPC m g V~ | print enum value at point |
| ~SPC m g X~ | fix fixit at point |
| ~SPC m g Y~ | cycle overlays on screen |

View File

@ -31,6 +31,9 @@
(defvar c-c++-enable-rtags-support nil
"If non nil Rtags related packages and configuration are enabled.")
(defvar c-c++-enable-cmake-ide-support nil
"If non nil CMake related packages and configuration are enabled.")
(defvar c-c++-enable-clang-format-on-save nil
"If non-nil, automatically format code with ClangFormat on
save. Clang support has to be enabled for this to work.")

View File

@ -9,6 +9,7 @@
;;
;;; License: GPLv3
;; clang
(defun spacemacs/clang-format-function (&optional style)
@ -130,6 +131,7 @@ and the arguments for flyckeck-clang based on a project-specific text file."
(spacemacs//c-c++-get-standard-include-paths "c"))
idirafter-paths)))))
;; realgud
(defun spacemacs//short-key-state (modeon)
@ -137,3 +139,46 @@ and the arguments for flyckeck-clang based on a project-specific text file."
(if modeon
(evil-evilified-state)
(evil-normal-state)))
;; rtags
(defun spacemacs/c-c++-use-rtags (&optional useFileManager)
(and (rtags-executable-find "rc")
(cond ((not (gtags-get-rootpath)) t)
((and (not (eq major-mode 'c++-mode))
(not (eq major-mode 'c-mode))) (rtags-has-filemanager))
(useFileManager (rtags-has-filemanager))
(t (rtags-is-indexed)))))
(defun spacemacs/c-c++-tags-find-symbol-at-point (&optional prefix)
(interactive "P")
(if (and (not (rtags-find-symbol-at-point prefix))
rtags-last-request-not-indexed)
(gtags-find-tag)))
(defun spacemacs/c-c++-tags-find-references-at-point (&optional prefix)
(interactive "P")
(if (and (not (rtags-find-references-at-point prefix))
rtags-last-request-not-indexed)
(gtags-find-rtag)))
(defun spacemacs/c-c++-tags-find-symbol ()
(interactive)
(call-interactively (if (spacemacs/c-c++-use-rtags)
'rtags-find-symbol 'gtags-find-symbol)))
(defun spacemacs/c-c++-tags-find-references ()
(interactive)
(call-interactively (if (spacemacs/c-c++-use-rtags)
'rtags-find-references 'gtags-find-rtag)))
(defun spacemacs/c-c++-tags-find-file ()
(interactive)
(call-interactively (if (spacemacs/c-c++-use-rtags t)
'rtags-find-file 'gtags-find-file)))
(defun spacemacs/c-c++-tags-imenu ()
(interactive)
(call-interactively (if (spacemacs/c-c++-use-rtags t)
'rtags-imenu 'idomenu)))

View File

@ -0,0 +1,12 @@
;;; layers.el --- C/C++ layer layers File for Spacemacs
;;
;; Copyright (c) 2012-2018 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(configuration-layer/declare-layer 'gtags)

View File

@ -18,25 +18,25 @@
cmake-mode
company
(company-c-headers :requires company)
(company-rtags :requires company rtags)
company-ycmd
flycheck
(flycheck-rtags :requires flycheck rtags)
gdb-mi
ggtags
counsel-gtags
google-c-style
helm-cscope
helm-gtags
(helm-rtags :requires helm rtags)
(ivy-rtags :requires ivy rtags)
rtags
realgud
semantic
srefactor
stickyfunc-enhance
ycmd
xcscope
rtags
(company-rtags :requires company rtags)
(flycheck-rtags :requires flycheck rtags)
(helm-rtags :requires helm rtags)
(ivy-rtags :requires ivy rtags)
))
(defun c-c++/init-cc-mode ()
@ -116,6 +116,17 @@
:backends company-c-headers
:modes c-mode-common)))
(defun c-c++/init-company-rtags ()
(use-package company-rtags
:if c-c++-enable-rtags-support
:defer t
:init
(progn
(setq rtags-completions-enabled t)
(spacemacs|add-company-backends
:backends company-rtags
:modes c-mode-common))))
(defun c-c++/post-init-flycheck ()
(dolist (mode c-c++-modes)
(spacemacs/enable-flycheck mode))
@ -124,6 +135,11 @@
(when c-c++-enable-c++11
(setq flycheck-clang-language-standard "c++11"))))
;; TODO lazy load this package
(defun c-c++/init-flycheck-rtags ()
(use-package flycheck-rtags
:if c-c++-enable-rtags-support))
(defun c-c++/post-init-ggtags ()
(add-hook 'c-mode-local-vars-hook #'spacemacs/ggtags-mode-enable)
(add-hook 'c++-mode-local-vars-hook #'spacemacs/ggtags-mode-enable))
@ -146,6 +162,65 @@
(dolist (mode c-c++-modes)
(spacemacs/helm-gtags-define-keys-for-mode mode)))
;; TODO lazy load this package
(defun c-c++/init-helm-rtags ()
(use-package helm-rtags
:if c-c++-enable-rtags-support
:init (setq rtags-display-result-backend 'helm)))
;; TODO lazy load this package
(defun c-c++/init-ivy-rtags ()
(use-package ivy-rtags
:if c-c++-enable-rtags-support
:init (setq rtags-display-result-backend 'ivy)))
;; TODO lazy load this package
(defun c-c++/init-rtags ()
(use-package rtags
:if c-c++-enable-rtags-support
:init
(progn
(setq rtags-autostart-diagnostics t)
(add-hook 'rtags-jump-hook 'evil-set-jump)
(rtags-diagnostics)
;; key bindings
(define-key evil-normal-state-map (kbd "RET") 'rtags-select-other-window)
(define-key evil-normal-state-map (kbd "M-RET") 'rtags-select)
(define-key (kbd "q") evil-normal-state-map 'rtags-bury-or-delete)
;; TODO check for consistency with gtags key bindings
;; see https://github.com/syl20bnr/spacemacs/blob/develop/layers/+tags/gtags/funcs.el#L70
(dolist (mode c-c++-modes)
(spacemacs/set-leader-keys-for-major-mode mode
"g." 'spacemacs/c-c++-tags-find-symbol-at-point
"g," 'spacemacs/c-c++-tags-find-references-at-point
"g;" 'spacemacs/c-c++-tags-find-file
"g/" 'rtags-find-all-references-at-point
"g[" 'rtags-location-stack-back
"g]" 'rtags-location-stack-forward
"g>" 'spacemacs/c-c++-tags-find-symbol
"g<" 'spacemacs/c-c++-tags-find-references
"gB" 'rtags-show-rtags-buffer
"gd" 'rtags-print-dependencies
"gD" 'rtags-diagnostics
"ge" 'rtags-reparse-file
"gE" 'rtags-preprocess-file
"gF" 'rtags-fixit
"gG" 'rtags-guess-function-at-point
"gh" 'rtags-print-class-hierarchy
"gI" 'spacemacs/c-c++-tags-imenu
"gL" 'rtags-copy-and-print-current-location
"gM" 'rtags-symbol-info
"gO" 'rtags-goto-offset
"gp" 'rtags-set-current-project
"gR" 'rtags-rename-symbol
"gs" 'rtags-print-source-arguments
"gS" 'rtags-display-summary
"gT" 'rtags-taglist
"gv" 'rtags-find-virtuals-at-point
"gV" 'rtags-print-enum-value-at-point
"gX" 'rtags-fix-fixit-at-point
"gY" 'rtags-cycle-overlays-on-screen)))))
(defun c-c++/init-realgud()
(use-package realgud
:defer t
@ -213,88 +288,3 @@
:post-init
(dolist (mode c-c++-modes)
(spacemacs/setup-helm-cscope mode))))
;;;
;;; Adapted from comment:
;;; https://github.com/syl20bnr/spacemacs/issues/2327#issuecomment-153283156
;;; by user
;;; https://github.com/autosquid
;;;
(defun rtags-major-mode-keybindings (mode)
(spacemacs/declare-prefix-for-mode mode "me" "refactor")
(spacemacs/set-leader-keys-for-major-mode mode
"e." 'rtags-find-symbol-at-point
"e," 'rtags-find-references-at-point
"ev" 'rtags-find-virtuals-at-point
"eV" 'rtags-print-enum-value-at-point
"e/" 'rtags-find-all-references-at-point
"eY" 'rtags-cycle-overlays-on-screen
"e>" 'rtags-find-symbol
"e<" 'rtags-find-references
"e[" 'rtags-location-stack-back
"e]" 'rtags-location-stack-forward
"eD" 'rtags-diagnostics
"eG" 'rtags-guess-function-at-point
"ep" 'rtags-set-current-project
"eP" 'rtags-print-dependencies
"ee" 'rtags-reparse-file
"eE" 'rtags-preprocess-file
"eR" 'rtags-rename-symbol
"eM" 'rtags-symbol-info
"eS" 'rtags-display-summary
"eO" 'rtags-goto-offset
"e;" 'rtags-find-file
"eF" 'rtags-fixit
"eL" 'rtags-copy-and-print-current-location
"eX" 'rtags-fix-fixit-at-point
"eB" 'rtags-show-rtags-buffer
"eI" 'rtags-imenu
"eT" 'rtags-taglist
"eh" 'rtags-print-class-hierarchy
"ea" 'rtags-print-source-arguments
))
(defun c-c++/init-rtags ()
(use-package rtags
:if c-c++-enable-rtags-support
:init
(setq rtags-autostart-diagnostics t)
(add-hook 'rtags-jump-hook 'evil-set-jump)
(rtags-diagnostics)
(define-key evil-normal-state-map (kbd "RET") 'rtags-select-other-window)
(define-key evil-normal-state-map (kbd "M-RET") 'rtags-select)
(define-key evil-normal-state-map (kbd "q") 'rtags-bury-or-delete)
(rtags-major-mode-keybindings 'c-mode)
(rtags-major-mode-keybindings 'c++-mode)
))
(defun c-c++/init-company-rtags ()
(use-package company-rtags
:if c-c++-enable-rtags-support
:defer t
))
(defun c-c++/post-init-company-rtags ()
(when c-c++-enable-rtags-support
(setq rtags-completions-enabled t)
(spacemacs|add-company-backends
:backends company-rtags
:modes c-mode-common)))
(defun c-c++/init-flycheck-rtags ()
(use-package flycheck-rtags
:if c-c++-enable-rtags-support))
(defun c-c++/init-helm-rtags ()
(use-package helm-rtags
:if c-c++-enable-rtags-support
:init
(setq rtags-display-result-backend 'helm)
))
(defun c-c++/init-ivy-rtags ()
(use-package ivy-rtags
:if c-c++-enable-rtags-support
:init
(setq rtags-display-result-backend 'ivy)
))