Skip to content

Commit cfcb1d4

Browse files
authored
feat(variant): add resume last search variant for 'files' (#408)
feat(variant): add resume last search variant for 'live_grep' (#408)
1 parent 1e21190 commit cfcb1d4

File tree

9 files changed

+233
-57
lines changed

9 files changed

+233
-57
lines changed

README.md

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ https://github.com/linrongbin16/fzfx.nvim/assets/6496887/47b03150-14e3-479a-b1af
5757
- Search by visual select.
5858
- Search by cursor word.
5959
- Search by yank text.
60+
- Search by previous search, e.g. resume last search.
6061
- Multiple data sources to avoid restart search flow:
6162
- Exclude or include the hidden/ignored files when searching files.
6263
- Local or remote branches when searching git branches.
@@ -253,6 +254,7 @@ Commands are named following below rules:
253254
- The visual select variant is named with `V` suffix.
254255
- The cursor word variant is named with `W` suffix.
255256
- The yank text variant is named with `P` suffix (just like press the `p` key).
257+
- The resume last search variant is named with `R` suffix.
256258
- The only current buffer variant is named with `B` suffix.
257259

258260
> Note: command names can be configured, see [Configuration](#-configuration).
@@ -291,8 +293,8 @@ Commands are named following below rules:
291293
<tr>
292294
<td>FzfxFiles(U)</td>
293295
<td>N</td>
294-
<td rowspan="4">Yes</td>
295-
<td rowspan="4">Yes</td>
296+
<td rowspan="5">Yes</td>
297+
<td rowspan="5">Yes</td>
296298
</tr>
297299
<tr>
298300
<td>FzfxFiles(U)V</td>
@@ -306,6 +308,10 @@ Commands are named following below rules:
306308
<td>FzfxFiles(U)P</td>
307309
<td>N</td>
308310
</tr>
311+
<tr>
312+
<td>FzfxFiles(U)R</td>
313+
<td>N</td>
314+
</tr>
309315
</tbody>
310316
</table>
311317

@@ -364,8 +370,8 @@ Commands are named following below rules:
364370
<tr>
365371
<td>FzfxLiveGrep(B/U)</td>
366372
<td>N</td>
367-
<td rowspan="4">Yes</td>
368-
<td rowspan="4">Yes</td>
373+
<td rowspan="5">Yes</td>
374+
<td rowspan="5">Yes</td>
369375
</tr>
370376
<tr>
371377
<td>FzfxLiveGrep(B/U)V</td>
@@ -379,6 +385,10 @@ Commands are named following below rules:
379385
<td>FzfxLiveGrep(B/U)P</td>
380386
<td>N</td>
381387
</tr>
388+
<tr>
389+
<td>FzfxLiveGrep(B/U)R</td>
390+
<td>N</td>
391+
</tr>
382392
</tbody>
383393
</table>
384394

@@ -763,6 +773,8 @@ xnoremap <space>f :\<C-U>FzfxFilesV<CR>
763773
nnoremap <space>wf :\<C-U>FzfxFilesW<CR>
764774
" by yank text
765775
nnoremap <space>pf :\<C-U>FzfxFilesP<CR>
776+
" by resume
777+
nnoremap <space>rf :\<C-U>FzfxFilesR<CR>
766778
767779
" ======== live grep ========
768780
@@ -774,6 +786,8 @@ xnoremap <space>l :\<C-U>FzfxLiveGrepV<CR>
774786
nnoremap <space>wl :\<C-U>FzfxLiveGrepW<CR>
775787
" by yank text
776788
nnoremap <space>pl :\<C-U>FzfxLiveGrepP<CR>
789+
" by resume
790+
nnoremap <space>rl :\<C-U>FzfxLiveGrepR<CR>
777791
778792
" ======== buffers ========
779793
@@ -809,12 +823,6 @@ nnoremap <space>gb :\<C-U>FzfxGBlame<CR>
809823
810824
" lsp diagnostics
811825
nnoremap <space>dg :\<C-U>FzfxLspDiagnostics<CR>
812-
" by visual select
813-
xnoremap <space>dg :\<C-U>FzfxLspDiagnosticsV<CR>
814-
" by cursor word
815-
nnoremap <space>wdg :\<C-U>FzfxLspDiagnosticsW<CR>
816-
" by yank text
817-
nnoremap <space>pdg :\<C-U>FzfxLspDiagnosticsP<CR>
818826
819827
" ======== lsp symbols ========
820828
@@ -885,6 +893,13 @@ vim.keymap.set(
885893
"<cmd>FzfxFilesP<cr>",
886894
{ silent = true, noremap = true, desc = "Find files by yank text" }
887895
)
896+
-- by resume
897+
vim.keymap.set(
898+
"n",
899+
"<space>rf",
900+
"<cmd>FzfxFilesR<cr>",
901+
{ silent = true, noremap = true, desc = "Find files by previous search" }
902+
)
888903

889904
-- ======== live grep ========
890905

@@ -914,7 +929,14 @@ vim.keymap.set(
914929
"n",
915930
"<space>pl",
916931
"<cmd>FzfxLiveGrepP<cr>",
917-
{ silent = true, noremap = true, desc = "Live grep by cursor word" }
932+
{ silent = true, noremap = true, desc = "Live grep by yank text" }
933+
)
934+
-- by resume
935+
vim.keymap.set(
936+
"n",
937+
"<space>rl",
938+
"<cmd>FzfxLiveGrepR<cr>",
939+
{ silent = true, noremap = true, desc = "Live grep by previous search" }
918940
)
919941

920942
-- ======== buffers ========
@@ -986,25 +1008,6 @@ vim.keymap.set(
9861008
"<cmd>FzfxLspDiagnostics<cr>",
9871009
{ silent = true, noremap = true, desc = "Search lsp diagnostics" }
9881010
)
989-
-- by visual select
990-
vim.keymap.set(
991-
"x",
992-
"<space>dg",
993-
"<cmd>FzfxLspDiagnosticsV<CR>",
994-
{ silent = true, noremap = true, desc = "Search lsp diagnostics" }
995-
)
996-
-- by cursor word
997-
vim.keymap.set("n", "<space>wdg", "<cmd>FzfxLspDiagnosticsW<cr>", {
998-
silent = true,
999-
noremap = true,
1000-
desc = "Search lsp diagnostics by cursor word",
1001-
})
1002-
-- by yank text
1003-
vim.keymap.set("n", "<space>pdg", "<cmd>FzfxLspDiagnosticsP<cr>", {
1004-
silent = true,
1005-
noremap = true,
1006-
desc = "Search lsp diagnostics by yank text",
1007-
})
10081011

10091012
-- ======== lsp symbols ========
10101013

lua/fzfx/config.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,25 @@ local Defaults = {
22292229
},
22302230
default_provider = "unrestricted_mode",
22312231
},
2232+
-- resume
2233+
{
2234+
name = "FzfxFilesR",
2235+
feed = CommandFeedEnum.RESUME,
2236+
opts = {
2237+
bang = true,
2238+
desc = "Find files by previous search",
2239+
},
2240+
default_provider = "restricted_mode",
2241+
},
2242+
{
2243+
name = "FzfxFilesUR",
2244+
feed = CommandFeedEnum.RESUME,
2245+
opts = {
2246+
bang = true,
2247+
desc = "Find files unrestricted by previous search",
2248+
},
2249+
default_provider = "unrestricted_mode",
2250+
},
22322251
},
22332252
providers = {
22342253
restricted_mode = {
@@ -2392,6 +2411,34 @@ local Defaults = {
23922411
},
23932412
default_provider = "buffer_mode",
23942413
},
2414+
-- resume
2415+
{
2416+
name = "FzfxLiveGrepR",
2417+
feed = CommandFeedEnum.RESUME,
2418+
opts = {
2419+
bang = true,
2420+
desc = "Live grep by previous search",
2421+
},
2422+
default_provider = "restricted_mode",
2423+
},
2424+
{
2425+
name = "FzfxLiveGrepUR",
2426+
feed = CommandFeedEnum.RESUME,
2427+
opts = {
2428+
bang = true,
2429+
desc = "Live grep unrestricted by previous search",
2430+
},
2431+
default_provider = "unrestricted_mode",
2432+
},
2433+
{
2434+
name = "FzfxLiveGrepBR",
2435+
feed = CommandFeedEnum.RESUME,
2436+
opts = {
2437+
bang = true,
2438+
desc = "Live grep on current buffer by previous search",
2439+
},
2440+
default_provider = "buffer_mode",
2441+
},
23952442
},
23962443
providers = {
23972444
restricted_mode = {

lua/fzfx/fzf_helpers.lua

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local color = require("fzfx.color")
44
local conf = require("fzfx.config")
55
local yank_history = require("fzfx.yank_history")
66
local utils = require("fzfx.utils")
7+
local json = require("fzfx.json")
78

89
-- visual select {
910

@@ -83,20 +84,53 @@ end
8384

8485
-- visual select }
8586

86-
--- @param opts Options
87+
--- @param name string
88+
local function make_last_query_cache(name)
89+
return path.join(
90+
conf.get_config().cache.dir,
91+
string.format("_%s_last_query_cache", name)
92+
)
93+
end
94+
8795
--- @param feed_type CommandFeed
88-
--- @return string
89-
local function get_command_feed(opts, feed_type)
96+
--- @param input_args string?
97+
--- @param pipeline_name string
98+
--- @return string, string?
99+
local function get_command_feed(feed_type, input_args, pipeline_name)
90100
feed_type = string.lower(feed_type)
91101
if feed_type == "args" then
92-
return opts.args
102+
return input_args or "", nil
93103
elseif feed_type == "visual" then
94104
return _visual_select()
95105
elseif feed_type == "cword" then
96-
return vim.fn.expand("<cword>")
106+
return vim.fn.expand("<cword>"), nil
97107
elseif feed_type == "put" then
98108
local y = yank_history.get_yank()
99-
return (y ~= nil and type(y.regtext) == "string") and y.regtext or ""
109+
return (y ~= nil and type(y.regtext) == "string") and y.regtext or "",
110+
nil
111+
elseif feed_type == "resume" then
112+
local cache = make_last_query_cache(pipeline_name)
113+
local data = utils.readfile(cache)
114+
-- log.debug(
115+
-- "|fzfx.fzf_helpers - get_command_feed| pipeline %s cache:%s",
116+
-- vim.inspect(pipeline_name),
117+
-- vim.inspect(data)
118+
-- )
119+
if
120+
type(data) ~= "string"
121+
or string.len(data) == 0
122+
or not utils.string_startswith(data, "{")
123+
or not utils.string_endswith(data, "}")
124+
then
125+
return "", nil
126+
end
127+
--- @alias LastQueryCacheObj {default_provider:string,query:string}
128+
local ok, obj = pcall(json.decode, data) --[[@as LastQueryCacheObj]]
129+
if ok and type(obj) == "table" then
130+
return obj.query or "", obj.default_provider
131+
else
132+
return "", nil
133+
end
100134
else
101135
log.throw(
102136
"|fzfx.fzf_helpers - get_command_feed| invalid command feed type! %s",
@@ -352,6 +386,7 @@ end
352386
local M = {
353387
_get_visual_lines = _get_visual_lines,
354388
_visual_select = _visual_select,
389+
make_last_query_cache = make_last_query_cache,
355390
get_command_feed = get_command_feed,
356391
preprocess_fzf_opts = preprocess_fzf_opts,
357392
_generate_fzf_color_opts = _generate_fzf_color_opts,

lua/fzfx/general.lua

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,8 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
905905
)
906906

907907
local fzf_opts = {
908+
"--print-query",
909+
"--listen",
908910
{ "--query", query },
909911
{
910912
"--preview",
@@ -1037,7 +1039,6 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
10371039
string.format("change:reload(%s)", reload_query_command),
10381040
})
10391041
end
1040-
table.insert(fzf_opts, "--listen")
10411042
table.insert(fzf_opts, fzf_start_binder:build())
10421043

10431044
fzf_opts =
@@ -1068,11 +1069,27 @@ local function general(name, query, bang, pipeline_configs, default_pipeline)
10681069
fzf_opts,
10691070
actions,
10701071
context,
1071-
function()
1072+
function(last_query)
10721073
vim.schedule(function()
10731074
for _, rpc_id in ipairs(rpc_registries) do
10741075
server:get_rpc_server():unregister(rpc_id)
10751076
end
1077+
local last_query_cache = fzf_helpers.make_last_query_cache(name)
1078+
local content = json.encode({
1079+
default_provider = provider_switch.pipeline,
1080+
query = last_query,
1081+
}) --[[@as string]]
1082+
utils.asyncwritefile(
1083+
last_query_cache,
1084+
content,
1085+
function(err, bytes)
1086+
log.debug(
1087+
"|fzfx.general - general| dump last query:%s, error:%s",
1088+
vim.inspect(bytes),
1089+
vim.inspect(err)
1090+
)
1091+
end
1092+
)
10761093
end)
10771094
end
10781095
)
@@ -1085,7 +1102,7 @@ end
10851102
local function _make_user_command(name, command_config, group_config)
10861103
vim.api.nvim_create_user_command(command_config.name, function(opts)
10871104
local query, last_provider =
1088-
fzf_helpers.get_command_feed(opts, command_config.feed)
1105+
fzf_helpers.get_command_feed(command_config.feed, opts.args, name)
10891106
local default_provider = last_provider
10901107
or command_config.default_provider
10911108
return general(name, query, opts.bang, group_config, default_provider)

lua/fzfx/popup.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ local function _make_fzf_command(fzf_opts, actions, result)
316316
return command
317317
end
318318

319-
--- @alias OnPopupExit fun(launch:Popup):nil
319+
--- @alias OnPopupExit fun(last_query:string):nil
320320
--- @param win_opts Options?
321321
--- @param source string
322322
--- @param fzf_opts Options
@@ -345,10 +345,9 @@ function Popup:new(win_opts, source, fzf_opts, actions, context, on_popup_exit)
345345
return
346346
end
347347

348+
-- press <ESC> if still in fzf terminal
348349
local esc_key =
349350
vim.api.nvim_replace_termcodes("<ESC>", true, false, true)
350-
351-
-- press <ESC> if still in fzf terminal
352351
if vim.o.buftype == "terminal" and vim.o.filetype == "fzf" then
353352
vim.api.nvim_feedkeys(esc_key, "x", false)
354353
end
@@ -370,8 +369,9 @@ function Popup:new(win_opts, source, fzf_opts, actions, context, on_popup_exit)
370369
-- vim.inspect(result),
371370
-- vim.inspect(lines)
372371
-- )
373-
local action_key = vim.trim(lines[1])
374-
local action_lines = vim.list_slice(lines, 2)
372+
local last_query = vim.trim(lines[1])
373+
local action_key = vim.trim(lines[2])
374+
local action_lines = vim.list_slice(lines, 3)
375375
-- log.debug(
376376
-- "|fzfx.popup - Popup:new.on_fzf_exit| action_key:%s, action_lines:%s",
377377
-- vim.inspect(action_key),
@@ -401,7 +401,7 @@ function Popup:new(win_opts, source, fzf_opts, actions, context, on_popup_exit)
401401
log.err("unknown action key: %s", vim.inspect(action_key))
402402
end
403403
if type(on_popup_exit) == "function" then
404-
on_popup_exit(self)
404+
on_popup_exit(last_query)
405405
end
406406
end
407407

lua/fzfx/schema.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ local PreviewerLabelTypeEnum = {
9595
-- User command feeds are method that what to feed the fzfx command.
9696
-- E.g. visual selected, cursor word, etc.
9797
--
98-
--- @alias CommandFeed "args"|"visual"|"cword"|"put"
98+
--- @alias CommandFeed "args"|"visual"|"cword"|"put"|"resume"
9999
--- @enum CommandFeedEnum
100100
local CommandFeedEnum = {
101101
ARGS = "args",
102102
VISUAL = "visual",
103103
CWORD = "cword",
104104
PUT = "put",
105+
RESUME = "resume",
105106
}
106107

107108
-- ========== Fzf Option ==========

0 commit comments

Comments
 (0)