diff options
| author | Simon Hauser <Simon-Hauser@outlook.de> | 2021-02-27 16:26:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-27 16:26:25 +0100 |
| commit | ca92ec1a830a61a0b88fff925f2146ab46e0f7c9 (patch) | |
| tree | 4d30266573d6a009280ba9c31348b9ea76f3daec /lua/telescope | |
| parent | 84732d1d780f1aecb799502be97ec3a403066268 (diff) | |
fix: a lot of small things and adds more customization for caret (#554)
Attention:
prompt_prefix will no longer add a space at the end. So if you still want a space at the end make sure your configuration has one.
The default should not be changed. So if you haven't copied prompt_prefix in your config this doesn't affect you.
Feat:
- prompt prefix does no longer always end with space
- selection_caret configurable. Default: `> `
- result_prefix configurable. Default: ` `
- more actions for git_branches
- <c-t> does track the branch
- <c-r> does rebase branch
- also added delete branch action but not configured. See readme on how to do that
Fixes:
- fix docgen ci
- Better error for lsp_workspace_symbols
- better formatting for CONTRIBUTING.md
- move from systemlist to plenary.job
- git branch now supports checkout on remote branches
Diffstat (limited to 'lua/telescope')
| -rw-r--r-- | lua/telescope/actions/init.lua | 67 | ||||
| -rw-r--r-- | lua/telescope/builtin/git.lua | 24 | ||||
| -rw-r--r-- | lua/telescope/builtin/lsp.lua | 8 | ||||
| -rw-r--r-- | lua/telescope/config.lua | 16 | ||||
| -rw-r--r-- | lua/telescope/init.lua | 11 | ||||
| -rw-r--r-- | lua/telescope/pickers.lua | 27 | ||||
| -rw-r--r-- | lua/telescope/previewers/buffer_previewer.lua | 31 | ||||
| -rw-r--r-- | lua/telescope/utils.lua | 6 |
8 files changed, 126 insertions, 64 deletions
diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index bf6bd4f..2fa7ce2 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -264,7 +264,72 @@ actions.git_checkout = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() actions.close(prompt_bufnr) - utils.get_os_command_output({ 'git', 'checkout', selection.value }, cwd) + local _, ret, stderr = utils.get_os_command_output({ 'git', 'checkout', selection.value }, cwd) + if ret == 0 then + print("Checked out: " .. selection.value) + else + print(string.format( + 'Error when checking out: %s. Git returned: "%s"', + selection.value, + table.concat(stderr, ' ') + )) + end +end + +actions.git_track_branch = function(prompt_bufnr) + local cwd = action_state.get_current_picker(prompt_bufnr).cwd + local selection = action_state.get_selected_entry() + actions.close(prompt_bufnr) + local _, ret, stderr = utils.get_os_command_output({ 'git', 'checkout', '--track', selection.value }, cwd) + if ret == 0 then + print("Tracking branch: " .. selection.value) + else + print(string.format( + 'Error when tracking branch: %s. Git returned: "%s"', + selection.value, + table.concat(stderr, ' ') + )) + end +end + +actions.git_delete_branch = function(prompt_bufnr) + local cwd = action_state.get_current_picker(prompt_bufnr).cwd + local selection = action_state.get_selected_entry() + + local confirmation = vim.fn.input('Do you really wanna delete branch ' .. selection.value .. '? [Y/n] ') + if confirmation ~= '' and string.lower(confirmation) ~= 'y' then return end + + actions.close(prompt_bufnr) + local _, ret, stderr = utils.get_os_command_output({ 'git', 'branch', '-D', selection.value }, cwd) + if ret == 0 then + print("Deleted branch: " .. selection.value) + else + print(string.format( + 'Error when deleting branch: %s. Git returned: "%s"', + selection.value, + table.concat(stderr, ' ') + )) + end +end + +actions.git_rebase_branch = function(prompt_bufnr) + local cwd = action_state.get_current_picker(prompt_bufnr).cwd + local selection = action_state.get_selected_entry() + + local confirmation = vim.fn.input('Do you really wanna delete branch ' .. selection.value .. '? [Y/n] ') + if confirmation ~= '' and string.lower(confirmation) ~= 'y' then return end + + actions.close(prompt_bufnr) + local _, ret, stderr = utils.get_os_command_output({ 'git', 'rebase', selection.value }, cwd) + if ret == 0 then + print("Rebased branch: " .. selection.value) + else + print(string.format( + 'Error when rebasing branch: %s. Git returned: "%s"', + selection.value, + table.concat(stderr, ' ') + )) + end end actions.git_staging_toggle = function(prompt_bufnr) diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua index a296517..c333159 100644 --- a/lua/telescope/builtin/git.lua +++ b/lua/telescope/builtin/git.lua @@ -79,20 +79,15 @@ end git.branches = function(opts) local output = utils.get_os_command_output({ 'git', 'branch', '--all' }, opts.cwd) - local tmp_results = {} + local results = {} for _, v in ipairs(output) do if not string.match(v, 'HEAD') and v ~= '' then v = string.gsub(v, '.* ', '') - v = string.gsub(v, '^remotes/[^/]*/', '') - tmp_results[v] = true + v = string.gsub(v, '^remotes/', '') + table.insert(results, v) end end - local results = {} - for k, _ in pairs(tmp_results) do - table.insert(results, k) - end - pickers.new(opts, { prompt_title = 'Git Branches', finder = finders.new_table { @@ -103,8 +98,13 @@ git.branches = function(opts) }, previewer = previewers.git_branch_log.new(opts), sorter = conf.file_sorter(opts), - attach_mappings = function() + attach_mappings = function(_, map) actions.select_default:replace(actions.git_checkout) + map('i', '<c-t>', actions.git_track_branch) + map('n', '<c-t>', actions.git_track_branch) + + map('i', '<c-r>', actions.git_rebase_branch) + map('n', '<c-r>', actions.git_rebase_branch) return true end }):find() @@ -155,14 +155,14 @@ local set_opts_cwd = function(opts) end -- Find root of git directory and remove trailing newline characters - local git_root = vim.fn.systemlist("git -C " .. opts.cwd .. " rev-parse --show-toplevel")[1] + local git_root, ret = utils.get_os_command_output({ "git", "rev-parse", "--show-toplevel" }, opts.cwd) local use_git_root = utils.get_default(opts.use_git_root, true) - if vim.v.shell_error ~= 0 then + if ret ~= 0 then error(opts.cwd .. ' is not a git directory') else if use_git_root then - opts.cwd = git_root + opts.cwd = git_root[1] end end end diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 93fcfd2..28ecf60 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -151,8 +151,11 @@ lsp.workspace_symbols = function(opts) local params = {query = opts.query or ''} local results_lsp = vim.lsp.buf_request_sync(0, "workspace/symbol", params, opts.timeout or 10000) - if not results_lsp or vim.tbl_isempty(results_lsp) then - print("No results from workspace/symbol") + -- Clangd returns { { result = {} } } for query='' + if not results_lsp or vim.tbl_isempty(results_lsp) or + vim.tbl_isempty(results_lsp[1]) or vim.tbl_isempty(results_lsp[1].result) then + print("No results from workspace/symbol. Maybe try a different query: " .. + "Telescope lsp_workspace_symbols query=example") return end @@ -167,7 +170,6 @@ lsp.workspace_symbols = function(opts) return end - opts.ignore_filename = utils.get_default(opts.ignore_filename, false) opts.hide_filename = utils.get_default(opts.hide_filename, false) diff --git a/lua/telescope/config.lua b/lua/telescope/config.lua index 9b749ed..de6d544 100644 --- a/lua/telescope/config.lua +++ b/lua/telescope/config.lua @@ -75,7 +75,21 @@ function config.set_defaults(defaults) set("results_height", 1) set("results_width", 0.8) - set("prompt_prefix", ">") + set("prompt_prefix", "> ", [[ + Will be shown in front of the prompt. + + Default: '> ' + ]]) + set("selection_caret", "> ", [[ + Will be shown in front of the selection. + + Default: '> ' + ]]) + set("entry_prefix", " ", [[ + Prefix in front of each result entry. Current selection not included. + + Default: ' ' + ]]) set("initial_mode", "insert") set("border", {}) diff --git a/lua/telescope/init.lua b/lua/telescope/init.lua index 2269efe..cca1a4c 100644 --- a/lua/telescope/init.lua +++ b/lua/telescope/init.lua @@ -14,12 +14,11 @@ local telescope = {} --- --- To find out more: --- https://github.com/nvim-telescope/telescope.nvim --- --- :h telescope.setup --- :h telescope.builtin --- :h telescope.layout --- :h telescope.actions --- +--- +--- :h telescope.setup +--- :h telescope.builtin +--- :h telescope.layout +--- :h telescope.actions ---@brief ]] ---@tag telescope.nvim diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index 7dc89ae..c04ce17 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -57,6 +57,8 @@ function Picker:new(opts) preview_title = get_default(opts.preview_title, "Preview"), prompt_prefix = get_default(opts.prompt_prefix, config.values.prompt_prefix), + selection_caret = get_default(opts.selection_caret, config.values.selection_caret), + entry_prefix = get_default(opts.entry_prefix, config.values.entry_prefix), initial_mode = get_default(opts.initial_mode, config.values.initial_mode), default_text = opts.default_text, @@ -112,7 +114,6 @@ function Picker:new(opts) preview_cutoff = get_default(opts.preview_cutoff, config.values.preview_cutoff), }, self) - obj.scroller = p_scroller.create( get_default(opts.scroll_strategy, config.values.scroll_strategy), obj.sorting_strategy @@ -375,10 +376,6 @@ function Picker:find() local prompt_prefix = self.prompt_prefix if prompt_prefix ~= '' then a.nvim_buf_set_option(prompt_bufnr, 'buftype', 'prompt') - - if not vim.endswith(prompt_prefix, " ") then - prompt_prefix = prompt_prefix .. " " - end vim.fn.prompt_setprompt(prompt_bufnr, prompt_prefix) end self.prompt_prefix = prompt_prefix @@ -655,9 +652,6 @@ function Picker:change_prompt_prefix(new_prefix, hl_group) if not new_prefix then return end if new_prefix ~= '' then - if not vim.endswith(new_prefix, " ") then - new_prefix = new_prefix .. " " - end vim.fn.prompt_setprompt(self.prompt_bufnr, new_prefix) else vim.api.nvim_buf_set_text(self.prompt_bufnr, 0, 0, 0, #self.prompt_prefix, {}) @@ -733,21 +727,21 @@ function Picker:set_selection(row) local display, display_highlights = entry_display.resolve(self, self._selection_entry) if display then - display = ' ' .. display + display = self.entry_prefix .. display a.nvim_buf_set_lines(results_bufnr, self._selection_row, self._selection_row + 1, false, {display}) - self.highlighter:hi_display(self._selection_row, ' ', display_highlights) + self.highlighter:hi_display(self._selection_row, self.entry_prefix, display_highlights) self.highlighter:hi_sorter(self._selection_row, prompt, display) self.highlighter:hi_multiselect(self._selection_row, self._selection_entry) end end - local caret = '>' + local caret = self.selection_caret -- local display = string.format('%s %s', caret, -- (a.nvim_buf_get_lines(results_bufnr, row, row + 1, false)[1] or ''):sub(3) -- ) local display, display_highlights = entry_display.resolve(self, entry) - display = caret .. ' ' .. display + display = caret .. display -- TODO: You should go back and redraw the highlights for this line from the sorter. -- That's the only smart thing to do. @@ -757,8 +751,9 @@ function Picker:set_selection(row) end a.nvim_buf_set_lines(results_bufnr, row, row + 1, false, {display}) - self.highlighter:hi_selection(row, caret) - self.highlighter:hi_display(row, ' ', display_highlights) + -- don't highlight the ' ' at the end of caret + self.highlighter:hi_selection(row, caret:sub(1, -2)) + self.highlighter:hi_display(row, caret, display_highlights) self.highlighter:hi_sorter(row, prompt, display) self.highlighter:hi_multiselect(row, entry) end) @@ -809,7 +804,7 @@ function Picker:entry_adder(index, entry, _, insert) -- This is the two spaces to manage the '> ' stuff. -- Maybe someday we can use extmarks or floaty text or something to draw this and not insert here. -- until then, insert two spaces - local prefix = ' ' + local prefix = self.entry_prefix display = prefix .. display self:_increment("displayed") @@ -1109,7 +1104,7 @@ end function Picker:_get_prompt() return vim.trim( - vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1]:sub(#self.prompt_prefix) + vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1]:sub(#self.prompt_prefix + 1) ) end diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua index a6e2322..6fa25de 100644 --- a/lua/telescope/previewers/buffer_previewer.lua +++ b/lua/telescope/previewers/buffer_previewer.lua @@ -466,40 +466,23 @@ previewers.git_branch_log = defaulter(function(opts) end end - local remotes = utils.get_os_command_output({ 'git', 'remote' }, opts.cwd) return previewers.new_buffer_previewer { get_buffer_by_name = function(_, entry) return entry.value end, define_preview = function(self, entry, status) - local current_remote = 1 + local cmd = { 'git', '--no-pager', 'log', '--graph', '--pretty=format:%h -%d %s (%cr)', + '--abbrev-commit', '--date=relative', entry.value } - local gen_cmd = function(v) - return { 'git', '--no-pager', 'log', '--graph', '--pretty=format:%h -%d %s (%cr)', - '--abbrev-commit', '--date=relative', v } - end - - local handle_results - handle_results = function(bufnr, content) - if content and table.getn(content) == 0 then - if current_remote <= table.getn(remotes) then - local value = 'remotes/' .. remotes[current_remote] .. '/' .. entry.value - current_remote = current_remote + 1 - putils.job_maker(gen_cmd(value), bufnr, { cwd = opts.cwd, callback = handle_results }) - else - vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { "No log found for branch: " .. entry.value }) - end - elseif content and table.getn(content) > 1 then - highlight_buffer(bufnr, content) - end - end - - putils.job_maker(gen_cmd(entry.value), self.state.bufnr, { + putils.job_maker(cmd, self.state.bufnr, { value = entry.value, bufname = self.state.bufname, cwd = opts.cwd, - callback = handle_results + callback = function(bufnr, content) + if not content then return end + highlight_buffer(bufnr, content) + end }) end } diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index ecd7fd4..4470c56 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -199,7 +199,11 @@ function utils.get_os_command_output(cmd, cwd) return {} end local command = table.remove(cmd, 1) - return Job:new({ command = command, args = cmd, cwd = cwd }):sync() + local stderr = {} + 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 utils.strdisplaywidth = (function() |
