fix: refacto path matchings throughout the code, change the way we call string methods

This commit is contained in:
kiyan42
2020-04-25 15:11:14 +02:00
parent 7fbcfa531c
commit 60adbfbdf3
6 changed files with 25 additions and 24 deletions

View File

@@ -25,7 +25,7 @@ end
local function create_matcher(arr) local function create_matcher(arr)
return function(name) return function(name)
for _, n in pairs(arr) do for _, n in pairs(arr) do
if string.match(name, n) then return true end if name:match(n) then return true end
end end
return false return false
end end
@@ -142,7 +142,7 @@ local function highlight_line(buffer)
elseif config.SHOW_FILE_ICON then elseif config.SHOW_FILE_ICON then
for k, v in pairs(HIGHLIGHT_ICON_GROUPS) do for k, v in pairs(HIGHLIGHT_ICON_GROUPS) do
if string.match(node.name, k) ~= nil then if node.name:match(k) ~= nil then
text_start = text_start + 4 text_start = text_start + 4
highlight('LuaTree' .. v, line, 0, text_start) highlight('LuaTree' .. v, line, 0, text_start)
break break

View File

@@ -17,15 +17,6 @@ local function link_to(path)
return luv.fs_readlink(path) or '' return luv.fs_readlink(path) or ''
end end
local function print_err(err)
if err ~= nil then
api.nvim_command('echohl ErrorMsg')
-- remove the \n with string.sub
api.nvim_command('echomsg "'..string.sub(err, 0, -2)..'"')
api.nvim_command('echohl None')
end
end
local function system(v) local function system(v)
print_err(api.nvim_call_function('system', { v })) print_err(api.nvim_call_function('system', { v }))
end end

View File

@@ -3,6 +3,7 @@ local api = vim.api
local update_view = require 'lib/winutils'.update_view local update_view = require 'lib/winutils'.update_view
local refresh_tree = require 'lib/state'.refresh_tree local refresh_tree = require 'lib/state'.refresh_tree
local refresh_git = require 'lib/git'.refresh_git local refresh_git = require 'lib/git'.refresh_git
local utils = require'lib/utils'
local fs = require 'lib/fs' local fs = require 'lib/fs'
local rm = fs.rm local rm = fs.rm
@@ -23,14 +24,14 @@ local function create_file(path)
local new_file = input("Create file: " .. path) local new_file = input("Create file: " .. path)
local file = nil local file = nil
if not string.match(new_file, '.*/$') then if not new_file:match('.*/$') then
file = string.reverse(string.gsub(string.reverse(new_file), '/.*$', '')) file = new_file:reverse():gsub('/.*$', ''):reverse()
new_file = string.gsub(new_file, '[^/]*$', '') new_file = new_file:gsub('[^/]*$', '')
end end
local folders = "" local folders = ""
if #new_file ~= 0 then if #new_file ~= 0 then
for p in string.gmatch(new_file, '[^/]*') do for p in new_file:gmatch('[^/]*') do
if p and p ~= "" then if p and p ~= "" then
folders = folders .. p .. '/' folders = folders .. p .. '/'
end end

View File

@@ -1,11 +1,12 @@
local config = require 'lib/config' local config = require 'lib/config'
local utils = require'lib.utils'
local function system(v) return vim.api.nvim_call_function('system', { v }) end local function system(v) return vim.api.nvim_call_function('system', { v }) end
local function systemlist(v) return vim.api.nvim_call_function('systemlist', { v }) end local function systemlist(v) return vim.api.nvim_call_function('systemlist', { v }) end
local function is_git_repo() local function is_git_repo()
local is_git = system('git rev-parse') local is_git = system('git rev-parse')
return string.match(is_git, 'fatal') == nil return is_git:match('fatal') == nil
end end
local IS_GIT_REPO = is_git_repo() local IS_GIT_REPO = is_git_repo()
@@ -30,17 +31,17 @@ end
local function is_folder_dirty(relpath) local function is_folder_dirty(relpath)
for _, status in pairs(GIT_STATUS) do for _, status in pairs(GIT_STATUS) do
local match_path = relpath:gsub('(%-)', '%%-') if status:match(utils.path_to_matching_str(relpath)) ~= nil then
if string.match(status, match_path) ~= nil then return true end return true
end
end end
end end
local function create_git_checker(pattern) local function create_git_checker(pattern)
return function(relpath) return function(relpath)
for _, status in pairs(GIT_STATUS) do for _, status in pairs(GIT_STATUS) do
local match_path = relpath:gsub('(%-)', '%%-') local ret = status:match('^.. .*' .. utils.path_to_matching_str(relpath))
local ret = string.match(status, '^.. .*' .. match_path) if ret ~= nil and ret:match(pattern) ~= nil then return true end
if ret ~= nil and string.match(ret, pattern) ~= nil then return true end
end end
return false return false
end end

View File

@@ -1,4 +1,5 @@
local api = vim.api local api = vim.api
local utils = require'lib.utils'
local function syslist(v) return api.nvim_call_function('systemlist', { v }) end local function syslist(v) return api.nvim_call_function('systemlist', { v }) end
@@ -52,8 +53,8 @@ end
local function create_nodes(path, relpath, depth, dirs) local function create_nodes(path, relpath, depth, dirs)
local tree = {} local tree = {}
if not string.find(path, '^.*/$') then path = path .. '/' end if not path:find('^.*/$') then path = path .. '/' end
if not string.find(relpath, '^.*/$') and depth > 0 then relpath = relpath .. '/' end if not relpath:find('^.*/$') and depth > 0 then relpath = relpath .. '/' end
for i, name in pairs(dirs) do for i, name in pairs(dirs) do
local full_path = path..name local full_path = path..name
@@ -132,7 +133,7 @@ local function find_file(path)
local tree_copy = clone(Tree) local tree_copy = clone(Tree)
for i, node in pairs(tree_copy) do for i, node in pairs(tree_copy) do
if node.relpath and string.match(relpath, node.relpath) then if node.relpath and relpath:find(utils.path_to_matching_str(node.relpath)) then
if node.relpath == relpath then if node.relpath == relpath then
Tree = clone(tree_copy) Tree = clone(tree_copy)
return i return i

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('(%-)', '%%-')
end
return M