Skip to content

Commit

Permalink
refactor: handle editRange in LSP source
Browse files Browse the repository at this point in the history
  • Loading branch information
Saghen committed Dec 16, 2024
1 parent 7a83acf commit 2dd12b1
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
8 changes: 4 additions & 4 deletions LSP_TRACKER.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

## Completion Items

- [x] `completionItem/resolve` <- Used to get information such as documentation which would be too expensive to include normally
- [x] `completionItem/resolve`

### Client Capabilities

- [ ] `dynamicRegistration`
- [ ] `CompletionItem`
- [x] `CompletionItem`
- [x] `snippetSupport`
- [ ] `commitCharacterSupport`
- [x] `documentationFormat`
- [x] `deprecatedSupport`
- [ ] `preselectSupport`
- [x] `tagSupport`
- [ ] `insertReplaceSupport`
- [x] `resolveSupport` <- Allows LSPs to resolve additional properties lazily, potentially improving latency
- [x] `resolveSupport`
- [x] `insertTextModeSupport`
- [x] `labelDetailsSupport`
- [x] `completionItemKind`
Expand All @@ -40,7 +40,7 @@
- [x] `isIncomplete`
- [x] `itemDefaults`
- [x] `commitCharacters`
- [ ] `editRange`
- [x] `editRange`
- [x] `insertTextFormat`
- [x] `insertTextMode`
- [x] `data`
Expand Down
9 changes: 0 additions & 9 deletions lua/blink/cmp/lib/text_edits.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,6 @@ end
function text_edits.get_from_item(item)
local text_edit = vim.deepcopy(item.textEdit)

-- Fallback to default edit range if defined, and no text edit was set
if text_edit == nil and item.editRange ~= nil then
text_edit = {
newText = item.textEditText or item.insertText or item.label,
-- FIXME: temporarily convert insertReplaceEdit to regular textEdit
range = vim.deepcopy(item.editRange.insert or item.editRange.replace or item.editRange),
}
end

-- Guess the text edit if the item doesn't define it
if text_edit == nil then return text_edits.guess(item) end

Expand Down
1 change: 0 additions & 1 deletion lua/blink/cmp/sources/lib/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function utils.blink_item_to_lsp_item(item)
lsp_item.source_name = nil
lsp_item.cursor_column = nil
lsp_item.client_id = nil
lsp_item.editRange = nil
return lsp_item
end

Expand Down
21 changes: 18 additions & 3 deletions lua/blink/cmp/sources/lsp.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local known_defaults = {
'commitCharacters',
'editRange',
'insertTextFormat',
'insertTextMode',
'data',
Expand Down Expand Up @@ -87,17 +86,33 @@ function lsp:get_completions(context, callback)
end

local items = result.items or result

-- add defaults, client id and score offset to the items
local default_edit_range = result.itemDefaults and result.itemDefaults.editRange
for _, item in ipairs(items) do
item.client_id = client.id

-- score offset for deprecated items
-- todo: make configurable
if item.deprecated or (item.tags and vim.tbl_contains(item.tags, 1)) then item.score_offset = -2 end

-- set defaults
for key, value in pairs(result.itemDefaults or {}) do
if vim.tbl_contains(known_defaults, key) then item[key] = item[key] or value end
end
if default_edit_range and item.textEdit == nil then
local new_text = item.textEditText or item.insertText or item.label
if default_edit_range.replace ~= nil then
item.textEdit = {
replace = default_edit_range.replace,
insert = default_edit_range.insert,
newText = new_text,
}
else
item.textEdit = {
range = result.itemDefaults.editRange,
newText = new_text,
}
end
end
end

callback({
Expand Down
1 change: 0 additions & 1 deletion lua/blink/cmp/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
--- @field source_name string
--- @field cursor_column number
--- @field client_id? number
--- @field editRange? lsp.Range | { insert: lsp.Range, replace: lsp.Range }

return {
-- some plugins mutate the vim.lsp.protocol.CompletionItemKind table
Expand Down

0 comments on commit 2dd12b1

Please sign in to comment.