Skip to content

Commit

Permalink
feat(lsp): better format/matching for workspace symbols
Browse files Browse the repository at this point in the history
- refactor `opts.line_field_index` to match `field_index_expr`
- misc bug fixes / improvements to bat_native previewer
- reomved tags file line number detection (not needed anymore)
  • Loading branch information
ibhagwan committed Nov 22, 2023
1 parent 38ee4aa commit c9ed5be
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lua/fzf-lua/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ end
-- for setting the preview offset (and on some
-- cases the highlighted line)
M.set_fzf_field_index = function(opts, default_idx, default_expr)
opts.line_field_index = opts.line_field_index or default_idx or 2
opts.line_field_index = opts.line_field_index or default_idx or "{2}"
-- when entry contains lines we set the fzf FIELD INDEX EXPRESSION
-- to the below so that only the filename is sent to the preview
-- action, otherwise we will have issues with entries with text
Expand Down
65 changes: 43 additions & 22 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -440,48 +440,50 @@ M.defaults.tabs = {
}

M.defaults.lines = {
previewer = M._default_previewer_fn,
prompt = "Lines> ",
file_icons = true and M._has_devicons,
color_icons = true,
show_unloaded = true,
show_unlisted = false,
no_term_buffers = true,
fzf_opts = {
previewer = M._default_previewer_fn,
prompt = "Lines> ",
file_icons = true and M._has_devicons,
color_icons = true,
show_unloaded = true,
show_unlisted = false,
no_term_buffers = true,
fzf_opts = {
["--delimiter"] = "'[\\]:]'",
["--nth"] = "2..",
["--tiebreak"] = "index",
["--tabstop"] = "1",
},
_actions = function() return M.globals.actions.buffers end,
actions = {
line_field_index = "{3}",
_actions = function() return M.globals.actions.buffers end,
actions = {
["default"] = actions.buf_edit_or_qf,
["alt-q"] = actions.buf_sel_to_qf,
["alt-l"] = actions.buf_sel_to_ll
},
_cached_hls = { "buf_name", "buf_nr", "buf_linenr" },
_cached_hls = { "buf_name", "buf_nr", "buf_linenr" },
}

M.defaults.blines = {
previewer = M._default_previewer_fn,
prompt = "BLines> ",
file_icons = false,
color_icons = false,
show_unlisted = true,
no_term_buffers = false,
fzf_opts = {
previewer = M._default_previewer_fn,
prompt = "BLines> ",
file_icons = false,
color_icons = false,
show_unlisted = true,
no_term_buffers = false,
fzf_opts = {
["--delimiter"] = "'[:]'",
["--with-nth"] = "2..",
["--tiebreak"] = "index",
["--tabstop"] = "1",
},
_actions = function() return M.globals.actions.buffers end,
actions = {
line_field_index = "{2}",
_actions = function() return M.globals.actions.buffers end,
actions = {
["default"] = actions.buf_edit_or_qf,
["alt-q"] = actions.buf_sel_to_qf,
["alt-l"] = actions.buf_sel_to_ll
},
_cached_hls = { "buf_name", "buf_nr", "buf_linenr" },
_cached_hls = { "buf_name", "buf_nr", "buf_linenr" },
}

M.defaults.tags = {
Expand All @@ -492,7 +494,7 @@ M.defaults.tags = {
grep_opts = "--color=auto --perl-regexp",
multiprocess = true,
file_icons = true and M._has_devicons,
git_icons = true,
git_icons = false,
color_icons = true,
fzf_opts = {
["--delimiter"] = string.format("'[:%s]'", utils.nbsp),
Expand Down Expand Up @@ -618,6 +620,25 @@ M.defaults.lsp.symbols = {
symbol_fmt = function(s, _) return "[" .. s .. "]" end,
child_prefix = true,
async_or_timeout = true,
-- new formatting options with symbol name at the start
fzf_opts = {
["--delimiter"] = string.format("'[:%s]'", utils.nbsp),
["--tiebreak"] = "begin",
},
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,
from = function(s, _)
local text, file = s:match(string.format("^(.-)%s(.*)", utils.nbsp))
return string.format("%s %s", file, text)
end
},
_actions = function() return M.globals.actions.files end,
actions = { ["ctrl-g"] = { actions.sym_lsym } },
}
Expand Down
33 changes: 16 additions & 17 deletions lua/fzf-lua/previewer/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Previewer.base:preview_offset()
'--preview-window '~3:+{2}+3/2''
]]
if self.opts.line_field_index then
return ("+{%d}-/2"):format(self.opts.line_field_index)
return ("+%s-/2"):format(self.opts.line_field_index)
end
end

Expand Down Expand Up @@ -66,7 +66,7 @@ function Previewer.cmd:new(o, opts)
end

function Previewer.cmd:format_cmd(cmd, args, action, extra_args)
return string.format([[%s %s %s $(%s)]],
return string.format([[%s %s %s "$(%s)"]],
cmd, args or "", extra_args or "", action)
end

Expand All @@ -79,7 +79,6 @@ end
function Previewer.cmd:action(o)
o = o or {}
local act = shell.raw_action(function(items, _, _)
-- only preview first item
local entry = path.entry_to_file(items[1], self.opts)
return entry.bufname or entry.path
end, self.opts.field_index_expr or "{}", self.opts.debug)
Expand All @@ -103,7 +102,7 @@ function Previewer.bat:cmdline(o)
extra_args = string.format([[ --theme="%s"]], self.theme)
end
if self.opts.line_field_index then
extra_args = extra_args .. string.format(" --highlight-line={%d}", self.opts.line_field_index)
extra_args = extra_args .. string.format(" --highlight-line=%s", self.opts.line_field_index)
end
return self:format_cmd(self.cmd, self.args, o.action, extra_args)
end
Expand All @@ -122,7 +121,7 @@ function Previewer.head:cmdline(o)
local lines = "--lines=-0"
-- print all lines instead
-- if self.opts.line_field_index then
-- lines = string.format("--lines={%d}", self.opts.line_field_index)
-- lines = string.format("--lines=%s", self.opts.line_field_index)
-- end
return self:format_cmd(self.cmd, self.args, o.action, lines)
end
Expand All @@ -138,7 +137,7 @@ end
local grep_tag = function(file, tag)
local line = 1
local filepath = file
local pattern = utils.rg_escape(tag)
local pattern = utils.rg_escape(vim.trim(tag))
if not pattern or not filepath then return line end
local grep_cmd = vim.fn.executable("rg") == 1
and { "rg", "--line-number" }
Expand Down Expand Up @@ -172,15 +171,16 @@ end
function Previewer.cmd_async:parse_entry_and_verify(entrystr)
local entry = path.entry_to_file(entrystr, self.opts)
local filepath = entry.bufname or entry.path or ""
if self.opts._ctag and entry.line <= 1 then
-- tags without line numbers
-- make sure we don't already have line #
-- (in the case the line no. is actually 1)
local line = entry.stripped:match("[^:]+(%d+):")
local ctag = path.entry_to_ctag(entry.stripped, true)
if not line and ctag then
entry.ctag = ctag
entry.line = grep_tag(filepath, entry.ctag)
if self.opts._ctag then
entry.ctag = path.entry_to_ctag(entry.stripped, true)
if entry.line <= 1 then
-- default tags are without line numbers
-- make sure we don't already have line #
-- (in the case the line no. is actually 1)
local line = entry.stripped:match("[^:]+(%d+):")
if not line and entry.ctag then
entry.line = grep_tag(filepath, entry.ctag)
end
end
end
local errcmd = nil
Expand Down Expand Up @@ -229,8 +229,7 @@ function Previewer.bat_async:cmdline(o)
local cmd = errcmd or ("%s %s %s %s %s %s"):format(
self.cmd, self.args,
self.theme and string.format([[--theme="%s"]], self.theme) or "",
self.opts.line_field_index and
string.format("--highlight-line=%d", entry.line) or "",
self.opts.line_field_index and string.format("--highlight-line=%d", entry.line) or "",
line_range,
vim.fn.shellescape(filepath))
-- uncomment to see the command in the preview window
Expand Down
6 changes: 2 additions & 4 deletions lua/fzf-lua/providers/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,12 @@ end

M.lines = function(opts)
opts = config.normalize_opts(opts, config.globals.lines)
opts.line_field_index = opts.line_field_index or 2
M.buffer_lines(opts)
end

M.blines = function(opts)
opts = config.normalize_opts(opts, config.globals.blines)
opts.current_buffer_only = true
opts.line_field_index = opts.line_field_index or 2
M.buffer_lines(opts)
end

Expand Down Expand Up @@ -305,7 +303,7 @@ M.buffer_lines = function(opts)
opts.fzf_opts["--query"] = vim.fn.shellescape(opts.search)
end

opts = core.set_fzf_field_index(opts, 3, opts._is_skim and "{}" or "{..-2}")
opts = core.set_fzf_field_index(opts, "{3}", opts._is_skim and "{}" or "{..-2}")

core.fzf_exec(contents, opts)
end
Expand Down Expand Up @@ -427,7 +425,7 @@ M.tabs = function(opts)
end

opts = core.set_header(opts, opts.headers or { "actions", "cwd" })
opts = core.set_fzf_field_index(opts, 3, "{}")
opts = core.set_fzf_field_index(opts, "{3}", "{}")

core.fzf_exec(contents, opts)
end
Expand Down
2 changes: 1 addition & 1 deletion lua/fzf-lua/providers/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ M.breakpoints = function(opts)
:format(utils.ansi_codes.yellow("<Ctrl-x>")))
end

opts = core.set_fzf_field_index(opts, 3, opts._is_skim and "{}" or "{..-2}")
opts = core.set_fzf_field_index(opts, "{3}", opts._is_skim and "{}" or "{..-2}")

core.fzf_exec(contents, opts)
end
Expand Down
5 changes: 2 additions & 3 deletions lua/fzf-lua/providers/lsp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ local function symbol_handler(opts, cb, _, result, _, _)
local items
if opts.child_prefix then
items = symbols_to_items(result, core.CTX().bufnr,
opts.child_prefix == true and string.rep("\xc2\xa0", 2) or opts.child_prefix)
opts.child_prefix == true and string.rep(" ", 2) or opts.child_prefix)
else
items = vim.lsp.util.symbols_to_items(result, core.CTX().bufnr)
end
Expand Down Expand Up @@ -654,8 +654,7 @@ M.document_symbols = function(opts)
if opts.force_uri == nil then opts.force_uri = true end
if not opts.fzf_opts or opts.fzf_opts["--with-nth"] == nil then
opts.fzf_opts = opts.fzf_opts or {}
opts.fzf_opts["--with-nth"] = "2.."
opts.fzf_opts["--tiebreak"] = "index"
opts.fzf_opts["--with-nth"] = "1"
end
if opts.symbol_style or opts.symbol_fmt then
opts.fn_pre_fzf = function() gen_sym2style_map(opts) end
Expand Down
5 changes: 3 additions & 2 deletions lua/fzf-lua/providers/tags.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ local function tags(opts)
end
end

if opts.line_field_index == nil then
-- NOT NEEDED ANYMORE SINCE WE ARE FORCING BAT ASYNC PREVIEWER
--[[ if opts.line_field_index == nil then
-- if caller did not specify the line field index
-- grep the first tag with '-m 1' and test for line presence
local cmd = get_tags_cmd({
Expand All @@ -150,7 +151,7 @@ local function tags(opts)
opts.line_field_index = 0
end
end
end
end ]]

-- prevents 'file|git_icons=false' from overriding processing
opts.requires_processing = true
Expand Down

0 comments on commit c9ed5be

Please sign in to comment.