2

I'm starting to feel a bit frustrated: this should be as simple as setting some variable or other, but I'm unable to find which one.

Say I'm editing file foo.c and I find something like

void foo(int n) {
  bar(2 * n);
}

I put the cursor on bar and hit s-l g g. The resulting call to lsp-find-function correctly finds the file bar.c, containing the definition of bar, and displays it. But it does it in the window previously holding foo.c.

I suppose there is some configuration option allowing one to say: "please, open the file you find in another window" but I'm unable to find it and I run out of queries to throw at google for that :)

Note that I did find this question, but the answer there doesn't help me, because introducing rules matching *.c in display-buffer-alist doesn't strike me as a smart thing to do.

Help is worth at least a coffe or a beer next time you happen to be in Rome

2
  • Does lsp-mode use xref under the bonnet for this stuff? Maybe there is a variable or a hook for xref? xref--show-xrefs is this what creates the actual display?
    – Arktik
    Mar 1 at 16:08
  • As far as I can understand the code, yes. I followed the call starting from lsp-find-definition and I end up with xref--show-xrefs. The display-action parameter to the latter comes from the :display-action argument given to lsp-find-definition (which I suppose should be nil, because lsp-find-function is called interactively and interactive in its definition has no argument)
    – TheHobbit
    Mar 1 at 16:28

1 Answer 1

2

You’ve already found the solution in the comments, but I’ll go ahead and spell out how you can use it:

(defun thehobbit-lsp-find-definition ()
  (interactive)
  (lsp-find-definition :display-action 'window))
(keymap-global-set "C-c ." 'thehobbit-lsp-find-definition)

This just adds a different key binding, one which calls lsp-find-definition with a different :display-action argument.

You could also do something similar with advice if you prefer not to change the key bindings.

You should also consider the xref-show-definitions-function variable, which controls what happens when there are multiple definitions to choose from.

3
  • Yea, needed to check the args for xref-show-xrefs-function, I stopped one short of that :).
    – Arktik
    Mar 1 at 17:53
  • 1
    I'll look into the advice idea, as there are a lot of lsp-find-* functions, all of them built using lsp-find-locations with different parameters. Thank you
    – TheHobbit
    Mar 1 at 20:35
  • Yes, that’s another good reason to use advice.
    – db48x
    Mar 2 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.