Skip to content

Instantly share code, notes, and snippets.

@jstaursky
Last active August 3, 2020 01:02
Show Gist options
  • Save jstaursky/74cd3a30c1540142dc374f62187a0fc5 to your computer and use it in GitHub Desktop.
Save jstaursky/74cd3a30c1540142dc374f62187a0fc5 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)
        (evil-jump-to-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)
        (evil-jump-to-tag)))

  (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
      ".c"  'c-toggle-comment-style
      )
    )

  (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

  (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-find-symbol-at-point 
   :around (lambda (oldfn &rest args)
             (apply oldfn args)
             (run-at-time ".1 sec" nil #'recenter)
             (redraw-display)))

  (advice-add 'rtags-references-tree 
   :after (lambda () (pop-to-buffer rtags-buffer-name)))


  ) ; End use-package rtags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment