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

feat(lsp): preserve cursor position for move_item command #532

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lua/rustaceanvim/commands/move_item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,41 @@ local function get_params(up)
return params
end

local function extract_cursor_position(text_edits)
local cursor = { text_edits[1].range.start.line }
local prev_te
for _, te in ipairs(text_edits) do
if te.newText and te.insertTextFormat == 2 then
if not cursor[2] then
if prev_te then
cursor[1] = cursor[1]
+ math.max(0, te.range.start.line - prev_te.range['end'].line - 1)
- (prev_te.range.start.line == te.range.start.line and 1 or 0)
end
local pos_start = string.find(te.newText, '%$0')
local lines = vim.split(string.sub(te.newText, 1, pos_start), '\n')
local total_lines = #lines
cursor[1] = cursor[1] + total_lines
if pos_start then
cursor[2] = (total_lines == 1 and te.range.start.character or 0) + #lines[total_lines] - 1
end
end
end
prev_te = te
end
return cursor
end

-- move it baby
local function handler(_, result, ctx)
if result == nil or #result == 0 then
return
end
local cursor = extract_cursor_position(result)
Copy link
Owner

Choose a reason for hiding this comment

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

question: This function does more than just extracting the cursor position, as it also implements the snippet_text_edits_to_text_edits logic.
Can it be reduced to just the single functionality of extracting the cursor position?

Correct me if I'm wrong; I don't think we'll see any meaningful performance gains by looping over the text edits once.

On a related note: I'm wondering if we even need to apply snippet_text_edits_to_text_edits here.
It seems unlikely to me that moving an item would result in a snippet text edit. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

question: This function does more than just extracting the cursor position, as it also implements the snippet_text_edits_to_text_edits logic.
Can it be reduced to just the single functionality of extracting the cursor position?

Yes, sorry about that. I just copy-pasted my old PR. Updated now.

On a related note: I'm wondering if we even need to apply snippet_text_edits_to_text_edits here.
It seems unlikely to me that moving an item would result in a snippet text edit. What do you think?

rust-analyzer sends that snippet text edit, nothing we can do here but to patch it for vim.lsp.util.apply_text_edits.

local overrides = require('rustaceanvim.overrides')
overrides.snippet_text_edits_to_text_edits(result)
vim.lsp.util.apply_text_edits(result, ctx.bufnr, vim.lsp.get_client_by_id(ctx.client_id).offset_encoding)
vim.api.nvim_win_set_cursor(0, cursor)
end

local rl = require('rustaceanvim.rust_analyzer')
Expand Down
Loading