From 1eea4571449ced812a03f5d5658785bb16564c5e Mon Sep 17 00:00:00 2001 From: cmp-cmdline-fix Date: Wed, 5 Nov 2025 03:33:11 -0500 Subject: [PATCH] Fix: Handle vim.fn.getcompletion errors gracefully with pcall vim.fn.getcompletion() can throw errors when the cmdline contains malformed or incomplete patterns (e.g., unmatched braces like 'HEAD@{}', incomplete regex patterns like '\(foo', etc). This causes crashes with error messages like: - E220: Missing } - E54: Unmatched \( - E869: Unknown operator This fix wraps the getcompletion call with pcall to handle errors gracefully, returning an empty completion list instead of crashing. Fixes #73 Fixes #101 Related to #102 --- lua/cmp_cmdline/init.lua | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lua/cmp_cmdline/init.lua b/lua/cmp_cmdline/init.lua index 53e7593..d69d860 100644 --- a/lua/cmp_cmdline/init.lua +++ b/lua/cmp_cmdline/init.lua @@ -133,15 +133,18 @@ local definitions = { --- create items. 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 ok, completions = pcall(vim.fn.getcompletion, escaped, 'cmdline') + if ok then + for _, word_or_item in ipairs(completions) 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