From ef7b6ada6d91a1b2932492d78c730e4fc00cd2ea Mon Sep 17 00:00:00 2001 From: tami5 Date: Sun, 13 Mar 2022 20:11:27 +0300 Subject: [PATCH] feat: improve UX with vim.notify (#1763) * fix(notify): don't report request on new line * ref(notify): update message format * ref(msgs): always quote values + decrease duplication * fix(ci): undefined variables * ref(actions): temporary silent actions.__index errors * cleanup * revert: panic effort, we continue to use error for those Co-authored-by: Simon Hauser --- lua/telescope/_extensions/init.lua | 2 +- lua/telescope/actions/history.lua | 17 ++- lua/telescope/actions/init.lua | 139 +++++++++++++----- lua/telescope/actions/set.lua | 11 +- lua/telescope/builtin/diagnostics.lua | 11 +- lua/telescope/builtin/files.lua | 23 ++- lua/telescope/builtin/git.lua | 26 +++- lua/telescope/builtin/internal.lua | 58 +++++--- lua/telescope/builtin/lsp.lua | 49 ++++-- lua/telescope/command.lua | 16 +- lua/telescope/config/resolve.lua | 12 +- lua/telescope/from_entry.lua | 2 +- lua/telescope/pickers/entry_display.lua | 2 +- lua/telescope/pickers/layout_strategies.lua | 4 +- lua/telescope/pickers/window.lua | 2 +- lua/telescope/previewers/buffer_previewer.lua | 2 +- lua/telescope/utils.lua | 51 ++++++- 17 files changed, 311 insertions(+), 116 deletions(-) diff --git a/lua/telescope/_extensions/init.lua b/lua/telescope/_extensions/init.lua index 3b9efe3..c31205a 100644 --- a/lua/telescope/_extensions/init.lua +++ b/lua/telescope/_extensions/init.lua @@ -7,7 +7,7 @@ extensions._health = {} local load_extension = function(name) local ok, ext = pcall(require, "telescope._extensions." .. name) if not ok then - error("This extension doesn't exist or is not installed: " .. name .. "\n" .. ext) + error(string.format("'%s' extension doesn't exist or isn't installed: %s", name, ext)) end return ext end diff --git a/lua/telescope/actions/history.lua b/lua/telescope/actions/history.lua index c4eb8c8..8323ea7 100644 --- a/lua/telescope/actions/history.lua +++ b/lua/telescope/actions/history.lua @@ -1,5 +1,6 @@ local conf = require("telescope.config").values local Path = require "plenary.path" +local utils = require "telescope.utils" local uv = vim.loop @@ -111,10 +112,10 @@ end ---@return string: the next history item function histories.History:get_next(line, picker) if not self.enabled then - print( - "You are cycling to next the history item but history is disabled.", - "Read ':help telescope.defaults.history'" - ) + utils.notify("History:get_next", { + msg = "You are cycling to next the history item but history is disabled. Read ':help telescope.defaults.history'", + level = "WARN", + }) return false end if self._pre_get then @@ -136,10 +137,10 @@ end ---@return string: the previous history item function histories.History:get_prev(line, picker) if not self.enabled then - print( - "You are cycling to previous the history item but history is disabled.", - "Read ':help telescope.defaults.history'" - ) + utils.notify("History:get_prev", { + msg = "You are cycling to next the history item but history is disabled. Read ':help telescope.defaults.history'", + level = "WARN", + }) return false end if self._pre_get then diff --git a/lua/telescope/actions/init.lua b/lua/telescope/actions/init.lua index b0a316e..c6b3514 100644 --- a/lua/telescope/actions/init.lua +++ b/lua/telescope/actions/init.lua @@ -275,7 +275,7 @@ end actions.edit_command_line = function(prompt_bufnr) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.edit_command_line" return end actions.close(prompt_bufnr) @@ -285,7 +285,7 @@ end actions.set_command_line = function(prompt_bufnr) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.set_command_line" return end actions.close(prompt_bufnr) @@ -296,7 +296,7 @@ end actions.edit_search_line = function(prompt_bufnr) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.edit_search_line" return end actions.close(prompt_bufnr) @@ -306,7 +306,7 @@ end actions.set_search_line = function(prompt_bufnr) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.set_search_line" return end actions.close(prompt_bufnr) @@ -338,7 +338,7 @@ end actions.paste_register = function(prompt_bufnr) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.paste_register" return end @@ -346,7 +346,6 @@ actions.paste_register = function(prompt_bufnr) -- ensure that the buffer can be written to if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then - print "Paste!" vim.api.nvim_paste(selection.content, true, -1) end end @@ -370,7 +369,7 @@ end actions.insert_value = function(prompt_bufnr) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.insert_value" return end @@ -388,11 +387,17 @@ actions.git_create_branch = function(prompt_bufnr) local new_branch = action_state.get_current_line() if new_branch == "" then - print "Please enter the name of the new branch to create" + utils.notify("actions.git_create_branch", { + msg = "Missing the new branch name", + level = "ERROR", + }) else - local confirmation = vim.fn.input(string.format('Create new branch "%s"? [y/n]: ', new_branch)) + local confirmation = vim.fn.input(string.format("Create new branch '%s'? [y/n]: ", new_branch)) if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= "y" then - print(string.format('Didn\'t create branch "%s"', new_branch)) + utils.notify("actions.git_create_branch", { + msg = string.format("fail to create branch: '%s'", new_branch), + level = "ERROR", + }) return end @@ -400,11 +405,19 @@ actions.git_create_branch = function(prompt_bufnr) local _, ret, stderr = utils.get_os_command_output({ "git", "checkout", "-b", new_branch }, cwd) if ret == 0 then - print(string.format("Switched to a new branch: %s", new_branch)) + utils.notify("actions.git_create_branch", { + msg = string.format("Switched to a new branch: %s", new_branch), + level = "INFO", + }) else - print( - string.format('Error when creating new branch: %s Git returned "%s"', new_branch, table.concat(stderr, " ")) - ) + utils.notify("actions.git_create_branch", { + msg = string.format( + "Error when creating new branch: '%s' Git returned '%s'", + new_branch, + table.concat(stderr, " ") + ), + level = "INFO", + }) end end end @@ -414,15 +427,21 @@ end actions.git_apply_stash = function(prompt_bufnr) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.git_apply_stash" return end actions.close(prompt_bufnr) local _, ret, stderr = utils.get_os_command_output { "git", "stash", "apply", "--index", selection.value } if ret == 0 then - print("applied: " .. selection.value) + utils.notify("actions.git_apply_stash", { + msg = string.format("applied: '%s' ", selection.value), + level = "INFO", + }) else - print(string.format('Error when applying: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))) + utils.notify("actions.git_apply_stash", { + msg = string.format("Error when applying: %s. Git returned: '%s'", selection.value, table.concat(stderr, " ")), + level = "ERROR", + }) end end @@ -432,15 +451,25 @@ actions.git_checkout = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.git_checkout" return end actions.close(prompt_bufnr) local _, ret, stderr = utils.get_os_command_output({ "git", "checkout", selection.value }, cwd) if ret == 0 then - print("Checked out: " .. selection.value) + utils.notify("actions.git_checkout", { + msg = string.format("Checked out: ", selection.value), + level = "INFO", + }) else - print(string.format('Error when checking out: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))) + utils.notify("actions.git_checkout", { + msg = string.format( + "Error when checking out: %s. Git returned: '%s'", + selection.value, + table.concat(stderr, " ") + ), + level = "ERORR", + }) end end @@ -452,7 +481,7 @@ actions.git_switch_branch = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.git_switch_branch" return end actions.close(prompt_bufnr) @@ -463,9 +492,19 @@ actions.git_switch_branch = function(prompt_bufnr) end local _, ret, stderr = utils.get_os_command_output({ "git", "switch", branch }, cwd) if ret == 0 then - print("Switched to: " .. branch) + utils.notify("actions.git_switch_branch", { + msg = string.format("Switched to: '%s'", branch), + level = "INFO", + }) else - print(string.format('Error when switching to: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))) + utils.notify("actions.git_switch_branch", { + msg = string.format( + "Error when switching to: %s. Git returned: '%s'", + selection.value, + table.concat(stderr, " ") + ), + level = "ERORR", + }) end end @@ -474,7 +513,7 @@ local function make_git_branch_action(opts) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection(opts.action_name) return end @@ -489,9 +528,15 @@ local function make_git_branch_action(opts) actions.close(prompt_bufnr) local _, ret, stderr = utils.get_os_command_output(opts.command(selection.value), cwd) if ret == 0 then - print(string.format(opts.success_message, selection.value)) + utils.notify(opts.action_name, { + msg = string.format(opts.success_message, selection.value), + level = "INFO", + }) else - print(string.format(opts.error_message, selection.value, table.concat(stderr, " "))) + utils.notify(opts.action_name, { + msg = string.format(opts.error_message, selection.value, table.concat(stderr, " ")), + level = "ERROR", + }) end end end @@ -500,8 +545,9 @@ end ---@param prompt_bufnr number: The prompt bufnr actions.git_track_branch = make_git_branch_action { should_confirm = false, + action_name = "actions.git_track_branch", success_message = "Tracking branch: %s", - error_message = 'Error when tracking branch: %s. Git returned: "%s"', + error_message = "Error when tracking branch: %s. Git returned: '%s'", command = function(branch_name) return { "git", "checkout", "--track", branch_name } end, @@ -511,9 +557,10 @@ actions.git_track_branch = make_git_branch_action { ---@param prompt_bufnr number: The prompt bufnr actions.git_delete_branch = make_git_branch_action { should_confirm = true, + action_name = "actions.git_delete_branch", confirmation_question = "Do you really wanna delete branch %s? [Y/n] ", success_message = "Deleted branch: %s", - error_message = 'Error when deleting branch: %s. Git returned: "%s"', + error_message = "Error when deleting branch: %s. Git returned: '%s'", command = function(branch_name) return { "git", "branch", "-D", branch_name } end, @@ -523,9 +570,10 @@ actions.git_delete_branch = make_git_branch_action { ---@param prompt_bufnr number: The prompt bufnr actions.git_merge_branch = make_git_branch_action { should_confirm = true, + action_name = "actions.git_merge_branch", confirmation_question = "Do you really wanna merge branch %s? [Y/n] ", success_message = "Merged branch: %s", - error_message = 'Error when merging branch: %s. Git returned: "%s"', + error_message = "Error when merging branch: %s. Git returned: '%s'", command = function(branch_name) return { "git", "merge", branch_name } end, @@ -535,9 +583,10 @@ actions.git_merge_branch = make_git_branch_action { ---@param prompt_bufnr number: The prompt bufnr actions.git_rebase_branch = make_git_branch_action { should_confirm = true, + action_name = "actions.git_rebase_branch", confirmation_question = "Do you really wanna rebase branch %s? [Y/n] ", success_message = "Rebased branch: %s", - error_message = 'Error when rebasing branch: %s. Git returned: "%s"', + error_message = "Error when rebasing branch: %s. Git returned: '%s'", command = function(branch_name) return { "git", "rebase", branch_name } end, @@ -547,7 +596,7 @@ local git_reset_branch = function(prompt_bufnr, mode) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.git_reset_branch" return end @@ -559,9 +608,15 @@ local git_reset_branch = function(prompt_bufnr, mode) actions.close(prompt_bufnr) local _, ret, stderr = utils.get_os_command_output({ "git", "reset", mode, selection.value }, cwd) if ret == 0 then - print("Reset to: " .. selection.value) + utils.notify("actions.git_rebase_branch", { + msg = string.format("Reset to: '%s'", selection.value), + level = "INFO", + }) else - print(string.format('Error when resetting to: %s. Git returned: "%s"', selection.value, table.concat(stderr, " "))) + utils.notify("actions.git_rebase_branch", { + msg = string.format("Rest to: %s. Git returned: '%s'", selection.value, table.concat(stderr, " ")), + level = "ERROR", + }) end end @@ -587,7 +642,8 @@ actions.git_checkout_current_buffer = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.git_checkout_current_buffer" + return end actions.close(prompt_bufnr) @@ -600,7 +656,7 @@ actions.git_staging_toggle = function(prompt_bufnr) local cwd = action_state.get_current_picker(prompt_bufnr).cwd local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "actions.git_staging_toggle" return end if selection.status:sub(2) == " " then @@ -707,7 +763,7 @@ end local smart_send = function(prompt_bufnr, mode, target) local picker = action_state.get_current_picker(prompt_bufnr) - if table.getn(picker:get_multi_selection()) > 0 then + if #picker:get_multi_selection() > 0 then send_selected_to_qf(prompt_bufnr, mode, target) else send_all_to_qf(prompt_bufnr, mode, target) @@ -744,7 +800,11 @@ actions.complete_tag = function(prompt_bufnr) local delimiter = current_picker.sorter._delimiter if not tags then - print "No tag pre-filtering set for this picker" + utils.notify("actions.complete_tag", { + msg = "No tag pre-filtering set for this picker", + level = "ERROR", + }) + return end @@ -771,7 +831,10 @@ actions.complete_tag = function(prompt_bufnr) end if vim.tbl_isempty(filtered_tags) then - print "No matches found" + utils.notify("complete_tag", { + msg = "No matches found", + level = "INFO", + }) return end diff --git a/lua/telescope/actions/set.lua b/lua/telescope/actions/set.lua index 81f9bc5..4e35d10 100644 --- a/lua/telescope/actions/set.lua +++ b/lua/telescope/actions/set.lua @@ -15,6 +15,7 @@ local a = vim.api local log = require "telescope.log" local Path = require "plenary.path" local state = require "telescope.state" +local utils = require "telescope.utils" local action_state = require "telescope.actions.state" @@ -88,7 +89,10 @@ action_set.edit = function(prompt_bufnr, command) local entry = action_state.get_selected_entry() if not entry then - print "[telescope] Nothing currently selected" + utils.notify("actions.set.edit", { + msg = "Nothing currently selected", + level = "WARN", + }) return end @@ -105,7 +109,10 @@ action_set.edit = function(prompt_bufnr, command) -- to put stuff into `filename` local value = entry.value if not value then - print "Could not do anything with blank line..." + utils.notify("actions.set.edit", { + msg = "Could not do anything with blank line...", + level = "WARN", + }) return end diff --git a/lua/telescope/builtin/diagnostics.lua b/lua/telescope/builtin/diagnostics.lua index cd9c07f..2614e36 100644 --- a/lua/telescope/builtin/diagnostics.lua +++ b/lua/telescope/builtin/diagnostics.lua @@ -2,6 +2,7 @@ local conf = require("telescope.config").values local finders = require "telescope.finders" local make_entry = require "telescope.make_entry" local pickers = require "telescope.pickers" +local utils = require "telescope.utils" local diagnostics = {} @@ -28,7 +29,10 @@ local diagnostics_to_tbl = function(opts) local diagnosis_opts = { severity = {}, namespace = opts.namespace } if opts.severity ~= nil then if opts.severity_limit ~= nil or opts.severity_bound ~= nil then - print "Invalid severity parameters. Both a specific severity and a limit/bound is not allowed" + utils.notify("builtin.diagnostics", { + msg = "Invalid severity parameters. Both a specific severity and a limit/bound is not allowed", + level = "ERROR", + }) return {} end diagnosis_opts.severity = opts.severity @@ -110,7 +114,10 @@ diagnostics.get = function(opts) local locations = diagnostics_to_tbl(opts) if vim.tbl_isempty(locations) then - print "No diagnostics found" + utils.notify("builtin.diagnostics", { + msg = "No diagnostics found", + level = "INFO", + }) return end diff --git a/lua/telescope/builtin/files.lua b/lua/telescope/builtin/files.lua index 90e52f5..893588f 100644 --- a/lua/telescope/builtin/files.lua +++ b/lua/telescope/builtin/files.lua @@ -163,10 +163,10 @@ files.find_files = function(opts) end)() if not find_command then - print( - "You need to install either find, fd, or rg. " - .. "You can also submit a PR to add support for another file finder :)" - ) + utils.notify("builtin.find_files", { + msg = "You need to install either find, fd, or rg", + level = "ERROR", + }) return end @@ -267,13 +267,19 @@ files.treesitter = function(opts) local has_nvim_treesitter, _ = pcall(require, "nvim-treesitter") if not has_nvim_treesitter then - print "You need to install nvim-treesitter" + utils.notify("builtin.treesitter", { + msg = "User need to install nvim-treesitter needs to be installed", + level = "ERROR", + }) return end local parsers = require "nvim-treesitter.parsers" if not parsers.has_parser(parsers.get_buf_lang(opts.bufnr)) then - print "No parser for the current buffer" + utils.notify("builtin.treesitter", { + msg = "No parser for the current buffer", + level = "ERROR", + }) return end @@ -397,7 +403,10 @@ end files.tags = function(opts) local tagfiles = opts.ctags_file and { opts.ctags_file } or vim.fn.tagfiles() if vim.tbl_isempty(tagfiles) then - print "No tags file found. Create one with ctags -R" + utils.notify("builtin.tags", { + msg = "No tags file found. Create one with ctags -R", + level = "ERROR", + }) return end diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua index af5d4c6..2683107 100644 --- a/lua/telescope/builtin/git.lua +++ b/lua/telescope/builtin/git.lua @@ -15,13 +15,21 @@ local git = {} git.files = function(opts) if opts.is_bare then - error "This operation must be run in a work tree" + utils.notify("builtin.git_files", { + msg = "This operation must be run in a work tree", + level = "ERROR", + }) + return end local show_untracked = utils.get_default(opts.show_untracked, true) local recurse_submodules = utils.get_default(opts.recurse_submodules, false) if show_untracked and recurse_submodules then - error "Git does not support both --others and --recurse-submodules" + utils.notify("builtin.git_files", { + msg = "Git does not support both --others and --recurse-submodules", + level = "ERROR", + }) + return end -- By creating the entry maker after the cwd options, @@ -300,7 +308,11 @@ end git.status = function(opts) if opts.is_bare then - error "This operation must be run in a work tree" + utils.notify("builtin.git_status", { + msg = "This operation must be run in a work tree", + level = "ERROR", + }) + return end local gen_new_finder = function() @@ -308,13 +320,17 @@ git.status = function(opts) local git_cmd = { "git", "status", "-s", "--", "." } if expand_dir then - table.insert(git_cmd, table.getn(git_cmd) - 1, "-u") + table.insert(git_cmd, #git_cmd - 1, "-u") end local output = utils.get_os_command_output(git_cmd, opts.cwd) - if table.getn(output) == 0 then + if #output == 0 then print "No changes found" + utils.notify("builtin.git_status", { + msg = "No changes found", + level = "WARN", + }) return end diff --git a/lua/telescope/builtin/internal.lua b/lua/telescope/builtin/internal.lua index 9b4ce85..7c3dcd7 100644 --- a/lua/telescope/builtin/internal.lua +++ b/lua/telescope/builtin/internal.lua @@ -84,7 +84,7 @@ internal.builtin = function(opts) actions.select_default:replace(function(_) local selection = action_state.get_selected_entry() if not selection then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.builtin" return end @@ -113,12 +113,18 @@ internal.resume = function(opts) local cached_pickers = state.get_global_key "cached_pickers" if cached_pickers == nil or vim.tbl_isempty(cached_pickers) then - print "No picker(s) cached." + utils.notify("builtin.resume", { + msg = "No cached picker(s).", + level = "INFO", + }) return end local picker = cached_pickers[opts.cache_index] if picker == nil then - print(string.format("Index too large as there are only %s pickers cached", #cached_pickers)) + utils.notify("builtin.resume", { + msg = string.format("Index too large as there are only '%s' pickers cached", #cached_pickers), + level = "ERROR", + }) return end -- reset layout strategy and get_window_options if default as only one is valid @@ -144,7 +150,10 @@ end internal.pickers = function(opts) local cached_pickers = state.get_global_key "cached_pickers" if cached_pickers == nil or vim.tbl_isempty(cached_pickers) then - print "No picker(s) cached." + utils.notify("builtin.pickers", { + msg = "No cached picker(s).", + level = "INFO", + }) return end @@ -213,7 +222,7 @@ internal.planets = function(opts) actions.select_default:replace(function() local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.planets" return end @@ -243,10 +252,11 @@ internal.symbols = function(opts) end if #files == 0 then - print( - "No sources found! Check out https://github.com/nvim-telescope/telescope-symbols.nvim " - .. "for some prebuild symbols or how to create you own symbol source." - ) + utils.notify("builtin.symbols", { + msg = "No sources found! Check out https://github.com/nvim-telescope/telescope-symbols.nvim " + .. "for some prebuild symbols or how to create you own symbol source.", + level = "ERROR", + }) return end @@ -317,7 +327,7 @@ internal.commands = function(opts) actions.select_default:replace(function() local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.commands" return end @@ -505,7 +515,7 @@ internal.vim_options = function(opts) actions.select_default:replace(function() local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.vim_options" return end @@ -636,7 +646,7 @@ internal.help_tags = function(opts) action_set.select:replace(function(_, cmd) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.help_tags" return end @@ -674,7 +684,7 @@ internal.man_pages = function(opts) action_set.select:replace(function(_, cmd) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.man_pages" return end @@ -725,13 +735,16 @@ internal.reloader = function(opts) actions.select_default:replace(function() local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.reloader" return end actions.close(prompt_bufnr) require("plenary.reload").reload_module(selection.value) - print(string.format("[%s] - module reloaded", selection.value)) + utils.notify("builtin.reloader", { + msg = string.format("[%s] - module reloaded", selection.value), + level = "INFO", + }) end) return true @@ -878,7 +891,7 @@ internal.colorscheme = function(opts) actions.select_default:replace(function() local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.colorscheme" return end @@ -995,7 +1008,7 @@ internal.keymaps = function(opts) actions.select_default:replace(function() local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.keymaps" return end @@ -1046,7 +1059,7 @@ internal.highlights = function(opts) actions.select_default:replace(function() local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.highlights" return end @@ -1143,7 +1156,7 @@ internal.autocommands = function(opts) action_set.select:replace(function(_, type) local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.autocommands" return end @@ -1170,7 +1183,7 @@ internal.spell_suggest = function(opts) actions.select_default:replace(function() local selection = action_state.get_selected_entry() if selection == nil then - print "[telescope] Nothing currently selected" + utils.__warn_no_selection "builtin.spell_suggest" return end @@ -1202,7 +1215,10 @@ internal.tagstack = function(opts) end if vim.tbl_isempty(tags) then - print "No tagstack available" + utils.notify("builtin.tagstack", { + msg = "No tagstack available", + level = "WARN", + }) return end diff --git a/lua/telescope/builtin/lsp.lua b/lua/telescope/builtin/lsp.lua index 464caef..e22c670 100644 --- a/lua/telescope/builtin/lsp.lua +++ b/lua/telescope/builtin/lsp.lua @@ -117,7 +117,10 @@ lsp.document_symbols = function(opts) end if not result or vim.tbl_isempty(result) then - print "No results from textDocument/documentSymbol" + utils.notify("builtin.lsp_document_symbols", { + msg = "No results from textDocument/documentSymbol", + level = "INFO", + }) return end @@ -129,7 +132,10 @@ lsp.document_symbols = function(opts) end if vim.tbl_isempty(locations) then - print "locations table empty" + utils.notify("builtin.lsp_document_symbols", { + msg = "No document_symbol locations found", + level = "INFO", + }) return end @@ -165,12 +171,18 @@ lsp.code_actions = function(opts) ) if err then - print("ERROR: " .. err) + utils.notify("builin.lsp_code_actions", { + msg = err, + level = "ERROR", + }) return end if not results_lsp or vim.tbl_isempty(results_lsp) then - print "No results from textDocument/codeAction" + utils.notify("builtin.lsp_document_symbols", { + msg = "No results from textDocument/codeAction", + level = "INFO", + }) return end @@ -206,7 +218,10 @@ lsp.code_actions = function(opts) end if #results == 0 then - print "No code actions available" + utils.notify("builtin.lsp_document_symbols", { + msg = "No code actions available", + level = "INFO", + }) return end @@ -317,7 +332,10 @@ lsp.code_actions = function(opts) then client.request("codeAction/resolve", action, function(resolved_err, resolved_action) if resolved_err then - vim.notify(resolved_err.code .. ": " .. resolved_err.message, vim.log.levels.ERROR) + utils.notify("builtin.lsp_code_actions", { + msg = string.format("codeAction/resolve failed: %s : %s", resolved_err.code, resolved_err.message), + level = "ERROR", + }) return end if resolved_action then @@ -358,10 +376,11 @@ lsp.workspace_symbols = function(opts) end if vim.tbl_isempty(locations) then - print( - "No results from workspace/symbol. Maybe try a different query: " - .. "Telescope lsp_workspace_symbols query=example" - ) + utils.notify("builtin.lsp_workspace_symbols", { + msg = "No results from workspace/symbol. Maybe try a different query: " + .. "'Telescope lsp_workspace_symbols query=example'", + level = "INFO", + }) return end @@ -429,9 +448,15 @@ local function check_capabilities(feature, bufnr) return true else if #clients == 0 then - print "LSP: no client attached" + utils.notify("builtin.lsp_*", { + msg = "no client attached", + level = "INFO", + }) else - print("LSP: server does not support " .. feature) + utils.notify("builtin.lsp_*", { + msg = "server does not support " .. feature, + level = "INFO", + }) end return false end diff --git a/lua/telescope/command.lua b/lua/telescope/command.lua index 89ed426..4bf30d4 100644 --- a/lua/telescope/command.lua +++ b/lua/telescope/command.lua @@ -45,6 +45,7 @@ local themes = require "telescope.themes" local builtin = require "telescope.builtin" local extensions = require("telescope._extensions").manager local config = require "telescope.config" +local utils = require "telescope.utils" local command = {} local arg_value = { @@ -119,7 +120,10 @@ command.convert_user_opts = function(user_opts) local _switch_metatable = { __index = function(_, k) - print(string.format("Type of %s does not match", k)) + utils.notify("command", { + msg = string.format("Type of '%s' does not match", k), + level = "WARN", + }) end, } @@ -153,7 +157,10 @@ end local function run_command(args) local user_opts = args or {} if next(user_opts) == nil and not user_opts.cmd then - print "[Telescope] your command miss args" + utils.notify("command", { + msg = "Command missing arguments", + level = "ERROR", + }) return end @@ -186,7 +193,10 @@ local function run_command(args) return end - print "[Telescope] unknown command" + utils.notify("run_command", { + msg = "Unknown command", + level = "ERROR", + }) end -- @Summary get extensions sub command diff --git a/lua/telescope/config/resolve.lua b/lua/telescope/config/resolve.lua index 3b47025..3c23406 100644 --- a/lua/telescope/config/resolve.lua +++ b/lua/telescope/config/resolve.lua @@ -95,6 +95,10 @@ local get_default = require("telescope.utils").get_default local resolver = {} local _resolve_map = {} +local throw_invalid_config_option = function(key, value) + error(string.format("Invalid configuration option for '%s': '%s'", key, tostring(value)), 2) +end + -- Booleans _resolve_map[function(val) return val == false @@ -148,8 +152,7 @@ end] = function(selector, val) return v(selector, value) end end - - error("invalid configuration option for padding:" .. tostring(value)) + throw_invalid_config_option("padding", value) end return function(...) @@ -182,8 +185,7 @@ resolver.resolve_height = function(val) return v(3, val) end end - - error("invalid configuration option for height:" .. tostring(val)) + throw_invalid_config_option("height", val) end --- Converts input to a function that returns the width. @@ -210,7 +212,7 @@ resolver.resolve_width = function(val) end end - error("invalid configuration option for width:" .. tostring(val)) + throw_invalid_config_option("width", val) end --- Calculates the adjustment required to move the picker from the middle of the screen to diff --git a/lua/telescope/from_entry.lua b/lua/telescope/from_entry.lua index 219d114..572a244 100644 --- a/lua/telescope/from_entry.lua +++ b/lua/telescope/from_entry.lua @@ -26,7 +26,7 @@ function from_entry.path(entry, validate, escape) path = entry.value end if path == nil then - print("Invalid entry", vim.inspect(entry)) + require("telescope.log").error(string.format("Invalid Entry: '%s'", vim.inspect(entry))) return end diff --git a/lua/telescope/pickers/entry_display.lua b/lua/telescope/pickers/entry_display.lua index 904641c..937d75e 100644 --- a/lua/telescope/pickers/entry_display.lua +++ b/lua/telescope/pickers/entry_display.lua @@ -39,7 +39,7 @@ entry_display.create = function(configuration) return function(self, picker) local results = {} local highlights = {} - for i = 1, table.getn(generator) do + for i = 1, #generator do if self[i] ~= nil then local str, hl = generator[i](self[i], picker) if hl then diff --git a/lua/telescope/pickers/layout_strategies.lua b/lua/telescope/pickers/layout_strategies.lua index 1483cf9..20e1fc0 100644 --- a/lua/telescope/pickers/layout_strategies.lua +++ b/lua/telescope/pickers/layout_strategies.lua @@ -220,7 +220,7 @@ layout_strategies._format = function(name) table.insert(results, string.format(" - %s", line)) end else - error("Unknown type:" .. type(val)) + error(string.format("expected string or table but found '%s'", type(val))) end end @@ -902,7 +902,7 @@ layout_strategies.bottom_pane = make_documented_layout( results.border = { 1, 1, 0, 1 } end else - error("Unknown prompt_position: " .. tostring(self.window.prompt_position) .. "\n" .. vim.inspect(layout_config)) + error(string.format("Unknown prompt_position: %s\n%s", self.window.prompt_position, vim.inspect(layout_config))) end -- Col diff --git a/lua/telescope/pickers/window.lua b/lua/telescope/pickers/window.lua index 95b89cf..945a7e3 100644 --- a/lua/telescope/pickers/window.lua +++ b/lua/telescope/pickers/window.lua @@ -7,7 +7,7 @@ function p_window.get_window_options(picker, max_columns, max_lines) local getter = require("telescope.pickers.layout_strategies")[layout_strategy] if not getter then - error("Not a valid layout strategy: " .. layout_strategy) + error(string.format("'%s' is not a valid layout strategy", layout_strategy)) end return getter(picker, max_columns, max_lines) diff --git a/lua/telescope/previewers/buffer_previewer.lua b/lua/telescope/previewers/buffer_previewer.lua index 459bdcc..70e08d0 100644 --- a/lua/telescope/previewers/buffer_previewer.lua +++ b/lua/telescope/previewers/buffer_previewer.lua @@ -948,7 +948,7 @@ previewers.highlights = defaulter(function(_) if v ~= "" then if v:sub(1, 1) == " " then local part_of_old = v:match "%s+(.*)" - hl_groups[table.getn(hl_groups)] = hl_groups[table.getn(hl_groups)] .. part_of_old + hl_groups[#hl_groups] = hl_groups[#hl_groups] .. part_of_old else table.insert(hl_groups, v) end diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index c564c06..b8935ca 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -50,7 +50,10 @@ utils.filter_symbols = function(results, opts) local filtered_symbols if has_symbols and has_ignore then - error "Either opts.symbols or opts.ignore_symbols, can't process opposing options at the same time!" + utils.notify("filter_symbols", { + msg = "Either opts.symbols or opts.ignore_symbols, can't process opposing options at the same time!", + level = "ERROR", + }) return elseif not (has_ignore or has_symbols) then return results @@ -59,7 +62,10 @@ utils.filter_symbols = function(results, opts) opts.ignore_symbols = { opts.ignore_symbols } end if type(opts.ignore_symbols) ~= "table" then - print "Please pass ignore_symbols as either a string or a list of strings" + utils.notify("filter_symbols", { + msg = "Please pass ignore_symbols as either a string or a list of strings", + level = "ERROR", + }) return end @@ -72,7 +78,10 @@ utils.filter_symbols = function(results, opts) opts.symbols = { opts.symbols } end if type(opts.symbols) ~= "table" then - print "Please pass filtering symbols as either a string or a list of strings" + utils.notify("filter_symbols", { + msg = "Please pass filtering symbols as either a string or a list of strings", + level = "ERROR", + }) return end @@ -113,10 +122,16 @@ utils.filter_symbols = function(results, opts) -- print message that filtered_symbols is now empty if has_symbols then local symbols = table.concat(opts.symbols, ", ") - print(string.format("%s symbol(s) were not part of the query results", symbols)) + utils.notify("filter_symbols", { + msg = string.format("%s symbol(s) were not part of the query results", symbols), + level = "WARN", + }) elseif has_ignore then local symbols = table.concat(opts.ignore_symbols, ", ") - print(string.format("%s ignore_symbol(s) have removed everything from the query result", symbols)) + utils.notify("filter_symbols", { + msg = string.format("%s ignore_symbol(s) have removed everything from the query result", symbols), + level = "WARN", + }) end end @@ -370,7 +385,10 @@ end function utils.get_os_command_output(cmd, cwd) if type(cmd) ~= "table" then - print "Telescope: [get_os_command_output]: cmd has to be a table" + utils.notify("get_os_command_output", { + msg = "cmd has to be a table", + level = "ERROR", + }) return {} end local command = table.remove(cmd, 1) @@ -457,4 +475,25 @@ utils.get_devicons = load_once(function() end end) +--- Telescope Wrapper around vim.notify +---@param funname string: name of the function that will be +---@param opts table: opts.level string, opts.msg string +utils.notify = function(funname, opts) + local level = vim.log.levels[opts.level] + if not level then + error("Invalid error level", 2) + end + + vim.notify(string.format("[telescope.%s]: %s", funname, opts.msg), level, { + title = "telescope.nvim", + }) +end + +utils.__warn_no_selection = function(name) + utils.notify(name, { + msg = "Nothing currently selected", + level = "WARN", + }) +end + return utils