Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lua/keycoach/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,25 @@ function M.undo(lhs)
if type(state.mapping_file) ~= "string" or state.mapping_file == "" then
return nil, { code = "no_mapping_file", message = "No KeyCoach mappings file configured." }
end
return appender.remove(state.mapping_file, lhs)
local result, remove_problem = appender.remove(state.mapping_file, lhs)
if remove_problem then
return nil, remove_problem
end

if type(lhs) == "string" and lhs ~= "" and type(state.checkpoint) == "table" then
for pattern_id, entry in pairs(state.checkpoint.accepted or {}) do
if entry.lhs == lhs then
record_feedback("rejected_key", pattern_id, lhs)
state.checkpoint.accepted[pattern_id] = nil
break
end
end
if state.tracking == "tracking" or state.tracking == "paused" then
M.flush(true)
end
end

return result, nil
end

function M.report()
Expand Down
109 changes: 13 additions & 96 deletions tests/collector_spec.lua
Original file line number Diff line number Diff line change
@@ -1,94 +1,11 @@
local collector = require("keycoach.nvim.collector")
local h = require("tests.harness")

local function fake_hooks(overrides)
local hooks = {
autocmds = {},
context_value = {
mode = "n",
filetype = "lua",
buffer_kind = "file",
plugin_context = "none",
},
now = 1000,
}

function hooks.create_namespace()
return 17
end

function hooks.on_key(callback)
hooks.key_callback = callback
end

function hooks.create_augroup()
return 23
end

function hooks.create_autocmd(events, options)
if type(events) == "string" then
events = { events }
end
for _, event in ipairs(events) do
hooks.autocmds[event] = options.callback
end
end

function hooks.delete_augroup(group)
hooks.deleted_group = group
hooks.autocmds = {}
end

function hooks.context()
return vim.deepcopy(hooks.context_value)
end

function hooks.now_ms()
return hooks.now
end

function hooks.keytrans(key)
table.insert(hooks.translated, key)
return key
end

function hooks.strchars(value)
return vim.fn.strchars(value)
end

function hooks.parse_command(line)
if type(hooks.parse_override) == "table" and hooks.parse_override[line] ~= nil then
return hooks.parse_override[line]
end
local ok, parsed = pcall(vim.api.nvim_parse_cmd, line, {})
if ok and type(parsed) == "table" then
return parsed
end
return nil
end

hooks.cmdline_value = {
cmdtype = ":",
abort = false,
line = "",
}

function hooks.cmdline_context()
return vim.deepcopy(hooks.cmdline_value)
end

hooks.translated = {}

for key, value in pairs(overrides or {}) do
hooks[key] = value
end

return hooks
end
local fake_hooks = h.fake_hooks

h.describe("Neovim collector", function()
h.it("captures typed keys and removes every hook when stopped", function()
local hooks = fake_hooks()
local hooks = h.fake_hooks()
local emitted = {}
local handle = collector.start({
session = 8,
Expand Down Expand Up @@ -133,7 +50,7 @@ h.describe("Neovim collector", function()
end)

h.it("resolves a complete mapping lhs before capturing individual keys", function()
local hooks = fake_hooks()
local hooks = h.fake_hooks()
local emitted = {}
local candidates = {}
local handle = collector.start({
Expand Down Expand Up @@ -191,7 +108,7 @@ h.describe("Neovim collector", function()
end)

h.it("normalizes mouse tokens and never persists content-bearing keys", function()
local hooks = fake_hooks()
local hooks = h.fake_hooks()
local emitted = {}
local handle = collector.start({
session = 10,
Expand Down Expand Up @@ -234,7 +151,7 @@ h.describe("Neovim collector", function()
end)

h.it("records an executed normal command identity without its arguments", function()
local hooks = fake_hooks()
local hooks = h.fake_hooks()
local emitted = {}
local handle = collector.start({
session = 11,
Expand Down Expand Up @@ -267,7 +184,7 @@ h.describe("Neovim collector", function()
end)

h.it("keeps the invoking mode when the live mode at CmdlineLeave is the cmdline", function()
local hooks = fake_hooks()
local hooks = h.fake_hooks()
local emitted = {}
local handle = collector.start({
session = 14,
Expand Down Expand Up @@ -328,7 +245,7 @@ h.describe("Neovim collector", function()
}

for _, case in ipairs(cases) do
local hooks = fake_hooks()
local hooks = h.fake_hooks()
hooks.context_value = case.context
hooks.cmdline_value = case.cmdline
local emitted = {}
Expand Down Expand Up @@ -356,7 +273,7 @@ h.describe("Neovim collector", function()
}

for _, case in ipairs(cases) do
local hooks = fake_hooks()
local hooks = h.fake_hooks()
hooks.cmdline_value = {
cmdtype = ":",
abort = false,
Expand Down Expand Up @@ -397,7 +314,7 @@ h.describe("Neovim collector", function()
}

for _, case in ipairs(cases) do
local hooks = fake_hooks()
local hooks = h.fake_hooks()
hooks.cmdline_value = {
cmdtype = ":",
abort = false,
Expand Down Expand Up @@ -425,7 +342,7 @@ h.describe("Neovim collector", function()

h.it("keeps user-defined commands that the real parser recognizes", function()
vim.cmd("command! -bar KeyCoachExistenceCheck")
local hooks = fake_hooks()
local hooks = h.fake_hooks()
local emitted = {}
local handle = collector.start({
session = 20,
Expand All @@ -447,7 +364,7 @@ h.describe("Neovim collector", function()
end)

h.it("does not record an unknown command that would error as E492", function()
local hooks = fake_hooks()
local hooks = h.fake_hooks()
hooks.cmdline_value = { cmdtype = ":", abort = false, line = "Telescoop" }
local emitted = {}
local handle = collector.start({
Expand All @@ -469,7 +386,7 @@ h.describe("Neovim collector", function()
end)

h.it("degrades to a category count when no invoking key context exists", function()
local hooks = fake_hooks()
local hooks = h.fake_hooks()
hooks.context_value.mode = "c"
hooks.parse_override = { Example = { cmd = "Example", range = {} } }
hooks.cmdline_value = { cmdtype = ":", abort = false, line = "Example" }
Expand All @@ -492,7 +409,7 @@ h.describe("Neovim collector", function()
end)

h.it("stops observing commands when the collector is stopped", function()
local hooks = fake_hooks()
local hooks = h.fake_hooks()
local emitted = {}
local handle = collector.start({
session = 13,
Expand Down
83 changes: 83 additions & 0 deletions tests/harness.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,89 @@ function M.matches(pattern, actual, message)
end
end

function M.fake_hooks(overrides)
local hooks = {
autocmds = {},
context_value = {
mode = "n",
filetype = "lua",
buffer_kind = "file",
plugin_context = "none",
},
cmdline_value = {
cmdtype = ":",
abort = false,
line = "",
},
now = 1000,
translated = {},
}

function hooks.create_namespace()
return 17
end

function hooks.on_key(callback)
hooks.key_callback = callback
end

function hooks.create_augroup()
return 23
end

function hooks.create_autocmd(events, options)
if type(events) == "string" then
events = { events }
end
for _, event in ipairs(events) do
hooks.autocmds[event] = options.callback
end
end

function hooks.delete_augroup(group)
hooks.deleted_group = group
hooks.autocmds = {}
end

function hooks.context()
return vim.deepcopy(hooks.context_value)
end

function hooks.now_ms()
return hooks.now
end

function hooks.keytrans(key)
table.insert(hooks.translated, key)
return key
end

function hooks.strchars(value)
return vim.fn.strchars(value)
end

function hooks.parse_command(line)
if type(hooks.parse_override) == "table" and hooks.parse_override[line] ~= nil then
return hooks.parse_override[line]
end
local ok, parsed = pcall(vim.api.nvim_parse_cmd, line, {})
if ok and type(parsed) == "table" then
return parsed
end
return nil
end

function hooks.cmdline_context()
return vim.deepcopy(hooks.cmdline_value)
end

for key, value in pairs(overrides or {}) do
hooks[key] = value
end

return hooks
end

function M.run()
local passed = 0
local failed = 0
Expand Down
42 changes: 42 additions & 0 deletions tests/keycoach_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,48 @@ h.describe("KeyCoach public interface", function()
pcall(vim.keymap.del, "n", recommendation.mapping.lhs)
end)

h.it("records a rejected key when an applied mapping is undone", function()
package.loaded["keycoach"] = nil

local state_path = temporary_path("undo-feedback/state.json")
local mapping_file = temporary_path("undo-feedback/mappings.lua")
local collector = stub_collector()
local keycoach = require("keycoach")
keycoach.setup({
enabled = true,
state_path = state_path,
mapping_file = mapping_file,
inventory = empty_inventory(),
collector = collector,
now_ms = function()
return 2000
end,
})

for _, observation in ipairs(observations_for("command:Example")) do
collector.captured.emit(observation)
end
local transition, flush_problem = keycoach.flush()
h.eq(nil, flush_problem)
local recommendation = transition.recommendations[1]
local result, apply_problem = keycoach.apply(recommendation, { confirmed = true })
h.eq(nil, apply_problem)

local undone, undo_problem = keycoach.undo(recommendation.mapping.lhs)
h.eq(nil, undo_problem)
h.truthy(undone.commented:find("keycoach undone", 1, true))

local after, after_problem = keycoach.flush(true)
h.eq(nil, after_problem)
h.eq(1, #after.recommendations)
h.falsy(
after.recommendations[1].mapping.lhs == recommendation.mapping.lhs,
"the undone key must not be re-proposed"
)

pcall(vim.keymap.del, "n", recommendation.mapping.lhs)
end)

h.it("regenerates a mapping candidate when its key is taken at apply time", function()
package.loaded["keycoach"] = nil

Expand Down
Loading
Loading