Skip to content

fix(#925): handle newlines in file names #2754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lua/nvim-tree/actions/node/open-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,14 @@ local function open_in_new_window(filename, mode)
local fname = vim.fn.fnameescape(filename)
fname = utils.escape_special_chars(fname)

local cmd
local command
if create_new_window then
cmd = string.format("%s vsplit %s", new_window_side, fname)
-- generated from vim.api.nvim_parse_cmd("belowright vsplit foo", {})
command = { cmd = "vsplit", mods = { split = new_window_side }, args = { fname } }
elseif mode:match "split$" then
cmd = string.format("%s %s", mode, fname)
command = { cmd = mode, args = { fname } }
else
cmd = string.format("edit %s", fname)
command = { cmd = "edit", args = { fname } }
end

if (mode == "preview" or mode == "preview_no_picker") and view.View.float.enable then
Expand All @@ -286,7 +287,7 @@ local function open_in_new_window(filename, mode)
set_current_win_no_autocmd(target_winid, { "BufEnter" })
end

pcall(vim.cmd, cmd)
pcall(vim.cmd, command)
lib.set_target_win()
end

Expand Down
10 changes: 10 additions & 0 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -422,11 +422,21 @@ function Builder:build_header()
end
end

---Sanitize lines for rendering.
---Replace newlines with literal \n
---@private
function Builder:sanitize_lines()
self.lines = vim.tbl_map(function(line)
return line and line:gsub("\n", "\\n") or ""
end, self.lines)
end

---Build all lines with highlights and signs
---@return Builder
function Builder:build()
self:build_header()
self:build_lines()
self:sanitize_lines()
return self
end

Expand Down
Loading