Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LSP References Single-Result Jump Behavior #2281

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

jls83
Copy link

@jls83 jls83 commented Dec 22, 2022

Description

When there is only a single result for a call to lsp_references, the previous cursor position is not added to the jumplist. This is inconsistent with the behavior of other Telescope built-ins.

This PR changes the implementation of the lsp.references method to use the list_or_jump helper function already in use for other LSP-related items (e.g. definitions, type_definitions, implementations). It also adds an optional results filter to list_or_jump via the results_filter attribute in opts.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list relevant details about your configuration

  • Find a symbol that has a single usage & call require('telescope.builtin').lsp_references(). Try jumping back (via <C-o>) to the previous cursor position.
  • Confirm symbols with multiple usages work as intended.

Configuration:

  • Neovim version (nvim --version): v0.8.1
  • Operating system and version: Debian (testing)

Checklist:

  • My code follows the style guidelines of this project (stylua)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (lua annotations)

@jls83 jls83 marked this pull request as ready for review December 22, 2022 18:41
@jls83
Copy link
Author

jls83 commented Jan 7, 2023

@Conni2461 sorry for the ping, but do I need to directly assign someone for the CI workflow and/or a review? Thanks!

end
end

return list_or_jump("textDocument/references", "LSP References", opts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params.context = { includeDeclaration = vim.F.if_nil(opts.include_declaration, true) }

we can either pass in params as opts.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this is referring to, sorry!

Copy link

@gbroques gbroques Jun 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is referring to the need to support the include_declaration option for lsp_references.

See the following code:

params.context = { includeDeclaration = vim.F.if_nil(opts.include_declaration, true) }
vim.lsp.buf_request(opts.bufnr, "textDocument/references", params, function(err, result, ctx, _)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gbroques thanks for the clarification! I added some logic in list_or_jump to handle this case.

Comment on lines 50 to 66
-- jump to location
local location = locations[1]
local bufnr = opts.bufnr
if location.filename then
bufnr = vim.uri_to_bufnr(vim.uri_from_fname(location.filename))
end
vim.api.nvim_win_set_buf(0, bufnr)
vim.api.nvim_win_set_cursor(0, { location.lnum, location.col - 1 })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe look at: #2229 (comment)

I've not check what they said but if you say it works, i'll will do some investigating/testing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something changed upstream and now the objects are the same. But we still have 0.7 support so we need to make sure it works for both versions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't run into the problem in the linked issue, but I've been making changes with NeoVim v0.8.1 so you might be right.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Conni2461 let me know if anything needs to be updated here.

@Conni2461
Copy link
Member

Conni2461 commented Jan 10, 2023

I am sorry about the late review. There is no excuse i should have done it a long time ago :|

Thanks for the PR :) I've also thought that using the same function should be possible, so thanks for implementing it.

I've done a quick review. opts.results_filter kinda gives us a new feature that should be documented for the all pickers that use list_or_jump. Do you know how to do that?

To fix CI you just have to rebase your branch because we fixed the tests already on master. There should be no conflicts, so rebase should be straight forward

@jls83 jls83 force-pushed the fix_lsp_references_jumps branch from 6f16402 to 9dce80b Compare January 12, 2023 00:53
@jls83
Copy link
Author

jls83 commented Jan 12, 2023

No worries on the delay! End of the year is always a busy time :)

I added docs for the affected pickers, but I'm not sure if anything else needs to be done besides the CI pushing the changes to the branch once the workflow permissions are granted. Also, I noticed a subtle bug with my initial implementation -- if someone passed in a results_filter fn for the lsp_references picker, it would be discarded in favor of the "remove current line" filter. I added some extra logic to run both filters if available.

Let me know if you need anything else!

@gbroques
Copy link

gbroques commented Jun 24, 2023

Thank you for the work on this @Conni2461 and @jls83!

This PR looks stale.

Anything I could do to help?

@nieomylnieja
Copy link

Upping this one ☝️ could also help if need be :)

@Conni2461
Copy link
Member

uff thats a 2022 PR, i'll finish it this weekend. thanks for the ping

@Conni2461 Conni2461 self-assigned this Jan 18, 2024
@jls83 jls83 force-pushed the fix_lsp_references_jumps branch from d86ab9e to a46205f Compare January 18, 2024 18:56
@jls83
Copy link
Author

jls83 commented Jan 18, 2024

@Conni2461 I fixed up my branch with master & made some of the requested changes. Let me know if you need anything else!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants