diff --git a/lua/lib/format.lua b/lua/lib/format.lua index ef40d8e1..6933cec7 100644 --- a/lua/lib/format.lua +++ b/lua/lib/format.lua @@ -25,7 +25,7 @@ end local function create_matcher(arr) return function(name) for _, n in pairs(arr) do - if string.match(name, n) then return true end + if name:match(n) then return true end end return false end @@ -142,7 +142,7 @@ local function highlight_line(buffer) elseif config.SHOW_FILE_ICON then 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 highlight('LuaTree' .. v, line, 0, text_start) break diff --git a/lua/lib/fs.lua b/lua/lib/fs.lua index c2c6d6e0..50163bc3 100644 --- a/lua/lib/fs.lua +++ b/lua/lib/fs.lua @@ -17,15 +17,6 @@ local function link_to(path) return luv.fs_readlink(path) or '' 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) print_err(api.nvim_call_function('system', { v })) end diff --git a/lua/lib/fs_update.lua b/lua/lib/fs_update.lua index 9d2c99b7..255f19ad 100644 --- a/lua/lib/fs_update.lua +++ b/lua/lib/fs_update.lua @@ -3,6 +3,7 @@ local api = vim.api local update_view = require 'lib/winutils'.update_view local refresh_tree = require 'lib/state'.refresh_tree local refresh_git = require 'lib/git'.refresh_git +local utils = require'lib/utils' local fs = require 'lib/fs' local rm = fs.rm @@ -23,14 +24,14 @@ local function create_file(path) local new_file = input("Create file: " .. path) local file = nil - if not string.match(new_file, '.*/$') then - file = string.reverse(string.gsub(string.reverse(new_file), '/.*$', '')) - new_file = string.gsub(new_file, '[^/]*$', '') + if not new_file:match('.*/$') then + file = new_file:reverse():gsub('/.*$', ''):reverse() + new_file = new_file:gsub('[^/]*$', '') end local folders = "" 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 folders = folders .. p .. '/' end diff --git a/lua/lib/git.lua b/lua/lib/git.lua index 7ca64c0b..93073004 100644 --- a/lua/lib/git.lua +++ b/lua/lib/git.lua @@ -1,11 +1,12 @@ 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 systemlist(v) return vim.api.nvim_call_function('systemlist', { v }) end local function is_git_repo() local is_git = system('git rev-parse') - return string.match(is_git, 'fatal') == nil + return is_git:match('fatal') == nil end local IS_GIT_REPO = is_git_repo() @@ -30,17 +31,17 @@ end local function is_folder_dirty(relpath) for _, status in pairs(GIT_STATUS) do - local match_path = relpath:gsub('(%-)', '%%-') - if string.match(status, match_path) ~= nil then return true end + if status:match(utils.path_to_matching_str(relpath)) ~= nil then + return true + end end end local function create_git_checker(pattern) return function(relpath) for _, status in pairs(GIT_STATUS) do - local match_path = relpath:gsub('(%-)', '%%-') - local ret = string.match(status, '^.. .*' .. match_path) - if ret ~= nil and string.match(ret, pattern) ~= nil then return true end + local ret = status:match('^.. .*' .. utils.path_to_matching_str(relpath)) + if ret ~= nil and ret:match(pattern) ~= nil then return true end end return false end diff --git a/lua/lib/state.lua b/lua/lib/state.lua index 9815f9d7..2767ec90 100644 --- a/lua/lib/state.lua +++ b/lua/lib/state.lua @@ -1,4 +1,5 @@ local api = vim.api +local utils = require'lib.utils' 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 tree = {} - if not string.find(path, '^.*/$') then path = path .. '/' end - if not string.find(relpath, '^.*/$') and depth > 0 then relpath = relpath .. '/' end + if not path:find('^.*/$') then path = path .. '/' end + if not relpath:find('^.*/$') and depth > 0 then relpath = relpath .. '/' end for i, name in pairs(dirs) do local full_path = path..name @@ -132,7 +133,7 @@ local function find_file(path) local tree_copy = clone(Tree) 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 Tree = clone(tree_copy) return i diff --git a/lua/lib/utils.lua b/lua/lib/utils.lua new file mode 100644 index 00000000..735fd01a --- /dev/null +++ b/lua/lib/utils.lua @@ -0,0 +1,7 @@ +local M = {} + +function M.path_to_matching_str(path) + return path:gsub('(%-)', '%%-') +end + +return M