Skip to content

Commit

Permalink
fix: make the plugin work even if g@ is remapped
Browse files Browse the repository at this point in the history
Similar to #2
  • Loading branch information
gregorias committed Jan 26, 2024
1 parent c6efff7 commit a50cea5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lua/coerce/conversion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ end
--@treturn Region The selected region.
M.select_with_motion = function()
local operator_m = require("coerce.operator")
-- The "i" mode is important. We might be running within a feedkeys() call, so we need to insert
-- The i-mode is important. We might be running within a feedkeys() call, so we need to insert
-- the operator into the typeahead buffer immediately before the motion.
return operator_m.operator("im", "")
-- The n-mode is also important. We don’t want user remaps of g@ to interfere with the operator.
return operator_m.operator("in", "")
end

--- Selects the current visual selection.
Expand Down
2 changes: 1 addition & 1 deletion tests/coerce/operator_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("coerce.operator", function()
describe("operator", function()
it("returns the selected region", function()
test_helpers.create_buf({ "Hello, world!" })
local selected_region = co.operator("m", "iw")
local selected_region = co.operator("n", "iw")

assert.are.same({
mode = region.modes.CHAR,
Expand Down
13 changes: 13 additions & 0 deletions tests/coerce_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ describe("coerce", function()
assert.are.same({ "MY_CASE" }, lines)
end)

it("works with motion selection even with a user-defined g@ keymap", function()
local buf = test_helpers.create_buf({ "myCase" })
vim.keymap.set("n", "g@", '<cmd>echo "Pressed g@"<cr>')
c.setup({})
-- `gcr` starts the operator pending mode
-- `u` select upper case coercion
-- `e` select the keyword
test_helpers.execute_keys("gcrue", "x")

local lines = vim.api.nvim_buf_get_lines(buf, 0, 1, true)
assert.are.same({ "MY_CASE" }, lines)
end)

it("displays an error for a multiline selection in visual mode", function()
local buf = test_helpers.create_buf({ "myCase", "yourCase" })
local notification = nil
Expand Down

0 comments on commit a50cea5

Please sign in to comment.