Skip to content

Commit

Permalink
feat(beta): render-markdown.nvim previewer integration
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Dec 4, 2024
1 parent 175ddbb commit e0c16c1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,8 @@ require'fzf-lua'.setup {
-- Custom filetype autocmds aren't triggered on
-- the preview buffer, define them here instead
-- ext_ft_override = { ["ksql"] = "sql", ... },
-- render_markdown.nvim integration, enabled by default for markdown
render_markdown = { enable = true, filetypes = { ["markdown"] = true } },
},
-- Code Action previewers, default is "codeaction" (set via `lsp.code_actions.previewer`)
-- "codeaction_native" uses fzf's native previewer, recommended when combined with git-delta
Expand Down
1 change: 1 addition & 0 deletions lua/fzf-lua/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ M.defaults = {
treesitter = { enable = true, disable = {} },
ueberzug_scaler = "cover",
title_fnamemodify = function(s) return path.tail(s) end,
render_markdown = { enable = true, filetypes = { ["markdown"] = true } },
_ctor = previewers.builtin.buffer_or_file,
},
codeaction = {
Expand Down
25 changes: 23 additions & 2 deletions lua/fzf-lua/previewer/builtin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ function Previewer.base:new(o, opts, fzf_win)
self.win = fzf_win
self.delay = self.win.winopts.preview.delay or 100
self.title = self.win.winopts.preview.title
self.title_fnamemodify = o.title_fnamemodify
self.title_pos = self.win.winopts.preview.title_pos
self.title_fnamemodify = o.title_fnamemodify
self.render_markdown = o.render_markdown
self.render_markdown.filetypes = type(o.render_markdown.filetypes) == "table" and
o.render_markdown.filetypes or {}
self.winopts = self.win.winopts.preview.winopts
self.syntax = default(o.syntax, true)
self.syntax_delay = tonumber(default(o.syntax_delay, 0))
Expand Down Expand Up @@ -404,6 +407,7 @@ function Previewer.base:scroll(direction)
vim.wo[preview_winid].cursorline = false
end
end
self:update_render_markdown()
self.win:update_scrollbar()
end

Expand Down Expand Up @@ -650,7 +654,8 @@ function Previewer.buffer_or_file:populate_preview_buf(entry_str)
-- in case of an error display the stacktrace in the preview buffer
local lines = vim.split(res, "\n") or { "null" }
table.insert(lines, 1,
string.format("lsp.util.%s failed for '%s':", utils.__HAS_NVIM_011 and "show_document" or "jump_to_location", entry.uri))
string.format("lsp.util.%s failed for '%s':",
utils.__HAS_NVIM_011 and "show_document" or "jump_to_location", entry.uri))
table.insert(lines, 2, "")
local tmpbuf = self:get_tmp_buffer()
vim.api.nvim_buf_set_lines(tmpbuf, 0, -1, false, lines)
Expand Down Expand Up @@ -766,6 +771,20 @@ local ts_attach = function(bufnr, ft)
end
end

function Previewer.base:update_render_markdown(ft)
local bufnr, winid = self.preview_bufnr, self.win.preview_winid
ft = ft or vim.bo[bufnr].ft
if not ft then return end
if not package.loaded["render-markdown"]
or not self.render_markdown.enable
or not self.render_markdown.filetypes[ft]
then
return
end
vim.bo[bufnr].ft = ft
require("render-markdown.core.ui").update(bufnr, winid, "FzfLua", true)
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 @@ -829,6 +848,8 @@ function Previewer.buffer_or_file:do_syntax(entry)
end
if not ts_enabled or not ts_success then
pcall(function() vim.bo[bufnr].syntax = ft end)
elseif ts_enabled and ts_success then
self:update_render_markdown(ft)
end
end)()
end
Expand Down

5 comments on commit e0c16c1

@daliusd
Copy link

@daliusd daliusd commented on e0c16c1 Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has broken FzfLua register_ui_select it seems.

Error executing vim.schedule lua callback: ...iusd/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:245: ...hare/nvim/lazy/fzf-lua/lua/
fzf-lua/previewer/builtin.lua:33: attempt to index field 'render_markdown' (a nil value)
stack traceback:
        [builtin#36]: at 0x75d13a59e300
        ...iusd/.local/share/nvim/lazy/fzf-lua/lua/fzf-lua/core.lua:245: in function 'fzf_exec'
        ...re/nvim/lazy/fzf-lua/lua/fzf-lua/providers/ui_select.lua:166: in function 'select'
        /usr/share/nvim/runtime/lua/vim/lsp/buf.lua:1142: in function 'on_code_action_results'
        /usr/share/nvim/runtime/lua/vim/lsp/buf.lua:1186: in function 'handler'
        /usr/share/nvim/runtime/lua/vim/lsp/client.lua:681: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

@ibhagwan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ty for letting me know @daliusd, update to HEAD, 77a8d4a should fix it.

@daliusd
Copy link

@daliusd daliusd commented on e0c16c1 Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not fixed. I see there is PR attempt to fix this from somebody #1545

@ibhagwan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You’re correct, force pushed, should be ok now.

@daliusd
Copy link

@daliusd daliusd commented on e0c16c1 Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. It is working now.

Please sign in to comment.