Skip to content

Commit

Permalink
Revert to previous partial symbol on autocomplete
Browse files Browse the repository at this point in the history
This change handles issue reported on lite-xl/lite-xl-lsp#67
by restoring previous partial since the completion request is async
and new text could have been written while handling the LSP server
response, which results in invalid insert positions for the received
completion items.

Depends on pragtical/pragtical#66
  • Loading branch information
jgmdev committed Feb 14, 2024
1 parent 877fc80 commit 921d8e9
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,16 @@ local function autocomplete_onselect(index, item)
local dv = get_active_docview()
if completion.textEdit then
if dv then
-- Restore previous partial since the complete request is async and
-- new text could have been written while retrieving autocomplete items
-- which results in invalid insert positions if we don't revert partial
local current_partial = {autocomplete.get_partial_symbol()}
if current_partial[1] ~= item.data.partial[1] then
local partial, pl1, pc1, pl2, pc2 = table.unpack(current_partial)
dv.doc:remove(pl1, pc1, pl2, pc2)
partial, pl1, pc1 = table.unpack(item.data.partial)
dv.doc:insert(pl1, pc1, partial)
end
local is_snippet = completion.insertTextFormat
and completion.insertTextFormat == protocol.InsertTextFormat.Snippet
local edit_applied = apply_edit(
Expand Down Expand Up @@ -587,8 +597,7 @@ local function autocomplete_onselect(index, item)
---@type core.doc
local doc = dv.doc
if dv then
local line2, col2 = doc:get_selection()
local line1, col1 = doc:position_offset(line2, col2, translate.start_of_word)
local _, line1, col1, line2, col2 = autocomplete.get_partial_symbol()
doc:set_selection(line1, col1, line2, col2)
snippets.execute {format = 'lsp', template = completion.insertText}
return true
Expand Down Expand Up @@ -1402,6 +1411,8 @@ function lsp.request_completion(doc, line, col, forced)
return false
end

local partial = { autocomplete.get_partial_symbol() }

server:push_request('textDocument/completion', {
params = request,
overwrite = true,
Expand Down Expand Up @@ -1511,7 +1522,9 @@ function lsp.request_completion(doc, line, col, forced)
info = info,
desc = desc,
data = {
server = server, completion_item = symbol
server = server,
completion_item = symbol,
partial = partial
},
onselect = autocomplete_onselect
}
Expand Down

0 comments on commit 921d8e9

Please sign in to comment.