From e53db6a53f85b1c0d56eed66811bfbac520abd6c Mon Sep 17 00:00:00 2001 From: Liam Dyer Date: Mon, 30 Dec 2024 15:07:05 -0500 Subject: [PATCH] feat: escape filenames in cmdline Closes #751 --- lua/blink/cmp/sources/cmdline/constants.lua | 40 +++++++++++++++++++++ lua/blink/cmp/sources/cmdline/init.lua | 12 +++++-- 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 lua/blink/cmp/sources/cmdline/constants.lua diff --git a/lua/blink/cmp/sources/cmdline/constants.lua b/lua/blink/cmp/sources/cmdline/constants.lua new file mode 100644 index 00000000..fd2ee856 --- /dev/null +++ b/lua/blink/cmp/sources/cmdline/constants.lua @@ -0,0 +1,40 @@ +return { + help_commands = { + 'help', + 'hel', + 'he', + 'h', + }, + file_commands = { + 'edit', + 'e', + 'read', + 'r', + 'write', + 'w', + 'saveas', + 'sav', + 'split', + 'sp', + 'vsplit', + 'vs', + 'tabedit', + 'tabe', + 'badd', + 'bad', + 'next', + 'n', + 'previous', + 'prev', + 'args', + 'source', + 'so', + 'find', + 'fin', + 'diffsplit', + 'diffs', + 'diffpatch', + 'diffp', + 'make', + }, +} diff --git a/lua/blink/cmp/sources/cmdline/init.lua b/lua/blink/cmp/sources/cmdline/init.lua index c2e46cd1..606d9c91 100644 --- a/lua/blink/cmp/sources/cmdline/init.lua +++ b/lua/blink/cmp/sources/cmdline/init.lua @@ -3,6 +3,7 @@ -- License: MIT local async = require('blink.cmp.lib.async') +local constants = require('blink.cmp.sources.cmdline.constants') --- @class blink.cmp.Source local cmdline = {} @@ -37,12 +38,19 @@ function cmdline:get_completions(context, callback) .empty() :map(function() -- Special case for help where we read all the tags ourselves - if vim.tbl_contains({ 'h', 'he', 'hel', 'help' }, arguments[1] or '') then + if vim.tbl_contains(constants.help_commands, arguments[1] or '') then return require('blink.cmp.sources.cmdline.help').get_completions(current_arg_prefix) end local query = (text_before_argument .. current_arg_prefix):gsub([[\\]], [[\\\\]]) - return vim.fn.getcompletion(query, 'cmdline') + local completions = vim.fn.getcompletion(query, 'cmdline') + + -- Special case for files, escape special characters + if vim.tbl_contains(constants.file_commands, arguments[1] or '') then + completions = vim.tbl_map(function(completion) return vim.fn.fnameescape(completion) end, completions) + end + + return completions end) :map(function(completions) local items = {}