diff --git a/lua/telescope/WIP.lua b/lua/telescope/WIP.lua index cab745a..1ababec 100644 --- a/lua/telescope/WIP.lua +++ b/lua/telescope/WIP.lua @@ -33,26 +33,25 @@ end -- TODO: Make it so that when you select stuff, it's inserted -- TODO: Make it so the previewer shows the help text. -WIP.completion = function() +WIP.completion = function(opts) local results = {} - for k, v in pairs(vim.api) do - table.insert(results, k) + for k, _ in pairs(vim.api) do + table.insert(results, k .. "()") end local lsp_reference_finder = finders.new { results = results } - -- local reference_previewer = previewers.qflist - local reference_picker = pickers.new { - -- previewer = reference_previewer - } - - reference_picker:find { - prompt = 'LSP References', + -- TODO: Open the help text for the line. + local reference_picker = pickers.new(opts, { + prompt = 'vim.api Help Reference', finder = lsp_reference_finder, sorter = sorters.get_norcalli_sorter(), - } + previewer = previewers.help, + }) + + reference_picker:find {} end -- TODO: Use tree sitter to get "everything" in your current scope / file / etc. 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 } diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 2533c12..cd1c20c 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -96,7 +96,7 @@ utils.path_shorten = (function() ]] return function(path) - local c_str = ffi.new("char[?]", #path) + local c_str = ffi.new("char[?]", #path + 1) ffi.copy(c_str, path) return ffi.string(ffi.C.shorten_dir(c_str)) end