Skip to content

Commit bf8e0d3

Browse files
committed
Handle length/start in completions response in repl.omnifunc
Doesn't handle mixed cases yet but should fix the cpptools case mentioned in #1037
1 parent 0e6b7c4 commit bf8e0d3

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lua/dap/protocol.lua

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,38 @@
248248
---@field request 'launch'|'attach'
249249

250250

251+
---@alias dap.CompletionItemType
252+
---|'method'
253+
---|'function'
254+
---|'constructor'
255+
---|'field'
256+
---|'variable'
257+
---|'class'
258+
---|'interface'
259+
---|'module'
260+
---|'property'
261+
---|'unit'
262+
---|'value'
263+
---|'enum'
264+
---|'keyword'
265+
---|'snippet'
266+
---|'text'
267+
---|'color'
268+
---|'file'
269+
---|'reference'
270+
---|'customcolor'
271+
272+
273+
---@class dap.CompletionItem
274+
---@field label string By default this is also the text that is inserted when selecting this completion
275+
---@field text? string If present and not empty this is inserted instead of the label
276+
---@field sortText? string Used to sort completion items if present and not empty. Otherwise label is used
277+
---@field detail? string human-readable string with additional information about this item. Like type or symbol information
278+
---@field type? dap.CompletionItemType
279+
---@field start? number Start position in UTF-16 code units. (within the `text` attribute of the `completions` request) 0- or 1-based depending on `columnsStartAt1` capability. If omitted, the text is added at the location of the `column` attribute of the `completions` request.
280+
---@field length? number How many characters are overwritten by the completion text. Measured in UTF-16 code units. If missing the value 0 is assumed which results in the completion text being inserted.
281+
---@field selectionStart? number
282+
---@field selectionLength? number
283+
284+
251285
---@alias dap.SteppingGranularity 'statement'|'line'|'instruction'

lua/dap/repl.lua

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,24 @@ do
409409
return items
410410
end
411411

412+
---@param items dap.CompletionItem[]
413+
---@return boolean mixed, integer? start
414+
local function get_start(items)
415+
local start = nil
416+
local mixed = false
417+
for _, item in ipairs(items) do
418+
if item.start and (item.length or 0) > 0 then
419+
if start and start ~= item.start then
420+
mixed = true
421+
start = math.min(start, item.start)
422+
else
423+
start = item.start
424+
end
425+
end
426+
end
427+
return mixed, start
428+
end
429+
412430
function M.omnifunc(findstart, base)
413431
local session = get_session()
414432
local col = api.nvim_win_get_cursor(0)[2]
@@ -456,8 +474,13 @@ do
456474
require('dap.utils').notify('completions request failed: ' .. err.message, vim.log.levels.WARN)
457475
return
458476
end
459-
local items = completions_to_items(response.targets)
460-
vim.fn.complete(offset + text_match + 1, items)
477+
local items = response.targets --[[@as dap.CompletionItem[]|]]
478+
local mixed, start = get_start(items)
479+
if start and not mixed then
480+
vim.fn.complete(offset + start + 1, completions_to_items(items))
481+
else
482+
vim.fn.complete(offset + text_match + 1, completions_to_items(items))
483+
end
461484
end)
462485

463486
-- cancel but stay in completion mode for completion via `completions` callback

0 commit comments

Comments
 (0)