From 706bac4bc863b91f5bcb9f6695745df857ced795 Mon Sep 17 00:00:00 2001 From: pynappo Date: Sat, 11 Jan 2025 03:54:18 -0800 Subject: [PATCH 1/2] fix(cmdline): parse cmdline to retrieve command when possible --- lua/blink/cmp/sources/cmdline/init.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lua/blink/cmp/sources/cmdline/init.lua b/lua/blink/cmp/sources/cmdline/init.lua index 5ff5e11a..454d8185 100644 --- a/lua/blink/cmp/sources/cmdline/init.lua +++ b/lua/blink/cmp/sources/cmdline/init.lua @@ -30,11 +30,13 @@ 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) + local valid_cmd, parsed = pcall(vim.api.nvim_parse_cmd, context.line, {}) local task = async.task .empty() :map(function() + local cmd = (valid_cmd and parsed.cmd) or arguments[1] or '' -- 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 @@ -52,7 +54,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 @@ -68,7 +70,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 From 66860d3d61345c0bfe9cd9039ef1e8ea98baef20 Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Sat, 11 Jan 2025 12:33:00 -0500 Subject: [PATCH 2/2] docs: explain nvim_parse_cmd reasoning --- lua/blink/cmp/sources/cmdline/init.lua | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lua/blink/cmp/sources/cmdline/init.lua b/lua/blink/cmp/sources/cmdline/init.lua index 454d8185..28663841 100644 --- a/lua/blink/cmp/sources/cmdline/init.lua +++ b/lua/blink/cmp/sources/cmdline/init.lua @@ -30,11 +30,14 @@ 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() - local cmd = (valid_cmd and parsed.cmd) or arguments[1] or '' -- Special case for help where we read all the tags ourselves if vim.tbl_contains(constants.help_commands, cmd) then return require('blink.cmp.sources.cmdline.help').get_completions(current_arg_prefix)