summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorTJ DeVries <devries.timothyj@gmail.com>2020-09-17 12:34:42 -0400
committerTJ DeVries <devries.timothyj@gmail.com>2020-09-17 12:34:42 -0400
commita9d9b3d8bf694914a07a01749306dd5157b868d7 (patch)
treef274d94a7c251bd25496cc3d3a66d8d8751d9de3 /lua
parent7bdb39b138ac49f20143d8a3a94467ecc32c23ed (diff)
feat: Improved previewers and cleanup
Diffstat (limited to 'lua')
-rw-r--r--lua/telescope/builtin.lua4
-rw-r--r--lua/telescope/make_entry.lua13
-rw-r--r--lua/telescope/pickers.lua3
-rw-r--r--lua/telescope/previewers.lua237
-rw-r--r--lua/telescope/utils.lua20
5 files changed, 142 insertions, 135 deletions
diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua
index 5b9a678..bd8e5d5 100644
--- a/lua/telescope/builtin.lua
+++ b/lua/telescope/builtin.lua
@@ -294,6 +294,8 @@ builtin.lsp_workspace_symbols = function(opts)
end
builtin.quickfix = function(opts)
+ opts = opts or {}
+
local locations = vim.fn.getqflist()
if vim.tbl_isempty(locations) then
@@ -603,6 +605,8 @@ builtin.planets = function(opts)
print("Enjoy astronomy! You viewed:", selection.display)
end)
+
+ return true
end,
}:find()
end
diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua
index 99735a2..4560c0a 100644
--- a/lua/telescope/make_entry.lua
+++ b/lua/telescope/make_entry.lua
@@ -2,6 +2,8 @@ local has_devicons, devicons = pcall(require, 'nvim-web-devicons')
local utils = require('telescope.utils')
+local get_default = utils.get_default
+
local make_entry = {}
make_entry.types = {
@@ -131,13 +133,16 @@ end
function make_entry.gen_from_quickfix(opts)
opts = opts or {}
+ opts.tail_path = get_default(opts.tail_path, true)
local make_display = function(entry)
local to_concat = {}
if not opts.hide_filename then
local filename = entry.filename
- if opts.shorten_path then
+ if opts.tail_path then
+ filename = utils.path_tail(filename)
+ elseif opts.shorten_path then
filename = utils.path_shorten(filename)
end
@@ -151,17 +156,19 @@ function make_entry.gen_from_quickfix(opts)
end
return function(entry)
+ local filename = entry.filename or vim.api.nvim_buf_get_name(entry.bufnr)
+
return {
valid = true,
value = entry,
ordinal = (
- not opts.ignore_filename and entry.filename
+ not opts.ignore_filename and filename
or ''
) .. ' ' .. entry.text,
display = make_display,
- filename = entry.filename,
+ filename = filename,
lnum = entry.lnum,
col = entry.col,
text = entry.text,
diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua
index bf65a36..f9427c3 100644
--- a/lua/telescope/pickers.lua
+++ b/lua/telescope/pickers.lua
@@ -593,6 +593,9 @@ function Picker:set_selection(row)
return
end
+ -- TODO: Probably should figure out what the rows are that made this happen...
+ -- Probably something with setting a row that's too high for this?
+ -- Not sure.
local set_ok, set_errmsg = pcall(function()
-- Handle adding '> ' to beginning of selections
if self._selection_row then
diff --git a/lua/telescope/previewers.lua b/lua/telescope/previewers.lua
index 2ad3130..bb8b3dc 100644
--- a/lua/telescope/previewers.lua
+++ b/lua/telescope/previewers.lua
@@ -4,7 +4,7 @@ local from_entry = require('telescope.from_entry')
local log = require('telescope.log')
local utils = require('telescope.utils')
-local get_default = utils.get_default
+local flatten = vim.tbl_flatten
local buf_delete = utils.buf_delete
local job_is_running = utils.job_is_running
@@ -16,7 +16,44 @@ local Previewer = {}
Previewer.__index = Previewer
-- TODO: Should play with these some more, ty @clason
-local bat_options = " --style=plain --color=always "
+local bat_options = {"--style=plain", "--color=always"}
+local bat_maker = function(filename, lnum, start, finish)
+ local command = {"bat"}
+
+ if lnum then
+ table.insert(command, { "--highlight-line", lnum})
+ end
+
+ if start and finish then
+ table.insert(command, { "-r", string.format("%s:%s", start, finish) })
+ end
+
+ return flatten {
+ command, bat_options, "--", filename
+ }
+end
+
+-- TODO: Add other options for cat to do this better
+local cat_maker = function(filename, lnum, start, finish)
+ return {
+ "cat", "--", filename
+ }
+end
+
+local get_maker = function(opts)
+ local maker = opts.maker
+ if not maker and 1 == vim.fn.executable("bat") then
+ maker = bat_maker
+ elseif not maker and 1 == vim.fn.executable("cat") then
+ maker = cat_maker
+ end
+
+ if not maker then
+ error("Needs maker")
+ end
+
+ return maker
+end
local previewer_ns = vim.api.nvim_create_namespace('telescope.previewers')
@@ -97,6 +134,7 @@ previewers.new_termopen_previewer = function(opts)
opts = opts or {}
assert(opts.get_command, "get_command is a required function")
+ assert(not opts.preview_fn, "preview_fn not allowed")
local opt_setup = opts.setup
local opt_teardown = opts.teardown
@@ -104,45 +142,32 @@ previewers.new_termopen_previewer = function(opts)
local old_bufs = {}
local function get_term_id(self)
- if not self.state then
- return nil
- end
-
+ if not self.state then return nil end
return self.state.termopen_id
end
+
local function get_bufnr(self)
- if not self.state then
- return nil
- end
+ if not self.state then return nil end
return self.state.termopen_bufnr
end
local function set_term_id(self, value)
- if job_is_running(get_term_id(self)) then
- vim.fn.jobstop(get_term_id(self))
- end
-
- if self.state then
- self.state.termopen_id = value
- end
+ if job_is_running(get_term_id(self)) then vim.fn.jobstop(get_term_id(self)) end
+ if self.state then self.state.termopen_id = value end
end
+
local function set_bufnr(self, value)
if get_bufnr(self) then table.insert(old_bufs, get_bufnr(self)) end
- if self.state then
- self.state.termopen_bufnr = value
- end
+ if self.state then self.state.termopen_bufnr = value end
end
- local function setup(self)
+ function opts.setup(self)
local state = {}
- if opt_setup then
- vim.tbl_deep_extend("force", state, opt_setup(self))
- end
-
+ if opt_setup then vim.tbl_deep_extend("force", state, opt_setup(self)) end
return state
end
- local function teardown(self)
+ function opts.teardown(self)
if opt_teardown then
opt_teardown(self)
end
@@ -158,10 +183,9 @@ previewers.new_termopen_previewer = function(opts)
for _, bufnr in ipairs(old_bufs) do
buf_delete(bufnr)
end
-
end
- local function preview_fn(self, entry, status)
+ function opts.preview_fn(self, entry, status)
if get_bufnr(self) == nil then
set_bufnr(self, vim.api.nvim_win_get_buf(status.preview_win))
end
@@ -185,21 +209,19 @@ previewers.new_termopen_previewer = function(opts)
vim.api.nvim_buf_set_name(bufnr, tostring(bufnr))
end
- return Previewer:new {
- setup = setup,
- teardown = teardown,
- preview_fn = preview_fn,
-
- send_input = function(self, input)
+ if not opts.send_input then
+ function opts.send_input(self, input)
local termcode = vim.api.nvim_replace_termcodes(input, true, false, true)
local term_id = get_term_id(self)
if term_id then
vim.fn.chansend(term_id, termcode)
end
- end,
+ end
+ end
- scroll_fn = function(self, direction)
+ if not opts.scroll_fn then
+ function opts.scroll_fn(self, direction)
if not self.state then
return
end
@@ -208,25 +230,11 @@ previewers.new_termopen_previewer = function(opts)
local count = math.abs(direction)
self:send_input(count..input)
- end,
- }
-end
-
-previewers.termopen = defaulter(function(opts)
- local command_string = assert(opts.command, 'opts.command is required')
-
- return previewers.new {
- preview_fn = function(_, entry, status)
- local bufnr = vim.api.nvim_create_buf(false, true)
-
- vim.api.nvim_win_set_buf(status.preview_win, bufnr)
-
- with_preview_window(status, bufnr, function()
- vim.fn.termopen(string.format(command_string, entry.value))
- end)
end
- }
-end, {})
+ end
+
+ return Previewer:new(opts)
+end
previewers.vim_buffer = defaulter(function(_)
return previewers.new {
@@ -282,42 +290,9 @@ previewers.vim_buffer = defaulter(function(_)
end, {})
-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
-
- local file_name = vim.split(value, ":")[1]
-
- log.trace("Previewing File: '%s'", file_name)
-
- -- vim.fn.termopen(
- -- string.format("bat --color=always --style=grid '%s'"),
- -- vim.fn.fnamemodify(file_name, ":p")
- local bufnr = vim.fn.bufadd(file_name)
-
- if vim.api.nvim_buf_is_loaded(bufnr) then
- vim.fn.bufload(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)
- 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, {})
-
previewers.cat = defaulter(function(opts)
+ local maker = get_maker(opts)
+
return previewers.new_termopen_previewer {
get_command = function(entry)
local path = from_entry.path(entry, true)
@@ -325,16 +300,13 @@ previewers.cat = defaulter(function(opts)
return
end
- return string.format('bat %s -- "%s"', bat_options, path)
+ return maker(path)
end
}
end, {})
-previewers.vimgrep = defaulter(function(_)
- local command_string = "cat -- '%s'"
- if vim.fn.executable("bat") then
- command_string = "bat --highlight-line '%s' -r '%s':'%s'" .. bat_options .. " -- '%s'"
- end
+previewers.vimgrep = defaulter(function(opts)
+ local maker = get_maker(opts)
return previewers.new_termopen_previewer {
get_command = function(entry, status)
@@ -348,31 +320,23 @@ previewers.vimgrep = defaulter(function(_)
local start = math.max(0, lnum - context)
local finish = lnum + context
- return string.format(command_string, lnum, start, finish, filename)
+ return maker(filename, lnum, start, finish)
end,
}
end, {})
-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
+previewers.qflist = defaulter(function(opts)
+ opts = opts or {}
- return {
- command_string = command_string
- }
- end,
+ local maker = get_maker(opts)
- preview_fn = function(self, entry, status)
- local bufnr = vim.api.nvim_create_buf(false, true)
+ return previewers.new_termopen_previewer {
+ get_command = function(entry, status)
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 filename = entry.filename
+ local lnum = entry.lnum
local start, finish
if entry.start and entry.finish then
@@ -384,17 +348,12 @@ previewers.qflist = defaulter(function(_)
finish = lnum + context
end
- vim.api.nvim_win_set_buf(status.preview_win, bufnr)
-
- local termopen_command = string.format(self.state.command_string, filename, lnum, start, finish)
-
- with_preview_window(status, bufnr, function()
- vim.fn.termopen(termopen_command)
- end)
+ return maker(filename, lnum, start, finish)
end
}
end, {})
+-- WIP
previewers.help = defaulter(function(_)
return previewers.new {
preview_fn = function(_, entry, status)
@@ -427,20 +386,42 @@ previewers.help = defaulter(function(_)
}
end, {})
-previewers.planet_previewer = previewers.new {
- preview_fn = function(self, entry, status)
- local bufnr = vim.api.nvim_create_buf(false, true)
+-- WIP
+-- TODO: This needs a big rewrite.
+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
- vim.api.nvim_win_set_buf(status.preview_win, bufnr)
+ local file_name = vim.split(value, ":")[1]
- local termopen_command = "bat " .. entry.value
+ log.trace("Previewing File: '%s'", file_name)
- -- 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))
- 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)
+
+ if vim.api.nvim_buf_is_loaded(bufnr) then
+ vim.fn.bufload(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)
+ 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, {})
previewers.Previewer = Previewer
diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua
index db77a7f..fe9eff3 100644
--- a/lua/telescope/utils.lua
+++ b/lua/telescope/utils.lua
@@ -1,5 +1,12 @@
local utils = {}
+utils.get_separator = (function()
+ local val = package.config:sub(1, 1)
+ return function()
+ return val
+ end
+end)()
+
utils.if_nil = function(x, was_nil, was_not_nil)
if x == nil then
return was_nil
@@ -114,6 +121,15 @@ utils.path_shorten = (function()
end
end)()
+utils.path_tail = (function()
+ local os_sep = utils.get_separator()
+ local match_string = '[^' .. os_sep .. ']*$'
+
+ return function(path)
+ return string.match(path, match_string)
+ end
+end)()
+
-- local x = utils.make_default_callable(function(opts)
-- return function()
-- print(opts.example, opts.another)
@@ -138,10 +154,6 @@ function utils.make_default_callable(f, default_opts)
})
end
-function utils.get_separator()
- return package.config:sub(1, 1)
-end
-
function utils.job_is_running(job_id)
if job_id == nil then return false end
return vim.fn.jobwait({job_id}, 0)[1] == -1