fix: sanitize newlines in entries and prompts (#88)

This commit is contained in:
Steven Arcangeli 2023-02-23 22:03:55 -08:00
parent db716a0f12
commit 55fd604006
3 changed files with 17 additions and 10 deletions

View File

@ -290,7 +290,7 @@ setmetatable(M, {
end
-- Create or update the window
local prompt = opts.prompt or config.default_prompt
local prompt = string.gsub(opts.prompt or config.default_prompt, "\n", " ")
local winid, start_in_insert = create_or_update_win(config, prompt, opts)
context = {
@ -322,7 +322,7 @@ setmetatable(M, {
end
vim.api.nvim_buf_set_option(bufnr, "filetype", "DressingInput")
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { opts.default or "" })
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { string.gsub(opts.default or "", "\n", " ") })
util.add_title_to_win(
winid,
string.gsub(prompt, "^%s*(.-)%s*$", "%1"),

View File

@ -15,6 +15,16 @@ local function get_backend(config)
return require("dressing.select.builtin"), "builtin"
end
local function sanitize_line(line)
return string.gsub(tostring(line), "\n", " ")
end
local function with_sanitize_line(fn)
return function(...)
return sanitize_line(fn(...))
end
end
-- use schedule_wrap to avoid a bug when vim opens
-- (see https://github.com/stevearc/dressing.nvim/issues/15)
-- also to prevent focus problems for providers
@ -37,24 +47,21 @@ return vim.schedule_wrap(function(items, opts, on_choice)
return patch.original_mods.select(items, opts, on_choice)
end
opts.prompt = opts.prompt or "Select one of:"
opts.prompt = sanitize_line(opts.prompt or "Select one of:")
if config.trim_prompt and opts.prompt:sub(-1, -1) == ":" then
opts.prompt = opts.prompt:sub(1, -2)
end
local format_override = config.format_item_override[opts.kind]
if format_override then
opts.format_item = format_override
opts.format_item = with_sanitize_line(format_override)
elseif opts.format_item then
-- format_item doesn't *technically* have to return a string for the
-- core implementation. We should maintain compatibility by wrapping the
-- return value with tostring
local format_item = opts.format_item
opts.format_item = function(item)
return tostring(format_item(item))
end
opts.format_item = with_sanitize_line(opts.format_item)
else
opts.format_item = tostring
opts.format_item = sanitize_line
end
local backend, name = get_backend(config)

View File

@ -150,7 +150,7 @@ M.add_title_to_win = function(winid, title, opts)
winid
))
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { " " .. title .. " " })
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, { " " .. title:gsub("\n", " ") .. " " })
local ns = vim.api.nvim_create_namespace("DressingWindow")
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
vim.api.nvim_buf_add_highlight(bufnr, ns, "FloatTitle", 0, 0, -1)