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

Completion for %(:*) and #(:*) in cmdline #105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
54 changes: 42 additions & 12 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,21 +127,50 @@ local definitions = {

local items = {}
local escaped = cmdline:gsub([[\\]], [[\\\\]]);
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 input_start = string.sub(fixed_input, 1, 1)
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 string.find(item.label, fixed_input, 1, true) then
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
item.textEdit = {
-- replace = replace_range,
range = {
start = {
line = cursorline,
character = #cmdline - #arglead - 1
},
['end'] = {
line = cursorline,
character = #cmdline - 1
}
},
newText = item.label
}
item.insert_text = item.label
item.label = item.label
item.filterText = arglead
end
end
return items
end
Expand Down Expand Up @@ -182,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