Skip to content

Commit

Permalink
fix(api): show error when an invalid mode was used. Fixes #465
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 1, 2024
1 parent d7f69ff commit 4b1914c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lua/trouble/api.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Actions = require("trouble.config.actions")
local Config = require("trouble.config")
local Util = require("trouble.util")
local View = require("trouble.view")

---@alias trouble.ApiFn fun(opts?: trouble.Config|string): trouble.View
Expand Down Expand Up @@ -41,13 +42,15 @@ end
-- When a view is already open and `opts.new = true`,
-- a new view will be created.
---@param opts? trouble.Mode | { new?: boolean } | string
---@return trouble.View
---@return trouble.View?
function M.open(opts)
opts = opts or {}
local view, _opts = M._find_last(opts)
if not view or _opts.new then
if not _opts.mode then
error("No mode specified")
return Util.error("No mode specified")
elseif not vim.tbl_contains(Config.modes(), _opts.mode) then
return Util.error("Invalid mode `" .. _opts.mode .. "`")
end
view = View.new(_opts)
end
Expand Down Expand Up @@ -75,7 +78,7 @@ end

-- Toggle the view with the given mode.
---@param opts? trouble.Mode|string
---@return trouble.View
---@return trouble.View?
function M.toggle(opts)
if M.is_open(opts) then
---@diagnostic disable-next-line: return-type-mismatch
Expand Down Expand Up @@ -107,7 +110,9 @@ function M._action(action)
---@cast action trouble.Action
return function(opts)
local view = M.open(opts)
view:action(action, opts)
if view then
view:action(action, opts)
end
return view
end
end
Expand Down

0 comments on commit 4b1914c

Please sign in to comment.