Skip to content

Commit 7c90974

Browse files
committed
feat(lsp_symbols): new option parent_postfix
Disabled by default, use any character as separator, e.g: ```vim :FzfLua lsp_document_symbols parent_postfix=-> ``` Closes #2381
1 parent 4927602 commit 7c90974

3 files changed

Lines changed: 17 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,6 +1266,8 @@ previewers = {
12661266
symbol_fmt = function(s, opts) return "[" .. s .. "]" end,
12671267
-- prefix child symbols. set to any string or `false` to disable
12681268
child_prefix = true,
1269+
-- prepend parent to symbol, set to any string or `false` to disable
1270+
-- parent_postfix = ".",
12691271
fzf_opts = { ["--tiebreak"] = "begin" },
12701272
},
12711273
code_actions = {

lua/fzf-lua/defaults.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,7 @@ M.defaults.lsp.symbols = {
10421042
symbol_hl = function(s) return "@" .. s:lower() end,
10431043
symbol_fmt = function(s, _) return "[" .. s .. "]" end,
10441044
child_prefix = true,
1045+
parent_postfix = false,
10451046
async_or_timeout = true,
10461047
-- new formatting options with symbol name at the start
10471048
fzf_opts = {

lua/fzf-lua/providers/lsp.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,23 @@ local function call_hierarchy_handler(opts, cb, _, result, ctx, _)
179179
end
180180

181181
-- Copied from vim.lsp.util.symbols_to_items, then added space prefix to child symbols.
182-
local function symbols_to_items(symbols, bufnr, child_prefix)
182+
local function symbols_to_items(opts, symbols, bufnr, child_prefix)
183183
---@private
184-
local function _symbols_to_items(_symbols, _items, _bufnr, prefix)
184+
local function _symbols_to_items(_symbols, _items, _bufnr, prefix, parents)
185+
parents = parents or {}
185186
for _, symbol in ipairs(_symbols) do
186187
local kind = vim.lsp.protocol.SymbolKind[symbol.kind] or "Unknown"
188+
local name = opts.parent_postfix and not utils.tbl_isempty(parents)
189+
and table.concat(parents, opts.parent_postfix) .. opts.parent_postfix .. symbol.name
190+
or symbol.name
187191
if symbol.location then -- SymbolInformation type
188192
local range = symbol.location.range
189193
table.insert(_items, {
190194
filename = vim.uri_to_fname(symbol.location.uri),
191195
lnum = range.start.line + 1,
192196
col = range.start.character + 1,
193197
kind = kind,
194-
text = prefix .. "[" .. kind .. "] " .. symbol.name,
198+
text = prefix .. "[" .. kind .. "] " .. name,
195199
})
196200
elseif symbol.selectionRange then -- DocumentSymbole type
197201
table.insert(_items, {
@@ -200,14 +204,18 @@ local function symbols_to_items(symbols, bufnr, child_prefix)
200204
lnum = symbol.selectionRange.start.line + 1,
201205
col = symbol.selectionRange.start.character + 1,
202206
kind = kind,
203-
text = prefix .. "[" .. kind .. "] " .. symbol.name,
207+
text = prefix .. "[" .. kind .. "] " .. name,
204208
})
205209
if symbol.children then
206-
for _, v in ipairs(_symbols_to_items(symbol.children, _items, _bufnr, prefix .. child_prefix)) do
210+
table.insert(parents, symbol.name)
211+
for _, v in ipairs(
212+
_symbols_to_items(symbol.children, _items, _bufnr, prefix .. child_prefix, parents))
213+
do
207214
for _, s in ipairs(v) do
208215
table.insert(_items, s)
209216
end
210217
end
218+
table.remove(parents)
211219
end
212220
end
213221
end
@@ -220,7 +228,7 @@ local function symbol_handler(opts, cb, _, result, ctx, _)
220228
result = utils.tbl_islist(result) and result or { result }
221229
local items
222230
if opts.child_prefix then
223-
items = symbols_to_items(result, utils.CTX().bufnr,
231+
items = symbols_to_items(opts, result, utils.CTX().bufnr,
224232
opts.child_prefix == true and string.rep(" ", 2) or opts.child_prefix)
225233
else
226234
local encoding = vim.lsp.get_client_by_id(ctx.client_id).offset_encoding

0 commit comments

Comments
 (0)