Skip to content

Commit

Permalink
fix(lsp_symbols): format entry earlier (#934)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Nov 22, 2023
1 parent c9ed5be commit 413c419
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -624,17 +624,22 @@ M.defaults.lsp.symbols = {
fzf_opts = {
["--delimiter"] = string.format("'[:%s]'", utils.nbsp),
["--tiebreak"] = "begin",
["--info"] = "default",
},
line_field_index = "{-2}", -- line field index
field_index_expr = "{}", -- entry field index
_fmt = {
to = function(s, _)
local file, text = s:match("^(.+:.+:.+:)%s(.*)")
-- fzf has alignment issues with ansi colorings of differnt escape length
local align = 56 + #utils.has_ansi_coloring(text)
return string.format("%-" .. align .. "s%s%s", text, utils.nbsp, file)
end,
-- NOT NEEDED: we format at the source in `lsp.symbol_handler`
-- to = function(s, _)
-- local file, text = s:match("^(.+:.+:.+:)%s(.*)")
-- -- fzf has alignment issues with ansi colorings of differnt escape length
-- local align = 56 + utils.ansi_col_len(text)
-- return string.format("%-" .. align .. "s%s%s", text, utils.nbsp, file)
-- end,
from = function(s, _)
-- restore the format to something that `path.entry_to_file` can
-- handle more robustly, while this can stil work due to the `utils.nbsp`
-- it will fail when the symbol contains "[%d]" (which we use as bufnr)
local text, file = s:match(string.format("^(.-)%s(.*)", utils.nbsp))
return string.format("%s %s", file, text)
end
Expand Down
6 changes: 6 additions & 0 deletions lua/fzf-lua/providers/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,14 @@ local function symbol_handler(opts, cb, _, result, _, _)
entry.text = entry.text:gsub("%[.-%]", M._sym2style[kind], 1)
end
end
-- move symbol `entry.text` to the start of the line
-- will be restored in preview/actions by `opts._fmt.from`
local symbol = entry.text
local align = 56 + utils.ansi_col_len(symbol)
entry.text = nil
entry = make_entry.lcol(entry, opts)
entry = make_entry.file(entry, opts)
entry = string.format("%-" .. align .. "s%s%s", symbol, utils.nbsp, entry)
if entry then
cb(opts._fmt and opts._fmt.to and opts._fmt.to(entry, opts) or entry)
end
Expand Down
5 changes: 5 additions & 0 deletions lua/fzf-lua/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ function M.has_ansi_coloring(str)
return str:match("%[[%d;]-m")
end

function M.ansi_col_len(str)
local match = M.has_ansi_coloring(str)
return match and #match or 0
end

function M.strip_ansi_coloring(str)
if not str then return str end
-- remove escape sequences of the following formats:
Expand Down

0 comments on commit 413c419

Please sign in to comment.