fix: Don't push past midnight. You'll make clason's stuff break :/

This commit is contained in:
TJ DeVries
2020-09-04 09:49:10 -04:00
parent 996f69465e
commit 14310ee6b1
9 changed files with 352 additions and 223 deletions

View File

@@ -33,13 +33,18 @@ local builtin = {}
builtin.git_files = function(opts) builtin.git_files = function(opts)
opts = opts or {} opts = opts or {}
opts.entry_maker = opts.entry_maker or make_entry.gen_from_file(opts)
if opts.cwd then
opts.cwd = vim.fn.expand(opts.cwd)
end
pickers.new(opts, { pickers.new(opts, {
prompt = 'Git File', prompt = 'Git File',
finder = finders.new_oneshot_job( finder = finders.new_oneshot_job(
{ "git", "ls-files", "-o", "--exclude-standard", "-c" }, { "git", "ls-files", "-o", "--exclude-standard", "-c" },
make_entry.gen_from_file(opts) opts
), ),
previewer = previewers.cat, previewer = previewers.cat.new(opts),
sorter = sorters.get_fuzzy_file(), sorter = sorters.get_fuzzy_file(),
}):find() }):find()
end end
@@ -63,13 +68,10 @@ builtin.live_grep = function(opts)
pickers.new(opts, { pickers.new(opts, {
prompt = 'Live Grep', prompt = 'Live Grep',
finder = live_grepper, finder = live_grepper,
previewer = previewers.vimgrep, previewer = previewers.vimgrep.new(opts),
}):find() }):find()
end end
-- TODO: document_symbol
-- TODO: workspace_symbol
builtin.lsp_references = function(opts) builtin.lsp_references = function(opts)
opts = opts or {} opts = opts or {}
opts.shorten_path = utils.get_default(opts.shorten_path, true) opts.shorten_path = utils.get_default(opts.shorten_path, true)
@@ -93,12 +95,14 @@ builtin.lsp_references = function(opts)
results = locations, results = locations,
entry_maker = make_entry.gen_from_quickfix(opts), entry_maker = make_entry.gen_from_quickfix(opts),
}, },
previewer = previewers.qflist, previewer = previewers.qflist.new(opts),
sorter = sorters.get_norcalli_sorter(), sorter = sorters.get_norcalli_sorter(),
}):find() }):find()
end end
builtin.lsp_document_symbols = function(opts) builtin.lsp_document_symbols = function(opts)
opts = opts or {}
local params = vim.lsp.util.make_position_params() local params = vim.lsp.util.make_position_params()
local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params) local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params)
@@ -122,12 +126,15 @@ builtin.lsp_document_symbols = function(opts)
results = locations, results = locations,
entry_maker = make_entry.gen_from_quickfix(opts) entry_maker = make_entry.gen_from_quickfix(opts)
}, },
previewer = previewers.vim_buffer, previewer = previewers.vim_buffer.new(opts),
sorter = sorters.get_norcalli_sorter(), sorter = sorters.get_norcalli_sorter(),
}):find() }):find()
end end
builtin.lsp_workspace_symbols = function(opts) builtin.lsp_workspace_symbols = function(opts)
opts = opts or {}
opts.shorten_path = utils.get_default(opts.shorten_path, true)
local params = {query = opts.query or ''} local params = {query = opts.query or ''}
local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params, 1000) local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params, 1000)
@@ -151,7 +158,7 @@ builtin.lsp_workspace_symbols = function(opts)
results = locations, results = locations,
entry_maker = make_entry.gen_from_quickfix(opts) entry_maker = make_entry.gen_from_quickfix(opts)
}, },
previewer = previewers.qflist, previewer = previewers.qflist.new(opts),
sorter = sorters.get_norcalli_sorter(), sorter = sorters.get_norcalli_sorter(),
}):find() }):find()
end end
@@ -169,7 +176,7 @@ builtin.quickfix = function(opts)
results = locations, results = locations,
entry_maker = make_entry.gen_from_quickfix(opts), entry_maker = make_entry.gen_from_quickfix(opts),
}, },
previewer = previewers.qflist, previewer = previewers.qflist.new(opts),
sorter = sorters.get_norcalli_sorter(), sorter = sorters.get_norcalli_sorter(),
}):find() }):find()
end end
@@ -192,36 +199,42 @@ builtin.loclist = function(opts)
results = locations, results = locations,
entry_maker = make_entry.gen_from_quickfix(opts), entry_maker = make_entry.gen_from_quickfix(opts),
}, },
previewer = previewers.qflist, previewer = previewers.qflist.new(opts),
sorter = sorters.get_norcalli_sorter(), sorter = sorters.get_norcalli_sorter(),
}):find() }):find()
end end
-- Special keys:
-- opts.search -- the string to search.
builtin.grep_string = function(opts) builtin.grep_string = function(opts)
opts = opts or {} opts = opts or {}
-- TODO: This should probably check your visual selection as well, if you've got one -- TODO: This should probably check your visual selection as well, if you've got one
local search = opts.search or vim.fn.expand("<cword>") local search = opts.search or vim.fn.expand("<cword>")
opts.entry_maker = opts.entry_maker or make_entry.gen_from_vimgrep(opts)
pickers.new(opts, { pickers.new(opts, {
prompt = 'Find Word', prompt = 'Find Word',
finder = finders.new_oneshot_job( finder = finders.new_oneshot_job(
flatten { vimgrep_arguments, search}, flatten { vimgrep_arguments, search},
make_entry.gen_from_vimgrep(opts) opts
), ),
previewer = previewers.vimgrep, previewer = previewers.vimgrep.new(opts),
sorter = sorters.get_norcalli_sorter(), sorter = sorters.get_norcalli_sorter(),
}):find() }):find()
end end
builtin.oldfiles = function(opts) builtin.oldfiles = function(opts)
opts = opts or {}
pickers.new(opts, { pickers.new(opts, {
prompt = 'Oldfiles', prompt = 'Oldfiles',
finder = finders.new_table(vim.tbl_filter(function(val) finder = finders.new_table(vim.tbl_filter(function(val)
return 0 ~= vim.fn.filereadable(val) return 0 ~= vim.fn.filereadable(val)
end, vim.v.oldfiles)), end, vim.v.oldfiles)),
sorter = sorters.get_fuzzy_file(), sorter = sorters.get_fuzzy_file(),
previewer = previewers.cat, previewer = previewers.cat.new(opts),
}):find() }):find()
end end
@@ -247,6 +260,7 @@ builtin.command_history = function(opts)
-- TODO: Find a way to insert the text... it seems hard. -- TODO: Find a way to insert the text... it seems hard.
-- map('i', '<C-i>', actions.insert_value, { expr = true }) -- map('i', '<C-i>', actions.insert_value, { expr = true })
-- Please add the default mappings for me for the rest of the keys.
return true return true
end, end,
@@ -285,7 +299,7 @@ builtin.builtin = function(opts)
results = objs, results = objs,
entry_maker = make_entry.gen_from_quickfix(opts), entry_maker = make_entry.gen_from_quickfix(opts),
}, },
previewer = previewers.qflist, previewer = previewers.qflist.new(opts),
sorter = sorters.get_norcalli_sorter(), sorter = sorters.get_norcalli_sorter(),
}):find() }):find()
end end
@@ -313,13 +327,15 @@ builtin.fd = function(opts)
cwd = vim.fn.expand(cwd) cwd = vim.fn.expand(cwd)
end end
opts.entry_maker = opts.entry_maker or make_entry.gen_from_file(opts)
pickers.new(opts, { pickers.new(opts, {
prompt = 'Find Files', prompt = 'Find Files',
finder = finders.new_oneshot_job( finder = finders.new_oneshot_job(
{fd_string}, {fd_string},
make_entry.gen_from_file(opts) opts
), ),
previewer = previewers.cat, previewer = previewers.cat.new(opts),
sorter = sorters.get_fuzzy_file(), sorter = sorters.get_fuzzy_file(),
}):find() }):find()
end end

View File

@@ -46,12 +46,15 @@ function JobFinder:new(opts)
-- pipe -- pipe
-- vim.loop.new_pipe (stdin / stdout). stdout => filter pipe -- vim.loop.new_pipe (stdin / stdout). stdout => filter pipe
-- rg huge_search | fzf --filter prompt_is > buffer. buffer could do stuff do w/ preview callback -- rg huge_search | fzf --filter prompt_is > buffer. buffer could do stuff do w/ preview callback
local obj = setmetatable({ local obj = setmetatable({
entry_maker = opts.entry_maker or make_entry.from_string, entry_maker = opts.entry_maker or make_entry.from_string,
fn_command = opts.fn_command, fn_command = opts.fn_command,
static = opts.static, static = opts.static,
state = {}, state = {},
cwd = opts.cwd,
-- Maximum number of results to process. -- Maximum number of results to process.
-- Particularly useful for live updating large queries. -- Particularly useful for live updating large queries.
maximum_results = opts.maximum_results, maximum_results = opts.maximum_results,
@@ -113,7 +116,7 @@ function JobFinder:_find(prompt, process_result, process_complete)
self.job = Job:new { self.job = Job:new {
command = opts.command, command = opts.command,
args = opts.args, args = opts.args,
cwd = opts.cwd, cwd = opts.cwd or self.cwd,
maximum_results = self.maximum_results, maximum_results = self.maximum_results,
@@ -210,8 +213,12 @@ finders.new_job = function(command_generator, entry_maker, maximum_results)
end end
---@param command_list string[] Command list to execute. ---@param command_list string[] Command list to execute.
---@param entry_maker function Optional: function(line: string) => table ---@param opts table
finders.new_oneshot_job = function(command_list, entry_maker) --- @key entry_maker function Optional: function(line: string) => table
--- @key cwd string
finders.new_oneshot_job = function(command_list, opts)
opts = opts or {}
command_list = vim.deepcopy(command_list) command_list = vim.deepcopy(command_list)
local command = table.remove(command_list, 1) local command = table.remove(command_list, 1)
@@ -219,7 +226,9 @@ finders.new_oneshot_job = function(command_list, entry_maker)
return JobFinder:new { return JobFinder:new {
static = true, static = true,
entry_maker = entry_maker or make_entry.from_string, entry_maker = opts.entry_maker or make_entry.gen_from_string,
cwd = opts.cwd,
fn_command = function() fn_command = function()
return { return {

View File

@@ -28,7 +28,7 @@ function make_entry.gen_from_string()
return function(line) return function(line)
return { return {
valid = line ~= "", valid = line ~= "",
entry_type = make_entry.types.SIMPLE, entry_type = make_entry.types.GENERIC,
value = line, value = line,
ordinal = line, ordinal = line,
@@ -40,6 +40,8 @@ end
function make_entry.gen_from_file(opts) function make_entry.gen_from_file(opts)
opts = opts or {} opts = opts or {}
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
local make_display = function(line) local make_display = function(line)
local display = line local display = line
if opts.shorten_path then if opts.shorten_path then
@@ -58,6 +60,7 @@ function make_entry.gen_from_file(opts)
entry_type = make_entry.types.FILE, entry_type = make_entry.types.FILE,
filename = line, filename = line,
path = cwd .. '/' .. line,
} }
entry.display = make_display(line) entry.display = make_display(line)
@@ -101,6 +104,13 @@ function make_entry.gen_from_vimgrep(opts)
-- Or could we just walk the text and check for colons faster? -- Or could we just walk the text and check for colons faster?
local _, _, filename, lnum, col, text = string.find(line, [[([^:]+):(%d+):(%d+):(.*)]]) local _, _, filename, lnum, col, text = string.find(line, [[([^:]+):(%d+):(%d+):(.*)]])
local ok
ok, lnum = pcall(tonumber, lnum)
if not ok then lnum = nil end
ok, col = pcall(tonumber, col)
if not ok then col = nil end
return { return {
valid = line ~= "", valid = line ~= "",

View File

@@ -122,6 +122,7 @@ function Picker:_get_initial_window_options(prompt_title)
local popup_borderchars = self.window.borderchars local popup_borderchars = self.window.borderchars
local preview = { local preview = {
title = 'Preview',
border = popup_border, border = popup_border,
borderchars = popup_borderchars, borderchars = popup_borderchars,
enter = false, enter = false,
@@ -129,6 +130,7 @@ function Picker:_get_initial_window_options(prompt_title)
} }
local results = { local results = {
title = 'Results',
border = popup_border, border = popup_border,
borderchars = popup_borderchars, borderchars = popup_borderchars,
enter = false, enter = false,

View File

@@ -1,6 +1,16 @@
local layout_strategies = {} local layout_strategies = {}
--[[
+-----------------+---------------------+
| | |
| Results | |
| | Preview |
| | |
+-----------------| |
| Prompt | |
+-----------------+---------------------+
--]]
layout_strategies.horizontal = function(self, max_columns, max_lines, prompt_title) layout_strategies.horizontal = function(self, max_columns, max_lines, prompt_title)
local initial_options = self:_get_initial_window_options(prompt_title) local initial_options = self:_get_initial_window_options(prompt_title)
local preview = initial_options.preview local preview = initial_options.preview
@@ -62,6 +72,21 @@ layout_strategies.horizontal = function(self, max_columns, max_lines, prompt_tit
} }
end end
--[[
+-----------------+
| Previewer |
| Previewer |
| Previewer |
+-----------------+
| Result |
| Result |
| Result |
+-----------------+
| Prompt |
+-----------------+
--]]
layout_strategies.vertical = function(self, max_columns, max_lines, prompt_title) layout_strategies.vertical = function(self, max_columns, max_lines, prompt_title)
local initial_options = self:_get_initial_window_options(prompt_title) local initial_options = self:_get_initial_window_options(prompt_title)

View File

@@ -1,6 +1,9 @@
local context_manager = require('plenary.context_manager') local context_manager = require('plenary.context_manager')
local log = require('telescope.log') local log = require('telescope.log')
local utils = require('telescope.utils')
local defaulter = utils.make_default_callable
local previewers = {} local previewers = {}
@@ -59,12 +62,8 @@ local with_preview_window = function(status, callable)
end, callable) end, callable)
end end
previewers.new_termopen = function(opts) previewers.termopen = defaulter(function(opts)
local entry_value = opts.get_value or function(entry) local command_string = assert(opts.command, 'opts.command is required')
return entry.value
end
local command_string = opts.command
return previewers.new { return previewers.new {
preview_fn = function(_, entry, status) preview_fn = function(_, entry, status)
@@ -73,79 +72,45 @@ previewers.new_termopen = function(opts)
vim.api.nvim_win_set_buf(status.preview_win, bufnr) vim.api.nvim_win_set_buf(status.preview_win, bufnr)
with_preview_window(status, function() 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 end
} }
end end, {})
previewers.vim_buffer = previewers.new { previewers.vim_buffer = defaulter(function(_)
setup = function() return { last_set_bufnr = nil } end, return previewers.new {
setup = function() return { last_set_bufnr = nil } end,
teardown = function(self) teardown = function(self)
if self.state.last_set_bufnr then if self.state.last_set_bufnr then
vim.api.nvim_buf_clear_namespace(self.state.last_set_bufnr, previewer_ns, 0, -1) vim.api.nvim_buf_clear_namespace(self.state.last_set_bufnr, previewer_ns, 0, -1)
end end
end, end,
preview_fn = function(self, entry, status) preview_fn = function(self, entry, status)
local filename = entry.filename -- TODO: Consider using path here? Might not work otherwise.
local filename = entry.filename
if filename == nil then if filename == nil then
local value = entry.value local value = entry.value
filename = vim.split(value, ":")[1] filename = vim.split(value, ":")[1]
end end
if filename == nil then if filename == nil then
return return
end end
log.trace("Previewing File: %s", filename) log.trace("Previewing File: %s", filename)
local bufnr = vim.fn.bufnr(filename) local bufnr = vim.fn.bufnr(filename)
if bufnr == -1 then if bufnr == -1 then
-- TODO: Is this the best way to load the buffer?... I'm not sure tbh -- TODO: Is this the best way to load the buffer?... I'm not sure tbh
bufnr = vim.fn.bufadd(bufnr) bufnr = vim.fn.bufadd(bufnr)
vim.fn.bufload(bufnr) vim.fn.bufload(bufnr)
end end
self.state.last_set_bufnr = 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))
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)
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,
}
previewers.vim_buffer_or_bat = 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. -- 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)) -- 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(preview_win, 'winblend', 20)
vim.api.nvim_win_set_option(status.preview_win, 'signcolumn', 'no') vim.api.nvim_win_set_option(status.preview_win, 'signcolumn', 'no')
vim.api.nvim_win_set_option(status.preview_win, 'foldlevel', 100) 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 { if entry.lnum then
setup = function() vim.api.nvim_buf_add_highlight(bufnr, previewer_ns, "Visual", entry.lnum - 1, 0, -1)
local command_string = "cat %s" -- print("LNUM:", entry.lnum)
if 1 == vim.fn.executable("bat") then
command_string = "bat %s --style=grid --paging=always"
end
return {
command_string = command_string
}
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)
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
}
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
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 line = entry.value
if type(line) == "table" then
line = entry.ordinal
end
local _, _, filename, lnum, col, text = string.find(line, [[([^:]+):(%d+):(%d+):(.*)]])
local context = math.floor(height / 2)
local start = math.max(0, lnum - context)
local finish = lnum + context
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, function()
vim.fn.termopen(termopen_command)
end)
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
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)
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
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, 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 end
end,
}
end, {})
if vim.tbl_isempty(taglist) then
previewers.vim_buffer_or_bat = defaulter(function(_)
return previewers.new {
preview_fn = function(_, entry, status)
local value = entry.value
if value == nil then
return return
end end
local best_entry = taglist[1] local file_name = vim.split(value, ":")[1]
local new_bufnr = vim.fn.bufnr(best_entry.filename, true)
vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help') log.trace("Previewing File: %s", file_name)
vim.api.nvim_win_set_buf(status.preview_win, new_bufnr)
vim.cmd [["gg"]] -- vim.fn.termopen(
print(best_entry.cmd) -- string.format("bat --color=always --style=grid %s"),
vim.cmd(string.format([[execute "%s"]], best_entry.cmd)) -- vim.fn.fnamemodify(file_name, ":p")
local bufnr = vim.fn.bufadd(file_name)
vim.o.tags = old_tags if vim.api.nvim_buf_is_loaded(bufnr) then
end) vim.fn.bufload(bufnr)
end
} -- 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)
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
return {
command_string = command_string
}
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)
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
local term_opts = vim.empty_dict()
term_opts.cwd = opts.cwd
with_preview_window(status, function()
vim.fn.termopen(string.format(self.state.command_string, path), term_opts)
end)
vim.api.nvim_buf_set_name(bufnr, tostring(bufnr))
end
}
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)
local line = entry.value
if type(line) == "table" then
line = entry.ordinal
end
local _, _, filename, lnum, col, text = string.find(line, [[([^:]+):(%d+):(%d+):(.*)]])
local context = math.floor(height / 2)
local start = math.max(0, lnum - context)
local finish = lnum + context
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, function()
vim.fn.termopen(termopen_command)
end)
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
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
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, function()
vim.fn.termopen(termopen_command)
end)
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 previewers.Previewer = Previewer

View File

@@ -26,7 +26,7 @@ end
function Sorter:score(prompt, entry) function Sorter:score(prompt, entry)
-- TODO: Decide if we actually want to check the type every time. -- TODO: Decide if we actually want to check the type every time.
return self:scoring_function(prompt, type(entry) == "string" and entry or entry.ordinal) return self:scoring_function(prompt, type(entry) == "string" and entry or entry.ordinal, entry)
end end
function sorters.new(...) function sorters.new(...)
@@ -232,7 +232,11 @@ sorters.get_norcalli_sorter = function()
end end
return Sorter:new { return Sorter:new {
scoring_function = function(_, prompt, line) -- self
-- prompt (which is the text on the line)
-- line (entry.ordinal)
-- entry (the whole entry)
scoring_function = function(_, prompt, line, _)
if prompt == 0 or #prompt < ngramlen then if prompt == 0 or #prompt < ngramlen then
return 0 return 0
end end

View File

@@ -114,4 +114,28 @@ utils.path_shorten = (function()
end end
end)() end)()
-- local x = utils.make_default_callable(function(opts)
-- return function()
-- print(opts.example, opts.another)
-- end
-- end, { example = 7, another = 5 })
-- x()
-- x.new { example = 3 }()
function utils.make_default_callable(f, default_opts)
return setmetatable({
new = function(opts)
opts = vim.tbl_extend("keep", opts, default_opts)
return f(opts)
end,
}, {
__call = function()
local ok, err = pcall(f(default_opts))
if not ok then
error(debug.traceback(err))
end
end
})
end
return utils return utils

View File

@@ -0,0 +1,19 @@
--[[
vim.api.nvim_buf_set_lines(0, 4, -1, false, vim.tbl_keys(require('telescope.builtin')))
--]]
require('telescope.builtin').git_files()
RELOAD('telescope'); require('telescope.builtin').oldfiles()
require('telescope.builtin').grep_string()
require('telescope.builtin').lsp_document_symbols()
RELOAD('telescope'); require('telescope.builtin').lsp_workspace_symbols()
require('telescope.builtin').lsp_references()
require('telescope.builtin').builtin()
require('telescope.builtin').fd()
require('telescope.builtin').command_history()
require('telescope.builtin').live_grep()
require('telescope.builtin').loclist()
-- TODO: make a function that puts stuff into quickfix.
-- that way we can test this better.
require('telescope.builtin').quickfix()