summaryrefslogtreecommitdiff
path: root/lua/telescope/builtin.lua
diff options
context:
space:
mode:
authorTJ DeVries <devries.timothyj@gmail.com>2020-08-27 22:12:44 -0400
committerTJ DeVries <devries.timothyj@gmail.com>2020-08-27 22:12:47 -0400
commit7e9f38a87e1dfc5226665e9602e39a900519c732 (patch)
treee58f8250f1d4802bcc21733edad21d690ccd38b0 /lua/telescope/builtin.lua
parentc4dd59ff65b85360dacb03f6a97d96627b4c1cb1 (diff)
feat: Add livegrep and lsp referecnes
Diffstat (limited to 'lua/telescope/builtin.lua')
-rw-r--r--lua/telescope/builtin.lua132
1 files changed, 124 insertions, 8 deletions
diff --git a/lua/telescope/builtin.lua b/lua/telescope/builtin.lua
index ff5a16d..fad98d1 100644
--- a/lua/telescope/builtin.lua
+++ b/lua/telescope/builtin.lua
@@ -4,29 +4,37 @@ A collection of builtin pipelines for telesceope.
Meant for both example and for easy startup.
--]]
+local finders = require('telescope.finders')
+local previewers = require('telescope.previewers')
+local pickers = require('telescope.pickers')
+local sorters = require('telescope.sorters')
+
local builtin = {}
-builtin.git_files = function(_)
+builtin.git_files = function()
-- TODO: Auto select bottom row
-- TODO: filter out results when they don't match at all anymore.
- local telescope = require('telescope')
-
- local file_finder = telescope.finders.new {
+ local file_finder = finders.new {
static = true,
- fn_command = function() return 'git ls-files' end,
+ fn_command = function(self)
+ return {
+ command = 'git',
+ args = {'ls-files'}
+ }
+ end,
}
- local file_previewer = telescope.previewers.vim_buffer
+ local file_previewer = previewers.cat
- local file_picker = telescope.pickers.new {
+ local file_picker = pickers.new {
previewer = file_previewer
}
-- local file_sorter = telescope.sorters.get_ngram_sorter()
-- local file_sorter = require('telescope.sorters').get_levenshtein_sorter()
- local file_sorter = telescope.sorters.get_norcalli_sorter()
+ local file_sorter = sorters.get_norcalli_sorter()
file_picker:find {
prompt = 'Simple File',
@@ -35,5 +43,113 @@ builtin.git_files = function(_)
}
end
+builtin.live_grep = function()
+ local live_grepper = finders.new {
+ maximum_results = 1000,
+
+ fn_command = function(self, prompt)
+ -- TODO: Make it so that we can start searching on the first character.
+ if not prompt or prompt == "" then
+ return nil
+ end
+
+ return {
+ command = 'rg',
+ args = {"--vimgrep", prompt},
+ }
+ end
+ }
+
+ local file_previewer = previewers.vimgrep
+ local file_picker = pickers.new {
+ previewer = file_previewer
+ }
+
+ -- local file_sorter = telescope.sorters.get_ngram_sorter()
+ -- local file_sorter = require('telescope.sorters').get_levenshtein_sorter()
+ -- local file_sorter = sorters.get_norcalli_sorter()
+
+ -- 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
+ -- }
+
+ file_picker:find {
+ prompt = 'Live Grep',
+ finder = live_grepper,
+ sorter = oldfiles_sorter,
+ }
+end
+
+builtin.lsp_references = function()
+ local params = vim.lsp.util.make_position_params()
+ params.context = { includeDeclaration = true }
+
+ local results_lsp = vim.lsp.buf_request_sync(0, "textDocument/references", params)
+ local locations = {}
+ for _, server_results in pairs(results_lsp) do
+ vim.list_extend(locations, vim.lsp.util.locations_to_items(server_results.result) or {})
+ end
+
+ local results = {}
+ for _, entry in ipairs(locations) do
+ local vimgrep_str = string.format(
+ "%s:%s:%s: %s",
+ vim.fn.fnamemodify(entry.filename, ":."),
+ entry.lnum,
+ entry.col,
+ entry.text
+ )
+
+ table.insert(results, {
+ valid = true,
+ value = entry,
+ ordinal = vimgrep_str,
+ display = vimgrep_str,
+ })
+ end
+
+ if vim.tbl_isempty(results) then
+ return
+ end
+
+ local lsp_reference_finder = finders.new {
+ results = results
+ }
+
+ local reference_previewer = previewers.qflist
+ local reference_picker = pickers.new {
+ previewer = reference_previewer
+ }
+
+ reference_picker:find {
+ prompt = 'LSP References',
+ finder = lsp_reference_finder,
+ sorter = sorters.get_norcalli_sorter(),
+ }
+end
+
+
return builtin