Skip to content

Commit

Permalink
Merge pull request #966 from NeogitOrg/escape-filepaths-before-opening
Browse files Browse the repository at this point in the history
  • Loading branch information
CKolkey authored Nov 24, 2023
2 parents ab59d5d + 07d36ed commit 3be9268
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions lua/neogit/status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1028,39 +1028,37 @@ end
---@param item File
---@see section_has_hunks
local function handle_section_item(item)
local path = item.absolute_path

if not path then
if not item.absolute_path then
notification.error("Cannot open file. No path found.")
return
end

local row, col
local cursor_row, cursor_col = unpack(vim.api.nvim_win_get_cursor(0))

local hunk = M.get_item_hunks(item, cursor_row, cursor_row, false)[1]
if hunk then
local line_offset = cursor_row - hunk.first
row = hunk.disk_from + line_offset - 1
for i = 1, line_offset do
if string.sub(hunk.lines[i], 1, 1) == "-" then
row = row - 1
end
end
-- adjust for diff sign column
col = math.max(0, cursor_col - 1)
end

notification.delete_all()
M.status_buffer:close()

local relpath = vim.fn.fnamemodify(path, ":.")

if not vim.o.hidden and vim.bo.buftype == "" and not vim.bo.readonly and vim.fn.bufname() ~= "" then
vim.cmd("update")
end

vim.cmd("e " .. relpath)

if hunk then
local line_offset = cursor_row - hunk.first
local path = vim.fn.fnameescape(vim.fn.fnamemodify(item.absolute_path, ":~:."))
vim.cmd(string.format("edit %s", path))

local row = hunk.disk_from + line_offset - 1
for i = 1, line_offset do
if string.sub(hunk.lines[i], 1, 1) == "-" then
row = row - 1
end
end
-- adjust for diff sign column
local col = cursor_col == 0 and 0 or cursor_col - 1
if row and col then
vim.api.nvim_win_set_cursor(0, { row, col })
end
end
Expand Down

0 comments on commit 3be9268

Please sign in to comment.