Skip to content

Commit

Permalink
feat(previewer): **BETA** display nvim-treesitter-context
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Dec 6, 2024
1 parent a569876 commit 1144eaa
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions lua/fzf-lua/previewer/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,32 @@ function Previewer.base:update_render_markdown()
end
end

function Previewer.base:update_ts_context(close)
-- excerpt from nvim-treesitter-context `update_single_context`
if not package.loaded["treesitter-context"] then return end
local bufnr, winid = self.preview_bufnr, self.win.preview_winid
if close then
-- Original cursorline (match) out of bounds, hide ts-context
require("treesitter-context.render").close(winid)
return
end
-- Temporarily set "zindex" higher than fzf-lua
local config = require("treesitter-context.config")
local saved_zindex = config.zindex
config.zindex = self.win._o.winopts.zindex + 20
require("treesitter-context.render").close_leaked_contexts()
local context_ranges, context_lines = require("treesitter-context.context").get(bufnr, winid)
if not context_ranges or #context_ranges == 0 then
require("treesitter-context.render").close(winid)
else
assert(context_lines)
require("treesitter-context.render").open(bufnr, winid, context_ranges, context_lines)
-- _G.dump("ts", bufnr, winid, context_ranges, context_lines)
end
-- Restore saved "zindex"
config.zindex = saved_zindex
end

function Previewer.buffer_or_file:do_syntax(entry)
if not self.preview_bufnr then return end
if not entry or not entry.path then return end
Expand Down Expand Up @@ -845,7 +871,6 @@ function Previewer.buffer_or_file:do_syntax(entry)
end

function Previewer.buffer_or_file:maybe_set_cursorline(win, pos)
if not self.winopts.cursorline then return end
local wininfo = utils.getwininfo(win)
if wininfo
and pos[1] >= wininfo.topline
Expand All @@ -854,9 +879,11 @@ function Previewer.buffer_or_file:maybe_set_cursorline(win, pos)
-- reset cursor pos even when it's already there, no bigggie
-- local curpos = vim.api.nvim_win_get_cursor(win)
vim.api.nvim_win_set_cursor(win, pos)
vim.wo[win].cursorline = true
vim.wo[win].cursorline = self.winopts.cursorline
self:update_ts_context()
else
vim.wo[win].cursorline = false
self:update_ts_context(true)
end
end

Expand Down

0 comments on commit 1144eaa

Please sign in to comment.