Skip to content

Commit

Permalink
fix: sanitize newlines in entries and prompts (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Feb 24, 2023
1 parent db716a0 commit 55fd604
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lua/dressing/input.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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"),
Expand Down
21 changes: 14 additions & 7 deletions lua/dressing/select/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lua/dressing/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 55fd604

Please sign in to comment.