Browse Source

Neovim: Attempt at Windows path support

wayland
Riyyi 4 days ago
parent
commit
7bc3ec6d98
  1. 19
      .config/nvim/lua/core/functions.lua

19
.config/nvim/lua/core/functions.lua

@ -6,13 +6,20 @@ M.is_buffer_a_file = function()
return buffer_name ~= "" and vim.fn.filereadable(buffer_name) == 1 return buffer_name ~= "" and vim.fn.filereadable(buffer_name) == 1
end end
M.normalize_path = function(path)
if not path then return nil end
return vim.loop.os_uname().sysname == "Windows_NT"
and path:gsub("\\", "/")
or path
end
M.get_file_path = function() M.get_file_path = function()
if not M.is_buffer_a_file() then if not M.is_buffer_a_file() then
return nil return nil
end end
local file_path = vim.fn.expand("%:p") local file_path = vim.fn.expand("%:p")
return vim.fn.fnamemodify(file_path, ":h") .. "/" return M.normalize_path(vim.fn.fnamemodify(file_path, ":h")) .. "/"
end end
M.get_netrw_path = function() -- b:netrw_curdir M.get_netrw_path = function() -- b:netrw_curdir
@ -20,11 +27,11 @@ M.get_netrw_path = function() -- b:netrw_curdir
return nil return nil
end end
return vim.fn.fnamemodify(vim.fn.bufname(), ":p") return M.normalize_path(vim.fn.fnamemodify(vim.fn.bufname(), ":p"))
end end
M.get_current_directory = function() M.get_current_directory = function()
return M.get_file_path() or M.get_netrw_path() or vim.fn.getcwd() return M.get_file_path() or M.get_netrw_path() or M.normalize_path(vim.fn.getcwd())
end end
M.find_project_root = function() M.find_project_root = function()
@ -34,15 +41,15 @@ M.find_project_root = function()
while directory ~= "/" do while directory ~= "/" do
local git_path = vim.loop.fs_stat(directory .. "/.git") local git_path = vim.loop.fs_stat(directory .. "/.git")
if git_path then if git_path then
return directory:gsub("/$", "") -- remove trailing slash return M.normalize_path(directory:gsub("/$", "")) -- remove trailing slash
end end
local project_file = vim.loop.fs_stat(directory .. "/.project") local project_file = vim.loop.fs_stat(directory .. "/.project")
if project_file and project_file.type == "file" then if project_file and project_file.type == "file" then
return directory:gsub("/$", "") -- remove trailing slash return M.normalize_path(directory:gsub("/$", "")) -- remove trailing slash
end end
directory = vim.fn.fnamemodify(directory, ":h") directory = M.normalize_path(vim.fn.fnamemodify(directory, ":h"))
end end
return nil, current_directory return nil, current_directory

Loading…
Cancel
Save