From 23b15238099bd3e92f85bae813cb9c67f08bee3c Mon Sep 17 00:00:00 2001 From: bekaboo Date: Tue, 24 Dec 2024 13:59:08 -0500 Subject: [PATCH] fix: escape special chars in filenames --- lua/cmp_cmdline/init.lua | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lua/cmp_cmdline/init.lua b/lua/cmp_cmdline/init.lua index 53e7593..3c9f4f4 100644 --- a/lua/cmp_cmdline/init.lua +++ b/lua/cmp_cmdline/init.lua @@ -130,18 +130,30 @@ local definitions = { -- cmp-cmdline corrects `no` prefix for option name. local is_option_name_completion = OPTION_NAME_COMPLETION_REGEX:match_str(cmdline) ~= nil + local compltype = vim.fn.getcmdcompltype() + local is_path_completion = compltype == 'dir' + or compltype == 'file' + or compltype == 'file_in_path' + or compltype == 'runtime' + --- 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 completion_ok, completion = pcall(vim.fn.getcompletion, escaped, 'cmdline') + if completion_ok then + for _, word_or_item in ipairs(completion) do + local word = type(word_or_item) == 'string' and word_or_item or word_or_item.word + if is_path_completion then + word = vim.fn.fnameescape(word) + end + 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