Skip to content

Commit 60adbfb

Browse files
committed
fix: refacto path matchings throughout the code, change the way we call string methods
1 parent 7fbcfa5 commit 60adbfb

File tree

6 files changed

+25
-24
lines changed

6 files changed

+25
-24
lines changed

lua/lib/format.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ end
2525
local function create_matcher(arr)
2626
return function(name)
2727
for _, n in pairs(arr) do
28-
if string.match(name, n) then return true end
28+
if name:match(n) then return true end
2929
end
3030
return false
3131
end
@@ -142,7 +142,7 @@ local function highlight_line(buffer)
142142

143143
elseif config.SHOW_FILE_ICON then
144144
for k, v in pairs(HIGHLIGHT_ICON_GROUPS) do
145-
if string.match(node.name, k) ~= nil then
145+
if node.name:match(k) ~= nil then
146146
text_start = text_start + 4
147147
highlight('LuaTree' .. v, line, 0, text_start)
148148
break

lua/lib/fs.lua

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ local function link_to(path)
1717
return luv.fs_readlink(path) or ''
1818
end
1919

20-
local function print_err(err)
21-
if err ~= nil then
22-
api.nvim_command('echohl ErrorMsg')
23-
-- remove the \n with string.sub
24-
api.nvim_command('echomsg "'..string.sub(err, 0, -2)..'"')
25-
api.nvim_command('echohl None')
26-
end
27-
end
28-
2920
local function system(v)
3021
print_err(api.nvim_call_function('system', { v }))
3122
end

lua/lib/fs_update.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local api = vim.api
33
local update_view = require 'lib/winutils'.update_view
44
local refresh_tree = require 'lib/state'.refresh_tree
55
local refresh_git = require 'lib/git'.refresh_git
6+
local utils = require'lib/utils'
67

78
local fs = require 'lib/fs'
89
local rm = fs.rm
@@ -23,14 +24,14 @@ local function create_file(path)
2324
local new_file = input("Create file: " .. path)
2425

2526
local file = nil
26-
if not string.match(new_file, '.*/$') then
27-
file = string.reverse(string.gsub(string.reverse(new_file), '/.*$', ''))
28-
new_file = string.gsub(new_file, '[^/]*$', '')
27+
if not new_file:match('.*/$') then
28+
file = new_file:reverse():gsub('/.*$', ''):reverse()
29+
new_file = new_file:gsub('[^/]*$', '')
2930
end
3031

3132
local folders = ""
3233
if #new_file ~= 0 then
33-
for p in string.gmatch(new_file, '[^/]*') do
34+
for p in new_file:gmatch('[^/]*') do
3435
if p and p ~= "" then
3536
folders = folders .. p .. '/'
3637
end

lua/lib/git.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
local config = require 'lib/config'
2+
local utils = require'lib.utils'
23

34
local function system(v) return vim.api.nvim_call_function('system', { v }) end
45
local function systemlist(v) return vim.api.nvim_call_function('systemlist', { v }) end
56

67
local function is_git_repo()
78
local is_git = system('git rev-parse')
8-
return string.match(is_git, 'fatal') == nil
9+
return is_git:match('fatal') == nil
910
end
1011

1112
local IS_GIT_REPO = is_git_repo()
@@ -30,17 +31,17 @@ end
3031

3132
local function is_folder_dirty(relpath)
3233
for _, status in pairs(GIT_STATUS) do
33-
local match_path = relpath:gsub('(%-)', '%%-')
34-
if string.match(status, match_path) ~= nil then return true end
34+
if status:match(utils.path_to_matching_str(relpath)) ~= nil then
35+
return true
36+
end
3537
end
3638
end
3739

3840
local function create_git_checker(pattern)
3941
return function(relpath)
4042
for _, status in pairs(GIT_STATUS) do
41-
local match_path = relpath:gsub('(%-)', '%%-')
42-
local ret = string.match(status, '^.. .*' .. match_path)
43-
if ret ~= nil and string.match(ret, pattern) ~= nil then return true end
43+
local ret = status:match('^.. .*' .. utils.path_to_matching_str(relpath))
44+
if ret ~= nil and ret:match(pattern) ~= nil then return true end
4445
end
4546
return false
4647
end

lua/lib/state.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local api = vim.api
2+
local utils = require'lib.utils'
23

34
local function syslist(v) return api.nvim_call_function('systemlist', { v }) end
45

@@ -52,8 +53,8 @@ end
5253
local function create_nodes(path, relpath, depth, dirs)
5354
local tree = {}
5455

55-
if not string.find(path, '^.*/$') then path = path .. '/' end
56-
if not string.find(relpath, '^.*/$') and depth > 0 then relpath = relpath .. '/' end
56+
if not path:find('^.*/$') then path = path .. '/' end
57+
if not relpath:find('^.*/$') and depth > 0 then relpath = relpath .. '/' end
5758

5859
for i, name in pairs(dirs) do
5960
local full_path = path..name
@@ -132,7 +133,7 @@ local function find_file(path)
132133
local tree_copy = clone(Tree)
133134

134135
for i, node in pairs(tree_copy) do
135-
if node.relpath and string.match(relpath, node.relpath) then
136+
if node.relpath and relpath:find(utils.path_to_matching_str(node.relpath)) then
136137
if node.relpath == relpath then
137138
Tree = clone(tree_copy)
138139
return i

lua/lib/utils.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local M = {}
2+
3+
function M.path_to_matching_str(path)
4+
return path:gsub('(%-)', '%%-')
5+
end
6+
7+
return M

0 commit comments

Comments
 (0)