Skip to content
Jerry edited this page Jul 23, 2023 · 6 revisions

Peek [简体中文版]

How to use lsp-bridge-peek?

lsp-bridge-peek

Basic Use

The basic use of lsp-bridge-peek is to read the definition and the references of a symbol, without leaving current buffer. This is often helpful when reading and writing code.

Type M-x lsp-bridge-peek on a symbol to peek its definition and references. A peek window will be opened under current line, and it follows the cursor. You can use these keybindings to browse in the peek window:

  • M-n, M-p: Next/prev line.
  • M-N, M-P: Next/prev definition or reference.
  • M-l j: Jump to the definition or reference.
  • M-l b: Jump back to the position before lsp-bridge-peek-jump.
  • C-g: Close the peek window.

These keys are defined in lsp-bridge-peek-keymap, and can be customized.

More Advanced: lsp-bridge-peek-symbol-tree

When reading a definition or reference, we often find it calls another function, and we are interested in the definition and references of that function too. Use a traditional code reading tools to do this, and we'll soon get lost in a lot of buffers.

Not a problem in lsp-bridge-peek. In a peek session, just type M-x lsp-bridge-peek-through (It can be bound to any key you like), than an ace string will be attached to each symbol in the peek window, and you know what to do now :) Just type it to see the definition and the references of that symbol, in the same peek window.

Do this several times, and the history is shown in the bottom of the peek window, like this:

func1 -> func2 -> [func3]

You can type <left> and <right> to move in the history. Now move back to func2, it becomes:

func1 -> [func2] -> func3

At this point you can browse the definition and references of func2 again.

If now we peek through func4, the history will become:

func1 -> func2 < [func4]

The < means you created a branch here. The current branch goes to func4, and the other one goes to func3. You can type / to switch between the branches. Congratulations! You just learned the most effective way to browse in a tree-like code reading history.

Implementing lsp-bridge-avy-peek

The code is as follows:

(require 'avy)

(defun lsp-bridge-avy-peek ()
  "Peek any symbol in the file by avy jump."
  (interactive)
  (setq lsp-bridge-peek-ace-list (make-list 5 nil))
  (setf (nth 1 lsp-bridge-peek-ace-list) (point))
  (setf (nth 2 lsp-bridge-peek-ace-list) (buffer-name))
  (save-excursion
    (call-interactively 'avy-goto-word-0)
    (lsp-bridge-peek)
    (setf (nth 3 lsp-bridge-peek-ace-list) (buffer-name))
    (setf (nth 4 lsp-bridge-peek-ace-list) (point))))

In fact, you can replace avy-goto-word-0 in (call-interactively 'avy-goto-word-0) with any jump function you like, such as avy-goto-char or avy-goto-char-2. Of course, if you prefer ace, you can also replace the function with ace's jump type😉, just remember to change the require statement.

What is the difference between lsp-bridge-peek and lsp-bridge-find-references?

Have you ever experienced a messy buffer-list when reading code? Have you ever tried to find the buffer you just looked at by jumping through many buffers? Have you ever been exhausted by facing many buffers to delete?

Don't worry, lsp-bridge-peek won't have this problem. All browsing happens in a dedicated peek window. When you're done, just press C-g and the birds will chirp, the water will gurgle, and the clouds will be light and breezy. Your work environment is still lovely :)

In addition, lsp-bridge-peek records your browsing history, so you can freely jump between them without having to rack your brain to remember which symbol you just saw. This reduces the time users spend managing buffers and work environments, and effectively reduces the cognitive load.

In conclusion, I believe lsp-bridge-peek is a tool that is committed to making it easier and more convenient for users to read code.

Of course, I am not comparing the pros and cons of lsp-bridge-peek and lsp-bridge-find-references. lsp-bridge-find-references is an extremely powerful code refactoring tool that can play a role that lsp-bridge-peek can't match when adjusting code. lsp-bridge-peek and lsp-bridge-find-references are almost completely different tools, and they can work wonders when they are in the best position for themselves. Therefore, the specific tool to use should be based on your current needs, and you should not let the tool bind you, just follow your heart😉