From 14310ee6b1f40425ba854d6ddc6374960fdf81ca Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Fri, 4 Sep 2020 09:49:10 -0400 Subject: [PATCH] fix: Don't push past midnight. You'll make clason's stuff break :/ --- lua/telescope/builtin.lua | 50 ++- lua/telescope/finders.lua | 17 +- lua/telescope/make_entry.lua | 12 +- lua/telescope/pickers.lua | 2 + lua/telescope/pickers/layout_strategies.lua | 25 ++ lua/telescope/previewers.lua | 416 ++++++++++---------- lua/telescope/sorters.lua | 8 +- lua/telescope/utils.lua | 26 +- lua/tests/manual/all_defaults.lua | 19 + 9 files changed, 352 insertions(+), 223 deletions(-) create mode 100644 lua/tests/manual/all_defaults.lua diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua index 7f4c72d..f96fa97 100644 --- a/lua/telescope/builtin.lua +++ b/lua/telescope/builtin.lua @@ -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("") + 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', '', 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 diff --git a/lua/telescope/finders.lua b/lua/telescope/finders.lua index 7682aeb..3957f26 100644 --- a/lua/telescope/finders.lua +++ b/lua/telescope/finders.lua @@ -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 { diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index aca8894..09c7d30 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -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 ~= "", diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index c747927..6fd7442 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -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, diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index 10c9613..0d66af3 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -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) 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 +end, {}) -previewers.vim_buffer = previewers.new { - setup = function() return { last_set_bufnr = nil } end, +previewers.vim_buffer = defaulter(function(_) + return 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, + 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 + preview_fn = function(self, entry, status) + -- TODO: Consider using path here? Might not work otherwise. + local filename = entry.filename - if filename == nil then - local value = entry.value - filename = vim.split(value, ":")[1] - end + if filename == nil then + local value = entry.value + filename = vim.split(value, ":")[1] + end - if filename == nil then - return - end + if filename == nil then + return + end - log.trace("Previewing File: %s", filename) + 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 + 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) - - 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) + 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, - - 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) + 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, {}) - 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 end - local best_entry = taglist[1] - local new_bufnr = vim.fn.bufnr(best_entry.filename, true) + local file_name = vim.split(value, ":")[1] - vim.api.nvim_buf_set_option(new_bufnr, 'filetype', 'help') - vim.api.nvim_win_set_buf(status.preview_win, new_bufnr) + log.trace("Previewing File: %s", file_name) - vim.cmd [["gg"]] - print(best_entry.cmd) - vim.cmd(string.format([[execute "%s"]], best_entry.cmd)) + -- vim.fn.termopen( + -- string.format("bat --color=always --style=grid %s"), + -- vim.fn.fnamemodify(file_name, ":p") + local bufnr = vim.fn.bufadd(file_name) - vim.o.tags = old_tags - end) - end -} + 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) + 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 diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua index e2a7639..474bdff 100644 --- a/lua/telescope/sorters.lua +++ b/lua/telescope/sorters.lua @@ -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 diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index b95f626..6154889 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -31,7 +31,7 @@ utils.reversed_ipairs = function(t) return reversedipairsiter, t, #t + 1 end -utils.default_table_mt = { +utils.default_table_mt = { __index = function(t, k) local obj = {} rawset(t, k, obj) @@ -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 diff --git a/lua/tests/manual/all_defaults.lua b/lua/tests/manual/all_defaults.lua new file mode 100644 index 0000000..ba77786 --- /dev/null +++ b/lua/tests/manual/all_defaults.lua @@ -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()