Skip to content

Commit

Permalink
Added function to get the correct file separator for both windows and…
Browse files Browse the repository at this point in the history
… linux.
  • Loading branch information
José Morano committed May 22, 2024
1 parent fd36131 commit 2b0e03e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lua/buffer_manager/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ local Path = require("plenary.path")
local M = {}



local function separator()
if vim.fn.has('win32') == 1 then
return "\\"
else
return "/"
end
end



function M.project_key()
return vim.loop.cwd()
end
Expand All @@ -15,7 +26,8 @@ function M.normalize_path(item)
end

function M.get_file_name(file)
return file:match("[^/\\]*$")
-- return file:match("[^/\\]*$")
return file:match("[^" .. separator() .. "]*$")
end


Expand All @@ -37,15 +49,16 @@ function M.get_short_file_name(file, current_short_fns)
local folders = {}
-- Convert file to string
local file_str = tostring(file)
for folder in string.gmatch(file_str, "([^/]+)") do
-- for folder in string.gmatch(file_str, "([^/]+)") do
for folder in string.gmatch(file_str, "([^" .. separator() .. "]+)") do
-- insert firts char only
table.insert(folders, folder)
end
-- File to string
file = tostring(file)
-- Count the number of slashes in the relative file path
local slash_count = 0
for _ in string.gmatch(file, "/") do
for _ in string.gmatch(file, separator()) do
slash_count = slash_count + 1
end
if slash_count == 0 then
Expand Down

0 comments on commit 2b0e03e

Please sign in to comment.