diff --git a/lua/neolog/actions.lua b/lua/neolog/actions.lua index 8b8dba7..608c021 100644 --- a/lua/neolog/actions.lua +++ b/lua/neolog/actions.lua @@ -350,7 +350,8 @@ local function build_batch_log_statement(log_template, batch) local current_line = vim.fn.getpos(".")[2] local result1, insert_cursor_offset = resolve_template_placeholders(result, { identifier = function() - error("%identifier placeholder can only be used inside %repeat placeholder") + vim.notify("neolog: Cannot use %identifier placeholder outside %repeat placeholder", vim.log.levels.ERROR) + return "%identifier" end, line_number = tostring(current_line + 1), }) @@ -369,13 +370,13 @@ end local function get_lang_log_template(template_set, kind) local lang = get_lang(vim.bo.filetype) if not lang then - vim.notify("Cannot determine language for current buffer", vim.log.levels.ERROR) + vim.notify("neolog: Treesitter cannot determine language for current buffer", vim.log.levels.ERROR) return end local log_template_set = (kind == "single" and M.log_templates or M.batch_log_templates)[template_set] if not log_template_set then - vim.notify(string.format("Log template '%s' is not found", template_set), vim.log.levels.ERROR) + vim.notify(string.format("neolog: Log template '%s' is not found", template_set), vim.log.levels.ERROR) return end @@ -383,7 +384,7 @@ local function get_lang_log_template(template_set, kind) if not log_template_lang then vim.notify( string.format( - "%s '%s' does not have '%s' language template", + "neolog: %s '%s' does not have '%s' language template", kind == "single" and "Log template" or "Batch log template", template_set, lang @@ -454,7 +455,7 @@ function M.__insert_batch_log(_) opts = vim.tbl_deep_extend("force", { template = "default" }, opts or {}) if #M.batch == 0 then - vim.notify("Log batch is empty", vim.log.levels.INFO) + vim.notify("neolog: Log batch is empty", vim.log.levels.INFO) return end @@ -485,7 +486,7 @@ end function M.__add_log_targets_to_batch() local lang = get_lang(vim.bo.filetype) if not lang then - vim.notify("Cannot determine language for current buffer", vim.log.levels.ERROR) + vim.notify("neolog: Treesitter cannot determine language for current buffer", vim.log.levels.ERROR) return end diff --git a/tests/neolog/actions/neolog_actions_spec.lua b/tests/neolog/actions/neolog_actions_spec.lua index cc09e4b..73be99b 100644 --- a/tests/neolog/actions/neolog_actions_spec.lua +++ b/tests/neolog/actions/neolog_actions_spec.lua @@ -327,6 +327,58 @@ describe("neolog.actions.insert_log", function() ]], }) end) + + describe("handles user errors", function() + it("notifies when the filetype is not recognized", function() + helper.assert_scenario({ + input = [[ + // Comment + const fo|o = bar + baz + ]], + filetype = "unknown", + expected = function() + assert.error_matches(function() + actions.insert_log({ position = "below" }) + end, "neolog: Treesitter cannot determine language for current buffer") + end, + }) + end) + + it("notifies when the log template is not found", function() + helper.assert_scenario({ + input = [[ + // Comment + const fo|o = bar + baz + ]], + filetype = "javascript", + expected = function() + assert.error_matches(function() + actions.insert_log({ template = "unknown", position = "below" }) + end, "neolog: Log template 'unknown' is not found") + end, + }) + end) + + it("notifies when the filetype is not recognized", function() + neolog.setup({ + log_templates = { + testing = {}, + }, + }) + helper.assert_scenario({ + input = [[ + // Comment + const fo|o = bar + baz + ]], + filetype = "javascript", + expected = function() + assert.error_matches(function() + actions.insert_log({ template = "testing", position = "below" }) + end, "neolog: Log template 'testing' does not have 'javascript' language template") + end, + }) + end) + end) end) describe("neolog.actions.insert_batch_log", function() @@ -427,7 +479,7 @@ describe("neolog.actions.insert_batch_log", function() end, expected = function() assert.spy(notify_spy).was_called(1) - assert.spy(notify_spy).was_called_with("Log batch is empty", vim.log.levels.INFO) + assert.spy(notify_spy).was_called_with("neolog: Log batch is empty", vim.log.levels.INFO) end, }) @@ -449,18 +501,22 @@ describe("neolog.actions.insert_batch_log", function() const baz = "baz" ]] + local notify_spy = spy.on(vim, "notify") + helper.assert_scenario({ input = input, filetype = "javascript", action = function() vim.cmd("normal! V2j") actions.add_log_targets_to_batch() + actions.__insert_batch_log() end, expected = function() - assert.has_error(function() - -- Use the internal function to capture the error message - actions.__insert_batch_log() - end, "%identifier placeholder can only be used inside %repeat placeholder") + assert.spy(notify_spy).was_called(1) + assert + .spy(notify_spy) + .was_called_with("neolog: Cannot use %identifier placeholder outside %repeat placeholder", vim.log.levels.ERROR) + notify_spy:clear() end, }) end) @@ -490,9 +546,64 @@ describe("neolog.actions.insert_batch_log", function() -- Dot repeat the action. Now the batch is empty, it should notify the user vim.cmd("normal! .") assert.spy(notify_spy).was_called(1) - assert.spy(notify_spy).was_called_with("Log batch is empty", vim.log.levels.INFO) + assert.spy(notify_spy).was_called_with("neolog: Log batch is empty", vim.log.levels.INFO) notify_spy:clear() end) + + describe("handles user errors", function() + it("notifies when the filetype is not recognized", function() + helper.assert_scenario({ + input = [[ + // Comment + const fo|o = bar + baz + ]], + filetype = "unknown", + expected = function() + assert.error_matches(function() + actions.add_log_targets_to_batch() + actions.insert_batch_log() + end, "neolog: Treesitter cannot determine language for current buffer") + end, + }) + end) + + it("notifies when the log template is not found", function() + helper.assert_scenario({ + input = [[ + // Comment + const fo|o = bar + baz + ]], + filetype = "javascript", + expected = function() + assert.error_matches(function() + actions.add_log_targets_to_batch() + actions.insert_batch_log({ template = "unknown" }) + end, "neolog: Log template 'unknown' is not found") + end, + }) + end) + + it("notifies when the filetype is not recognized", function() + neolog.setup({ + batch_log_templates = { + testing = {}, + }, + }) + helper.assert_scenario({ + input = [[ + // Comment + const fo|o = bar + baz + ]], + filetype = "javascript", + expected = function() + assert.error_matches(function() + actions.add_log_targets_to_batch() + actions.insert_batch_log({ template = "testing" }) + end, "neolog: Batch log template 'testing' does not have 'javascript' language template") + end, + }) + end) + end) end) describe("neolog.actions.add_log_targets_to_batch", function() @@ -561,4 +672,21 @@ describe("neolog.actions.add_log_targets_to_batch", function() end, }) end) + + describe("handles user errors", function() + it("notifies when the filetype is not recognized", function() + helper.assert_scenario({ + input = [[ + // Comment + const fo|o = bar + baz + ]], + filetype = "unknown", + expected = function() + assert.error_matches(function() + actions.add_log_targets_to_batch() + end, "neolog: Treesitter cannot determine language for current buffer") + end, + }) + end) + end) end)