From f838695459d75a889ae8d96e370957f0f675902a Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Thu, 7 Jul 2022 08:27:46 +0200 Subject: [PATCH] chore: reformat with stylua 0.14.0 --- lua/telescope/actions/init.lua | 34 +- lua/telescope/builtin/__diagnostics.lua | 26 +- lua/telescope/builtin/__files.lua | 180 +-- lua/telescope/builtin/__git.lua | 354 +++--- lua/telescope/builtin/__internal.lua | 1109 ++++++++++--------- lua/telescope/builtin/__lsp.lua | 156 +-- lua/telescope/mappings.lua | 15 +- lua/telescope/pickers.lua | 7 +- lua/telescope/pickers/layout_strategies.lua | 7 +- lua/telescope/previewers/utils.lua | 42 +- lua/telescope/sorters.lua | 22 +- lua/telescope/utils.lua | 18 +- 12 files changed, 1016 insertions(+), 954 deletions(-) diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index 8f9e5c8..768dc65 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -242,10 +242,9 @@ end ---@param prompt_bufnr number: The prompt bufnr actions.select_default = { pre = function(prompt_bufnr) - action_state.get_current_history():append( - action_state.get_current_line(), - action_state.get_current_picker(prompt_bufnr) - ) + action_state + .get_current_history() + :append(action_state.get_current_line(), action_state.get_current_picker(prompt_bufnr)) end, action = function(prompt_bufnr) return action_set.select(prompt_bufnr, "default") @@ -259,10 +258,9 @@ actions.select_default = { ---@param prompt_bufnr number: The prompt bufnr actions.select_horizontal = { pre = function(prompt_bufnr) - action_state.get_current_history():append( - action_state.get_current_line(), - action_state.get_current_picker(prompt_bufnr) - ) + action_state + .get_current_history() + :append(action_state.get_current_line(), action_state.get_current_picker(prompt_bufnr)) end, action = function(prompt_bufnr) return action_set.select(prompt_bufnr, "horizontal") @@ -276,10 +274,9 @@ actions.select_horizontal = { ---@param prompt_bufnr number: The prompt bufnr actions.select_vertical = { pre = function(prompt_bufnr) - action_state.get_current_history():append( - action_state.get_current_line(), - action_state.get_current_picker(prompt_bufnr) - ) + action_state + .get_current_history() + :append(action_state.get_current_line(), action_state.get_current_picker(prompt_bufnr)) end, action = function(prompt_bufnr) return action_set.select(prompt_bufnr, "vertical") @@ -293,10 +290,9 @@ actions.select_vertical = { ---@param prompt_bufnr number: The prompt bufnr actions.select_tab = { pre = function(prompt_bufnr) - action_state.get_current_history():append( - action_state.get_current_line(), - action_state.get_current_picker(prompt_bufnr) - ) + action_state + .get_current_history() + :append(action_state.get_current_line(), action_state.get_current_picker(prompt_bufnr)) end, action = function(prompt_bufnr) return action_set.select(prompt_bufnr, "tab") @@ -1175,10 +1171,8 @@ actions.which_key = function(prompt_bufnr, opts) + opts.name_width + (3 * #opts.separator) local num_total_columns = math.floor((vim.o.columns - #column_indent) / entry_width) - opts.num_rows = math.min( - math.ceil(#mappings / num_total_columns), - resolver.resolve_height(opts.max_height)(_, _, vim.o.lines) - ) + opts.num_rows = + math.min(math.ceil(#mappings / num_total_columns), resolver.resolve_height(opts.max_height)(_, _, vim.o.lines)) local total_available_entries = opts.num_rows * num_total_columns local winheight = opts.num_rows + 2 * opts.line_padding diff --git a/lua/telescope/builtin/__diagnostics.lua b/lua/telescope/builtin/__diagnostics.lua index 2614e36..ad50cfa 100644 --- a/lua/telescope/builtin/__diagnostics.lua +++ b/lua/telescope/builtin/__diagnostics.lua @@ -122,18 +122,20 @@ diagnostics.get = function(opts) end opts.path_display = vim.F.if_nil(opts.path_display, "hidden") - pickers.new(opts, { - prompt_title = opts.bufnr == nil and "Workspace Diagnostics" or "Document Diagnostics", - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_diagnostics(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.prefilter_sorter { - tag = "type", - sorter = conf.generic_sorter(opts), - }, - }):find() + pickers + .new(opts, { + prompt_title = opts.bufnr == nil and "Workspace Diagnostics" or "Document Diagnostics", + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_diagnostics(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.prefilter_sorter { + tag = "type", + sorter = conf.generic_sorter(opts), + }, + }) + :find() end local function apply_checks(mod) diff --git a/lua/telescope/builtin/__files.lua b/lua/telescope/builtin/__files.lua index 7036f6a..2a40936 100644 --- a/lua/telescope/builtin/__files.lua +++ b/lua/telescope/builtin/__files.lua @@ -110,18 +110,20 @@ files.live_grep = function(opts) return flatten { vimgrep_arguments, additional_args, "--", prompt, search_list } end, opts.entry_maker or make_entry.gen_from_vimgrep(opts), opts.max_results, opts.cwd) - pickers.new(opts, { - prompt_title = "Live Grep", - finder = live_grepper, - previewer = conf.grep_previewer(opts), - -- TODO: It would be cool to use `--json` output for this - -- and then we could get the highlight positions directly. - sorter = sorters.highlighter_only(opts), - attach_mappings = function(_, map) - map("i", "", actions.to_fuzzy_refine) - return true - end, - }):find() + pickers + .new(opts, { + prompt_title = "Live Grep", + finder = live_grepper, + previewer = conf.grep_previewer(opts), + -- TODO: It would be cool to use `--json` output for this + -- and then we could get the highlight positions directly. + sorter = sorters.highlighter_only(opts), + attach_mappings = function(_, map) + map("i", "", actions.to_fuzzy_refine) + return true + end, + }) + :find() end files.grep_string = function(opts) @@ -161,12 +163,14 @@ files.grep_string = function(opts) end opts.entry_maker = opts.entry_maker or make_entry.gen_from_vimgrep(opts) - pickers.new(opts, { - prompt_title = "Find Word (" .. word:gsub("\n", "\\n") .. ")", - finder = finders.new_oneshot_job(args, opts), - previewer = conf.grep_previewer(opts), - sorter = conf.generic_sorter(opts), - }):find() + pickers + .new(opts, { + prompt_title = "Find Word (" .. word:gsub("\n", "\\n") .. ")", + finder = finders.new_oneshot_job(args, opts), + previewer = conf.grep_previewer(opts), + sorter = conf.generic_sorter(opts), + }) + :find() end files.find_files = function(opts) @@ -291,12 +295,14 @@ files.find_files = function(opts) opts.entry_maker = opts.entry_maker or make_entry.gen_from_file(opts) - pickers.new(opts, { - prompt_title = "Find Files", - finder = finders.new_oneshot_job(find_command, opts), - previewer = conf.file_previewer(opts), - sorter = conf.file_sorter(opts), - }):find() + pickers + .new(opts, { + prompt_title = "Find Files", + finder = finders.new_oneshot_job(find_command, opts), + previewer = conf.file_previewer(opts), + sorter = conf.file_sorter(opts), + }) + :find() end local function prepare_match(entry, kind) @@ -349,18 +355,20 @@ files.treesitter = function(opts) return end - pickers.new(opts, { - prompt_title = "Treesitter Symbols", - finder = finders.new_table { - results = results, - entry_maker = opts.entry_maker or make_entry.gen_from_treesitter(opts), - }, - previewer = conf.grep_previewer(opts), - sorter = conf.prefilter_sorter { - tag = "kind", - sorter = conf.generic_sorter(opts), - }, - }):find() + pickers + .new(opts, { + prompt_title = "Treesitter Symbols", + finder = finders.new_table { + results = results, + entry_maker = opts.entry_maker or make_entry.gen_from_treesitter(opts), + }, + previewer = conf.grep_previewer(opts), + sorter = conf.prefilter_sorter { + tag = "kind", + sorter = conf.generic_sorter(opts), + }, + }) + :find() end files.current_buffer_fuzzy_find = function(opts) @@ -432,25 +440,27 @@ files.current_buffer_fuzzy_find = function(opts) opts.line_highlights = line_highlights end - pickers.new(opts, { - prompt_title = "Current Buffer Fuzzy", - finder = finders.new_table { - results = lines_with_numbers, - entry_maker = opts.entry_maker or make_entry.gen_from_buffer_lines(opts), - }, - sorter = conf.generic_sorter(opts), - previewer = conf.grep_previewer(opts), - attach_mappings = function() - action_set.select:enhance { - post = function() - local selection = action_state.get_selected_entry() - vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 }) - end, - } + pickers + .new(opts, { + prompt_title = "Current Buffer Fuzzy", + finder = finders.new_table { + results = lines_with_numbers, + entry_maker = opts.entry_maker or make_entry.gen_from_buffer_lines(opts), + }, + sorter = conf.generic_sorter(opts), + previewer = conf.grep_previewer(opts), + attach_mappings = function() + action_set.select:enhance { + post = function() + local selection = action_state.get_selected_entry() + vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 }) + end, + } - return true - end, - }):find() + return true + end, + }) + :find() end files.tags = function(opts) @@ -467,38 +477,40 @@ files.tags = function(opts) end opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_ctags(opts)) - pickers.new(opts, { - prompt_title = "Tags", - finder = finders.new_oneshot_job(flatten { "cat", tagfiles }, opts), - previewer = previewers.ctags.new(opts), - sorter = conf.generic_sorter(opts), - attach_mappings = function() - action_set.select:enhance { - post = function() - local selection = action_state.get_selected_entry() - if not selection then - return - end + pickers + .new(opts, { + prompt_title = "Tags", + finder = finders.new_oneshot_job(flatten { "cat", tagfiles }, opts), + previewer = previewers.ctags.new(opts), + sorter = conf.generic_sorter(opts), + attach_mappings = function() + action_set.select:enhance { + post = function() + local selection = action_state.get_selected_entry() + if not selection then + return + end - if selection.scode then - -- un-escape / then escape required - -- special chars for vim.fn.search() - -- ] ~ * - local scode = selection.scode:gsub([[\/]], "/"):gsub("[%]~*]", function(x) - return "\\" .. x - end) + if selection.scode then + -- un-escape / then escape required + -- special chars for vim.fn.search() + -- ] ~ * + local scode = selection.scode:gsub([[\/]], "/"):gsub("[%]~*]", function(x) + return "\\" .. x + end) - vim.cmd "norm! gg" - vim.fn.search(scode) - vim.cmd "norm! zz" - else - vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 }) - end - end, - } - return true - end, - }):find() + vim.cmd "norm! gg" + vim.fn.search(scode) + vim.cmd "norm! zz" + else + vim.api.nvim_win_set_cursor(0, { selection.lnum, 0 }) + end + end, + } + return true + end, + }) + :find() end files.current_buffer_tags = function(opts) diff --git a/lua/telescope/builtin/__git.lua b/lua/telescope/builtin/__git.lua index 7db45ef..7c4f28b 100644 --- a/lua/telescope/builtin/__git.lua +++ b/lua/telescope/builtin/__git.lua @@ -37,70 +37,76 @@ git.files = function(opts) opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_file(opts)) local git_command = vim.F.if_nil(opts.git_command, { "git", "ls-files", "--exclude-standard", "--cached" }) - pickers.new(opts, { - prompt_title = "Git Files", - finder = finders.new_oneshot_job( - vim.tbl_flatten { - git_command, - show_untracked and "--others" or nil, - recurse_submodules and "--recurse-submodules" or nil, - }, - opts - ), - previewer = conf.file_previewer(opts), - sorter = conf.file_sorter(opts), - }):find() + pickers + .new(opts, { + prompt_title = "Git Files", + finder = finders.new_oneshot_job( + vim.tbl_flatten { + git_command, + show_untracked and "--others" or nil, + recurse_submodules and "--recurse-submodules" or nil, + }, + opts + ), + previewer = conf.file_previewer(opts), + sorter = conf.file_sorter(opts), + }) + :find() end git.commits = function(opts) opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_git_commits(opts)) local git_command = vim.F.if_nil(opts.git_command, { "git", "log", "--pretty=oneline", "--abbrev-commit", "--", "." }) - pickers.new(opts, { - prompt_title = "Git Commits", - finder = finders.new_oneshot_job(git_command, opts), - previewer = { - previewers.git_commit_diff_to_parent.new(opts), - previewers.git_commit_diff_to_head.new(opts), - previewers.git_commit_diff_as_was.new(opts), - previewers.git_commit_message.new(opts), - }, - sorter = conf.file_sorter(opts), - attach_mappings = function(_, map) - actions.select_default:replace(actions.git_checkout) - map("i", "m", actions.git_reset_mixed) - map("n", "m", actions.git_reset_mixed) - map("i", "s", actions.git_reset_soft) - map("n", "s", actions.git_reset_soft) - map("i", "h", actions.git_reset_hard) - map("n", "h", actions.git_reset_hard) - return true - end, - }):find() + pickers + .new(opts, { + prompt_title = "Git Commits", + finder = finders.new_oneshot_job(git_command, opts), + previewer = { + previewers.git_commit_diff_to_parent.new(opts), + previewers.git_commit_diff_to_head.new(opts), + previewers.git_commit_diff_as_was.new(opts), + previewers.git_commit_message.new(opts), + }, + sorter = conf.file_sorter(opts), + attach_mappings = function(_, map) + actions.select_default:replace(actions.git_checkout) + map("i", "m", actions.git_reset_mixed) + map("n", "m", actions.git_reset_mixed) + map("i", "s", actions.git_reset_soft) + map("n", "s", actions.git_reset_soft) + map("i", "h", actions.git_reset_hard) + map("n", "h", actions.git_reset_hard) + return true + end, + }) + :find() end git.stash = function(opts) opts.show_branch = vim.F.if_nil(opts.show_branch, true) opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_git_stash(opts)) - pickers.new(opts, { - prompt_title = "Git Stash", - finder = finders.new_oneshot_job( - vim.tbl_flatten { - "git", - "--no-pager", - "stash", - "list", - }, - opts - ), - previewer = previewers.git_stash_diff.new(opts), - sorter = conf.file_sorter(opts), - attach_mappings = function() - actions.select_default:replace(actions.git_apply_stash) - return true - end, - }):find() + pickers + .new(opts, { + prompt_title = "Git Stash", + finder = finders.new_oneshot_job( + vim.tbl_flatten { + "git", + "--no-pager", + "stash", + "list", + }, + opts + ), + previewer = previewers.git_stash_diff.new(opts), + sorter = conf.file_sorter(opts), + attach_mappings = function() + actions.select_default:replace(actions.git_apply_stash) + return true + end, + }) + :find() end local get_current_buf_line = function(winnr) @@ -112,83 +118,83 @@ git.bcommits = function(opts) opts.current_line = (opts.current_file == nil) and get_current_buf_line(opts.winnr) or nil opts.current_file = vim.F.if_nil(opts.current_file, vim.api.nvim_buf_get_name(opts.bufnr)) opts.entry_maker = vim.F.if_nil(opts.entry_maker, make_entry.gen_from_git_commits(opts)) - local git_command = vim.F.if_nil( - opts.git_command, - { "git", "log", "--pretty=oneline", "--abbrev-commit", "--follow" } - ) + local git_command = + vim.F.if_nil(opts.git_command, { "git", "log", "--pretty=oneline", "--abbrev-commit", "--follow" }) - pickers.new(opts, { - prompt_title = "Git BCommits", - finder = finders.new_oneshot_job( - vim.tbl_flatten { - git_command, - opts.current_file, + pickers + .new(opts, { + prompt_title = "Git BCommits", + finder = finders.new_oneshot_job( + vim.tbl_flatten { + git_command, + opts.current_file, + }, + opts + ), + previewer = { + previewers.git_commit_diff_to_parent.new(opts), + previewers.git_commit_diff_to_head.new(opts), + previewers.git_commit_diff_as_was.new(opts), + previewers.git_commit_message.new(opts), }, - opts - ), - previewer = { - previewers.git_commit_diff_to_parent.new(opts), - previewers.git_commit_diff_to_head.new(opts), - previewers.git_commit_diff_as_was.new(opts), - previewers.git_commit_message.new(opts), - }, - sorter = conf.file_sorter(opts), - attach_mappings = function() - actions.select_default:replace(actions.git_checkout_current_buffer) - local transfrom_file = function() - return opts.current_file and Path:new(opts.current_file):make_relative(opts.cwd) or "" - end + sorter = conf.file_sorter(opts), + attach_mappings = function() + actions.select_default:replace(actions.git_checkout_current_buffer) + local transfrom_file = function() + return opts.current_file and Path:new(opts.current_file):make_relative(opts.cwd) or "" + end - local get_buffer_of_orig = function(selection) - local value = selection.value .. ":" .. transfrom_file() - local content = utils.get_os_command_output({ "git", "--no-pager", "show", value }, opts.cwd) + local get_buffer_of_orig = function(selection) + local value = selection.value .. ":" .. transfrom_file() + local content = utils.get_os_command_output({ "git", "--no-pager", "show", value }, opts.cwd) - local bufnr = vim.api.nvim_create_buf(false, true) - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, content) - vim.api.nvim_buf_set_name(bufnr, "Original") - return bufnr - end + local bufnr = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, content) + vim.api.nvim_buf_set_name(bufnr, "Original") + return bufnr + end - local vimdiff = function(selection, command) - local ft = vim.bo.filetype - vim.cmd "diffthis" + local vimdiff = function(selection, command) + local ft = vim.bo.filetype + vim.cmd "diffthis" - local bufnr = get_buffer_of_orig(selection) - vim.cmd(string.format("%s %s", command, bufnr)) - vim.bo.filetype = ft - vim.cmd "diffthis" + local bufnr = get_buffer_of_orig(selection) + vim.cmd(string.format("%s %s", command, bufnr)) + vim.bo.filetype = ft + vim.cmd "diffthis" - vim.api.nvim_create_autocmd("WinClosed", { - buffer = bufnr, - nested = true, - once = true, - callback = function() - vim.api.nvim_buf_delete(bufnr, { force = true }) - end, - }) - end + vim.api.nvim_create_autocmd("WinClosed", { + buffer = bufnr, + nested = true, + once = true, + callback = function() + vim.api.nvim_buf_delete(bufnr, { force = true }) + end, + }) + end - actions.select_vertical:replace(function(prompt_bufnr) - actions.close(prompt_bufnr) - local selection = action_state.get_selected_entry() - vimdiff(selection, "leftabove vert sbuffer") - end) + actions.select_vertical:replace(function(prompt_bufnr) + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + vimdiff(selection, "leftabove vert sbuffer") + end) - actions.select_horizontal:replace(function(prompt_bufnr) - actions.close(prompt_bufnr) - local selection = action_state.get_selected_entry() - vimdiff(selection, "belowright sbuffer") - end) + actions.select_horizontal:replace(function(prompt_bufnr) + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + vimdiff(selection, "belowright sbuffer") + end) - actions.select_tab:replace(function(prompt_bufnr) - actions.close(prompt_bufnr) - local selection = action_state.get_selected_entry() - vim.cmd("tabedit " .. transfrom_file()) - vimdiff(selection, "leftabove vert sbuffer") - end) - return true - end, - }):find() + actions.select_tab:replace(function(prompt_bufnr) + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + vim.cmd("tabedit " .. transfrom_file()) + vimdiff(selection, "leftabove vert sbuffer") + end) + return true + end, + }) + :find() end git.branches = function(opts) @@ -197,10 +203,8 @@ git.branches = function(opts) .. "%(authorname)" .. "%(upstream:lstrip=2)" .. "%(committerdate:format-local:%Y/%m/%d %H:%M:%S)" - local output = utils.get_os_command_output( - { "git", "for-each-ref", "--perl", "--format", format, opts.pattern }, - opts.cwd - ) + local output = + utils.get_os_command_output({ "git", "for-each-ref", "--perl", "--format", format, opts.pattern }, opts.cwd) local results = {} local widths = { @@ -273,41 +277,43 @@ git.branches = function(opts) } end - pickers.new(opts, { - prompt_title = "Git Branches", - finder = finders.new_table { - results = results, - entry_maker = function(entry) - entry.value = entry.name - entry.ordinal = entry.name - entry.display = make_display - return make_entry.set_default_entry_mt(entry, opts) + pickers + .new(opts, { + prompt_title = "Git Branches", + finder = finders.new_table { + results = results, + entry_maker = function(entry) + entry.value = entry.name + entry.ordinal = entry.name + entry.display = make_display + return make_entry.set_default_entry_mt(entry, opts) + end, + }, + previewer = previewers.git_branch_log.new(opts), + sorter = conf.file_sorter(opts), + attach_mappings = function(_, map) + actions.select_default:replace(actions.git_checkout) + map("i", "", actions.git_track_branch) + map("n", "", actions.git_track_branch) + + map("i", "", actions.git_rebase_branch) + map("n", "", actions.git_rebase_branch) + + map("i", "", actions.git_create_branch) + map("n", "", actions.git_create_branch) + + map("i", "", actions.git_switch_branch) + map("n", "", actions.git_switch_branch) + + map("i", "", actions.git_delete_branch) + map("n", "", actions.git_delete_branch) + + map("i", "", actions.git_merge_branch) + map("n", "", actions.git_merge_branch) + return true end, - }, - previewer = previewers.git_branch_log.new(opts), - sorter = conf.file_sorter(opts), - attach_mappings = function(_, map) - actions.select_default:replace(actions.git_checkout) - map("i", "", actions.git_track_branch) - map("n", "", actions.git_track_branch) - - map("i", "", actions.git_rebase_branch) - map("n", "", actions.git_rebase_branch) - - map("i", "", actions.git_create_branch) - map("n", "", actions.git_create_branch) - - map("i", "", actions.git_switch_branch) - map("n", "", actions.git_switch_branch) - - map("i", "", actions.git_delete_branch) - map("n", "", actions.git_delete_branch) - - map("i", "", actions.git_merge_branch) - map("n", "", actions.git_merge_branch) - return true - end, - }):find() + }) + :find() end git.status = function(opts) @@ -349,23 +355,25 @@ git.status = function(opts) return end - pickers.new(opts, { - prompt_title = "Git Status", - finder = initial_finder, - previewer = previewers.git_file_diff.new(opts), - sorter = conf.file_sorter(opts), - attach_mappings = function(prompt_bufnr, map) - actions.git_staging_toggle:enhance { - post = function() - action_state.get_current_picker(prompt_bufnr):refresh(gen_new_finder(), { reset_prompt = true }) - end, - } + pickers + .new(opts, { + prompt_title = "Git Status", + finder = initial_finder, + previewer = previewers.git_file_diff.new(opts), + sorter = conf.file_sorter(opts), + attach_mappings = function(prompt_bufnr, map) + actions.git_staging_toggle:enhance { + post = function() + action_state.get_current_picker(prompt_bufnr):refresh(gen_new_finder(), { reset_prompt = true }) + end, + } - map("i", "", actions.git_staging_toggle) - map("n", "", actions.git_staging_toggle) - return true - end, - }):find() + map("i", "", actions.git_staging_toggle) + map("n", "", actions.git_staging_toggle) + return true + end, + }) + :find() end local set_opts_cwd = function(opts) diff --git a/lua/telescope/builtin/__internal.lua b/lua/telescope/builtin/__internal.lua index 4277e2e..79e67e4 100644 --- a/lua/telescope/builtin/__internal.lua +++ b/lua/telescope/builtin/__internal.lua @@ -65,52 +65,54 @@ internal.builtin = function(opts) opts.bufnr = vim.api.nvim_get_current_buf() opts.winnr = vim.api.nvim_get_current_win() - pickers.new(opts, { - prompt_title = title, - finder = finders.new_table { - results = objs, - entry_maker = function(entry) - return make_entry.set_default_entry_mt({ - value = entry, - text = entry.text, - display = entry.text, - ordinal = entry.text, - filename = entry.filename, - }, opts) + pickers + .new(opts, { + prompt_title = title, + finder = finders.new_table { + results = objs, + entry_maker = function(entry) + return make_entry.set_default_entry_mt({ + value = entry, + text = entry.text, + display = entry.text, + ordinal = entry.text, + filename = entry.filename, + }, opts) + end, + }, + previewer = previewers.builtin.new(opts), + sorter = conf.generic_sorter(opts), + attach_mappings = function(_) + actions.select_default:replace(function(_) + local selection = action_state.get_selected_entry() + if not selection then + utils.__warn_no_selection "builtin.builtin" + return + end + + -- we do this to avoid any surprises + opts.include_extensions = nil + + local picker_opts + if not opts.use_default_opts then + picker_opts = opts + end + + if string.match(selection.text, " : ") then + -- Call appropriate function from extensions + local split_string = vim.split(selection.text, " : ") + local ext = split_string[1] + local func = split_string[2] + require("telescope").extensions[ext][func](picker_opts) + else + -- Call appropriate telescope builtin + require("telescope.builtin")[selection.text](picker_opts) + end + end) + return true end, - }, - previewer = previewers.builtin.new(opts), - sorter = conf.generic_sorter(opts), - attach_mappings = function(_) - actions.select_default:replace(function(_) - local selection = action_state.get_selected_entry() - if not selection then - utils.__warn_no_selection "builtin.builtin" - return - end - - -- we do this to avoid any surprises - opts.include_extensions = nil - - local picker_opts - if not opts.use_default_opts then - picker_opts = opts - end - - if string.match(selection.text, " : ") then - -- Call appropriate function from extensions - local split_string = vim.split(selection.text, " : ") - local ext = split_string[1] - local func = split_string[2] - require("telescope").extensions[ext][func](picker_opts) - else - -- Call appropriate telescope builtin - require("telescope.builtin")[selection.text](picker_opts) - end - end) - return true - end, - }):find() + }) + :find() end internal.resume = function(opts) @@ -176,30 +178,32 @@ internal.pickers = function(opts) opts.cache_picker = nil end - pickers.new(opts, { - prompt_title = "Pickers", - finder = finders.new_table { - results = cached_pickers, - entry_maker = make_entry.gen_from_picker(opts), - }, - previewer = previewers.pickers.new(opts), - sorter = conf.generic_sorter(opts), - cache_picker = false, - attach_mappings = function(_, map) - actions.select_default:replace(function(prompt_bufnr) - local current_picker = action_state.get_current_picker(prompt_bufnr) - local selection_index = current_picker:get_index(current_picker:get_selection_row()) - actions.close(prompt_bufnr) - opts.cache_picker = opts._cache_picker - opts["cache_index"] = selection_index - opts["initial_mode"] = cached_pickers[selection_index].initial_mode - internal.resume(opts) - end) - map("i", "", actions.remove_selected_picker) - map("n", "", actions.remove_selected_picker) - return true - end, - }):find() + pickers + .new(opts, { + prompt_title = "Pickers", + finder = finders.new_table { + results = cached_pickers, + entry_maker = make_entry.gen_from_picker(opts), + }, + previewer = previewers.pickers.new(opts), + sorter = conf.generic_sorter(opts), + cache_picker = false, + attach_mappings = function(_, map) + actions.select_default:replace(function(prompt_bufnr) + local current_picker = action_state.get_current_picker(prompt_bufnr) + local selection_index = current_picker:get_index(current_picker:get_selection_row()) + actions.close(prompt_bufnr) + opts.cache_picker = opts._cache_picker + opts["cache_index"] = selection_index + opts["initial_mode"] = cached_pickers[selection_index].initial_mode + internal.resume(opts) + end) + map("i", "", actions.remove_selected_picker) + map("n", "", actions.remove_selected_picker) + return true + end, + }) + :find() end internal.planets = function(opts) @@ -216,35 +220,37 @@ internal.planets = function(opts) end end - pickers.new(opts, { - prompt_title = "Planets", - finder = finders.new_table { - results = acceptable_files, - entry_maker = function(line) - return make_entry.set_default_entry_mt({ - ordinal = line, - display = line, - filename = base_directory .. "/data/memes/planets/" .. line, - }, opts) + pickers + .new(opts, { + prompt_title = "Planets", + finder = finders.new_table { + results = acceptable_files, + entry_maker = function(line) + return make_entry.set_default_entry_mt({ + ordinal = line, + display = line, + filename = base_directory .. "/data/memes/planets/" .. line, + }, opts) + end, + }, + previewer = previewers.cat.new(opts), + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.planets" + return + end + + actions.close(prompt_bufnr) + print("Enjoy astronomy! You viewed:", selection.display) + end) + + return true end, - }, - previewer = previewers.cat.new(opts), - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.planets" - return - end - - actions.close(prompt_bufnr) - print("Enjoy astronomy! You viewed:", selection.display) - end) - - return true - end, - }):find() + }) + :find() end internal.symbols = function(opts) @@ -293,80 +299,84 @@ internal.symbols = function(opts) end end - pickers.new(opts, { - prompt_title = "Symbols", - finder = finders.new_table { - results = results, - entry_maker = function(entry) - return make_entry.set_default_entry_mt({ - value = entry, - ordinal = entry[1] .. " " .. entry[2], - display = entry[1] .. " " .. entry[2], - }, opts) + pickers + .new(opts, { + prompt_title = "Symbols", + finder = finders.new_table { + results = results, + entry_maker = function(entry) + return make_entry.set_default_entry_mt({ + value = entry, + ordinal = entry[1] .. " " .. entry[2], + display = entry[1] .. " " .. entry[2], + }, opts) + end, + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(_) + if initial_mode == "i" then + actions.select_default:replace(actions.insert_symbol_i) + else + actions.select_default:replace(actions.insert_symbol) + end + return true end, - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(_) - if initial_mode == "i" then - actions.select_default:replace(actions.insert_symbol_i) - else - actions.select_default:replace(actions.insert_symbol) - end - return true - end, - }):find() + }) + :find() end internal.commands = function(opts) - pickers.new(opts, { - prompt_title = "Commands", - finder = finders.new_table { - results = (function() - local command_iter = vim.api.nvim_get_commands {} - local commands = {} + pickers + .new(opts, { + prompt_title = "Commands", + finder = finders.new_table { + results = (function() + local command_iter = vim.api.nvim_get_commands {} + local commands = {} - for _, cmd in pairs(command_iter) do - table.insert(commands, cmd) - end - - local need_buf_command = vim.F.if_nil(opts.show_buf_command, true) - - if need_buf_command then - local buf_command_iter = vim.api.nvim_buf_get_commands(0, {}) - buf_command_iter[true] = nil -- remove the redundant entry - for _, cmd in pairs(buf_command_iter) do + for _, cmd in pairs(command_iter) do table.insert(commands, cmd) end - end - return commands - end)(), - entry_maker = opts.entry_maker or make_entry.gen_from_commands(opts), - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.commands" - return - end + local need_buf_command = vim.F.if_nil(opts.show_buf_command, true) - actions.close(prompt_bufnr) - local val = selection.value - local cmd = string.format([[:%s ]], val.name) + if need_buf_command then + local buf_command_iter = vim.api.nvim_buf_get_commands(0, {}) + buf_command_iter[true] = nil -- remove the redundant entry + for _, cmd in pairs(buf_command_iter) do + table.insert(commands, cmd) + end + end + return commands + end)(), - if val.nargs == "0" then - vim.cmd(cmd) - else - vim.cmd [[stopinsert]] - vim.fn.feedkeys(cmd, "n") - end - end) + entry_maker = opts.entry_maker or make_entry.gen_from_commands(opts), + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.commands" + return + end - return true - end, - }):find() + actions.close(prompt_bufnr) + local val = selection.value + local cmd = string.format([[:%s ]], val.name) + + if val.nargs == "0" then + vim.cmd(cmd) + else + vim.cmd [[stopinsert]] + vim.fn.feedkeys(cmd, "n") + end + end) + + return true + end, + }) + :find() end internal.quickfix = function(opts) @@ -377,15 +387,17 @@ internal.quickfix = function(opts) return end - pickers.new(opts, { - prompt_title = "Quickfix", - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.generic_sorter(opts), - }):find() + pickers + .new(opts, { + prompt_title = "Quickfix", + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + }) + :find() end internal.quickfixhistory = function(opts) @@ -409,42 +421,44 @@ internal.quickfixhistory = function(opts) }, opts) end local qf_entry_maker = make_entry.gen_from_quickfix(opts) - pickers.new(opts, { - prompt_title = "Quickfix History", - finder = finders.new_table { - results = qflists, - entry_maker = entry_maker, - }, - previewer = previewers.new_buffer_previewer { - title = "Quickfix List Preview", - dyn_title = function(_, entry) - return entry.title - end, + pickers + .new(opts, { + prompt_title = "Quickfix History", + finder = finders.new_table { + results = qflists, + entry_maker = entry_maker, + }, + previewer = previewers.new_buffer_previewer { + title = "Quickfix List Preview", + dyn_title = function(_, entry) + return entry.title + end, - get_buffer_by_name = function(_, entry) - return "quickfixlist_" .. tostring(entry.nr) - end, + get_buffer_by_name = function(_, entry) + return "quickfixlist_" .. tostring(entry.nr) + end, - define_preview = function(self, entry) - if self.state.bufname then - return - end - local entries = vim.tbl_map(function(i) - return qf_entry_maker(i):display() - end, entry.items) - vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, entries) + define_preview = function(self, entry) + if self.state.bufname then + return + end + local entries = vim.tbl_map(function(i) + return qf_entry_maker(i):display() + end, entry.items) + vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, entries) + end, + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(_, _) + action_set.select:replace(function(prompt_bufnr) + local nr = action_state.get_selected_entry().nr + actions.close(prompt_bufnr) + internal.quickfix { nr = nr } + end) + return true end, - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(_, _) - action_set.select:replace(function(prompt_bufnr) - local nr = action_state.get_selected_entry().nr - actions.close(prompt_bufnr) - internal.quickfix { nr = nr } - end) - return true - end, - }):find() + }) + :find() end internal.loclist = function(opts) @@ -462,15 +476,17 @@ internal.loclist = function(opts) return end - pickers.new(opts, { - prompt_title = "Loclist", - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.generic_sorter(opts), - }):find() + pickers + .new(opts, { + prompt_title = "Loclist", + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + }) + :find() end internal.oldfiles = function(opts) @@ -508,15 +524,17 @@ internal.oldfiles = function(opts) end, results) end - pickers.new(opts, { - prompt_title = "Oldfiles", - finder = finders.new_table { - results = results, - entry_maker = opts.entry_maker or make_entry.gen_from_file(opts), - }, - sorter = conf.file_sorter(opts), - previewer = conf.file_previewer(opts), - }):find() + pickers + .new(opts, { + prompt_title = "Oldfiles", + finder = finders.new_table { + results = results, + entry_maker = opts.entry_maker or make_entry.gen_from_file(opts), + }, + sorter = conf.file_sorter(opts), + previewer = conf.file_previewer(opts), + }) + :find() end internal.command_history = function(opts) @@ -530,23 +548,25 @@ internal.command_history = function(opts) table.insert(results, string.sub(item, finish + 1)) end - pickers.new(opts, { - prompt_title = "Command History", - finder = finders.new_table(results), - sorter = conf.generic_sorter(opts), + pickers + .new(opts, { + prompt_title = "Command History", + finder = finders.new_table(results), + sorter = conf.generic_sorter(opts), - attach_mappings = function(_, map) - map("i", "", actions.set_command_line) - map("n", "", actions.set_command_line) - map("n", "", actions.edit_command_line) - map("i", "", actions.edit_command_line) + attach_mappings = function(_, map) + map("i", "", actions.set_command_line) + map("n", "", actions.set_command_line) + map("n", "", actions.edit_command_line) + map("i", "", actions.edit_command_line) - -- TODO: Find a way to insert the text... it seems hard. - -- map('i', '', actions.insert_value, { expr = true }) + -- TODO: Find a way to insert the text... it seems hard. + -- map('i', '', actions.insert_value, { expr = true }) - return true - end, - }):find() + return true + end, + }) + :find() end internal.search_history = function(opts) @@ -560,23 +580,25 @@ internal.search_history = function(opts) table.insert(results, string.sub(item, finish + 1)) end - pickers.new(opts, { - prompt_title = "Search History", - finder = finders.new_table(results), - sorter = conf.generic_sorter(opts), + pickers + .new(opts, { + prompt_title = "Search History", + finder = finders.new_table(results), + sorter = conf.generic_sorter(opts), - attach_mappings = function(_, map) - map("i", "", actions.set_search_line) - map("n", "", actions.set_search_line) - map("n", "", actions.edit_search_line) - map("i", "", actions.edit_search_line) + attach_mappings = function(_, map) + map("i", "", actions.set_search_line) + map("n", "", actions.set_search_line) + map("n", "", actions.edit_search_line) + map("i", "", actions.edit_search_line) - -- TODO: Find a way to insert the text... it seems hard. - -- map('i', '', actions.insert_value, { expr = true }) + -- TODO: Find a way to insert the text... it seems hard. + -- map('i', '', actions.insert_value, { expr = true }) - return true - end, - }):find() + return true + end, + }) + :find() end internal.vim_options = function(opts) @@ -588,36 +610,38 @@ internal.vim_options = function(opts) return left.name < right.name end) - pickers.new(opts, { - prompt_title = "options", - finder = finders.new_table { - results = res, - entry_maker = opts.entry_maker or make_entry.gen_from_vimoptions(opts), - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function() - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.vim_options" - return - end + pickers + .new(opts, { + prompt_title = "options", + finder = finders.new_table { + results = res, + entry_maker = opts.entry_maker or make_entry.gen_from_vimoptions(opts), + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function() + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.vim_options" + return + end - local esc = "" - if vim.fn.mode() == "i" then - esc = vim.api.nvim_replace_termcodes("", true, false, true) - end + local esc = "" + if vim.fn.mode() == "i" then + esc = vim.api.nvim_replace_termcodes("", true, false, true) + end - vim.api.nvim_feedkeys( - string.format("%s:set %s=%s", esc, selection.value.name, selection.value.value), - "m", - true - ) - end) + vim.api.nvim_feedkeys( + string.format("%s:set %s=%s", esc, selection.value.name, selection.value.value), + "m", + true + ) + end) - return true - end, - }):find() + return true + end, + }) + :find() end internal.help_tags = function(opts) @@ -685,43 +709,45 @@ internal.help_tags = function(opts) end end - pickers.new(opts, { - prompt_title = "Help", - finder = finders.new_table { - results = tags, - entry_maker = function(entry) - return make_entry.set_default_entry_mt({ - value = entry.name .. "@" .. entry.lang, - display = entry.name, - ordinal = entry.name, - filename = entry.filename, - cmd = entry.cmd, - }, opts) + pickers + .new(opts, { + prompt_title = "Help", + finder = finders.new_table { + results = tags, + entry_maker = function(entry) + return make_entry.set_default_entry_mt({ + value = entry.name .. "@" .. entry.lang, + display = entry.name, + ordinal = entry.name, + filename = entry.filename, + cmd = entry.cmd, + }, opts) + end, + }, + previewer = previewers.help.new(opts), + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + action_set.select:replace(function(_, cmd) + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.help_tags" + return + end + + actions.close(prompt_bufnr) + if cmd == "default" or cmd == "horizontal" then + vim.cmd("help " .. selection.value) + elseif cmd == "vertical" then + vim.cmd("vert help " .. selection.value) + elseif cmd == "tab" then + vim.cmd("tab help " .. selection.value) + end + end) + + return true end, - }, - previewer = previewers.help.new(opts), - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - action_set.select:replace(function(_, cmd) - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.help_tags" - return - end - - actions.close(prompt_bufnr) - if cmd == "default" or cmd == "horizontal" then - vim.cmd("help " .. selection.value) - elseif cmd == "vertical" then - vim.cmd("vert help " .. selection.value) - elseif cmd == "tab" then - vim.cmd("tab help " .. selection.value) - end - end) - - return true - end, - }):find() + }) + :find() end internal.man_pages = function(opts) @@ -734,33 +760,35 @@ internal.man_pages = function(opts) opts.entry_maker = opts.entry_maker or make_entry.gen_from_apropos(opts) opts.env = { PATH = vim.env.PATH, MANPATH = vim.env.MANPATH } - pickers.new(opts, { - prompt_title = "Man", - finder = finders.new_oneshot_job(opts.man_cmd, opts), - previewer = previewers.man.new(opts), - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - action_set.select:replace(function(_, cmd) - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.man_pages" - return - end + pickers + .new(opts, { + prompt_title = "Man", + finder = finders.new_oneshot_job(opts.man_cmd, opts), + previewer = previewers.man.new(opts), + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + action_set.select:replace(function(_, cmd) + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.man_pages" + return + end - local args = selection.section .. " " .. selection.value - actions.close(prompt_bufnr) - if cmd == "default" or cmd == "horizontal" then - vim.cmd("Man " .. args) - elseif cmd == "vertical" then - vim.cmd("vert Man " .. args) - elseif cmd == "tab" then - vim.cmd("tab Man " .. args) - end - end) + local args = selection.section .. " " .. selection.value + actions.close(prompt_bufnr) + if cmd == "default" or cmd == "horizontal" then + vim.cmd("Man " .. args) + elseif cmd == "vertical" then + vim.cmd("vert Man " .. args) + elseif cmd == "tab" then + vim.cmd("tab Man " .. args) + end + end) - return true - end, - }):find() + return true + end, + }) + :find() end internal.reloader = function(opts) @@ -781,34 +809,36 @@ internal.reloader = function(opts) end opts.column_len = vim.F.if_nil(opts.column_len, column_len) - pickers.new(opts, { - prompt_title = "Packages", - finder = finders.new_table { - results = package_list, - entry_maker = opts.entry_maker or make_entry.gen_from_packages(opts), - }, - -- previewer = previewers.vim_buffer.new(opts), - sorter = conf.generic_sorter(opts), + pickers + .new(opts, { + prompt_title = "Packages", + finder = finders.new_table { + results = package_list, + entry_maker = opts.entry_maker or make_entry.gen_from_packages(opts), + }, + -- previewer = previewers.vim_buffer.new(opts), + sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.reloader" - return - end + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.reloader" + return + end - actions.close(prompt_bufnr) - require("plenary.reload").reload_module(selection.value) - utils.notify("builtin.reloader", { - msg = string.format("[%s] - module reloaded", selection.value), - level = "INFO", - }) - end) + actions.close(prompt_bufnr) + require("plenary.reload").reload_module(selection.value) + utils.notify("builtin.reloader", { + msg = string.format("[%s] - module reloaded", selection.value), + level = "INFO", + }) + end) - return true - end, - }):find() + return true + end, + }) + :find() end internal.buffers = function(opts) @@ -866,16 +896,18 @@ internal.buffers = function(opts) opts.bufnr_width = #tostring(max_bufnr) end - pickers.new(opts, { - prompt_title = "Buffers", - finder = finders.new_table { - results = buffers, - entry_maker = opts.entry_maker or make_entry.gen_from_buffer(opts), - }, - previewer = conf.grep_previewer(opts), - sorter = conf.generic_sorter(opts), - default_selection_index = default_selection_idx, - }):find() + pickers + .new(opts, { + prompt_title = "Buffers", + finder = finders.new_table { + results = buffers, + entry_maker = opts.entry_maker or make_entry.gen_from_buffer(opts), + }, + previewer = conf.grep_previewer(opts), + sorter = conf.generic_sorter(opts), + default_selection_index = default_selection_idx, + }) + :find() end internal.colorscheme = function(opts) @@ -1020,17 +1052,19 @@ internal.marks = function(opts) end marks_table = vim.fn.extend(marks_table, marks_others) - pickers.new(opts, { - prompt_title = "Marks", - finder = finders.new_table { - results = marks_table, - entry_maker = opts.entry_maker or make_entry.gen_from_marks(opts), - }, - previewer = conf.grep_previewer(opts), - sorter = conf.generic_sorter(opts), - push_cursor_on_edit = true, - push_tagstack_on_edit = true, - }):find() + pickers + .new(opts, { + prompt_title = "Marks", + finder = finders.new_table { + results = marks_table, + entry_maker = opts.entry_maker or make_entry.gen_from_marks(opts), + }, + previewer = conf.grep_previewer(opts), + sorter = conf.generic_sorter(opts), + push_cursor_on_edit = true, + push_tagstack_on_edit = true, + }) + :find() end internal.registers = function(opts) @@ -1046,21 +1080,23 @@ internal.registers = function(opts) table.insert(registers_table, string.char(i)) end - pickers.new(opts, { - prompt_title = "Registers", - finder = finders.new_table { - results = registers_table, - entry_maker = opts.entry_maker or make_entry.gen_from_registers(opts), - }, - -- use levenshtein as n-gram doesn't support <2 char matches - sorter = sorters.get_levenshtein_sorter(), - attach_mappings = function(_, map) - actions.select_default:replace(actions.paste_register) - map("i", "", actions.edit_register) + pickers + .new(opts, { + prompt_title = "Registers", + finder = finders.new_table { + results = registers_table, + entry_maker = opts.entry_maker or make_entry.gen_from_registers(opts), + }, + -- use levenshtein as n-gram doesn't support <2 char matches + sorter = sorters.get_levenshtein_sorter(), + attach_mappings = function(_, map) + actions.select_default:replace(actions.paste_register) + map("i", "", actions.edit_register) - return true - end, - }):find() + return true + end, + }) + :find() end -- TODO: make filtering include the mapping and the action @@ -1094,79 +1130,85 @@ internal.keymaps = function(opts) end opts.width_lhs = max_len_lhs + 1 - pickers.new(opts, { - prompt_title = "Key Maps", - finder = finders.new_table { - results = keymaps_table, - entry_maker = opts.entry_maker or make_entry.gen_from_keymaps(opts), - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.keymaps" - return - end + pickers + .new(opts, { + prompt_title = "Key Maps", + finder = finders.new_table { + results = keymaps_table, + entry_maker = opts.entry_maker or make_entry.gen_from_keymaps(opts), + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.keymaps" + return + end - vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(selection.value.lhs, true, false, true), "t", true) - return actions.close(prompt_bufnr) - end) - return true - end, - }):find() + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(selection.value.lhs, true, false, true), "t", true) + return actions.close(prompt_bufnr) + end) + return true + end, + }) + :find() end internal.filetypes = function(opts) local filetypes = vim.fn.getcompletion("", "filetype") - pickers.new(opts, { - prompt_title = "Filetypes", - finder = finders.new_table { - results = filetypes, - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - if selection == nil then - print "[telescope] Nothing currently selected" - return - end + pickers + .new(opts, { + prompt_title = "Filetypes", + finder = finders.new_table { + results = filetypes, + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + if selection == nil then + print "[telescope] Nothing currently selected" + return + end - actions.close(prompt_bufnr) - vim.cmd("setfiletype " .. selection[1]) - end) - return true - end, - }):find() + actions.close(prompt_bufnr) + vim.cmd("setfiletype " .. selection[1]) + end) + return true + end, + }) + :find() end internal.highlights = function(opts) local highlights = vim.fn.getcompletion("", "highlight") - pickers.new(opts, { - prompt_title = "Highlights", - finder = finders.new_table { - results = highlights, - entry_maker = opts.entry_maker or make_entry.gen_from_highlights(opts), - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.highlights" - return - end + pickers + .new(opts, { + prompt_title = "Highlights", + finder = finders.new_table { + results = highlights, + entry_maker = opts.entry_maker or make_entry.gen_from_highlights(opts), + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.highlights" + return + end - actions.close(prompt_bufnr) - vim.cmd("hi " .. selection.value) - end) - return true - end, - previewer = previewers.highlights.new(opts), - }):find() + actions.close(prompt_bufnr) + vim.cmd("hi " .. selection.value) + end) + return true + end, + previewer = previewers.highlights.new(opts), + }) + :find() end internal.autocommands = function(opts) @@ -1174,71 +1216,76 @@ internal.autocommands = function(opts) table.sort(autocmds, function(lhs, rhs) return lhs.event < rhs.event end) - pickers.new(opts, { - prompt_title = "autocommands", - finder = finders.new_table { - results = autocmds, - entry_maker = opts.entry_maker or make_entry.gen_from_autocommands(opts), - }, - previewer = previewers.autocommands.new(opts), - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - action_set.select:replace_if(function() - local selection = action_state.get_selected_entry() - if selection == nil then - return false - end - local val = selection.value - local group_name = val.group_name ~= "" and val.group_name or "" - local output = vim.fn.execute("verb autocmd " .. group_name .. " " .. val.event .. " " .. val.pattern, "silent") - for line in output:gmatch "[^\r\n]+" do - local source_file = line:match "Last set from (.*) line %d*$" or line:match "Last set from (.*)$" - if source_file and source_file ~= "Lua" then - selection.filename = source_file - local source_lnum = line:match "line (%d*)$" or "1" - selection.lnum = tonumber(source_lnum) - selection.col = 1 + pickers + .new(opts, { + prompt_title = "autocommands", + finder = finders.new_table { + results = autocmds, + entry_maker = opts.entry_maker or make_entry.gen_from_autocommands(opts), + }, + previewer = previewers.autocommands.new(opts), + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + action_set.select:replace_if(function() + local selection = action_state.get_selected_entry() + if selection == nil then return false end - end - return true - end, function() - local selection = action_state.get_selected_entry() - actions.close(prompt_bufnr) - print("You selected autocmd: " .. vim.inspect(selection.value)) - end) + local val = selection.value + local group_name = val.group_name ~= "" and val.group_name or "" + local output = + vim.fn.execute("verb autocmd " .. group_name .. " " .. val.event .. " " .. val.pattern, "silent") + for line in output:gmatch "[^\r\n]+" do + local source_file = line:match "Last set from (.*) line %d*$" or line:match "Last set from (.*)$" + if source_file and source_file ~= "Lua" then + selection.filename = source_file + local source_lnum = line:match "line (%d*)$" or "1" + selection.lnum = tonumber(source_lnum) + selection.col = 1 + return false + end + end + return true + end, function() + local selection = action_state.get_selected_entry() + actions.close(prompt_bufnr) + print("You selected autocmd: " .. vim.inspect(selection.value)) + end) - return true - end, - }):find() + return true + end, + }) + :find() end internal.spell_suggest = function(opts) local cursor_word = vim.fn.expand "" local suggestions = vim.fn.spellsuggest(cursor_word) - pickers.new(opts, { - prompt_title = "Spelling Suggestions", - finder = finders.new_table { - results = suggestions, - }, - sorter = conf.generic_sorter(opts), - attach_mappings = function(prompt_bufnr) - actions.select_default:replace(function() - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection "builtin.spell_suggest" - return - end + pickers + .new(opts, { + prompt_title = "Spelling Suggestions", + finder = finders.new_table { + results = suggestions, + }, + sorter = conf.generic_sorter(opts), + attach_mappings = function(prompt_bufnr) + actions.select_default:replace(function() + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection "builtin.spell_suggest" + return + end - action_state.get_current_picker(prompt_bufnr)._original_mode = "i" - actions.close(prompt_bufnr) - vim.cmd("normal! ciw" .. selection[1]) - vim.cmd "stopinsert" - end) - return true - end, - }):find() + action_state.get_current_picker(prompt_bufnr)._original_mode = "i" + actions.close(prompt_bufnr) + vim.cmd("normal! ciw" .. selection[1]) + vim.cmd "stopinsert" + end) + return true + end, + }) + :find() end internal.tagstack = function(opts) @@ -1267,15 +1314,17 @@ internal.tagstack = function(opts) return end - pickers.new(opts, { - prompt_title = "TagStack", - finder = finders.new_table { - results = tags, - entry_maker = make_entry.gen_from_quickfix(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.generic_sorter(opts), - }):find() + pickers + .new(opts, { + prompt_title = "TagStack", + finder = finders.new_table { + results = tags, + entry_maker = make_entry.gen_from_quickfix(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + }) + :find() end internal.jumplist = function(opts) @@ -1292,15 +1341,17 @@ internal.jumplist = function(opts) end end - pickers.new(opts, { - prompt_title = "Jumplist", - finder = finders.new_table { - results = sorted_jumplist, - entry_maker = make_entry.gen_from_quickfix(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.generic_sorter(opts), - }):find() + pickers + .new(opts, { + prompt_title = "Jumplist", + finder = finders.new_table { + results = sorted_jumplist, + entry_maker = make_entry.gen_from_quickfix(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + }) + :find() end local function apply_checks(mod) diff --git a/lua/telescope/builtin/__lsp.lua b/lua/telescope/builtin/__lsp.lua index b07d872..c1a01db 100644 --- a/lua/telescope/builtin/__lsp.lua +++ b/lua/telescope/builtin/__lsp.lua @@ -39,17 +39,19 @@ lsp.references = function(opts) return end - pickers.new(opts, { - prompt_title = "LSP References", - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.generic_sorter(opts), - push_cursor_on_edit = true, - push_tagstack_on_edit = true, - }):find() + pickers + .new(opts, { + prompt_title = "LSP References", + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + push_cursor_on_edit = true, + push_tagstack_on_edit = true, + }) + :find() end) end @@ -77,17 +79,19 @@ local function call_hierarchy(opts, method, title, direction, item) end end - pickers.new(opts, { - prompt_title = title, - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.generic_sorter(opts), - push_cursor_on_edit = true, - push_tagstack_on_edit = true, - }):find() + pickers + .new(opts, { + prompt_title = title, + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + push_cursor_on_edit = true, + push_tagstack_on_edit = true, + }) + :find() end) end @@ -171,17 +175,19 @@ local function list_or_jump(action, title, opts) vim.lsp.util.jump_to_location(flattened_results[1], offset_encoding) else local locations = vim.lsp.util.locations_to_items(flattened_results, offset_encoding) - pickers.new(opts, { - prompt_title = title, - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.generic_sorter(opts), - push_cursor_on_edit = true, - push_tagstack_on_edit = true, - }):find() + pickers + .new(opts, { + prompt_title = title, + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_quickfix(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.generic_sorter(opts), + push_cursor_on_edit = true, + push_tagstack_on_edit = true, + }) + :find() end end) end @@ -230,20 +236,22 @@ lsp.document_symbols = function(opts) end opts.path_display = { "hidden" } - pickers.new(opts, { - prompt_title = "LSP Document Symbols", - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.prefilter_sorter { - tag = "symbol_type", - sorter = conf.generic_sorter(opts), - }, - push_cursor_on_edit = true, - push_tagstack_on_edit = true, - }):find() + pickers + .new(opts, { + prompt_title = "LSP Document Symbols", + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.prefilter_sorter { + tag = "symbol_type", + sorter = conf.generic_sorter(opts), + }, + push_cursor_on_edit = true, + push_tagstack_on_edit = true, + }) + :find() end) end @@ -273,18 +281,20 @@ lsp.workspace_symbols = function(opts) opts.ignore_filename = vim.F.if_nil(opts.ignore_filename, false) - pickers.new(opts, { - prompt_title = "LSP Workspace Symbols", - finder = finders.new_table { - results = locations, - entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = conf.prefilter_sorter { - tag = "symbol_type", - sorter = conf.generic_sorter(opts), - }, - }):find() + pickers + .new(opts, { + prompt_title = "LSP Workspace Symbols", + finder = finders.new_table { + results = locations, + entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = conf.prefilter_sorter { + tag = "symbol_type", + sorter = conf.generic_sorter(opts), + }, + }) + :find() end) end @@ -309,19 +319,21 @@ local function get_workspace_symbols_requester(bufnr, opts) end lsp.dynamic_workspace_symbols = function(opts) - pickers.new(opts, { - prompt_title = "LSP Dynamic Workspace Symbols", - finder = finders.new_dynamic { - entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), - fn = get_workspace_symbols_requester(opts.bufnr, opts), - }, - previewer = conf.qflist_previewer(opts), - sorter = sorters.highlighter_only(opts), - attach_mappings = function(_, map) - map("i", "", actions.to_fuzzy_refine) - return true - end, - }):find() + pickers + .new(opts, { + prompt_title = "LSP Dynamic Workspace Symbols", + finder = finders.new_dynamic { + entry_maker = opts.entry_maker or make_entry.gen_from_lsp_symbols(opts), + fn = get_workspace_symbols_requester(opts.bufnr, opts), + }, + previewer = conf.qflist_previewer(opts), + sorter = sorters.highlighter_only(opts), + attach_mappings = function(_, map) + map("i", "", actions.to_fuzzy_refine) + return true + end, + }) + :find() end local function check_capabilities(feature, bufnr) diff --git a/lua/telescope/mappings.lua b/lua/telescope/mappings.lua index b81552e..b7baa16 100644 --- a/lua/telescope/mappings.lua +++ b/lua/telescope/mappings.lua @@ -147,11 +147,8 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts) local map_string if opts.expr then - map_string = string.format( - [[luaeval("require('telescope.mappings').execute_keymap(%s, %s)")]], - prompt_bufnr, - key_id - ) + map_string = + string.format([[luaeval("require('telescope.mappings').execute_keymap(%s, %s)")]], prompt_bufnr, key_id) else if mode == "i" and not opts.expr then prefix = "" @@ -161,12 +158,8 @@ local telescope_map = function(prompt_bufnr, mode, key_bind, key_func, opts) prefix = ":" end - map_string = string.format( - "%slua require('telescope.mappings').execute_keymap(%s, %s)", - prefix, - prompt_bufnr, - key_id - ) + map_string = + string.format("%slua require('telescope.mappings').execute_keymap(%s, %s)", prefix, prompt_bufnr, key_id) end a.nvim_buf_set_keymap(prompt_bufnr, mode, key_bind, map_string, opts) diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 82ccda9..251761c 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -372,11 +372,8 @@ function Picker:find() popup_opts.preview.titlehighlight = "TelescopePreviewTitle" end - local results_win, results_opts, results_border_win = self:_create_window( - "", - popup_opts.results, - not self.wrap_results - ) + local results_win, results_opts, results_border_win = + self:_create_window("", popup_opts.results, not self.wrap_results) local results_bufnr = a.nvim_win_get_buf(results_win) pcall(a.nvim_buf_set_option, results_bufnr, "tabstop", 1) -- #1834 diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index 5e74b85..3a01d7f 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -693,11 +693,8 @@ layout_strategies.vertical = make_documented_layout( -- Cap over/undersized height (with previewer) height, h_space = calc_size_and_spacing(height, max_lines, bs, 3, 6, 2) - preview.height = resolve.resolve_height(vim.F.if_nil(layout_config.preview_height, 0.5))( - self, - max_columns, - height - ) + preview.height = + resolve.resolve_height(vim.F.if_nil(layout_config.preview_height, 0.5))(self, max_columns, height) else -- Cap over/undersized height (without previewer) height, h_space = calc_size_and_spacing(height, max_lines, bs, 2, 4, 1) diff --git a/lua/telescope/previewers/utils.lua b/lua/telescope/previewers/utils.lua index b1d1d5e..823562e 100644 --- a/lua/telescope/previewers/utils.lua +++ b/lua/telescope/previewers/utils.lua @@ -45,28 +45,26 @@ utils.job_maker = function(cmd, bufnr, opts) end end)() - Job - :new({ - command = command, - args = cmd, - env = opts.env, - cwd = opts.cwd, - writer = writer, - on_exit = vim.schedule_wrap(function(j) - if not vim.api.nvim_buf_is_valid(bufnr) then - return - end - if opts.mode == "append" then - vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, j:result()) - elseif opts.mode == "insert" then - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, j:result()) - end - if opts.callback then - opts.callback(bufnr, j:result()) - end - end), - }) - :start() + Job:new({ + command = command, + args = cmd, + env = opts.env, + cwd = opts.cwd, + writer = writer, + on_exit = vim.schedule_wrap(function(j) + if not vim.api.nvim_buf_is_valid(bufnr) then + return + end + if opts.mode == "append" then + vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, j:result()) + elseif opts.mode == "insert" then + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, j:result()) + end + if opts.callback then + opts.callback(bufnr, j:result()) + end + end), + }):start() else if opts.callback then opts.callback(bufnr) diff --git a/lua/telescope/sorters.lua b/lua/telescope/sorters.lua index 3fb263a..1ad9deb 100644 --- a/lua/telescope/sorters.lua +++ b/lua/telescope/sorters.lua @@ -291,17 +291,17 @@ sorters.get_fuzzy_file = function(opts) end local denominator = ( - (10 * match_count / #prompt_lower_ngrams) - -- biases for shorter strings - + 3 * match_count * ngram_len / #line - + consecutive_matches - + N / (contains_string or (2 * #line)) - -- + 30/(c1 or 2*N) - -- TODO: It might be possible that this too strongly correlates, - -- but it's unlikely for people to type capital letters without actually - -- wanting to do something with a capital letter in it. - + uppers_matching - ) * tail_modifier + (10 * match_count / #prompt_lower_ngrams) + -- biases for shorter strings + + 3 * match_count * ngram_len / #line + + consecutive_matches + + N / (contains_string or (2 * #line)) + -- + 30/(c1 or 2*N) + -- TODO: It might be possible that this too strongly correlates, + -- but it's unlikely for people to type capital letters without actually + -- wanting to do something with a capital letter in it. + + uppers_matching + ) * tail_modifier if denominator == 0 or denominator ~= denominator then return -1 diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 4db75a8..70ded91 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -417,16 +417,14 @@ function utils.get_os_command_output(cmd, cwd) end local command = table.remove(cmd, 1) local stderr = {} - local stdout, ret = Job - :new({ - command = command, - args = cmd, - cwd = cwd, - on_stderr = function(_, data) - table.insert(stderr, data) - end, - }) - :sync() + local stdout, ret = Job:new({ + command = command, + args = cmd, + cwd = cwd, + on_stderr = function(_, data) + table.insert(stderr, data) + end, + }):sync() return stdout, ret, stderr end