From 8ba7f8161d7fd73853ae0abc078778fd25861be7 Mon Sep 17 00:00:00 2001 From: Howard Abrams Date: Wed, 31 Aug 2022 21:47:18 -0700 Subject: [PATCH] Adding dumb-jump to navigate my code trees --- ha-config.org | 49 +++++++++++++++++++++++++++++++--------------- ha-programming.org | 16 +++++++++++++++ 2 files changed, 49 insertions(+), 16 deletions(-) diff --git a/ha-config.org b/ha-config.org index 1e40b23..ee212f3 100644 --- a/ha-config.org +++ b/ha-config.org @@ -826,28 +826,45 @@ Let's try this out with a Hydra since some I can /repeat/ some commands (e.g. en (ha-leader "w" '("windows" . hydra-window-resize/body)) #+end_src *** Search Operations -Ways to search for information goes under the ~s~ key. This primarily depends on the [[https://github.com/dajva/rg.el][rg]] package, which builds on the internal =grep= system, and creates a =*rg*= window with =compilation= mode, so ~C-j~ and ~C-k~ will move and show the results by loading those files. +Ways to search for information goes under the ~s~ key. The venerable sage has always been =grep=, but we now have new-comers, like [[https://github.com/BurntSushi/ripgrep][ripgrep]], which are really fast. +**** ripgrep +Install the [[https://github.com/dajva/rg.el][rg]] package, which builds on the internal =grep= system, and creates a =*rg*= window with =compilation= mode, so ~C-j~ and ~C-k~ will move and show the results by loading those files. #+begin_src emacs-lisp (use-package rg - :init ; I sometimes call `grep`: - ; (grep-apply-setting 'grep-command "rg -n -H --no-heading -e ") + :general (:states 'normal "gr" 'rg-dwim) :config + ;; Make an interesting Magit-like menu of options, which I don't use much: (rg-enable-default-bindings (kbd "M-R")) + ;; In case I call (or something else) calls `grep': + (grep-apply-setting 'grep-command "rg -n -H --no-heading -e ") + + ;; Old habits die hard ... + (define-key global-map [remap xref-find-references] 'rg-dwim) + + (advice-add 'wgrep-change-to-wgrep-mode :after #'evil-normal-state) + (advice-add 'wgrep-to-original-mode :after #'evil-motion-state) + (defvar rg-mode-map) + (add-to-list 'evil-motion-state-modes 'rg-mode) + (evil-add-hjkl-bindings rg-mode-map 'motion + "e" #'wgrep-change-to-wgrep-mode + "g" #'rg-recompile + "t" #'rg-rerun-change-literal) + (ha-leader - "s" '(:ignore t :which-key "search") - "s q" '("close" . ha-rg-close-results-buffer) - "s r" '("dwim" . rg-dwim) - "s s" '("search" . rg) - "s S" '("literal" . rg-literal) - "s p" '("project" . rg-project) ; or projectile-ripgrep - "s d" '("directory" . rg-dwim-project-dir) - "s f" '("file only" . rg-dwim-current-file) - "s j" '("next results" . ha-rg-go-next-results) - "s k" '("prev results" . ha-rg-go-previous-results) - "s b" '("results buffer" . ha-rg-go-results-buffer)) + "s" '(:ignore t :which-key "search") + "s q" '("close" . ha-rg-close-results-buffer) + "s r" '("dwim" . rg-dwim) + "s s" '("search" . rg) + "s S" '("literal" . rg-literal) + "s p" '("project" . rg-project) ; or projectile-ripgrep + "s d" '("directory" . rg-dwim-project-dir) + "s f" '("file only" . rg-dwim-current-file) + "s j" '("next results" . ha-rg-go-next-results) + "s k" '("prev results" . ha-rg-go-previous-results) + "s b" '("results buffer" . ha-rg-go-results-buffer)) (defun ha-rg-close-results-buffer () "Close to the `*rg*' buffer that `rg' creates." @@ -873,9 +890,9 @@ Ways to search for information goes under the ~s~ key. This primarily depends on (previous-error-no-select) (compile-goto-error))) #+end_src - +Note we bind the key ~M-R~ to the [[help:rg-menu][rg-menu]], which is a Magit-like interface to =ripgrep=. +**** wgrep The [[https://github.com/mhayashi1120/Emacs-wgrep][wgrep package]] integrates with =ripgrep=. Typically, you hit ~i~ to automatically go into =wgrep-mode= and edit away, but since I typically want to edit everything at the same time, I have a toggle that should work as well: - #+begin_src emacs-lisp (use-package wgrep :after rg diff --git a/ha-programming.org b/ha-programming.org index 3d081ab..94878c2 100644 --- a/ha-programming.org +++ b/ha-programming.org @@ -171,6 +171,22 @@ To take advantage of this, type: - ~z a~ :: Toggles open to close Note: Yes, we could use [[https://github.com/mrkkrp/vimish-fold][vimish-fold]] (and its cousin, [[https://github.com/alexmurray/evil-vimish-fold][evil-vimish-fold]]) and we’ll see if I need those. +** Navigation +Once upon a time, we use to create a =TAGS= file that contained the database for navigating code bases, but with new faster versions of grep, e.g. [[https://beyondgrep.com][ack]], [[https://github.com/ggreer/the_silver_searcher][ag]] (aka, the Silver Searcher), [[https://github.com/Genivia/ugrep][ugrep]] and [[https://github.com/BurntSushi/ripgrep][ripgrep]], we should just be able to use them. but I want to: + - Be in a function, and see its callers. For this, the [[help:rg-dwim][rg-dwim]] function is my bread-and-butter. + - Be on a function, and jump to the definition. For this, I use [[https://github.com/jacktasia/dumb-jump][dumb-jump]], which uses the above utilities. + +#+begin_src emacs-lisp + (use-package dumb-jump + :config + (setq xref-show-definitions-function #'xref-show-definitions-completing-read) + (add-hook 'xref-backend-functions #'dumb-jump-xref-activate)) +#+end_src + +Remember the following: + - ~g d~ jumps to definition of symbol at point … which is amazeballs. + - ~g r~ shows us all entries of the symbol at point, like [[help:xref-find-references][xref-find-references]] + - ~M-,~ to go back along the jump history ** Language Server Protocol (LSP) Integration The [[https://microsoft.github.io/language-server-protocol/][LSP]] is a way to connect /editors/ (like Emacs) to /languages/ (like Lisp)… wait, no, it was originally designed for VS Code and probably Python, but we now abstract away [[https://github.com/davidhalter/jedi][Jedi]] and the [[http://tkf.github.io/emacs-jedi/latest/][Emacs integration to Jedi]] (and duplicate everything for Ruby, and Clojure, and…).