diff options
| author | TJ DeVries <devries.timothyj@gmail.com> | 2020-08-31 22:23:12 -0400 |
|---|---|---|
| committer | TJ DeVries <devries.timothyj@gmail.com> | 2020-08-31 22:23:12 -0400 |
| commit | 51ed9c3e98541a0e85120ea322acfe9abdfcfa02 (patch) | |
| tree | 01026b4a051a9a64a3ab676b35740d0b29869411 /lua/telescope/previewers.lua | |
| parent | 8cf3952f27a3f7895100ff547cf1c8cdfeee72f7 (diff) | |
Add some WIP stuff
Diffstat (limited to 'lua/telescope/previewers.lua')
| -rw-r--r-- | lua/telescope/previewers.lua | 68 |
1 files changed, 52 insertions, 16 deletions
diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index 4743639..673e2a7 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -1,3 +1,5 @@ +local context_manager = require('plenary.context_manager') + local log = require('telescope.log') local previewers = {} @@ -34,6 +36,14 @@ previewers.new = function(...) return Previewer:new(...) end +local with_preview_window = function(status, callable) + return context_manager.with(function() + vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.preview_win)) + coroutine.yield() + vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.prompt_win)) + end, callable) +end + previewers.new_termopen = function(opts) local entry_value = opts.get_value or function(entry) return entry.value @@ -47,10 +57,9 @@ previewers.new_termopen = function(opts) vim.api.nvim_win_set_buf(status.preview_win, bufnr) - -- HACK! Requires `termopen` to accept buffer argument. - vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.preview_win)) - vim.fn.termopen(string.format(command_string, entry_value(entry))) - vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.prompt_win)) + with_preview_window(status, function() + vim.fn.termopen(string.format(command_string, entry_value(entry))) + end) end } end @@ -133,10 +142,9 @@ previewers.cat = previewers.new { vim.api.nvim_win_set_buf(status.preview_win, bufnr) - -- HACK! Requires `termopen` to accept buffer argument. - vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.preview_win)) - vim.fn.termopen(string.format(self.state.command_string, entry.value)) - vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.prompt_win)) + with_preview_window(status, function() + vim.fn.termopen(string.format(self.state.command_string, entry.value)) + end) vim.api.nvim_buf_set_name(bufnr, tostring(bufnr)) end @@ -174,10 +182,9 @@ previewers.vimgrep = previewers.new { local termopen_command = string.format(self.state.command_string, filename, lnum, start, finish) - -- HACK! Requires `termopen` to accept buffer argument. - vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.preview_win)) - vim.fn.termopen(termopen_command) - vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.prompt_win)) + with_preview_window(status, function() + vim.fn.termopen(termopen_command) + end) end } @@ -210,10 +217,39 @@ previewers.qflist = previewers.new { local termopen_command = string.format(self.state.command_string, filename, lnum, start, finish) - -- HACK! Requires `termopen` to accept buffer argument. - vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.preview_win)) - vim.fn.termopen(termopen_command) - vim.cmd(string.format("noautocmd call win_gotoid(%s)", status.prompt_win)) + with_preview_window(status, function() + vim.fn.termopen(termopen_command) + end) + end +} + +previewers.help = previewers.new { + preview_fn = function(_, entry, status) + with_preview_window(status, function() + local old_tags = vim.o.tags + vim.o.tags = vim.fn.expand("$VIMRUNTIME") .. '/doc/tags' + + local taglist = vim.fn.taglist('^' .. entry.value .. '$') + if vim.tbl_isempty(taglist) then + taglist = vim.fn.taglist(entry.value) + end + + if vim.tbl_isempty(taglist) then + return + end + + local best_entry = taglist[1] + local new_bufnr = vim.fn.bufnr(best_entry.filename, true) + + vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help') + vim.api.nvim_win_set_buf(status.preview_win, new_bufnr) + + vim.cmd [["gg"]] + print(best_entry.cmd) + vim.cmd(string.format([[execute "%s"]], best_entry.cmd)) + + vim.o.tags = old_tags + end) end } |
