feat(sidebar): support files outside of the current working directory. (#1065)
This commit is contained in:
parent
24641d8264
commit
f401983737
@ -36,6 +36,7 @@ function FileSelector:add_selected_file(filepath)
|
||||
if not filepath or filepath == "" then return end
|
||||
|
||||
local uniform_path = Utils.uniform_path(filepath)
|
||||
|
||||
-- Avoid duplicates
|
||||
if not vim.tbl_contains(self.selected_filepaths, uniform_path) then
|
||||
table.insert(self.selected_filepaths, uniform_path)
|
||||
@ -273,11 +274,16 @@ end
|
||||
function FileSelector:get_selected_files_contents()
|
||||
local contents = {}
|
||||
for _, file_path in ipairs(self.selected_filepaths) do
|
||||
local file = io.open(file_path, "r")
|
||||
local file, open_err = io.open(file_path, "r")
|
||||
|
||||
if open_err then Utils.debug("error reading file:", open_err) end
|
||||
|
||||
if file then
|
||||
local content = file:read("*all")
|
||||
local content, read_err = file:read("*all")
|
||||
file:close()
|
||||
|
||||
if read_err then Utils.debug("failed to read:", file_path, read_err) end
|
||||
|
||||
-- Detect the file type
|
||||
local filetype = vim.filetype.match({ filename = file_path, contents = contents }) or "unknown"
|
||||
|
||||
|
@ -1148,8 +1148,13 @@ function Sidebar:initialize()
|
||||
|
||||
if not self.code.bufnr or not api.nvim_buf_is_valid(self.code.bufnr) then return self end
|
||||
|
||||
local buf_path = api.nvim_buf_get_name(self.code.bufnr)
|
||||
-- if the filepath is outside of the current working directory then we want the absolute path
|
||||
local file_path = Utils.file.is_in_cwd(buf_path) and Utils.relative_path(buf_path) or buf_path
|
||||
Utils.debug("Sidebar:initialize adding buffer to file selector", buf_path)
|
||||
|
||||
self.file_selector:reset()
|
||||
self.file_selector:add_selected_file(Utils.relative_path(api.nvim_buf_get_name(self.code.bufnr)))
|
||||
self.file_selector:add_selected_file(file_path)
|
||||
|
||||
return self
|
||||
end
|
||||
|
@ -39,6 +39,15 @@ function M.exists(filepath)
|
||||
return stat ~= nil
|
||||
end
|
||||
|
||||
function M.is_in_cwd(filepath)
|
||||
local cwd = vim.fn.getcwd()
|
||||
-- Make both paths absolute for comparison
|
||||
local abs_filepath = vim.fn.fnamemodify(filepath, ":p")
|
||||
local abs_cwd = vim.fn.fnamemodify(cwd, ":p")
|
||||
-- Check if filepath starts with cwd
|
||||
return abs_filepath:sub(1, #abs_cwd) == abs_cwd
|
||||
end
|
||||
|
||||
function M.get_file_icon(filepath)
|
||||
local filetype = Filetype.detect(filepath, {}) or "unknown"
|
||||
---@type string
|
||||
|
@ -826,6 +826,7 @@ function M.get_current_selection_diagnostics(bufnr, selection)
|
||||
end
|
||||
|
||||
function M.uniform_path(path)
|
||||
if not M.file.is_in_cwd(path) then return path end
|
||||
local project_root = M.get_project_root()
|
||||
local abs_path = Path:new(project_root):joinpath(path):absolute()
|
||||
local relative_path = Path:new(abs_path):make_relative(project_root)
|
||||
|
Loading…
x
Reference in New Issue
Block a user