fix: Don't push past midnight. You'll make clason's stuff break :/
This commit is contained in:
@@ -33,13 +33,18 @@ local builtin = {}
|
||||
builtin.git_files = function(opts)
|
||||
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, {
|
||||
prompt = 'Git File',
|
||||
finder = finders.new_oneshot_job(
|
||||
{ "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(),
|
||||
}):find()
|
||||
end
|
||||
@@ -63,13 +68,10 @@ builtin.live_grep = function(opts)
|
||||
pickers.new(opts, {
|
||||
prompt = 'Live Grep',
|
||||
finder = live_grepper,
|
||||
previewer = previewers.vimgrep,
|
||||
previewer = previewers.vimgrep.new(opts),
|
||||
}):find()
|
||||
end
|
||||
|
||||
-- TODO: document_symbol
|
||||
-- TODO: workspace_symbol
|
||||
|
||||
builtin.lsp_references = function(opts)
|
||||
opts = opts or {}
|
||||
opts.shorten_path = utils.get_default(opts.shorten_path, true)
|
||||
@@ -93,12 +95,14 @@ builtin.lsp_references = function(opts)
|
||||
results = locations,
|
||||
entry_maker = make_entry.gen_from_quickfix(opts),
|
||||
},
|
||||
previewer = previewers.qflist,
|
||||
previewer = previewers.qflist.new(opts),
|
||||
sorter = sorters.get_norcalli_sorter(),
|
||||
}):find()
|
||||
end
|
||||
|
||||
builtin.lsp_document_symbols = function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local params = vim.lsp.util.make_position_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,
|
||||
entry_maker = make_entry.gen_from_quickfix(opts)
|
||||
},
|
||||
previewer = previewers.vim_buffer,
|
||||
previewer = previewers.vim_buffer.new(opts),
|
||||
sorter = sorters.get_norcalli_sorter(),
|
||||
}):find()
|
||||
end
|
||||
|
||||
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 results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params, 1000)
|
||||
|
||||
@@ -151,7 +158,7 @@ builtin.lsp_workspace_symbols = function(opts)
|
||||
results = locations,
|
||||
entry_maker = make_entry.gen_from_quickfix(opts)
|
||||
},
|
||||
previewer = previewers.qflist,
|
||||
previewer = previewers.qflist.new(opts),
|
||||
sorter = sorters.get_norcalli_sorter(),
|
||||
}):find()
|
||||
end
|
||||
@@ -169,7 +176,7 @@ builtin.quickfix = function(opts)
|
||||
results = locations,
|
||||
entry_maker = make_entry.gen_from_quickfix(opts),
|
||||
},
|
||||
previewer = previewers.qflist,
|
||||
previewer = previewers.qflist.new(opts),
|
||||
sorter = sorters.get_norcalli_sorter(),
|
||||
}):find()
|
||||
end
|
||||
@@ -192,36 +199,42 @@ builtin.loclist = function(opts)
|
||||
results = locations,
|
||||
entry_maker = make_entry.gen_from_quickfix(opts),
|
||||
},
|
||||
previewer = previewers.qflist,
|
||||
previewer = previewers.qflist.new(opts),
|
||||
sorter = sorters.get_norcalli_sorter(),
|
||||
}):find()
|
||||
end
|
||||
|
||||
-- Special keys:
|
||||
-- opts.search -- the string to search.
|
||||
builtin.grep_string = function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
-- TODO: This should probably check your visual selection as well, if you've got one
|
||||
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, {
|
||||
prompt = 'Find Word',
|
||||
finder = finders.new_oneshot_job(
|
||||
flatten { vimgrep_arguments, search},
|
||||
make_entry.gen_from_vimgrep(opts)
|
||||
opts
|
||||
),
|
||||
previewer = previewers.vimgrep,
|
||||
previewer = previewers.vimgrep.new(opts),
|
||||
sorter = sorters.get_norcalli_sorter(),
|
||||
}):find()
|
||||
end
|
||||
|
||||
builtin.oldfiles = function(opts)
|
||||
opts = opts or {}
|
||||
|
||||
pickers.new(opts, {
|
||||
prompt = 'Oldfiles',
|
||||
finder = finders.new_table(vim.tbl_filter(function(val)
|
||||
return 0 ~= vim.fn.filereadable(val)
|
||||
end, vim.v.oldfiles)),
|
||||
sorter = sorters.get_fuzzy_file(),
|
||||
previewer = previewers.cat,
|
||||
previewer = previewers.cat.new(opts),
|
||||
}):find()
|
||||
end
|
||||
|
||||
@@ -247,6 +260,7 @@ builtin.command_history = function(opts)
|
||||
-- TODO: Find a way to insert the text... it seems hard.
|
||||
-- map('i', '<C-i>', actions.insert_value, { expr = true })
|
||||
|
||||
-- Please add the default mappings for me for the rest of the keys.
|
||||
return true
|
||||
end,
|
||||
|
||||
@@ -285,7 +299,7 @@ builtin.builtin = function(opts)
|
||||
results = objs,
|
||||
entry_maker = make_entry.gen_from_quickfix(opts),
|
||||
},
|
||||
previewer = previewers.qflist,
|
||||
previewer = previewers.qflist.new(opts),
|
||||
sorter = sorters.get_norcalli_sorter(),
|
||||
}):find()
|
||||
end
|
||||
@@ -313,13 +327,15 @@ builtin.fd = function(opts)
|
||||
cwd = vim.fn.expand(cwd)
|
||||
end
|
||||
|
||||
opts.entry_maker = opts.entry_maker or make_entry.gen_from_file(opts)
|
||||
|
||||
pickers.new(opts, {
|
||||
prompt = 'Find Files',
|
||||
finder = finders.new_oneshot_job(
|
||||
{fd_string},
|
||||
make_entry.gen_from_file(opts)
|
||||
opts
|
||||
),
|
||||
previewer = previewers.cat,
|
||||
previewer = previewers.cat.new(opts),
|
||||
sorter = sorters.get_fuzzy_file(),
|
||||
}):find()
|
||||
end
|
||||
|
||||
@@ -46,12 +46,15 @@ function JobFinder:new(opts)
|
||||
-- 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
|
||||
|
||||
local obj = setmetatable({
|
||||
entry_maker = opts.entry_maker or make_entry.from_string,
|
||||
fn_command = opts.fn_command,
|
||||
static = opts.static,
|
||||
state = {},
|
||||
|
||||
cwd = opts.cwd,
|
||||
|
||||
-- Maximum number of results to process.
|
||||
-- Particularly useful for live updating large queries.
|
||||
maximum_results = opts.maximum_results,
|
||||
@@ -113,7 +116,7 @@ function JobFinder:_find(prompt, process_result, process_complete)
|
||||
self.job = Job:new {
|
||||
command = opts.command,
|
||||
args = opts.args,
|
||||
cwd = opts.cwd,
|
||||
cwd = opts.cwd or self.cwd,
|
||||
|
||||
maximum_results = self.maximum_results,
|
||||
|
||||
@@ -210,8 +213,12 @@ finders.new_job = function(command_generator, entry_maker, maximum_results)
|
||||
end
|
||||
|
||||
---@param command_list string[] Command list to execute.
|
||||
---@param entry_maker function Optional: function(line: string) => table
|
||||
finders.new_oneshot_job = function(command_list, entry_maker)
|
||||
---@param opts table
|
||||
--- @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)
|
||||
|
||||
local command = table.remove(command_list, 1)
|
||||
@@ -219,7 +226,9 @@ finders.new_oneshot_job = function(command_list, entry_maker)
|
||||
return JobFinder:new {
|
||||
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()
|
||||
return {
|
||||
|
||||
@@ -28,7 +28,7 @@ function make_entry.gen_from_string()
|
||||
return function(line)
|
||||
return {
|
||||
valid = line ~= "",
|
||||
entry_type = make_entry.types.SIMPLE,
|
||||
entry_type = make_entry.types.GENERIC,
|
||||
|
||||
value = line,
|
||||
ordinal = line,
|
||||
@@ -40,6 +40,8 @@ end
|
||||
function make_entry.gen_from_file(opts)
|
||||
opts = opts or {}
|
||||
|
||||
local cwd = vim.fn.expand(opts.cwd or vim.fn.getcwd())
|
||||
|
||||
local make_display = function(line)
|
||||
local display = line
|
||||
if opts.shorten_path then
|
||||
@@ -58,6 +60,7 @@ function make_entry.gen_from_file(opts)
|
||||
|
||||
entry_type = make_entry.types.FILE,
|
||||
filename = line,
|
||||
path = cwd .. '/' .. 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?
|
||||
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 {
|
||||
valid = line ~= "",
|
||||
|
||||
|
||||
@@ -122,6 +122,7 @@ function Picker:_get_initial_window_options(prompt_title)
|
||||
local popup_borderchars = self.window.borderchars
|
||||
|
||||
local preview = {
|
||||
title = 'Preview',
|
||||
border = popup_border,
|
||||
borderchars = popup_borderchars,
|
||||
enter = false,
|
||||
@@ -129,6 +130,7 @@ function Picker:_get_initial_window_options(prompt_title)
|
||||
}
|
||||
|
||||
local results = {
|
||||
title = 'Results',
|
||||
border = popup_border,
|
||||
borderchars = popup_borderchars,
|
||||
enter = false,
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
|
||||
local layout_strategies = {}
|
||||
|
||||
--[[
|
||||
+-----------------+---------------------+
|
||||
| | |
|
||||
| Results | |
|
||||
| | Preview |
|
||||
| | |
|
||||
+-----------------| |
|
||||
| Prompt | |
|
||||
+-----------------+---------------------+
|
||||
--]]
|
||||
layout_strategies.horizontal = function(self, max_columns, max_lines, prompt_title)
|
||||
local initial_options = self:_get_initial_window_options(prompt_title)
|
||||
local preview = initial_options.preview
|
||||
@@ -62,6 +72,21 @@ layout_strategies.horizontal = function(self, max_columns, max_lines, prompt_tit
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
|
||||
--[[
|
||||
+-----------------+
|
||||
| Previewer |
|
||||
| Previewer |
|
||||
| Previewer |
|
||||
+-----------------+
|
||||
| Result |
|
||||
| Result |
|
||||
| Result |
|
||||
+-----------------+
|
||||
| Prompt |
|
||||
+-----------------+
|
||||
--]]
|
||||
layout_strategies.vertical = function(self, max_columns, max_lines, prompt_title)
|
||||
local initial_options = self:_get_initial_window_options(prompt_title)
|
||||
|
||||
|
||||
@@ -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,13 +72,14 @@ 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
|
||||
end, {})
|
||||
|
||||
previewers.vim_buffer = previewers.new {
|
||||
previewers.vim_buffer = defaulter(function(_)
|
||||
return previewers.new {
|
||||
setup = function() return { last_set_bufnr = nil } end,
|
||||
|
||||
teardown = function(self)
|
||||
@@ -89,6 +89,7 @@ previewers.vim_buffer = previewers.new {
|
||||
end,
|
||||
|
||||
preview_fn = function(self, entry, status)
|
||||
-- TODO: Consider using path here? Might not work otherwise.
|
||||
local filename = entry.filename
|
||||
|
||||
if filename == nil then
|
||||
@@ -125,10 +126,12 @@ previewers.vim_buffer = previewers.new {
|
||||
-- print("LNUM:", entry.lnum)
|
||||
end
|
||||
end,
|
||||
}
|
||||
}
|
||||
end, {})
|
||||
|
||||
|
||||
previewers.vim_buffer_or_bat = previewers.new {
|
||||
previewers.vim_buffer_or_bat = defaulter(function(_)
|
||||
return previewers.new {
|
||||
preview_fn = function(_, entry, status)
|
||||
local value = entry.value
|
||||
if value == nil then
|
||||
@@ -159,9 +162,11 @@ previewers.vim_buffer_or_bat = previewers.new {
|
||||
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 = previewers.new {
|
||||
previewers.cat = defaulter(function(opts)
|
||||
return previewers.new {
|
||||
setup = function()
|
||||
local command_string = "cat %s"
|
||||
if 1 == vim.fn.executable("bat") then
|
||||
@@ -178,15 +183,25 @@ previewers.cat = previewers.new {
|
||||
|
||||
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, entry.value))
|
||||
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 = previewers.new {
|
||||
previewers.vimgrep = defaulter(function(_)
|
||||
return previewers.new {
|
||||
setup = function()
|
||||
local command_string = "cat %s"
|
||||
if vim.fn.executable("bat") then
|
||||
@@ -223,9 +238,11 @@ previewers.vimgrep = previewers.new {
|
||||
end)
|
||||
|
||||
end
|
||||
}
|
||||
}
|
||||
end, {})
|
||||
|
||||
previewers.qflist = previewers.new {
|
||||
previewers.qflist = defaulter(function(_)
|
||||
return previewers.new {
|
||||
setup = function()
|
||||
local command_string = "cat %s"
|
||||
if vim.fn.executable("bat") then
|
||||
@@ -263,9 +280,11 @@ previewers.qflist = previewers.new {
|
||||
vim.fn.termopen(termopen_command)
|
||||
end)
|
||||
end
|
||||
}
|
||||
}
|
||||
end, {})
|
||||
|
||||
previewers.help = previewers.new {
|
||||
previewers.help = defaulter(function(_)
|
||||
return previewers.new {
|
||||
preview_fn = function(_, entry, status)
|
||||
with_preview_window(status, function()
|
||||
local old_tags = vim.o.tags
|
||||
@@ -293,7 +312,8 @@ previewers.help = previewers.new {
|
||||
vim.o.tags = old_tags
|
||||
end)
|
||||
end
|
||||
}
|
||||
}
|
||||
end, {})
|
||||
|
||||
previewers.Previewer = Previewer
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ end
|
||||
|
||||
function Sorter:score(prompt, entry)
|
||||
-- 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
|
||||
|
||||
function sorters.new(...)
|
||||
@@ -232,7 +232,11 @@ sorters.get_norcalli_sorter = function()
|
||||
end
|
||||
|
||||
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
|
||||
return 0
|
||||
end
|
||||
|
||||
@@ -114,4 +114,28 @@ utils.path_shorten = (function()
|
||||
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
|
||||
|
||||
19
lua/tests/manual/all_defaults.lua
Normal file
19
lua/tests/manual/all_defaults.lua
Normal 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()
|
||||
Reference in New Issue
Block a user