Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Dec 10, 2024
1 parent b7e4d47 commit 28c35b9
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 89 deletions.
6 changes: 6 additions & 0 deletions OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,12 @@ Type: `string`, Default: `FzfLuaBufNr`

Highlight group for buffer number in buffer type pickers, i.e. `buffers`, `tabs`, `lines`.

#### globals.hls.buf_linenr

Type: `string`, Default: `LineNr`

Highlight group for buffer line number in `lines`, `blines` and `treesitter`.

#### globals.hls.buf_flag_cur

Type: `string`, Default: `FzfLuaBufFlagCur`
Expand Down
38 changes: 5 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1106,45 +1106,16 @@ require'fzf-lua'.setup {
["--with-nth"] = '2..',
},
},
-- `blines` has the same defaults as `lines` aside from prompt and `show_bufname`
lines = {
previewer = "builtin", -- set to 'false' to disable
prompt = 'Lines❯ ',
show_bufname = true, -- display buffer name
show_unloaded = true, -- show unloaded buffers
show_unlisted = false, -- exclude 'help' buffers
no_term_buffers = true, -- exclude 'term' buffers
fzf_opts = {
-- do not include bufnr in fuzzy matching
-- tiebreak by line no.
["--delimiter"] = "[\\]:]",
["--nth"] = '2..',
["--tiebreak"] = 'index',
["--tabstop"] = "1",
},
-- actions inherit from 'actions.files' and merge
actions = {
["enter"] = actions.buf_edit_or_qf,
["alt-q"] = actions.buf_sel_to_qf,
["alt-l"] = actions.buf_sel_to_ll
},
},
blines = {
previewer = "builtin", -- set to 'false' to disable
prompt = 'BLines❯ ',
show_unlisted = true, -- include 'help' buffers
no_term_buffers = false, -- include 'term' buffers
-- start = "cursor" -- start display from cursor?
fzf_opts = {
-- hide filename, tiebreak by line no.
["--delimiter"] = "[:]",
["--with-nth"] = '2..',
["--tiebreak"] = 'index',
["--tabstop"] = "1",
},
-- actions inherit from 'actions.files' and merge
actions = {
["enter"] = actions.buf_edit_or_qf,
["alt-q"] = actions.buf_sel_to_qf,
["alt-l"] = actions.buf_sel_to_ll
},
},
tags = {
Expand Down Expand Up @@ -1418,10 +1389,11 @@ temporarily overridden by its corresponding `winopts` option:
|FzfLuaHelpBorder |FzfLuaBorder |`hls.help_border` |Help win border|
|FzfLuaHeaderBind |*BlanchedAlmond |`hls.header_bind` |Header keybind|
|FzfLuaHeaderText |*Brown1 |`hls.header_text` |Header text|
|FzfLuaPathColNr |*CadetBlue1 |`hls.path_colnr` |Path col nr (`lines,qf,lsp,diag`)|
|FzfLuaPathLineNr |*LightGreen |`hls.path_linenr` |Path line nr (`lines,qf,lsp,diag`)|
|FzfLuaPathColNr |*CadetBlue1 |`hls.path_colnr` |Path col nr (`qf,lsp,diag`)|
|FzfLuaPathLineNr |*LightGreen |`hls.path_linenr` |Path line nr (`qf,lsp,diag`)|
|FzfLuaBufName |*LightMagenta |`hls.buf_name` |Buffer name (`lines`)|
|FzfLuaBufNr |*BlanchedAlmond |`hls.buf_nr` |Buffer number (all buffers)|
|FzfLuaBufLineNr |LineNr |`hls.buf_linenr` |Buffer line nr (`lines,blines`)|
|FzfLuaBufFlagCur |*Brown1 |`hls.buf_flag_cur` |Buffer line (`buffers`)|
|FzfLuaBufFlagAlt |*CadetBlue1 |`hls.buf_flag_alt` |Buffer line (`buffers`)|
|FzfLuaTabTitle |*LightSkyBlue1 |`hls.tab_title` |Tab title (`tabs`)|
Expand Down
10 changes: 9 additions & 1 deletion doc/fzf-lua-opts.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*fzf-lua-opts.txt* For Neovim >= 0.8.0 Last change: 2024 November 18
*fzf-lua-opts.txt* For Neovim >= 0.8.0 Last change: 2024 December 06

==============================================================================
Table of Contents *fzf-lua-opts-table-of-contents*
Expand Down Expand Up @@ -766,6 +766,14 @@ Highlight group for buffer number in buffer type pickers, i.e. `buffers`,



globals.hls.buf_linenr *fzf-lua-opts-globals.hls.buf_linenr*

Type: `string`, Default: `LineNr`

Highlight group for buffer line number in `lines`, `blines` and `treesitter`.



globals.hls.buf_flag_cur *fzf-lua-opts-globals.hls.buf_flag_cur*

Type: `string`, Default: `FzfLuaBufFlagCur`
Expand Down
60 changes: 32 additions & 28 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -620,55 +620,58 @@ M.defaults.lines = {
prompt = "Lines> ",
file_icons = true and M._has_devicons,
color_icons = true,
show_bufname = true,
show_unloaded = true,
show_unlisted = false,
no_term_buffers = true,
winopts = { treesitter = true },
hls = { buf_nr = "TabLine", buf_name = "Directory" },
fzf_opts = {
["--multi"] = true,
["--delimiter"] = "[\\]:]",
["--nth"] = "2..",
["--delimiter"] = "[\t]",
["--tabstop"] = "1",
["--tiebreak"] = "index",
["--with-nth"] = "2..",
["--nth"] = "3..",
},
fzf_colors = {
["hl"] = "-1:reverse:underline",
["hl+"] = "-1:reverse:underline",
},
line_field_index = "{3}",
_fmt = {
-- NOTE: `to` is not needed, we format at the source in `buffer_lines`
to = false,
from = function(s, _)
-- restore the format to something that `path.entry_to_file` can handle
local bufnr, lnum, text = s:match("%[(%d+)%].-(%d+) (.+)$")
if not bufnr then return "" end
return string.format("[%s]%s%s:%s:%s",
bufnr, utils.nbsp,
path.tail(vim.api.nvim_buf_get_name(tonumber(bufnr))),
lnum, text)
end
},
_actions = function()
return M.globals.actions.buffers or M.globals.actions.files
end,
actions = {
["enter"] = 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", "path_linenr" },
_cached_hls = { "buf_name", "buf_nr", "buf_linenr" },
}

M.defaults.blines = {
previewer = M._default_previewer_fn,
M.defaults.blines = vim.tbl_deep_extend("force", M.defaults.lines, {
prompt = "BLines> ",
file_icons = false,
color_icons = false,
show_unlisted = true,
no_term_buffers = false,
show_bufname = false,
fzf_opts = {
["--multi"] = true,
["--delimiter"] = "[:]",
["--with-nth"] = "2..",
["--tiebreak"] = "index",
["--with-nth"] = "4..",
["--nth"] = "2..",
},
line_field_index = "{2}",
_actions = function()
return M.globals.actions.buffers or M.globals.actions.files
end,
actions = {
["enter"] = 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", "path_linenr" },
}
})

M.defaults.treesitter = {
previewer = M._default_previewer_fn,
prompt = "Treesitter> ",
show_bufname = false,
file_icons = false,
color_icons = false,
fzf_opts = {
Expand Down Expand Up @@ -1190,6 +1193,7 @@ M.defaults.__HLS = {
path_linenr = "FzfLuaPathLineNr",
buf_name = "FzfLuaBufName",
buf_nr = "FzfLuaBufNr",
buf_linenr = "FzfLuaBufLineNr",
buf_flag_cur = "FzfLuaBufFlagCur",
buf_flag_alt = "FzfLuaBufFlagAlt",
tab_title = "FzfLuaTabTitle",
Expand Down
2 changes: 2 additions & 0 deletions lua/fzf-lua/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ function M.setup_highlights(override)
{ default = default, fg = is_light and "DarkOrchid3" or "LightMagenta" } },
{ "FzfLuaBufNr", "buf_nr", -- buffers|tabs|lines|blines
{ default = default, fg = is_light and "AquaMarine3" or "BlanchedAlmond" } },
{ "FzfLuaBufLineNr", "buf_linenr", -- lines|blines|treesitter
{ default = default, link = "LineNr" } },
{ "FzfLuaBufFlagCur", "buf_flag_cur", -- buffers|tabs
{ default = default, fg = is_light and "Brown4" or "Brown1" } },
{ "FzfLuaBufFlagAlt", "buf_flag_alt", -- buffers|tabs
Expand Down
68 changes: 45 additions & 23 deletions lua/fzf-lua/providers/buffers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,6 @@ end
M.buffer_lines = function(opts)
if not opts then return end

-- formatter doesn't work with lines|blines as only filename is displayed
opts._fmt = false

opts.fn_pre_fzf = function() core.CTX(true) end
opts.fn_pre_fzf()

Expand All @@ -242,10 +239,22 @@ M.buffer_lines = function(opts)
local buffers = filter_buffers(opts,
opts.current_buffer_only and { core.CTX().bufnr } or core.CTX().buflist)

local bnames = {}
local longest_bname = 0
for _, b in ipairs(buffers) do
local bname = utils.nvim_buf_get_name(b)
if not bname:match("^%[") then
bname = path.shorten(vim.fn.fnamemodify(bname, ":~:."))
end
longest_bname = math.max(longest_bname, string.len(bname))
bnames[tostring(b)] = bname
end
local len_bufnames = math.min(15, longest_bname)

for _, bufnr in ipairs(buffers) do
local data = {}
local bufname, buficon, hl
-- use vim.schedule to avoid

-- Use vim.schedule to avoid
-- E5560: vimL function must not be called in a lua loop callback
vim.schedule(function()
local filepath = vim.api.nvim_buf_get_name(bufnr)
Expand All @@ -254,22 +263,31 @@ M.buffer_lines = function(opts)
elseif vim.fn.filereadable(filepath) ~= 0 then
data = vim.fn.readfile(filepath, "")
end
bufname = path.basename(filepath)
if opts.file_icons then
buficon, hl = devicons.get_devicon(bufname)
if hl and opts.color_icons then
buficon = utils.ansi_from_rgb(hl, buficon)
end
end
if not bufname or #bufname == 0 then
bufname = utils.nvim_buf_get_name(bufnr)
end
coroutine.resume(co)
end)

-- wait for vim.schedule
coroutine.yield()

local bname, bicon = (function()
if not opts.show_bufname then return end
local bicon, hl = "", nil
local bname = bnames[tostring(bufnr)]
assert(bname)

if string.len(bname) > len_bufnames + 1 then
bname = "" .. bname:sub(#bname - len_bufnames + 2)
end

if opts.file_icons then
bicon, hl = devicons.get_devicon(bname)
if hl and opts.color_icons then
bicon = utils.ansi_from_rgb(hl, bicon)
end
end
return bname, bicon
end)()

local offset, lines = 0, #data
if opts.current_buffer_only and opts.start == "cursor" then
-- start display from current line and wrap from bottom (#822)
Expand All @@ -281,14 +299,18 @@ M.buffer_lines = function(opts)
if lnum > lines then
lnum = lnum % lines
end
add_entry(string.format("[%s]%s%s%s%s:%s: %s",
utils.ansi_codes[opts.hls.buf_nr](tostring(bufnr)),
utils.nbsp,
buficon or "",
buficon and utils.nbsp or "",
utils.ansi_codes[opts.hls.buf_name](bufname),
utils.ansi_codes[opts.hls.path_linenr](tostring(lnum)),
data[lnum]), co)

-- NOTE: Space after `lnum` is U+00A0 (decimal: 160)
add_entry(string.format("[%s]\t%s\t%s%s%s\t%s \t%s",
tostring(bufnr),
utils.ansi_codes[opts.hls.buf_nr](string.format("%2d", bufnr)),
bicon or "",
bicon and utils.nbsp or "",
not bname and "" or utils.ansi_codes[opts.hls.buf_name](
string.format("%" .. tostring(len_bufnames) .. "s", bname)),
utils.ansi_codes[opts.hls.buf_linenr](string.format("%4d", lnum)),
data[lnum]
), co)
end
end
cb(nil)
Expand Down
17 changes: 13 additions & 4 deletions lua/fzf-lua/win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -836,12 +836,21 @@ function FzfWin:treesitter_attach()
-- file:line:col:text (grep_xxx)
-- file:line:text (grep_project or missing "--column" flag)
-- line:col:text (grep_curbuf)
-- line:text (blines)
local filepath, _lnum, text = line:match("(.-):?(%d+):(.+)$")
-- line<U+00A0>text (lines|blines)
local filepath, _lnum, text = line:match("(.-):?(%d+)[: ](.+)$")
if not text or text == 0 then return end

filepath = trim(filepath)
local ft = #filepath == 0 and vim.bo[utils.CTX().bufnr].ft

local ft_bufnr = (function()
-- NOTE: blines|lines has U+00A0 (decimal: 160) following lnum
if string.byte(text, 1) == 160 then
text = text:sub(2)
local b = filepath:match("^%d+") or utils.CTX().bufnr
return vim.api.nvim_buf_is_valid(tonumber(b)) and b or nil
end
end)()

local ft = ft_bufnr and vim.bo[tonumber(ft_bufnr)].ft
or vim.filetype.match({ filename = path.tail(filepath) })
if not ft then return end

Expand Down

0 comments on commit 28c35b9

Please sign in to comment.