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 jump regression in LSP references action handler #3091

Merged
merged 5 commits into from
May 14, 2024

Conversation

adriangoransson
Copy link
Contributor

@adriangoransson adriangoransson commented May 7, 2024

Description

This change restores the possibility to exclude the current line when invoking the lsp_references picker.

  • opts.include_current_line is by default unset, so the previous equality check would fail unless the option was set explicitly.
  • vim.api.nvim_win_get_cursor() returns both line and column, so it can't be directly compared with the returned line number.
  • The actual comparison was expecting quickfix-like items, when in actuality, we were dealing with raw LSP location objects.

Type of change

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

How Has This Been Tested?

  • Triggering lsp_references on a symbol which is only referenced in one other location jumps directly to that location.
  • Triggering lsp_references on a symbol which is referenced in multiple other locations lists reference locations. References on the cursor line where the picker was triggered are excluded.
  • Triggering lsp_references with opts = { include_current_line = true } yields the same behavior as it does today.

Configuration:

  • Neovim version (nvim --version):

    NVIM v0.9.5
    Build type: Release
    LuaJIT 2.1.1713773202

  • Operating system and version:

    macOs 14.4.1 (23E224)

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) N/A, I think?

This change restores the possibility to exclude the current line when
invoking the lsp_references picker.

- opts.include_current_line is by default unset, so the previous
  equality check would fail unless the option was set explicitly.
- vim.api.nvim_win_get_cursor() returns both line and column, so it
  can't be directly compared with the returned line number.
- The actual comparison was expecting quickfix-like items, when in
  actuality, we were dealing with raw LSP location objects.
@adriangoransson
Copy link
Contributor Author

adriangoransson commented May 8, 2024

Everything has been working for a couple of days, but I got warning messages:

locations_to_items must be called with valid offset encoding

I added offset_encoding as a parameter to action_handlers to resolve it. It's not very pretty, and currently not annotated correctly either, but the warnings are gone. Any assistance to make it nicer is kindly accepted!

@jamestrew
Copy link
Contributor

Thanks!
For the offset_encoding part, I think it's probably better to call vim.lsp.util.locations_to_items earlier, before calling apply_action_handler, then pass the return value of it to apply_action_handler.

This is what we used to do before my (terrible 😅) refactor. See here:

local locations = {}
if result then
local results = vim.lsp.util.locations_to_items(result, vim.lsp.get_client_by_id(ctx.client_id).offset_encoding)
if not include_current_line then
locations = vim.tbl_filter(function(v)
-- Remove current line from result
return not (v.filename == filepath and v.lnum == lnum)
end, vim.F.if_nil(results, {}))
else
locations = vim.F.if_nil(results, {})
end
end
if vim.tbl_isempty(locations) then
return
end
if #locations == 1 and opts.jump_type ~= "never" then
if filepath ~= locations[1].filename then
if opts.jump_type == "tab" then
vim.cmd "tabedit"
elseif opts.jump_type == "split" then
vim.cmd "new"
elseif opts.jump_type == "vsplit" then
vim.cmd "vnew"
elseif opts.jump_type == "tab drop" then
vim.cmd("tab drop " .. locations[1].filename)
end
end
-- jump to location
local location = locations[1]
local bufnr = opts.bufnr
if location.filename then
local uri = location.filename
if not utils.is_uri(uri) then
uri = vim.uri_from_fname(uri)
end
bufnr = vim.uri_to_bufnr(uri)
end
vim.api.nvim_win_set_buf(0, bufnr)
vim.api.nvim_win_set_cursor(0, { location.lnum, location.col - 1 })
return

@adriangoransson
Copy link
Contributor Author

For the offset_encoding part, I think it's probably better to call vim.lsp.util.locations_to_items earlier, before calling apply_action_handler, then pass the return value of it to apply_action_handler.

Thanks for the help (and for your work on the plugin! 🌟). I think I managed to solve it in an okay way, by letting action handlers receive and process both the lsp locations as well as the qf items. It requires some discipline to keep the processing "synced" between the two, but it keeps the calling function (that doesn't yet know which representation it is going to use) simpler.

I added a couple of refactoring commits to the PR as well, but can drop those if you wish.

@jamestrew
Copy link
Contributor

thanks lgtm!

@jamestrew jamestrew merged commit df4dd30 into nvim-telescope:master May 14, 2024
9 checks passed
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.

2 participants