fix: show error message when nothing is selected (#1289)

* fix: always show error message when nothing is selected

This continues the fixes done in 4816a27d76 (ref #1283)

* Use correct variable name (selection) and add one more nil check

* Fix indentation
This commit is contained in:
Jonas Strittmatter
2021-09-27 21:45:27 +02:00
committed by GitHub
parent 9402484794
commit 4e629cdea1
2 changed files with 132 additions and 32 deletions

View File

@@ -300,48 +300,62 @@ function actions.close(prompt_bufnr)
end end
actions.edit_command_line = function(prompt_bufnr) actions.edit_command_line = function(prompt_bufnr)
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
a.nvim_feedkeys(a.nvim_replace_termcodes(":" .. entry.value, true, false, true), "t", true) a.nvim_feedkeys(a.nvim_replace_termcodes(":" .. selection.value, true, false, true), "t", true)
end end
actions.set_command_line = function(prompt_bufnr) actions.set_command_line = function(prompt_bufnr)
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
vim.fn.histadd("cmd", entry.value) vim.fn.histadd("cmd", selection.value)
vim.cmd(entry.value) vim.cmd(selection.value)
end end
actions.edit_search_line = function(prompt_bufnr) actions.edit_search_line = function(prompt_bufnr)
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
a.nvim_feedkeys(a.nvim_replace_termcodes("/" .. entry.value, true, false, true), "t", true) a.nvim_feedkeys(a.nvim_replace_termcodes("/" .. selection.value, true, false, true), "t", true)
end end
actions.set_search_line = function(prompt_bufnr) actions.set_search_line = function(prompt_bufnr)
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
a.nvim_feedkeys(a.nvim_replace_termcodes("/" .. entry.value .. "<CR>", true, false, true), "t", true) a.nvim_feedkeys(a.nvim_replace_termcodes("/" .. selection.value .. "<CR>", true, false, true), "t", true)
end end
actions.edit_register = function(prompt_bufnr) actions.edit_register = function(prompt_bufnr)
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
local picker = action_state.get_current_picker(prompt_bufnr) local picker = action_state.get_current_picker(prompt_bufnr)
vim.fn.inputsave() vim.fn.inputsave()
local updated_value = vim.fn.input("Edit [" .. entry.value .. "] ", entry.content) local updated_value = vim.fn.input("Edit [" .. selection.value .. "] ", selection.content)
vim.fn.inputrestore() vim.fn.inputrestore()
if updated_value ~= entry.content then if updated_value ~= selection.content then
vim.fn.setreg(entry.value, updated_value) vim.fn.setreg(selection.value, updated_value)
entry.content = updated_value selection.content = updated_value
end end
-- update entry in results table -- update entry in results table
-- TODO: find way to redraw finder content -- TODO: find way to redraw finder content
for _, v in pairs(picker.finder.results) do for _, v in pairs(picker.finder.results) do
if v == entry then if v == selection then
v.content = updated_value v.content = updated_value
end end
end end
@@ -349,30 +363,38 @@ actions.edit_register = function(prompt_bufnr)
end end
actions.paste_register = function(prompt_bufnr) actions.paste_register = function(prompt_bufnr)
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
-- ensure that the buffer can be written to -- ensure that the buffer can be written to
if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then
print "Paste!" print "Paste!"
vim.api.nvim_paste(entry.content, true, -1) vim.api.nvim_paste(selection.content, true, -1)
end end
end end
actions.run_builtin = function(prompt_bufnr) actions.run_builtin = function(prompt_bufnr)
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions._close(prompt_bufnr, true) actions._close(prompt_bufnr, true)
if string.match(entry.text, " : ") then if string.match(selection.text, " : ") then
-- Call appropriate function from extensions -- Call appropriate function from extensions
local split_string = vim.split(entry.text, " : ") local split_string = vim.split(selection.text, " : ")
local ext = split_string[1] local ext = split_string[1]
local func = split_string[2] local func = split_string[2]
require("telescope").extensions[ext][func]() require("telescope").extensions[ext][func]()
else else
-- Call appropriate telescope builtin -- Call appropriate telescope builtin
require("telescope.builtin")[entry.text]() require("telescope.builtin")[selection.text]()
end end
end end
@@ -394,13 +416,17 @@ end
-- TODO: Think about how to do this. -- TODO: Think about how to do this.
actions.insert_value = function(prompt_bufnr) actions.insert_value = function(prompt_bufnr)
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
vim.schedule(function() vim.schedule(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
end) end)
return entry.value return selection.value
end end
--- Create and checkout a new git branch if it doesn't already exist --- Create and checkout a new git branch if it doesn't already exist
@@ -435,6 +461,10 @@ end
---@param prompt_bufnr number: The prompt bufnr ---@param prompt_bufnr number: The prompt bufnr
actions.git_apply_stash = function(prompt_bufnr) actions.git_apply_stash = function(prompt_bufnr)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local _, ret, stderr = utils.get_os_command_output { "git", "stash", "apply", "--index", selection.value } local _, ret, stderr = utils.get_os_command_output { "git", "stash", "apply", "--index", selection.value }
if ret == 0 then if ret == 0 then
@@ -449,6 +479,10 @@ end
actions.git_checkout = function(prompt_bufnr) actions.git_checkout = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local _, ret, stderr = 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 if ret == 0 then
@@ -465,6 +499,10 @@ end
actions.git_switch_branch = function(prompt_bufnr) actions.git_switch_branch = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local pattern = "^refs/remotes/%w+/" local pattern = "^refs/remotes/%w+/"
local branch = selection.value local branch = selection.value
@@ -483,6 +521,10 @@ local function make_git_branch_action(opts)
return function(prompt_bufnr) return function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
local should_confirm = opts.should_confirm local should_confirm = opts.should_confirm
if should_confirm then if should_confirm then
@@ -552,6 +594,10 @@ actions.git_rebase_branch = make_git_branch_action {
local git_reset_branch = function(prompt_bufnr, mode) local git_reset_branch = function(prompt_bufnr, mode)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
local confirmation = vim.fn.input("Do you really wanna " .. mode .. " reset to " .. selection.value .. "? [Y/n] ") local confirmation = vim.fn.input("Do you really wanna " .. mode .. " reset to " .. selection.value .. "? [Y/n] ")
if confirmation ~= "" and string.lower(confirmation) ~= "y" then if confirmation ~= "" and string.lower(confirmation) ~= "y" then
@@ -588,6 +634,10 @@ end
actions.git_checkout_current_buffer = function(prompt_bufnr) actions.git_checkout_current_buffer = function(prompt_bufnr)
local cwd = actions.get_current_picker(prompt_bufnr).cwd local cwd = actions.get_current_picker(prompt_bufnr).cwd
local selection = actions.get_selected_entry() local selection = actions.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
utils.get_os_command_output({ "git", "checkout", selection.value, "--", selection.file }, cwd) utils.get_os_command_output({ "git", "checkout", selection.value, "--", selection.file }, cwd)
end end
@@ -597,7 +647,10 @@ end
actions.git_staging_toggle = function(prompt_bufnr) actions.git_staging_toggle = function(prompt_bufnr)
local cwd = action_state.get_current_picker(prompt_bufnr).cwd local cwd = action_state.get_current_picker(prompt_bufnr).cwd
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
if selection.status:sub(2) == " " then if selection.status:sub(2) == " " then
utils.get_os_command_output({ "git", "restore", "--staged", selection.value }, cwd) utils.get_os_command_output({ "git", "restore", "--staged", selection.value }, cwd)
else else

View File

@@ -178,8 +178,12 @@ internal.planets = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr) if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr)
print("Enjoy astronomy! You viewed:", selection.display) print("Enjoy astronomy! You viewed:", selection.display)
end) end)
@@ -278,6 +282,11 @@ internal.commands = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local val = selection.value local val = selection.value
local cmd = string.format([[:%s ]], val.name) local cmd = string.format([[:%s ]], val.name)
@@ -456,8 +465,12 @@ internal.vim_options = function(opts)
attach_mappings = function() attach_mappings = function()
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
local esc = "" if selection == nil then
print "[telescope] Nothing currently selected"
return
end
local esc = ""
if vim.fn.mode() == "i" then if vim.fn.mode() == "i" then
-- TODO: don't make this local -- TODO: don't make this local
esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true) esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true)
@@ -587,6 +600,7 @@ internal.help_tags = function(opts)
print "[telescope] Nothing currently selected" print "[telescope] Nothing currently selected"
return return
end end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
if cmd == "default" or cmd == "horizontal" then if cmd == "default" or cmd == "horizontal" then
vim.cmd("help " .. selection.value) vim.cmd("help " .. selection.value)
@@ -623,8 +637,8 @@ internal.man_pages = function(opts)
print "[telescope] Nothing currently selected" print "[telescope] Nothing currently selected"
return return
end end
local args = selection.section .. " " .. selection.value
local args = selection.section .. " " .. selection.value
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
if cmd == "default" or cmd == "horizontal" then if cmd == "default" or cmd == "horizontal" then
vim.cmd("Man " .. args) vim.cmd("Man " .. args)
@@ -670,6 +684,10 @@ internal.reloader = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
require("plenary.reload").reload_module(selection.value) require("plenary.reload").reload_module(selection.value)
@@ -747,8 +765,8 @@ internal.buffers = function(opts)
attach_mappings = function(_, _) attach_mappings = function(_, _)
action_set.select:enhance { action_set.select:enhance {
post = function() post = function()
local entry = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
vim.api.nvim_win_set_cursor(0, { entry.lnum, entry.col or 0 }) vim.api.nvim_win_set_cursor(0, { selection.lnum, selection.col or 0 })
end, end,
} }
return true return true
@@ -827,8 +845,12 @@ internal.colorscheme = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr) if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr)
need_restore = false need_restore = false
vim.cmd("colorscheme " .. selection.value) vim.cmd("colorscheme " .. selection.value)
end) end)
@@ -932,6 +954,11 @@ internal.keymaps = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(selection.value.lhs, true, false, true), "t", true) vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(selection.value.lhs, true, false, true), "t", true)
return actions.close(prompt_bufnr) return actions.close(prompt_bufnr)
end) end)
@@ -952,6 +979,11 @@ internal.filetypes = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
vim.cmd("setfiletype " .. selection[1]) vim.cmd("setfiletype " .. selection[1])
end) end)
@@ -973,6 +1005,11 @@ internal.highlights = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
vim.cmd("hi " .. selection.value) vim.cmd("hi " .. selection.value)
end) end)
@@ -1064,6 +1101,11 @@ internal.autocommands = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
action_set.select:replace(function(_, type) action_set.select:replace(function(_, type)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
vim.cmd(action_state.select_key_to_edit_key(type) .. " " .. selection.value) vim.cmd(action_state.select_key_to_edit_key(type) .. " " .. selection.value)
end) end)
@@ -1090,6 +1132,11 @@ internal.spell_suggest = function(opts)
attach_mappings = function(prompt_bufnr) attach_mappings = function(prompt_bufnr)
actions.select_default:replace(function() actions.select_default:replace(function()
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
if selection == nil then
print "[telescope] Nothing currently selected"
return
end
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
vim.cmd("normal! ciw" .. selection[1]) vim.cmd("normal! ciw" .. selection[1])
vim.cmd "stopinsert" vim.cmd "stopinsert"