Skip to content

Commit aed64a1

Browse files
committed
refactor(headers): consolidate set_header calls
1 parent cc9cf57 commit aed64a1

14 files changed

Lines changed: 41 additions & 51 deletions

File tree

lua/fzf-lua/core.lua

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ M.fzf_resume = function(opts)
228228
end
229229
opts = utils.tbl_deep_extend("force", config.__resume_data.opts, opts or {})
230230
assert(opts == config.__resume_data.opts)
231-
opts = M.set_header(opts, opts.headers or {})
232231
opts.cwd = opts.cwd and libuv.expand(opts.cwd) or nil
233232
M.fzf_wrap(config.__resume_data.contents, config.__resume_data.opts)
234233
end
@@ -239,6 +238,7 @@ end
239238
---@return thread, string, table
240239
M.fzf_wrap = function(cmd, opts, convert_actions)
241240
opts = opts or {}
241+
M.set_header(opts)
242242
if convert_actions and type(opts.actions) == "table" then
243243
opts = M.convert_reload_actions(cmd, opts)
244244
opts = M.convert_exec_silent_actions(opts)
@@ -774,7 +774,15 @@ M.set_title_flags = function(opts, titles)
774774
return opts
775775
end
776776

777-
M.set_header = function(opts, hdr_tbl)
777+
M.set_header = function(opts)
778+
opts = opts or {}
779+
if opts.header ~= nil
780+
or opts.headers == false
781+
or type(opts._headers) ~= "table"
782+
or vim.tbl_isempty(opts._headers) then
783+
return opts
784+
end
785+
778786
---@param cwd string
779787
---@return string
780788
local function normalize_cwd(cwd)
@@ -787,7 +795,6 @@ M.set_header = function(opts, hdr_tbl)
787795
return path.HOME_to_tilde(cwd)
788796
end
789797

790-
if not opts then opts = {} end
791798
if opts.cwd_prompt then
792799
opts.prompt = normalize_cwd(opts.cwd or uv.cwd())
793800
if tonumber(opts.cwd_prompt_shorten_len) and
@@ -796,9 +803,6 @@ M.set_header = function(opts, hdr_tbl)
796803
end
797804
opts.prompt = path.add_trailing(opts.prompt)
798805
end
799-
if opts.no_header or opts.headers == false then
800-
return opts
801-
end
802806
local definitions = {
803807
-- key: opt name
804808
-- val.hdr_txt_opt: opt header string name
@@ -893,13 +897,8 @@ M.set_header = function(opts, hdr_tbl)
893897
end,
894898
},
895899
}
896-
-- by default we only display cwd headers
897-
-- header string constructed in array order
898-
if not opts.headers then
899-
opts.headers = hdr_tbl or { "cwd" }
900-
end
901900
-- override header text with the user's settings
902-
for _, h in ipairs(opts.headers) do
901+
for _, h in ipairs(opts._headers) do
903902
assert(definitions[h])
904903
local hdr_text = opts[definitions[h].hdr_txt_opt]
905904
if hdr_text then
@@ -908,7 +907,7 @@ M.set_header = function(opts, hdr_tbl)
908907
end
909908
-- build the header string
910909
local hdr_str
911-
for _, h in ipairs(opts.headers) do
910+
for _, h in ipairs(opts._headers) do
912911
assert(definitions[h])
913912
local def = definitions[h]
914913
local txt = def.val(opts)

lua/fzf-lua/defaults.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ M.defaults.files = {
360360
toggle_hidden_flag = "--hidden",
361361
toggle_follow_flag = "-L",
362362
_actions = function() return M.globals.actions.files end,
363+
_headers = { "actions", "cwd" },
363364
winopts = { preview = { winopts = { cursorline = false } } },
364365
}
365366

@@ -425,6 +426,7 @@ M.defaults.git = {
425426
fzf_opts = { ["--multi"] = true, ["--scheme"] = "path" },
426427
_fzf_nth_devicons = true,
427428
_actions = function() return M.globals.actions.files end,
429+
_headers = { "cwd" },
428430
winopts = { preview = { winopts = { cursorline = false } } },
429431
},
430432
status = {
@@ -440,6 +442,7 @@ M.defaults.git = {
440442
fzf_opts = { ["--multi"] = true },
441443
_fzf_nth_devicons = true,
442444
_actions = function() return M.globals.actions.files end,
445+
_headers = { "actions", "cwd" },
443446
actions = {
444447
["right"] = { fn = actions.git_unstage, reload = true },
445448
["left"] = { fn = actions.git_stage, reload = true },
@@ -461,6 +464,7 @@ M.defaults.git = {
461464
fzf_opts = { ["--multi"] = true },
462465
_fzf_nth_devicons = true,
463466
_actions = function() return M.globals.actions.files end,
467+
_headers = { "cwd" },
464468
},
465469
hunks = {
466470
previewer = M._default_previewer_fn,
@@ -478,6 +482,7 @@ M.defaults.git = {
478482
},
479483
_fzf_nth_devicons = true,
480484
_actions = function() return M.globals.actions.files end,
485+
_headers = { "cwd" },
481486
},
482487
commits = {
483488
cmd = [[git log --color --pretty=format:"%C(yellow)%h%Creset ]]
@@ -489,6 +494,7 @@ M.defaults.git = {
489494
["ctrl-y"] = { fn = actions.git_yank_commit, exec_silent = true },
490495
},
491496
fzf_opts = { ["--no-multi"] = true },
497+
_headers = { "actions", "cwd" },
492498
_multiline = false,
493499
},
494500
bcommits = {
@@ -504,6 +510,7 @@ M.defaults.git = {
504510
["ctrl-y"] = { fn = actions.git_yank_commit, exec_silent = true },
505511
},
506512
fzf_opts = { ["--no-multi"] = true },
513+
_headers = { "actions", "cwd" },
507514
_multiline = false,
508515
},
509516
blame = {
@@ -534,6 +541,7 @@ M.defaults.git = {
534541
cmd_add = { "git", "branch" },
535542
cmd_del = { "git", "branch", "--delete" },
536543
fzf_opts = { ["--no-multi"] = true },
544+
_headers = { "actions", "cwd" },
537545
_multiline = false,
538546
},
539547
tags = {
@@ -545,6 +553,7 @@ M.defaults.git = {
545553
.. [[%Cgreen(%><(12)%cr%><|(12))%Creset %s %C(blue)<%an>%Creset" {1}]],
546554
actions = { ["enter"] = actions.git_checkout },
547555
fzf_opts = { ["--no-multi"] = true },
556+
_headers = { "cwd" },
548557
_multiline = false,
549558
},
550559
stash = {
@@ -562,6 +571,7 @@ M.defaults.git = {
562571
["--no-multi"] = true,
563572
["--delimiter"] = "[:]",
564573
},
574+
_headers = { "actions", "cwd", "search" },
565575
},
566576
icons = {
567577
["M"] = { icon = "M", color = "yellow" },
@@ -598,6 +608,7 @@ M.defaults.grep = {
598608
glob_flag = "--iglob", -- for case sensitive globs use '--glob'
599609
glob_separator = "%s%-%-", -- query separator pattern (lua): ' --'
600610
_treesitter = true,
611+
_headers = { "actions", "cwd" },
601612
}
602613

603614
---@diagnostic disable-next-line: assign-type-mismatch
@@ -621,6 +632,7 @@ M.defaults.args = {
621632
_fzf_nth_devicons = true,
622633
_actions = function() return M.globals.actions.files end,
623634
actions = { ["ctrl-x"] = { fn = actions.arg_del, reload = true } },
635+
_headers = { "actions", "cwd" },
624636
}
625637

626638
M.defaults.oldfiles = {
@@ -632,6 +644,7 @@ M.defaults.oldfiles = {
632644
fzf_opts = { ["--tiebreak"] = "index", ["--multi"] = true },
633645
_fzf_nth_devicons = true,
634646
_actions = function() return M.globals.actions.files end,
647+
_headers = { "cwd" },
635648
}
636649

637650
M.defaults.quickfix = {
@@ -650,6 +663,7 @@ M.defaults.quickfix = {
650663
_actions = function() return M.globals.actions.files end,
651664
_treesitter = true,
652665
_cached_hls = { "path_colnr", "path_linenr" },
666+
_headers = { "actions", "cwd" },
653667
}
654668

655669
M.defaults.quickfix_stack = {
@@ -675,6 +689,7 @@ M.defaults.loclist = {
675689
_actions = function() return M.globals.actions.files end,
676690
_treesitter = true,
677691
_cached_hls = { "path_colnr", "path_linenr" },
692+
_headers = { "actions", "cwd" },
678693
}
679694

680695
M.defaults.loclist_stack = {
@@ -701,6 +716,7 @@ M.defaults.buffers = {
701716
end,
702717
actions = { ["ctrl-x"] = { fn = actions.buf_del, reload = true } },
703718
_cached_hls = { "buf_nr", "buf_flag_cur", "buf_flag_alt", "path_linenr" },
719+
_headers = { "actions", "cwd" },
704720
_ctx = { includeBuflist = true },
705721
_resume_reload = true,
706722
}
@@ -725,6 +741,7 @@ M.defaults.tabs = {
725741
["--with-nth"] = "5..",
726742
},
727743
_cached_hls = { "buf_nr", "buf_flag_cur", "buf_flag_alt", "tab_title", "tab_marker", "path_linenr" },
744+
_headers = { "actions", "cwd" },
728745
_ctx = { includeBuflist = true },
729746
_resume_reload = true,
730747
}
@@ -824,6 +841,7 @@ M.defaults.spellcheck = {
824841
["ctrl-s"] = { fn = actions.spell_suggest, header = "spell suggest" }
825842
},
826843
_cached_hls = { "buf_name", "buf_nr", "buf_linenr", "path_colnr" },
844+
_headers = { "actions" },
827845
_fmt = {
828846
to = false,
829847
from = function(s, _)
@@ -882,6 +900,7 @@ M.defaults.colorschemes = {
882900
winopts = { height = 0.55, width = 0.50, backdrop = false },
883901
fzf_opts = { ["--no-multi"] = true },
884902
actions = { ["enter"] = actions.colorscheme },
903+
_headers = { "actions" },
885904
}
886905

887906
M.defaults.highlights = {
@@ -958,6 +977,7 @@ M.defaults.lsp = {
958977
_treesitter = true,
959978
-- Signals actions to use uri triggering the use of `lsp.util.show_document`
960979
_uri = true,
980+
_headers = { "actions", "regex_filter" },
961981
}
962982

963983
M.defaults.lsp.symbols = {
@@ -1029,6 +1049,7 @@ M.defaults.lsp.symbols = {
10291049
_actions = function() return M.globals.actions.files end,
10301050
actions = { ["ctrl-g"] = { actions.sym_lsym } },
10311051
_cached_hls = { "live_sym", "path_colnr", "path_linenr" },
1052+
_headers = { "actions", "cwd", "regex_filter" },
10321053
_uri = true,
10331054
}
10341055

@@ -1064,6 +1085,7 @@ M.defaults.lsp.finder = {
10641085
fzf_opts = { ["--multi"] = true },
10651086
_treesitter = true,
10661087
_cached_hls = { "path_colnr", "path_linenr" },
1088+
_headers = { "actions", "regex_filter" },
10671089
_uri = true,
10681090
}
10691091

@@ -1094,6 +1116,7 @@ M.defaults.diagnostics = {
10941116
},
10951117
_actions = function() return M.globals.actions.files end,
10961118
_cached_hls = { "path_colnr", "path_linenr" },
1119+
_headers = { "actions", "cwd" },
10971120
-- signs = {
10981121
-- ["Error"] = { text = "e", texthl = "DiagnosticError" },
10991122
-- ["Warn"] = { text = "w", texthl = "DiagnosticWarn" },
@@ -1179,6 +1202,7 @@ M.defaults.command_history = {
11791202
["enter"] = actions.ex_run_cr,
11801203
["ctrl-e"] = actions.ex_run,
11811204
},
1205+
_headers = { "actions" },
11821206
}
11831207

11841208
M.defaults.search_history = {
@@ -1189,6 +1213,7 @@ M.defaults.search_history = {
11891213
["enter"] = actions.search_cr,
11901214
["ctrl-e"] = actions.search,
11911215
},
1216+
_headers = { "actions" },
11921217
}
11931218

11941219
M.defaults.registers = {
@@ -1284,6 +1309,7 @@ M.defaults.dap = {
12841309
["--with-nth"] = "2..",
12851310
},
12861311
_cached_hls = { "path_colnr", "path_linenr" },
1312+
_headers = { "actions", "cwd" },
12871313
},
12881314
}
12891315

lua/fzf-lua/providers/buffers.lua

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ M.buffers = function(opts)
205205
opts.fzf_opts["--header-lines"] = opts.fzf_opts["--header-lines"] == nil
206206
and (not opts.ignore_current_buffer and opts.sort_lastused) and "1" or nil
207207

208-
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
209208
opts = opts.filename_only and opts or core.set_fzf_field_index(opts)
210209

211210
return core.fzf_exec(contents, opts)
@@ -450,7 +449,6 @@ M.tabs = function(opts)
450449
cb(nil)
451450
end
452451

453-
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
454452
opts = opts.filename_only and opts or core.set_fzf_field_index(opts, "{4}", "{}")
455453

456454
return core.fzf_exec(contents, opts)
@@ -585,8 +583,6 @@ M.treesitter = function(opts)
585583
end)()
586584
end
587585

588-
opts = core.set_header(opts, opts.headers or { "actions" })
589-
590586
return core.fzf_exec(contents, opts)
591587
end
592588

@@ -690,8 +686,6 @@ M.spellcheck = function(opts)
690686
end)()
691687
end
692688

693-
opts = core.set_header(opts, opts.headers or { "actions" })
694-
695689
return core.fzf_exec(contents, opts)
696690
end
697691

lua/fzf-lua/providers/colorschemes.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,6 @@ M.awesome_colorschemes = function(opts)
510510
utils.setup_highlights()
511511
end
512512

513-
opts = core.set_header(opts, opts.headers or { "actions" })
514513
return core.fzf_exec(contents, opts)
515514
end
516515

lua/fzf-lua/providers/dap.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ M.breakpoints = function(opts)
117117
end)()
118118
end
119119

120-
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
121120
opts = core.set_fzf_field_index(opts, "{3}", opts._is_skim and "{}" or "{..-2}")
122-
123121
return core.fzf_exec(contents, opts)
124122
end
125123

lua/fzf-lua/providers/diagnostic.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,6 @@ M.diagnostics = function(opts)
258258
end)()
259259
end
260260

261-
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
262261
opts = core.set_fzf_field_index(opts)
263262
return core.fzf_exec(contents, opts)
264263
end

lua/fzf-lua/providers/files.lua

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ M.files = function(opts)
9191
opts.cwd = uv.cwd()
9292
end
9393
opts = core.set_title_flags(opts, { "cmd" })
94-
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
9594
return core.fzf_exec(opts.cmd, opts)
9695
end
9796

@@ -136,7 +135,6 @@ M.args = function(opts)
136135
end)()
137136
end
138137

139-
opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
140138
return core.fzf_exec(contents, opts)
141139
end
142140

0 commit comments

Comments
 (0)