Skip to content

Commit

Permalink
feat: escape filenames in cmdline
Browse files Browse the repository at this point in the history
Closes #751
  • Loading branch information
Saghen committed Dec 30, 2024
1 parent 73a5f4e commit e53db6a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
40 changes: 40 additions & 0 deletions lua/blink/cmp/sources/cmdline/constants.lua
Original file line number Diff line number Diff line change
@@ -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',
},
}
12 changes: 10 additions & 2 deletions lua/blink/cmp/sources/cmdline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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 = {}
Expand Down

0 comments on commit e53db6a

Please sign in to comment.