Skip to content

Commit 6175f20

Browse files
committed
WIP
1 parent e1eab46 commit 6175f20

File tree

16 files changed

+314
-480
lines changed

16 files changed

+314
-480
lines changed

OPTIONS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ Scrollbar style in the builtin previewer, set to `false` to disable, possible va
382382

383383
#### globals.winopts.preview.scrolloff
384384

385-
Type: `number`, Default: `-2`
385+
Type: `number`, Default: `-1`
386386

387387
Float style scrollbar offset from the right edge of the preview window.
388388

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# fzf :heart: lua
44

5-
![Neovim version](https://img.shields.io/badge/Neovim-0.5-57A143?style=flat-square&logo=neovim)
5+
![Neovim version](https://img.shields.io/badge/Neovim-0.7-57A143?style=flat-square&logo=neovim)
66

77
[Quickstart](#quickstart)[Installation](#installation)[Usage](#usage)[Commands](#commands)[Customization](#customization)[Wiki](https://github.com/ibhagwan/fzf-lua/wiki)
88

@@ -55,7 +55,7 @@ Using [lazy.nvim](https://github.com/folke/lazy.nvim)
5555

5656
### Dependencies
5757

58-
- [`neovim`](https://github.com/neovim/neovim/releases) version > `0.5.0`
58+
- [`neovim`](https://github.com/neovim/neovim/releases) version > `0.7.0`
5959
- [`fzf`](https://github.com/junegunn/fzf) version > `0.25`
6060
or [`skim`](https://github.com/skim-rs/skim) binary installed
6161
- [nvim-web-devicons](https://github.com/nvim-tree/nvim-web-devicons)
@@ -439,7 +439,7 @@ winopts = {
439439
scrollbar = 'float', -- `false` or string:'float|border'
440440
-- float: in-window floating border
441441
-- border: in-border "block" marker
442-
scrolloff = '-2', -- float scrollbar offset from right
442+
scrolloff = -1, -- float scrollbar offset from right
443443
-- applies only when scrollbar = 'float'
444444
delay = 20, -- delay(ms) displaying the preview
445445
-- prevents lag on fast scrolling

doc/fzf-lua-opts.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*fzf-lua-opts.txt* For Neovim >= 0.8.0 Last change: 2024 December 18
1+
*fzf-lua-opts.txt* For Neovim >= 0.8.0 Last change: 2024 December 27
22

33
==============================================================================
44
Table of Contents *fzf-lua-opts-table-of-contents*
@@ -504,7 +504,7 @@ values are `float|border`.
504504

505505
globals.winopts.preview.scrolloff*fzf-lua-opts-globals.winopts.preview.scrolloff*
506506

507-
Type: `number`, Default: `-2`
507+
Type: `number`, Default: `-1`
508508

509509
Float style scrollbar offset from the right edge of the preview window.
510510

lua/fzf-lua/core.lua

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,35 @@ M.fzf = function(contents, opts)
464464
return selected
465465
end
466466

467+
-- Best approximation of neovim border types to fzf border types
468+
local function translate_border(border)
469+
local neovim2fzf = {
470+
none = "noborder",
471+
single = "border-sharp",
472+
double = "border-double",
473+
rounded = "border-rounded",
474+
solid = "noborder",
475+
empty = "border-block",
476+
shadow = "border-thinblock",
477+
bold = "border-bold",
478+
block = "border-block",
479+
solidblock = "border-block",
480+
thicc = "border-bold",
481+
thiccc = "border-block",
482+
thicccc = "border-block",
483+
}
484+
border = type(border) == "string" and (neovim2fzf[border] or border) or "rounded"
485+
return border
486+
end
487+
467488
---@param o table
468489
---@return string
469490
M.preview_window = function(o, fzf_win)
470491
local layout
471492
local prefix = string.format("%s:%s:%s",
472-
o.winopts.preview.hidden, o.winopts.preview.border, o.winopts.preview.wrap)
493+
o.winopts.preview.hidden,
494+
translate_border(o.winopts.preview.border),
495+
o.winopts.preview.wrap)
473496
if utils.has(o, "fzf", { 0, 31 })
474497
and o.winopts.preview.layout == "flex"
475498
and tonumber(o.winopts.preview.flip_columns) > 0

lua/fzf-lua/defaults.lua

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ M.defaults = {
6565
title = true,
6666
title_pos = "center",
6767
scrollbar = "border",
68-
scrolloff = "-2",
68+
scrolloff = -1,
6969
-- default preview delay, fzf native previewers has a 100ms delay:
7070
-- https://github.com/junegunn/fzf/issues/2417#issuecomment-809886535
7171
delay = 20,
@@ -1255,27 +1255,4 @@ M.defaults.__HLS = {
12551255
}
12561256
}
12571257

1258-
M.defaults.__WINOPTS = {
1259-
borderchars = {
1260-
["none"] = { "", "", "", "", "", "", "", "" },
1261-
["empty"] = { " ", " ", " ", " ", " ", " ", " ", " " },
1262-
["single"] = { "", "", "", "", "", "", "", "" },
1263-
["double"] = { "", "", "", "", "", "", "", "" },
1264-
["rounded"] = { "", "", "", "", "", "", "", "" },
1265-
["thicc"] = { "", "", "", "", "", "", "", "" },
1266-
["thiccc"] = { "", "", "", "", "", "", "", "" },
1267-
["thicccc"] = { "", "", "", "", "", "", "", "" },
1268-
},
1269-
-- border chars reverse lookup for ambiwidth="double"
1270-
border2string = {
1271-
[" "] = "empty",
1272-
[""] = "single",
1273-
[""] = "double",
1274-
[""] = "rounded",
1275-
[""] = "double",
1276-
[""] = "double",
1277-
[""] = "double",
1278-
},
1279-
}
1280-
12811258
return M

lua/fzf-lua/devicons.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747
function NvimWebDevicons:load(do_not_lazy_load)
4848
-- limit devicons support to nvim >=0.8, although official support is >=0.7
4949
-- running setup on 0.7 errs with "W18: Invalid character in group name"
50-
if not self._package_loaded and utils.__HAS_NVIM_07
50+
if not self._package_loaded
5151
-- do not trigger lazy loading
5252
and (not do_not_lazy_load or package.loaded[self._package_name])
5353
then

lua/fzf-lua/fzf.lua

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,24 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
221221
-- sending `feedkeys|startinsert` after the term job is started, this approach
222222
-- seems more consistent as it triggers when entering terminal normal mode "nt"
223223
-- NOTE: "ModeChanged" was added with neovim 0.7
224-
if utils.__HAS_NVIM_07 then
225-
vim.api.nvim_create_autocmd("ModeChanged", {
226-
once = true,
227-
buffer = 0,
228-
callback = function(e)
229-
if e.match:match(":nt") then
230-
vim.defer_fn(function()
231-
-- Prevents inserting "i" when spamming `ctrl-g` in `grep_lgrep`
232-
-- Also verify we're not already in TERMINAL mode, could happen
233-
-- if the user has an autocmd for TermOpen with `startinsert`
234-
if vim.api.nvim_buf_is_valid(e.buf)
235-
and vim.api.nvim_get_mode().mode ~= "t"
236-
then
237-
vim.cmd("startinsert")
238-
end
239-
end, 0)
240-
end
224+
vim.api.nvim_create_autocmd("ModeChanged", {
225+
once = true,
226+
buffer = 0,
227+
callback = function(e)
228+
if e.match:match(":nt") then
229+
vim.defer_fn(function()
230+
-- Prevents inserting "i" when spamming `ctrl-g` in `grep_lgrep`
231+
-- Also verify we're not already in TERMINAL mode, could happen
232+
-- if the user has an autocmd for TermOpen with `startinsert`
233+
if vim.api.nvim_buf_is_valid(e.buf)
234+
and vim.api.nvim_get_mode().mode ~= "t"
235+
then
236+
vim.cmd("startinsert")
237+
end
238+
end, 0)
241239
end
242-
})
243-
end
240+
end
241+
})
244242
end
245243

246244
if opts.debug then
@@ -318,22 +316,6 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
318316
-- fzf-tmux spawns outside neovim, don't set filetype/insert mode
319317
if not opts.is_fzf_tmux then
320318
vim.bo.filetype = "fzf"
321-
322-
-- https://github.com/neovim/neovim/pull/15878
323-
-- Since patch-8.2.3461 which was released with 0.6 neovim distinguishes between
324-
-- Normal mode and Terminal-Normal mode. However, this seems to have also introduced
325-
-- a bug with `startinsert`: When fzf-lua reuses interfaces (e.g. called from "builtin"
326-
-- or grep<->live_grep toggle) the current mode will be "t" which is Terminal (INSERT)
327-
-- mode but our interface is still opened in NORMAL mode, either `startinsert` is not
328-
-- working (as it's technically already in INSERT) or creating a new terminal buffer
329-
-- within the same window starts in NORMAL mode while returning the wrong `nvim_get_mode`
330-
if not utils.__HAS_NVIM_07 then
331-
if utils.__HAS_NVIM_06 and vim.api.nvim_get_mode().mode == "t" then
332-
utils.feed_keys_termcodes("i")
333-
else
334-
vim.cmd [[startinsert]]
335-
end
336-
end
337319
end
338320

339321
if not utils.__IS_WINDOWS

lua/fzf-lua/init.lua

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,12 @@ function M.setup_highlights(override)
127127
hl_def.default = false
128128
end
129129
end
130-
if utils.__HAS_NVIM_07 then
131-
vim.api.nvim_set_hl(0, hl_name, hl_def)
132-
else
133-
if hl_def.link then
134-
vim.cmd(string.format("hi! %s link %s %s",
135-
hl_def.default and "default" or "",
136-
hl_name, hl_def.link))
137-
else
138-
vim.cmd(string.format("hi! %s %s %s%s%s",
139-
hl_def.default and "default" or "", hl_name,
140-
hl_def.fg and string.format(" guifg=%s", hl_def.fg) or "",
141-
hl_def.bg and string.format(" guibg=%s", hl_def.bg) or "",
142-
hl_def.bold and " gui=bold" or ""))
143-
end
144-
end
130+
vim.api.nvim_set_hl(0, hl_name, hl_def)
145131
end
146132

147133
-- linking to a cleared hl is bugged in neovim 0.8.x
148134
-- resulting in a pink background for hls linked to `Normal`
149-
if vim.fn.has("nvim-0.9") == 0 and vim.fn.has("nvim-0.8") == 1 then
135+
if not utils.__HAS_NVIM_09 and utils.__HAS_NVIM_08 then
150136
for _, a in ipairs(highlights) do
151137
local hl_name, opt_name = a[1], a[2]
152138
if utils.is_hl_cleared(hl_name) then

lua/fzf-lua/previewer/builtin.lua

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,18 @@ function TSContext.update(winid, bufnr, opts)
9999
TSContext._winids[tostring(winid)] = bufnr
100100
end
101101
end
102-
if TSContext.is_attached(winid) == bufnr then
102+
-- NOTE: when previewer win has border, calling `open` on the same win/buf combo
103+
-- while using `winopts.previewer.border=none` or "noboder" works correctly, most
104+
-- likely an upstream issue and we've seen no cooperation from upstream so defer
105+
-- to the same "hack"
106+
-- local open_once = false
107+
local open_once = TSContext.is_attached(winid) == bufnr
108+
if open_once then
103109
open()
104110
else
105111
-- HACK: but the entire nvim-treesitter-context is essentially a hack
106112
-- https://github.com/ibhagwan/fzf-lua/issues/1552#issuecomment-2525456813
107-
for _, t in ipairs({ 0, 20 }) do
113+
for _, t in ipairs({ 0, 20, 50 }) do
108114
vim.defer_fn(function() open() end, t)
109115
end
110116
end
@@ -322,8 +328,7 @@ function Previewer.base:clear_preview_buf(newbuf)
322328
retbuf = self:get_tmp_buffer()
323329
utils.win_set_buf_noautocmd(self.win.preview_winid, retbuf)
324330
-- redraw the title line and clear the scrollbar
325-
self.win:redraw_preview_border()
326-
self.win:update_scrollbar(true)
331+
self.win:update_preview_scrollbar(true)
327332
end
328333
-- since our temp buffers have 'bufhidden=wipe' the tmp
329334
-- buffer will be automatically wiped and 'nvim_buf_is_valid'
@@ -372,10 +377,6 @@ function Previewer.base:display_entry(entry_str)
372377
local populate_preview_buf = function(entry_str_)
373378
if not self.win or not self.win:validate_preview() then return end
374379

375-
-- redraw the preview border, resets title
376-
-- border scrollbar and border highlights
377-
self.win:redraw_preview_border()
378-
379380
-- specialized previewer populate function
380381
self:populate_preview_buf(entry_str_)
381382

@@ -519,7 +520,7 @@ function Previewer.base:scroll(direction)
519520
end
520521
self:update_ts_context()
521522
self:update_render_markdown()
522-
self.win:update_scrollbar()
523+
self.win:update_preview_scrollbar()
523524
end
524525

525526
function Previewer.base:ts_ctx_toggle()
@@ -1078,31 +1079,29 @@ function Previewer.buffer_or_file:set_cursor_hl(entry)
10781079
end)
10791080
end
10801081

1081-
function Previewer.buffer_or_file:update_border(entry)
1082-
if self.title then
1083-
local filepath = entry.path
1084-
if filepath then
1085-
if filepath:match("^%[DEBUG]") then
1086-
filepath = "[DEBUG]"
1087-
else
1088-
if self.opts.cwd then
1089-
filepath = path.relative_to(entry.path, self.opts.cwd)
1090-
end
1091-
filepath = path.HOME_to_tilde(filepath)
1082+
function Previewer.buffer_or_file:update_title(entry)
1083+
if not self.title then return end
1084+
local filepath = entry.path
1085+
if filepath then
1086+
if filepath:match("^%[DEBUG]") then
1087+
filepath = "[DEBUG]"
1088+
else
1089+
if self.opts.cwd then
1090+
filepath = path.relative_to(entry.path, self.opts.cwd)
10921091
end
1092+
filepath = path.HOME_to_tilde(filepath)
10931093
end
1094-
local title = filepath or entry.uri or entry.bufname
1095-
-- was transform function defined?
1096-
if self.title_fnamemodify then
1097-
local wincfg = vim.api.nvim_win_get_config(self.win.border_winid)
1098-
title = self.title_fnamemodify(title, wincfg and wincfg.width)
1099-
end
1100-
if entry.bufnr then
1101-
title = string.format("buf %d: %s", entry.bufnr, title)
1102-
end
1103-
self.win:update_title(" " .. title .. " ")
11041094
end
1105-
self.win:update_scrollbar(entry.no_scrollbar)
1095+
local title = filepath or entry.uri or entry.bufname
1096+
-- was transform function defined?
1097+
if self.title_fnamemodify then
1098+
local wincfg = vim.api.nvim_win_get_config(self.win.preview_winid)
1099+
title = self.title_fnamemodify(title, wincfg and wincfg.width)
1100+
end
1101+
if entry.bufnr then
1102+
title = string.format("buf %d: %s", entry.bufnr, title)
1103+
end
1104+
self.win:update_preview_title(" " .. title .. " ")
11061105
end
11071106

11081107
function Previewer.buffer_or_file:preview_buf_post(entry, min_winopts)
@@ -1127,7 +1126,8 @@ function Previewer.buffer_or_file:preview_buf_post(entry, min_winopts)
11271126
end
11281127
end
11291128

1130-
self:update_border(entry)
1129+
self:update_title(entry)
1130+
self.win:update_preview_scrollbar(entry.no_scrollbar)
11311131

11321132
-- save the loaded entry so we can compare
11331133
-- bufnr|path with the next entry. If equal
@@ -1222,7 +1222,7 @@ function Previewer.man_pages:populate_preview_buf(entry_str)
12221222
vim.api.nvim_buf_set_lines(tmpbuf, 0, -1, false, output)
12231223
vim.bo[tmpbuf].filetype = self.filetype
12241224
self:set_preview_buf(tmpbuf)
1225-
self.win:update_scrollbar()
1225+
self.win:update_preview_scrollbar()
12261226
end
12271227

12281228
Previewer.marks = Previewer.buffer_or_file:extend()
@@ -1393,7 +1393,7 @@ function Previewer.highlights:populate_preview_buf(entry_str)
13931393
self.orig_pos = api.nvim_win_get_cursor(0)
13941394
utils.zz()
13951395
end)
1396-
self.win:update_scrollbar()
1396+
self.win:update_preview_scrollbar()
13971397
end
13981398

13991399
Previewer.quickfix = Previewer.base:extend()
@@ -1444,8 +1444,8 @@ function Previewer.quickfix:populate_preview_buf(entry_str)
14441444
vim.api.nvim_buf_set_lines(self.tmpbuf, 0, -1, false, lines)
14451445
vim.bo[self.tmpbuf].filetype = "qf"
14461446
self:set_preview_buf(self.tmpbuf)
1447-
self.win:update_title(string.format("%s: %s", nr, qf_list.title))
1448-
self.win:update_scrollbar()
1447+
self.win:update_preview_title(string.format("%s: %s", nr, qf_list.title))
1448+
self.win:update_preview_scrollbar()
14491449
end
14501450

14511451
Previewer.autocmds = Previewer.buffer_or_file:extend()

lua/fzf-lua/previewer/codeaction.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ function M.builtin:populate_preview_buf(entry_str)
263263
vim.api.nvim_buf_set_lines(self.tmpbuf, 0, -1, false, lines)
264264
vim.bo[self.tmpbuf].filetype = "git"
265265
self:set_preview_buf(self.tmpbuf)
266-
self.win:update_title(string.format(" Action #%d ", idx))
267-
self.win:update_scrollbar()
266+
self.win:update_preview_title(string.format(" Action #%d ", idx))
267+
self.win:update_preview_scrollbar()
268268
end
269269

270270
M.native = native.base:extend()

0 commit comments

Comments
 (0)