Skip to content

Commit

Permalink
Complete filenames which are indicated by %*
Browse files Browse the repository at this point in the history
This is based on the cmp_cmdline pr that iteratee
It will offer completions for % and %:* as well as # and #:* in cmdline
  • Loading branch information
m42e committed Aug 26, 2023
1 parent ee73376 commit f8afeb0
Showing 1 changed file with 36 additions and 30 deletions.
66 changes: 36 additions & 30 deletions lua/cmp_cmdline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ local definitions = {
regex = [=[[^[:blank:]]*$]=],
kind = cmp.lsp.CompletionItemKind.Variable,
isIncomplete = true,
exec = function(option, arglead, cmdline, force)
exec = function(option, arglead, cmdline, force, cursorline)
-- Ignore range only cmdline. (e.g.: 4, '<,'>)
if not force and ONLY_RANGE_REGEX:match_str(cmdline) then
return {}
Expand Down Expand Up @@ -127,44 +127,49 @@ local definitions = {

local items = {}
local escaped = cmdline:gsub([[\\]], [[\\\\]]);
local is_magic_file = false
local input_start = string.sub(fixed_input, 1, 1)
if input_start == '%' then
is_magic_file = true
elseif input_start == '#' then
is_magic_file = true
end
for _, word_or_item in ipairs(vim.fn.getcompletion(escaped, 'cmdline')) do
local word = type(word_or_item) == 'string' and word_or_item or word_or_item.word
local item = { label = word }
table.insert(items, item)
if is_option_name_completion and is_boolean_option(word) then
table.insert(items, vim.tbl_deep_extend('force', {}, item, {
label = 'no' .. word,
filterText = word,
}))
local is_magic_file = (#arglead ~= 1 and (input_start == '%' or input_start == '#'))
if is_magic_file then
for _, word_or_item in ipairs(vim.fn.getcompletion(arglead, 'file')) do
local word = type(word_or_item) == 'string' and word_or_item or word_or_item.word
local item = { label = word, ismagic=true }
table.insert(items, item)
end
else
for _, word_or_item in ipairs(vim.fn.getcompletion(escaped, 'cmdline')) do
local word = type(word_or_item) == 'string' and word_or_item or word_or_item.word
local item = { label = word }
table.insert(items, item)
if is_option_name_completion and is_boolean_option(word) then
table.insert(items, vim.tbl_deep_extend('force', {}, item, {
label = 'no' .. word,
filterText = word,
}))
end
end
end
for _, item in ipairs(items) do
if not is_magic_file and not string.find(item.label, fixed_input, 1, true) then
item.label = fixed_input .. item.label
end
if is_magic_file then
local replace_range = {
start = {
line = 0,
character = #cmdline - #fixed_input - 1
},
['end'] = {
line = 0,
character = #cmdline - 1
}
}
item.textEdit = {
replace = replace_range,
range = replace_range,
-- replace = replace_range,
range = {
start = {
line = cursorline,
character = #cmdline - #arglead - 1
},
['end'] = {
line = cursorline,
character = #cmdline - 1
}
},
newText = item.label
}
item.label = fixed_input .. item.label
item.insert_text = item.label
item.label = item.label
item.filterText = arglead
end
end
return items
Expand Down Expand Up @@ -206,7 +211,8 @@ source.complete = function(self, params, callback)
vim.tbl_deep_extend('keep', params.option or {}, DEFAULT_OPTION),
string.sub(params.context.cursor_before_line, s + 1),
params.context.cursor_before_line,
params.context:get_reason() == cmp.ContextReason.Manual
params.context:get_reason() == cmp.ContextReason.Manual,
params.context.cursor.line
)
kind = def.kind
isIncomplete = def.isIncomplete
Expand Down

0 comments on commit f8afeb0

Please sign in to comment.