Fix help preview and add highlighting to search result (#124)
* fix: make sure that `$VIMRUNTIME/doc/tags` is in `tags` path * feat: highlight matched topic in helptags preview * fixup: Use previewer setup and teardown Co-authored-by: TJ DeVries <devries.timothyj@gmail.com>
This commit is contained in:
@@ -163,8 +163,7 @@ function actions.close(prompt_bufnr)
|
|||||||
vim.api.nvim_win_close(prompt_win, true)
|
vim.api.nvim_win_close(prompt_win, true)
|
||||||
|
|
||||||
pcall(vim.cmd, string.format([[silent bdelete! %s]], prompt_bufnr))
|
pcall(vim.cmd, string.format([[silent bdelete! %s]], prompt_bufnr))
|
||||||
|
pcall(a.nvim_set_current_win, original_win_id)
|
||||||
a.nvim_set_current_win(original_win_id)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
actions.set_command_line = function(prompt_bufnr)
|
actions.set_command_line = function(prompt_bufnr)
|
||||||
|
|||||||
@@ -368,13 +368,29 @@ end, {})
|
|||||||
|
|
||||||
previewers.help = defaulter(function(_)
|
previewers.help = defaulter(function(_)
|
||||||
return previewers.new {
|
return previewers.new {
|
||||||
preview_fn = function(_, entry, status)
|
setup = function()
|
||||||
|
return {}
|
||||||
|
end,
|
||||||
|
|
||||||
|
teardown = function(self)
|
||||||
|
if self.state and self.state.hl_id then
|
||||||
|
pcall(vim.fn.matchdelete, self.state.hl_id, self.state.hl_win)
|
||||||
|
self.state.hl_id = nil
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
|
preview_fn = function(self, entry, status)
|
||||||
with_preview_window(status, nil, function()
|
with_preview_window(status, nil, function()
|
||||||
local special_chars = ":~^.?/%[%]%*"
|
local special_chars = ":~^.?/%[%]%*"
|
||||||
|
|
||||||
local escaped = vim.fn.escape(entry.value, special_chars)
|
local escaped = vim.fn.escape(entry.value, special_chars)
|
||||||
local tagfile = vim.fn.expand("$VIMRUNTIME") .. '/doc/tags'
|
local tagfile = vim.fn.expand("$VIMRUNTIME") .. '/doc/tags'
|
||||||
|
local old_tags = vim.o.tags
|
||||||
|
|
||||||
|
vim.o.tags = tagfile
|
||||||
local taglist = vim.fn.taglist('^' .. escaped .. '$', tagfile)
|
local taglist = vim.fn.taglist('^' .. escaped .. '$', tagfile)
|
||||||
|
vim.o.tags = old_tags
|
||||||
|
|
||||||
if vim.tbl_isempty(taglist) then
|
if vim.tbl_isempty(taglist) then
|
||||||
taglist = vim.fn.taglist(escaped, tagfile)
|
taglist = vim.fn.taglist(escaped, tagfile)
|
||||||
end
|
end
|
||||||
@@ -385,6 +401,7 @@ previewers.help = defaulter(function(_)
|
|||||||
|
|
||||||
local best_entry = taglist[1]
|
local best_entry = taglist[1]
|
||||||
local new_bufnr = vim.fn.bufnr(best_entry.filename, true)
|
local new_bufnr = vim.fn.bufnr(best_entry.filename, true)
|
||||||
|
|
||||||
vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help')
|
vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help')
|
||||||
vim.api.nvim_win_set_buf(status.preview_win, new_bufnr)
|
vim.api.nvim_win_set_buf(status.preview_win, new_bufnr)
|
||||||
|
|
||||||
@@ -402,6 +419,9 @@ previewers.help = defaulter(function(_)
|
|||||||
vim.cmd "norm! gg"
|
vim.cmd "norm! gg"
|
||||||
vim.fn.search(search_query, "W")
|
vim.fn.search(search_query, "W")
|
||||||
vim.cmd "norm zt"
|
vim.cmd "norm zt"
|
||||||
|
|
||||||
|
self.state.hl_win = status.preview_win
|
||||||
|
self.state.hl_id = vim.fn.matchadd('Search', search_query)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user