Skip to content

Commit fca303e

Browse files
committed
feat(symbols): "locate" current symbol (closes #2190)
Disabled by default, to use: ```lua :FzfLua lsp_document_symbols locate=true ```
1 parent f913e6b commit fca303e

5 files changed

Lines changed: 33 additions & 15 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ previewers = {
12331233
symbols = {
12341234
-- lsp_query = "foo" -- query passed to the LSP directly
12351235
-- query = "bar" -- query passed to fzf prompt for fuzzy matching
1236+
locate = false, -- attempt to position cursor at current symbol
12361237
async_or_timeout = true, -- symbols are async by default
12371238
symbol_style = 1, -- style for document/workspace symbols
12381239
-- false: disable, 1: icon+kind

lua/fzf-lua/config.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,15 @@ function M.normalize_opts(opts, globals, __resume_key)
929929
opts.fn_preprocess = [[return require("fzf-lua.make_entry").preprocess]]
930930
end
931931

932+
if opts.locate and utils.has(opts, "fzf", { 0, 36 }) then
933+
table.insert(opts._fzf_cli_args, "--bind=" .. libuv.shellescape("load:+transform:"
934+
.. FzfLua.shell.stringify_data(function(_, _, _)
935+
if opts.__locate_pos then
936+
return string.format("pos(%d)", opts.__locate_pos)
937+
end
938+
end, opts)))
939+
end
940+
932941
if opts.line_query and not utils.has(opts, "fzf", { 0, 59 }) then
933942
utils.warn("'line_query' requires fzf >= 0.59, ignoring.")
934943
elseif opts.line_query then

lua/fzf-lua/defaults.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,9 @@ M.defaults.files = {
362362

363363
M.defaults.global = vim.tbl_deep_extend("force", M.defaults.files, {
364364
silent = true,
365+
-- TODO: lsp_workspace_symbols locate, not working yet
366+
-- as opts.__locate_pos is inside the symbols picker opts
367+
-- locate = true,
365368
cwd_prompt = true,
366369
line_query = true,
367370
pickers = function()
@@ -953,6 +956,7 @@ M.defaults.lsp = {
953956

954957
M.defaults.lsp.symbols = {
955958
previewer = M._default_previewer_fn,
959+
locate = false,
956960
file_icons = 1,
957961
color_icons = true,
958962
git_icons = false,

lua/fzf-lua/providers/buffers.lua

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -375,24 +375,20 @@ M.tabs = function(opts)
375375
return msg, hl
376376
end
377377

378-
if opts.locate and utils.has(opts, "fzf", { 0, 36 }) then
379-
-- Set cursor to current buffer
380-
table.insert(opts._fzf_cli_args, "--bind=" .. libuv.shellescape("load:+transform:"
381-
.. FzfLua.shell.stringify_data(function(_, _, _)
382-
local pos = 0
383-
for tabnr, tabh in ipairs(vim.api.nvim_list_tabpages()) do
378+
if opts.locate then
379+
local pos = 0
380+
for tabnr, tabh in ipairs(vim.api.nvim_list_tabpages()) do
381+
pos = pos + 1
382+
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(tabh)) do
383+
local b = filter_buffers(opts, { vim.api.nvim_win_get_buf(w) })[1]
384+
if b then
384385
pos = pos + 1
385-
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(tabh)) do
386-
local b = filter_buffers(opts, { vim.api.nvim_win_get_buf(w) })[1]
387-
if b then
388-
pos = pos + 1
389-
if tabnr == utils.CTX().tabnr and w == utils.CTX().winid then
390-
return string.format("pos(%d)", pos)
391-
end
392-
end
386+
if tabnr == utils.CTX().tabnr and w == utils.CTX().winid then
387+
opts.__locate_pos = pos
393388
end
394389
end
395-
end, opts)))
390+
end
391+
end
396392
end
397393

398394
local contents = function(cb)

lua/fzf-lua/providers/lsp.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ local function symbol_handler(opts, cb, _, result, ctx, _)
265265
local entry0 = make_entry.lcol(entry, opts)
266266
local entry1 = make_entry.file(entry0, opts)
267267
if entry1 then
268+
if opts.locate and not opts.__locate_pos then
269+
opts.__locate_count = opts.__locate_count or 0
270+
opts.__locate_count = opts.__locate_count + 1
271+
if entry.lnum == utils.CTX().cursor[1] then
272+
opts.__locate_pos = opts.__locate_count
273+
end
274+
end
268275
local align = 48 + mbicon_align + utils.ansi_escseq_len(symbol)
269276
-- TODO: string.format %-{n}s fails with align > ~100?
270277
-- entry1 = string.format("%-" .. align .. "s%s%s", symbol, utils.nbsp, entry1)
@@ -755,6 +762,7 @@ end
755762
M.workspace_symbols = function(opts)
756763
opts = normalize_lsp_opts(opts, "lsp.symbols", "lsp_workspace_symbols")
757764
if not opts then return end
765+
opts.locate = false -- Makes no sense for workspace symbols
758766
opts.__ACT_TO = opts.__ACT_TO or M.live_workspace_symbols
759767
opts.__call_fn = utils.__FNCREF__()
760768
opts.lsp_params = { query = opts.lsp_query or "" }

0 commit comments

Comments
 (0)