diff options
| author | TJ DeVries <devries.timothyj@gmail.com> | 2020-09-03 23:56:49 -0400 |
|---|---|---|
| committer | TJ DeVries <devries.timothyj@gmail.com> | 2020-09-03 23:56:49 -0400 |
| commit | 839f57efb37cba7a9542b67b31370e1babaf194a (patch) | |
| tree | 2e502a58d83bc528e78b3d44d92134ec96d1345a /lua/telescope/builtin.lua | |
| parent | 737363097b8710566cf624776f1d0a18c03a0d8b (diff) | |
feat: Major improvements in API. Particularly relating to entries.
Diffstat (limited to 'lua/telescope/builtin.lua')
| -rw-r--r-- | lua/telescope/builtin.lua | 197 |
1 files changed, 119 insertions, 78 deletions
diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua index d207b99..0430d09 100644 --- a/lua/telescope/builtin.lua +++ b/lua/telescope/builtin.lua @@ -11,94 +11,69 @@ This will use the default configuration options. Other configuration options still in flux at the moment --]] +-- TODO: Give some bonus weight to files we've picked before +-- TODO: Give some bonus weight to oldfiles + local actions = require('telescope.actions') local finders = require('telescope.finders') +local make_entry = require('telescope.make_entry') local previewers = require('telescope.previewers') local pickers = require('telescope.pickers') local sorters = require('telescope.sorters') local utils = require('telescope.utils') +local flatten = vim.tbl_flatten + +-- TODO: Support silver search here. +-- TODO: Support normal grep here (in case neither are installed). +local vimgrep_arguments = {'rg', '--color=never', '--no-heading', '--with-filename', '--line-number', '--column'} + local builtin = {} builtin.git_files = function(opts) opts = opts or {} - local make_entry = ( - opts.shorten_path - and function(value) - local result = { - valid = true, - display = utils.path_shorten(value), - ordinal = value, - value = value - } - - return result - end) - - or nil - pickers.new(opts, { prompt = 'Git File', - finder = finders.new_oneshot_job({ "git", "ls-files" }, make_entry), + finder = finders.new_oneshot_job( + { "git", "ls-files", "-o", "--exclude-standard", "-c" }, + make_entry.gen_from_file(opts) + ), previewer = previewers.cat, sorter = sorters.get_fuzzy_file(), }):find() end builtin.live_grep = function(opts) - local live_grepper = finders.new { - fn_command = function(_, prompt) - -- TODO: Make it so that we can start searching on the first character. + opts = opts or {} + + local live_grepper = finders.new_job(function(prompt) + -- TODO: Probably could add some options for smart case and whatever else rg offers. + if not prompt or prompt == "" then return nil end - return { - command = 'rg', - args = {"--vimgrep", prompt}, - } - end - } + return flatten { vimgrep_arguments, prompt } + end, + opts.entry_maker or make_entry.gen_from_vimgrep(opts), + opts.max_results + ) pickers.new(opts, { prompt = 'Live Grep', finder = live_grepper, previewer = previewers.vimgrep, }):find() - - -- TODO: Incorporate this. - -- Weight the results somehow to be more likely to be the ones that you've opened. - -- local old_files = {} - -- for _, f in ipairs(vim.v.oldfiles) do - -- old_files[f] = true - -- end - - -- local oldfiles_sorter = sorters.new { - -- scoring_function = function(prompt, entry) - -- local line = entry.value - - -- if not line then - -- return - -- end - - -- local _, finish = string.find(line, ":") - -- local filename = string.sub(line, 1, finish - 1) - -- local expanded_fname = vim.fn.fnamemodify(filename, ':p') - -- if old_files[expanded_fname] then - -- print("Found oldfiles: ", entry.value) - -- return 0 - -- else - -- return 1 - -- end - -- end - -- } 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) + local params = vim.lsp.util.make_position_params() params.context = { includeDeclaration = true } @@ -108,15 +83,74 @@ builtin.lsp_references = function(opts) vim.list_extend(locations, vim.lsp.util.locations_to_items(server_results.result) or {}) end - local results = utils.quickfix_items_to_entries(locations) - - if vim.tbl_isempty(results) then + if vim.tbl_isempty(locations) then return end - local reference_picker = pickers.new(opts, { + pickers.new(opts, { prompt = 'LSP References', - finder = finders.new_table(results), + finder = finders.new_table { + results = locations, + entry_maker = make_entry.gen_from_quickfix(opts), + }, + previewer = previewers.qflist, + sorter = sorters.get_norcalli_sorter(), + }):find() +end + +builtin.lsp_document_symbols = function(opts) + local params = vim.lsp.util.make_position_params() + local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/documentSymbol", params) + + if not results_lsp or vim.tbl_isempty(results_lsp) then + print("No results from textDocument/documentSymbol") + return + end + + local locations = {} + for _, server_results in pairs(results_lsp) do + vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {}) + end + + if vim.tbl_isempty(locations) then + return + end + + pickers.new(opts, { + prompt = 'LSP Document Symbols', + finder = finders.new_table { + results = locations, + entry_maker = make_entry.gen_from_quickfix(opts) + }, + previewer = previewers.vim_buffer, + sorter = sorters.get_norcalli_sorter(), + }):find() +end + +builtin.lsp_workspace_symbols = function(opts) + local params = {query = opts.query or ''} + local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params, 1000) + + if not results_lsp or vim.tbl_isempty(results_lsp) then + print("No results from textDocument/documentSymbol") + return + end + + local locations = {} + for _, server_results in pairs(results_lsp) do + vim.list_extend(locations, vim.lsp.util.symbols_to_items(server_results.result, 0) or {}) + end + + if vim.tbl_isempty(locations) then + return + end + + pickers.new(opts, { + prompt = 'LSP Workspace Symbols', + finder = finders.new_table { + results = locations, + entry_maker = make_entry.gen_from_quickfix(opts) + }, previewer = previewers.qflist, sorter = sorters.get_norcalli_sorter(), }):find() @@ -124,15 +158,17 @@ end builtin.quickfix = function(opts) local locations = vim.fn.getqflist() - local results = utils.quickfix_items_to_entries(locations) - if vim.tbl_isempty(results) then + if vim.tbl_isempty(locations) then return end pickers.new(opts, { prompt = 'Quickfix', - finder = finders.new_table(results), + finder = finders.new_table { + results = locations, + entry_maker = make_entry.gen_from_quickfix(opts), + }, previewer = previewers.qflist, sorter = sorters.get_norcalli_sorter(), }):find() @@ -146,15 +182,16 @@ builtin.loclist = function(opts) value.filename = filename end - local results = utils.quickfix_items_to_entries(locations) - - if vim.tbl_isempty(results) then + if vim.tbl_isempty(locations) then return end pickers.new(opts, { prompt = 'Loclist', - finder = finders.new_table(results), + finder = finders.new_table { + results = locations, + entry_maker = make_entry.gen_from_quickfix(opts), + }, previewer = previewers.qflist, sorter = sorters.get_norcalli_sorter(), }):find() @@ -165,9 +202,12 @@ builtin.grep_string = function(opts) local search = opts.search or vim.fn.expand("<cword>") - local file_picker = pickers.new(opts, { + pickers.new(opts, { prompt = 'Find Word', - finder = finders.new_oneshot_job {'rg', '--vimgrep', search}, + finder = finders.new_oneshot_job( + flatten { vimgrep_arguments, search}, + make_entry.gen_from_vimgrep(opts) + ), previewer = previewers.vimgrep, sorter = sorters.get_norcalli_sorter(), }):find() @@ -218,27 +258,32 @@ end -- vim.fn.setreg("+", "nnoremap $TODO :lua require('telescope.builtin').<whatever>()<CR>") -- TODO: Can we just do the names instead? builtin.builtin = function(opts) + opts = opts or {} + opts.hide_filename = utils.get_default(opts.hide_filename, true) + opts.ignore_filename = utils.get_default(opts.ignore_filename, true) + local objs = {} for k, v in pairs(builtin) do local debug_info = debug.getinfo(v) table.insert(objs, { - vimgrep_str = k, filename = string.sub(debug_info.source, 2), lnum = debug_info.linedefined, col = 0, + text = k, start = debug_info.linedefined, finish = debug_info.lastlinedefined, }) end - local entries = utils.quickfix_items_to_entries(objs) - pickers.new(opts, { prompt = 'Telescope Builtin', - finder = finders.new_table(entries), + finder = finders.new_table { + results = objs, + entry_maker = make_entry.gen_from_quickfix(opts), + }, previewer = previewers.qflist, sorter = sorters.get_norcalli_sorter(), }):find() @@ -269,14 +314,10 @@ builtin.fd = function(opts) pickers.new(opts, { prompt = 'Find Files', - finder = finders.new { - fn_command = function() - return { - command = fd_string, - cwd = cwd, - } - end, - }, + finder = finders.new_oneshot_job( + {fd_string}, + make_entry.gen_from_file(opts) + ), previewer = previewers.cat, sorter = sorters.get_fuzzy_file(), }):find() |
