diff options
| author | TJ DeVries <devries.timothyj@gmail.com> | 2020-09-04 09:49:10 -0400 |
|---|---|---|
| committer | TJ DeVries <devries.timothyj@gmail.com> | 2020-09-04 09:49:10 -0400 |
| commit | 14310ee6b1f40425ba854d6ddc6374960fdf81ca (patch) | |
| tree | c6ea9c7a2acb62f853e91b4d1544b899a8af9f3e /lua/telescope/previewers.lua | |
| parent | 996f69465ed51856aa18093d88795fae2b8565f4 (diff) | |
fix: Don't push past midnight. You'll make clason's stuff break :/
Diffstat (limited to 'lua/telescope/previewers.lua')
| -rw-r--r-- | lua/telescope/previewers.lua | 366 |
1 files changed, 193 insertions, 173 deletions
diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua index c1acbee..6df7f78 100644 --- a/lua/telescope/previewers.lua +++ b/lua/telescope/previewers.lua @@ -1,6 +1,9 @@ local context_manager = require('plenary.context_manager') local log = require('telescope.log') +local utils = require('telescope.utils') + +local defaulter = utils.make_default_callable local previewers = {} @@ -59,12 +62,8 @@ local with_preview_window = function(status, callable) end, callable) end -previewers.new_termopen = function(opts) - local entry_value = opts.get_value or function(entry) - return entry.value - end - - local command_string = opts.command +previewers.termopen = defaulter(function(opts) + local command_string = assert(opts.command, 'opts.command is required') return previewers.new { preview_fn = function(_, entry, status) @@ -73,79 +72,45 @@ previewers.new_termopen = function(opts) vim.api.nvim_win_set_buf(status.preview_win, bufnr) with_preview_window(status, function() - vim.fn.termopen(string.format(command_string, entry_value(entry))) + vim.fn.termopen(string.format(command_string, entry.value)) end) end } -end - -previewers.vim_buffer = previewers.new { - setup = function() return { last_set_bufnr = nil } end, - - teardown = function(self) - if self.state.last_set_bufnr then - vim.api.nvim_buf_clear_namespace(self.state.last_set_bufnr, previewer_ns, 0, -1) - end - end, - - preview_fn = function(self, entry, status) - local filename = entry.filename - - if filename == nil then - local value = entry.value - filename = vim.split(value, ":")[1] - end +end, {}) - if filename == nil then - return - end - - log.trace("Previewing File: %s", filename) - - local bufnr = vim.fn.bufnr(filename) - if bufnr == -1 then - -- TODO: Is this the best way to load the buffer?... I'm not sure tbh - bufnr = vim.fn.bufadd(bufnr) - vim.fn.bufload(bufnr) - end - - self.state.last_set_bufnr = bufnr - - -- TODO: We should probably call something like this because we're not always getting highlight and all that stuff. - -- api.nvim_command('doautocmd filetypedetect BufRead ' .. vim.fn.fnameescape(filename)) - vim.api.nvim_win_set_buf(status.preview_win, bufnr) - vim.api.nvim_win_set_option(status.preview_win, 'wrap', false) - vim.api.nvim_win_set_option(status.preview_win, 'winhl', 'Normal:Normal') - -- vim.api.nvim_win_set_option(preview_win, 'winblend', 20) - vim.api.nvim_win_set_option(status.preview_win, 'signcolumn', 'no') - vim.api.nvim_win_set_option(status.preview_win, 'foldlevel', 100) +previewers.vim_buffer = defaulter(function(_) + return previewers.new { + setup = function() return { last_set_bufnr = nil } end, - if entry.lnum then - vim.api.nvim_buf_add_highlight(bufnr, previewer_ns, "Visual", entry.lnum - 1, 0, -1) - -- print("LNUM:", entry.lnum) - end - end, -} + teardown = function(self) + if self.state.last_set_bufnr then + vim.api.nvim_buf_clear_namespace(self.state.last_set_bufnr, previewer_ns, 0, -1) + end + end, + preview_fn = function(self, entry, status) + -- TODO: Consider using path here? Might not work otherwise. + local filename = entry.filename -previewers.vim_buffer_or_bat = previewers.new { - preview_fn = function(_, entry, status) - local value = entry.value - if value == nil then - return - end + if filename == nil then + local value = entry.value + filename = vim.split(value, ":")[1] + end - local file_name = vim.split(value, ":")[1] + if filename == nil then + return + end - log.trace("Previewing File: %s", file_name) + log.trace("Previewing File: %s", filename) - -- vim.fn.termopen( - -- string.format("bat --color=always --style=grid %s"), - -- vim.fn.fnamemodify(file_name, ":p") - local bufnr = vim.fn.bufadd(file_name) + local bufnr = vim.fn.bufnr(filename) + if bufnr == -1 then + -- TODO: Is this the best way to load the buffer?... I'm not sure tbh + bufnr = vim.fn.bufadd(bufnr) + vim.fn.bufload(bufnr) + end - if vim.api.nvim_buf_is_loaded(bufnr) then - vim.fn.bufload(bufnr) + self.state.last_set_bufnr = bufnr -- TODO: We should probably call something like this because we're not always getting highlight and all that stuff. -- api.nvim_command('doautocmd filetypedetect BufRead ' .. vim.fn.fnameescape(filename)) @@ -155,145 +120,200 @@ previewers.vim_buffer_or_bat = previewers.new { -- vim.api.nvim_win_set_option(preview_win, 'winblend', 20) vim.api.nvim_win_set_option(status.preview_win, 'signcolumn', 'no') vim.api.nvim_win_set_option(status.preview_win, 'foldlevel', 100) - else - vim.api.nvim_buf_set_lines(status.preview_bufnr, 0, -1, false, vim.fn.systemlist(string.format('bat %s', file_name))) - end - end, -} - -previewers.cat = previewers.new { - setup = function() - local command_string = "cat %s" - if 1 == vim.fn.executable("bat") then - command_string = "bat %s --style=grid --paging=always" - end - return { - command_string = command_string - } - end, + if entry.lnum then + vim.api.nvim_buf_add_highlight(bufnr, previewer_ns, "Visual", entry.lnum - 1, 0, -1) + -- print("LNUM:", entry.lnum) + end + end, + } +end, {}) - preview_fn = function(self, entry, status) - local bufnr = vim.api.nvim_create_buf(false, true) - vim.api.nvim_win_set_buf(status.preview_win, bufnr) +previewers.vim_buffer_or_bat = defaulter(function(_) + return previewers.new { + preview_fn = function(_, entry, status) + local value = entry.value + if value == nil then + return + end - with_preview_window(status, function() - vim.fn.termopen(string.format(self.state.command_string, entry.value)) - end) + local file_name = vim.split(value, ":")[1] - vim.api.nvim_buf_set_name(bufnr, tostring(bufnr)) - end -} + log.trace("Previewing File: %s", file_name) -previewers.vimgrep = previewers.new { - setup = function() - local command_string = "cat %s" - if vim.fn.executable("bat") then - command_string = "bat %s --highlight-line %s -r %s:%s" .. bat_options - end + -- vim.fn.termopen( + -- string.format("bat --color=always --style=grid %s"), + -- vim.fn.fnamemodify(file_name, ":p") + local bufnr = vim.fn.bufadd(file_name) - return { - command_string = command_string - } - end, + if vim.api.nvim_buf_is_loaded(bufnr) then + vim.fn.bufload(bufnr) - preview_fn = function(self, entry, status) - local bufnr = vim.api.nvim_create_buf(false, true) - local win_id = status.preview_win - local height = vim.api.nvim_win_get_height(win_id) + -- TODO: We should probably call something like this because we're not always getting highlight and all that stuff. + -- api.nvim_command('doautocmd filetypedetect BufRead ' .. vim.fn.fnameescape(filename)) + vim.api.nvim_win_set_buf(status.preview_win, bufnr) + vim.api.nvim_win_set_option(status.preview_win, 'wrap', false) + vim.api.nvim_win_set_option(status.preview_win, 'winhl', 'Normal:Normal') + -- vim.api.nvim_win_set_option(preview_win, 'winblend', 20) + vim.api.nvim_win_set_option(status.preview_win, 'signcolumn', 'no') + vim.api.nvim_win_set_option(status.preview_win, 'foldlevel', 100) + else + vim.api.nvim_buf_set_lines(status.preview_bufnr, 0, -1, false, vim.fn.systemlist(string.format('bat %s', file_name))) + end + end, + } +end, {}) - local line = entry.value - if type(line) == "table" then - line = entry.ordinal - end +previewers.cat = defaulter(function(opts) + return previewers.new { + setup = function() + local command_string = "cat %s" + if 1 == vim.fn.executable("bat") then + command_string = "bat %s --style=grid --paging=always" + end - local _, _, filename, lnum, col, text = string.find(line, [[([^:]+):(%d+):(%d+):(.*)]]) + return { + command_string = command_string + } + end, - local context = math.floor(height / 2) - local start = math.max(0, lnum - context) - local finish = lnum + context + preview_fn = function(self, entry, status) + local bufnr = vim.api.nvim_create_buf(false, true) - vim.api.nvim_win_set_buf(status.preview_win, bufnr) + vim.api.nvim_win_set_buf(status.preview_win, bufnr) - local termopen_command = string.format(self.state.command_string, filename, lnum, start, finish) + local path = entry.path + if path == nil then path = entry.filename end + if path == nil then path = entry.value end + if path == nil then print("Invalid entry", vim.inspect(entry)); return end - with_preview_window(status, function() - vim.fn.termopen(termopen_command) - end) + local term_opts = vim.empty_dict() + term_opts.cwd = opts.cwd - end -} + with_preview_window(status, function() + vim.fn.termopen(string.format(self.state.command_string, path), term_opts) + end) -previewers.qflist = previewers.new { - setup = function() - local command_string = "cat %s" - if vim.fn.executable("bat") then - command_string = "bat %s --highlight-line %s -r %s:%s" .. bat_options + vim.api.nvim_buf_set_name(bufnr, tostring(bufnr)) end + } +end, {}) - return { - command_string = command_string - } - end, +previewers.vimgrep = defaulter(function(_) + return previewers.new { + setup = function() + local command_string = "cat %s" + if vim.fn.executable("bat") then + command_string = "bat %s --highlight-line %s -r %s:%s" .. bat_options + end + + return { + command_string = command_string + } + end, + + preview_fn = function(self, entry, status) + local bufnr = vim.api.nvim_create_buf(false, true) + local win_id = status.preview_win + local height = vim.api.nvim_win_get_height(win_id) - preview_fn = function(self, entry, status) - local bufnr = vim.api.nvim_create_buf(false, true) - local win_id = status.preview_win - local height = vim.api.nvim_win_get_height(win_id) + local line = entry.value + if type(line) == "table" then + line = entry.ordinal + end - local filename = entry.value.filename - local lnum = entry.value.lnum + local _, _, filename, lnum, col, text = string.find(line, [[([^:]+):(%d+):(%d+):(.*)]]) - local start, finish - if entry.start and entry.finish then - start = entry.start - finish = entry.finish - else local context = math.floor(height / 2) - start = math.max(0, lnum - context) - finish = lnum + context - end + local start = math.max(0, lnum - context) + local finish = lnum + context - vim.api.nvim_win_set_buf(status.preview_win, bufnr) + vim.api.nvim_win_set_buf(status.preview_win, bufnr) - local termopen_command = string.format(self.state.command_string, filename, lnum, start, finish) + local termopen_command = string.format(self.state.command_string, filename, lnum, start, finish) - with_preview_window(status, function() - vim.fn.termopen(termopen_command) - end) - end -} + with_preview_window(status, function() + vim.fn.termopen(termopen_command) + 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' + end + } +end, {}) - local taglist = vim.fn.taglist('^' .. entry.value .. '$') - if vim.tbl_isempty(taglist) then - taglist = vim.fn.taglist(entry.value) +previewers.qflist = defaulter(function(_) + return previewers.new { + setup = function() + local command_string = "cat %s" + if vim.fn.executable("bat") then + command_string = "bat %s --highlight-line %s -r %s:%s" .. bat_options end - if vim.tbl_isempty(taglist) then - return + return { + command_string = command_string + } + end, + + preview_fn = function(self, entry, status) + local bufnr = vim.api.nvim_create_buf(false, true) + local win_id = status.preview_win + local height = vim.api.nvim_win_get_height(win_id) + + local filename = entry.value.filename + local lnum = entry.value.lnum + + local start, finish + if entry.start and entry.finish then + start = entry.start + finish = entry.finish + else + local context = math.floor(height / 2) + start = math.max(0, lnum - context) + finish = lnum + context end - local best_entry = taglist[1] - local new_bufnr = vim.fn.bufnr(best_entry.filename, true) + vim.api.nvim_win_set_buf(status.preview_win, bufnr) - vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help') - vim.api.nvim_win_set_buf(status.preview_win, new_bufnr) + local termopen_command = string.format(self.state.command_string, filename, lnum, start, finish) - vim.cmd [["gg"]] - print(best_entry.cmd) - vim.cmd(string.format([[execute "%s"]], best_entry.cmd)) + with_preview_window(status, function() + vim.fn.termopen(termopen_command) + end) + end + } +end, {}) - vim.o.tags = old_tags - end) - end -} +previewers.help = defaulter(function(_) + return 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 + } +end, {}) previewers.Previewer = Previewer |
