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

fix(cmdline): parse for command name if possible #993

Merged
merged 2 commits into from
Jan 11, 2025
Merged
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
11 changes: 8 additions & 3 deletions lua/blink/cmp/sources/cmdline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,16 @@ function cmdline:get_completions(context, callback)
local keyword = context.get_bounds(keyword_config.range)
local current_arg_prefix = current_arg:sub(1, keyword.start_col - #text_before_argument - 1)

-- Parse the command to ignore modifiers like :vert help
-- Fails in some cases, like context.line = ':vert' so we fallback to the first argument
local valid_cmd, parsed = pcall(vim.api.nvim_parse_cmd, context.line, {})
local cmd = (valid_cmd and parsed.cmd) or arguments[1] or ''

local task = async.task
.empty()
:map(function()
-- Special case for help where we read all the tags ourselves
if vim.tbl_contains(constants.help_commands, arguments[1] or '') then
if vim.tbl_contains(constants.help_commands, cmd) then
return require('blink.cmp.sources.cmdline.help').get_completions(current_arg_prefix)
end

Expand All @@ -52,7 +57,7 @@ function cmdline:get_completions(context, callback)
end

-- Special case for files, escape special characters
if vim.tbl_contains(constants.file_commands, arguments[1] or '') then
if vim.tbl_contains(constants.file_commands, cmd) then
completions = vim.tbl_map(function(completion) return vim.fn.fnameescape(completion) end, completions)
end

Expand All @@ -68,7 +73,7 @@ function cmdline:get_completions(context, callback)
if has_prefix then filter_text = completion:sub(#current_arg_prefix + 1) end

-- for lua, use the filter text as the label since it doesn't include the prefix
local label = arguments[1] == 'lua' and filter_text or completion
local label = cmd == 'lua' and filter_text or completion

-- add prefix to the newText
local new_text = completion
Expand Down
Loading