Skip to content

Commit a9e5755

Browse files
committed
feat(lsp): regex_filter improvements (#1210)
1 parent 56ae880 commit a9e5755

2 files changed

Lines changed: 36 additions & 6 deletions

File tree

lua/fzf-lua/core.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,13 @@ M.set_header = function(opts, hdr_tbl)
875875
hdr_txt_str = "Regex filter: ",
876876
hdr_txt_col = opts.hls.header_text,
877877
val = function()
878-
return opts.regex_filter and #opts.regex_filter > 0 and opts.regex_filter
878+
if type(opts.regex_filter) == "string" then
879+
return opts.regex_filter
880+
elseif type(opts.regex_filter) == "table" and type(opts.regex_filter[1]) == "string" then
881+
return string.format("%s%s", opts.regex_filter.exclude and "not " or "", opts.regex_filter[1])
882+
elseif type(opts.regex_filter) == "function" then
883+
return "<function>"
884+
end
879885
end,
880886
},
881887
actions = {

lua/fzf-lua/providers/lsp.lua

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ local jump_to_location = function(opts, result, enc)
8888
return vim.lsp.util.jump_to_location(result, enc)
8989
end
9090

91+
local regex_filter_fn = function(regex_filter)
92+
if type(regex_filter) == "string" then
93+
regex_filter = { regex_filter }
94+
end
95+
if type(regex_filter) == "function" then
96+
return regex_filter
97+
end
98+
if type(regex_filter) == "table" and type(regex_filter[1]) == "string" then
99+
return function(item, _)
100+
if not item.text then return true end
101+
local is_match = item.text:match(regex_filter[1]) ~= nil
102+
if regex_filter.exclude then
103+
return not is_match
104+
else
105+
return is_match
106+
end
107+
end
108+
end
109+
return false
110+
end
111+
91112
local function location_handler(opts, cb, _, result, ctx, _)
92113
local encoding = vim.lsp.get_client_by_id(ctx.client_id).offset_encoding
93114
result = utils.tbl_islist(result) and result or { result }
@@ -101,12 +122,15 @@ local function location_handler(opts, cb, _, result, ctx, _)
101122
end, result)
102123
end
103124
local items = {}
125+
if opts.regex_filter and opts._regex_filter_fn == nil then
126+
opts._regex_filter_fn = regex_filter_fn(opts.regex_filter)
127+
end
104128
-- Although `make_entry.file` filters for `cwd_only` we filter
105129
-- here to accurately determine `jump_to_single_result` (#980)
106130
result = vim.tbl_filter(function(x)
107131
local item = vim.lsp.util.locations_to_items({ x }, encoding)[1]
108132
if (opts.cwd_only and not path.is_relative_to(item.filename, opts.cwd)) or
109-
(opts.regex_filter and not item.text:match(opts.regex_filter)) then
133+
(opts._regex_filter_fn and not opts._regex_filter_fn(item, core.CTX())) then
110134
return false
111135
end
112136
table.insert(items, item)
@@ -116,9 +140,6 @@ local function location_handler(opts, cb, _, result, ctx, _)
116140
if opts.jump_to_single_result and #result == 1 then
117141
jump_to_location(opts, result[1], encoding)
118142
end
119-
if opts.filter and type(opts.filter) == "function" then
120-
items = opts.filter(items)
121-
end
122143
for _, entry in ipairs(items) do
123144
if not opts.current_buffer_only or core.CTX().bname == entry.filename then
124145
entry = make_entry.lcol(entry, opts)
@@ -193,9 +214,12 @@ local function symbol_handler(opts, cb, _, result, _, _)
193214
else
194215
items = vim.lsp.util.symbols_to_items(result, core.CTX().bufnr)
195216
end
217+
if opts.regex_filter and opts._regex_filter_fn == nil then
218+
opts._regex_filter_fn = regex_filter_fn(opts.regex_filter)
219+
end
196220
for _, entry in ipairs(items) do
197221
if (not opts.current_buffer_only or core.CTX().bname == entry.filename) and
198-
(not opts.regex_filter or entry.text:match(opts.regex_filter)) then
222+
(not opts._regex_filter_fn or opts._regex_filter_fn(entry, core.CTX())) then
199223
local mbicon_align = 0
200224
if opts.fn_reload and type(opts.query) == "string" and #opts.query > 0 then
201225
-- highlight exact matches with `live_workspace_symbols` (#1028)

0 commit comments

Comments
 (0)