Skip to content

Instantly share code, notes, and snippets.

@jstaursky
Created June 22, 2021 02:43
Show Gist options
  • Save jstaursky/d873bea3110bbc042172996f10906d0a to your computer and use it in GitHub Desktop.
Save jstaursky/d873bea3110bbc042172996f10906d0a to your computer and use it in GitHub Desktop.
(use-package rtags
;; Note that if you recompile and create new compile_commands.json
;; you will need to run "rc -J ." for rtags to reflect the changes.
;; REMEMBER RTAGS DOES NOT WORK FOR PROJECTS INSIDE /tmp
:init
(add-hook 'c++-mode-hook #'rtags-start-process-unless-running)
(add-hook 'c-mode-hook #'rtags-start-process-unless-running)
(add-hook 'rtags-jump-hook 'evil-set-jump)
(setq rtags-completions-enabled t)
(setq lsp-enable-file-watchers nil)
(defun my/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 my/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 my/c-c++-tags-find-symbol ()
(interactive)
(call-interactively 'rtags-find-symbol))
(defun spacemacs/rtags-define-keys-for-mode (mode)
(spacemacs/set-leader-keys-for-major-mode mode
".." 'my/c-c++-tags-find-symbol-at-point
".," 'my/c-c++-tags-find-references-at-point
"./" 'rtags-find-all-references-at-point
".<" '(lambda () (interactive) (call-interactively 'rtags-find-references))
".>" '(lambda () (interactive) (call-interactively 'rtags-find-symbol))
".f" 'rtags-find-symbol-at-point
".r" 'rtags-references-tree
".R" 'rtags-rename-symbol
".T" 'rtags-taglist
".h" 'rtags-print-class-hierarchy
".v" 'rtags-find-virtuals-at-point
".D" 'rtags-print-dependencies
".d" 'rtags-dependency-tree
".C" 'rtags-check-includes
".c" 'c-toggle-comment-style
".H" 'staur|toggle-headerline
".p" 'rtags-peek-definition
".s" 'rtags-show-target-in-other-window
)
)
(spacemacs/rtags-define-keys-for-mode 'c-mode)
(spacemacs/rtags-define-keys-for-mode 'c++-mode)
(spacemacs/declare-prefix-for-mode 'c++-mode "m." "my cmds")
(spacemacs/declare-prefix-for-mode 'c++-mode "m.," "rtags-find-references-at-point")
(spacemacs/declare-prefix-for-mode 'c++-mode "m.." "rtags-find-symbol-at-point")
(spacemacs/declare-prefix-for-mode 'c++-mode "m.<" "rtags-find-references")
(spacemacs/declare-prefix-for-mode 'c++-mode "m.>" "rtags-find-symbol")
(spacemacs/declare-prefix-for-mode 'c-mode "m." "my cmds")
(spacemacs/declare-prefix-for-mode 'c-mode "m.," "rtags-find-references-at-point")
(spacemacs/declare-prefix-for-mode 'c-mode "m.." "rtags-find-symbol-at-point")
(spacemacs/declare-prefix-for-mode 'c-mode "m.<" "rtags-find-references")
(spacemacs/declare-prefix-for-mode 'c++-mode "m.>" "rtags-find-symbol")
:config
(rtags-enable-standard-keybindings) ; enable C-c r bindings
(advice-add 'rtags-show-in-other-window
:around
(lambda (oldfn &rest args)
(let ((window (selected-window)))
(apply oldfn args)
(run-at-time ".1 sec" nil `(lambda ()
(select-window (get-mru-window 'visible nil t))
(recenter)
(select-window ,window))))))
(advice-add 'rtags-references-tree :after (lambda () (pop-to-buffer rtags-buffer-name)))
(setq rtags-completions-enabled t)
(defun rtags-peek-definition ()
"Peek at definition at point using rtags."
;; toggle persp-mode, as this seems to break peek-func.
(if (bound-and-true-p persp-mode)
(persp-mode -1))
;; begin main section of rtags-peek-definition.
(interactive)
(let ((func (lambda ()
(rtags-find-symbol-at-point)
(rtags-location-stack-forward))))
(rtags-start-process-unless-running)
(make-peek-frame func))
;; restore previous state whether persp-mode was active or not.
(if (not (bound-and-true-p persp-mode))
(persp-mode 1))
)
(defun make-peek-frame (find-definition-function &rest args)
"Make a new frame for peeking definition"
(when (or (not (rtags-called-interactively-p)) (rtags-sandbox-id-matches))
(let (doc-frame x y (abs-pixel-pos (save-excursion (beginning-of-thing 'symbol)
(window-absolute-pixel-position))))
(setq x (car abs-pixel-pos))
(setq y (+ (cdr abs-pixel-pos) (frame-char-height)))
(setq doc-frame (make-frame '((minibuffer . nil) (name . "*RTags Peek*")
(width . 80) (visibility . nil)
(height . 15))))
(set-frame-position doc-frame x y)
(with-selected-frame doc-frame
(apply find-definition-function args)
(read-only-mode)
(recenter-top-bottom 0))
(make-frame-visible doc-frame))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment