fix lua path matching again

This commit is contained in:
kiyan42 2020-06-01 16:06:23 +02:00
parent 5b69702a52
commit df0c522a81
3 changed files with 12 additions and 10 deletions

View File

@ -1,3 +1,4 @@
local utils = require'lib.utils'
local M = {} local M = {}
local roots = {} local roots = {}
@ -26,10 +27,6 @@ function M.reload_roots()
end end
end end
local function path_to_matching_str(path)
return path:gsub('(%-)', '(%%-)'):gsub('(%.)', '(%%.)')
end
local function get_git_root(path) local function get_git_root(path)
if roots[path] then if roots[path] then
return path, roots[path] return path, roots[path]
@ -37,7 +34,7 @@ local function get_git_root(path)
for name, status in pairs(roots) do for name, status in pairs(roots) do
if status ~= not_git then if status ~= not_git then
if path:match(path_to_matching_str(name)) then if path:match(utils.path_to_matching_str(name)) then
return name, status return name, status
end end
end end
@ -65,7 +62,7 @@ function M.update_status(entries, cwd)
return return
end end
local matching_cwd = path_to_matching_str(git_root..'/') local matching_cwd = utils.path_to_matching_str(git_root..'/')
for _, node in pairs(entries) do for _, node in pairs(entries) do
local relpath = node.absolute_path:gsub(matching_cwd, '') local relpath = node.absolute_path:gsub(matching_cwd, '')
if node.entries ~= nil then if node.entries ~= nil then
@ -77,7 +74,7 @@ function M.update_status(entries, cwd)
if status then if status then
node.git_status = status node.git_status = status
elseif node.entries ~= nil then elseif node.entries ~= nil then
local matcher = '^'..path_to_matching_str(relpath) local matcher = '^'..utils.path_to_matching_str(relpath)
for key, _ in pairs(git_status) do for key, _ in pairs(git_status) do
if key:match(matcher) then if key:match(matcher) then
node.git_status = 'dirty' node.git_status = 'dirty'

View File

@ -7,9 +7,7 @@ local luv = vim.loop
local M = {} local M = {}
local function path_to_matching_str(path) local path_to_matching_str = require'lib.utils'.path_to_matching_str
return path:gsub('(%-)', '(%%-)'):gsub('(%.)', '(%%.)')
end
local function dir_new(cwd, name) local function dir_new(cwd, name)
local absolute_path = cwd..'/'..name local absolute_path = cwd..'/'..name

7
lua/lib/utils.lua Normal file
View File

@ -0,0 +1,7 @@
local M = {}
function M.path_to_matching_str(path)
return path:gsub('(%-)', '(%%-)'):gsub('(%.)', '(%%.)'):gsub('(%_)', '(%%_)')
end
return M