Skip to content

Commit cbaf282

Browse files
committed
fixup! feat(files): make file system actions LSP aware
refactor: Simplify `lsp_fs_hook` code
1 parent 7409c19 commit cbaf282

1 file changed

Lines changed: 23 additions & 38 deletions

File tree

lua/mini/files.lua

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,61 +2811,46 @@ H.lsp_fs_hook = function(method, diffs)
28112811
end
28122812
end
28132813

2814-
local params = { files = files }
28152814
local full_method = 'workspace/' .. method .. 'Files'
28162815
local clients = vim.lsp.get_clients({ method = full_method })
28172816

28182817
-- TODO(TheLeoP): configurable timeout
28192818
local timeout = 1000
28202819
for _, client in ipairs(clients) do
2821-
local filters = client.server_capabilities.workspace.fileOperations[method].filters --[=[@as lsp.FileOperationFilter[]]=]
2822-
local grouped_matching_functions = {}
2823-
for _, filter in ipairs(filters) do
2824-
local ignore_case = filter.pattern.options and filter.pattern.options.ignoreCase
2825-
local glob = filter.pattern.glob
2820+
local filters, filter_configs = {}, client.server_capabilities.workspace.fileOperations[method].filters --[=[@as lsp.FileOperationFilter[]]=]
2821+
for _, fc in ipairs(filter_configs) do
2822+
local ignore_case = fc.pattern.options and fc.pattern.options.ignoreCase
2823+
local glob, matches, scheme = fc.pattern.glob, fc.pattern.matches, fc.scheme
2824+
28262825
if ignore_case then glob = vim.fn.tolower(glob) end
28272826

2828-
local matching_functions = {}
2829-
table.insert(matching_functions, function(uri)
2830-
local path = vim.uri_to_fname(uri)
2831-
return vim.glob.to_lpeg(glob):match(path) ~= nil
2832-
end)
2833-
if filter.scheme then
2834-
table.insert(matching_functions, function(uri) return uri:find('^' .. filter.scheme .. ':') ~= nil end)
2835-
end
2836-
if filter.pattern.matches then
2837-
table.insert(matching_functions, function(uri)
2838-
local path = vim.uri_to_fname(uri)
2839-
local type = H.fs_get_type(path)
2840-
if filter.pattern.matches == 'folder' then return type == 'directory' end
2841-
if filter.pattern.matches == 'file' then return type == 'file' end
2827+
local filter_funcs = {}
2828+
table.insert(filter_funcs, function(uri) return vim.glob.to_lpeg(glob):match((vim.uri_to_fname(uri))) ~= nil end)
2829+
if scheme then table.insert(filter_funcs, function(uri) return uri:find('^' .. scheme .. ':') ~= nil end) end
2830+
if matches then
2831+
table.insert(filter_funcs, function(uri)
2832+
local type = H.fs_get_type((vim.uri_to_fname(uri))):gsub('directory', 'folder')
2833+
return matches == type
28422834
end)
28432835
end
2844-
table.insert(grouped_matching_functions, matching_functions)
2836+
table.insert(filters, filter_funcs)
28452837
end
28462838

2847-
local filtered_params = {
2848-
files = {},
2849-
}
2850-
for _, file in ipairs(params.files) do
2839+
local filtered_files = {}
2840+
for _, file in ipairs(files) do
28512841
local uri = file.uri or file.oldUri
2852-
local matches_any_filter = false
2853-
for _, matching_functions in ipairs(grouped_matching_functions) do
2854-
if not matches_any_filter then
2855-
local matches_current_filter = true
2856-
for _, matching_function in ipairs(matching_functions) do
2857-
matches_current_filter = matches_current_filter and matching_function(uri)
2858-
end
2859-
matches_any_filter = matches_any_filter or matches_current_filter
2860-
end
2861-
end
2862-
if matches_any_filter then table.insert(filtered_params.files, file) end
2842+
2843+
local ok = vim.iter(filters):any(function(f)
2844+
return vim.iter(f):all(function(func) return func(uri) end)
2845+
end)
2846+
if ok then table.insert(filtered_files, file) end
28632847
end
2848+
local params = { files = filtered_files }
28642849

28652850
if method:sub(1, 3) == 'did' then
2866-
client:notify(full_method, filtered_params)
2851+
client:notify(full_method, params)
28672852
else
2868-
local response, err = client:request_sync(full_method, filtered_params, timeout)
2853+
local response, err = client:request_sync(full_method, params, timeout)
28692854
-- TODO(TheLeoP): show error to user?
28702855
if response and response.result then
28712856
vim.lsp.util.apply_workspace_edit(response.result, client.offset_encoding)

0 commit comments

Comments
 (0)