Skip to content

Commit

Permalink
feat: implement motion mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorias committed Jan 21, 2024
1 parent 2620491 commit a8fdd92
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
- [x] Support visual selection
- [x] Make sure that WhichKey works for (visual) `cr`.
- [x] Document the `gp` trick to reselect latest change.
- [ ] Support operator motion
- [x] Support operator motion
- [ ] Allow clients to ditch built-in cases and configure their own.
- [ ] Create a VimDoc.
- [x] Advertise on Reddit.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ the word.
| Vim mode | Keymap prefix | Selector |
| :-- | :-- | :-- |
| Normal | cr | current [word][iskeyword] |
| Normal | gcr | motion selection |
| Visual | cr | visual selection |

### Tips & tricks
Expand Down Expand Up @@ -182,7 +183,7 @@ to change case of the current keyword, Coerce is simpler.
| [Telescope] integration ||||
| Current keyword coerce ||||
| Visual selection ||||
| Operator motion support | |||
| Motion selection | |||
| LSP rename ||||
| Kebab case ||||
| [Numeronym] “case” ||||
Expand Down
1 change: 1 addition & 0 deletions lua/coerce.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ M.default_cases = {

M.default_modes = {
{ vim_mode = "n", keymap_prefix = "cr", selector = conversion_m.select_current_word },
{ vim_mode = "n", keymap_prefix = "gcr", selector = conversion_m.select_with_motion },
{ vim_mode = "v", keymap_prefix = "cr", selector = conversion_m.select_current_visual_selection },
}

Expand Down
2 changes: 2 additions & 0 deletions lua/coerce/conversion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ 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 operator into the typeahead buffer immediately before the motion.
return operator_m.operator("im", "")
end

Expand Down
5 changes: 0 additions & 5 deletions lua/coerce/operator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ M.get_selected_region = function(mode)
local sln = cvim.api.nvim_buf_get_mark(buffer, "[")
local eln = cvim.api.nvim_buf_get_mark(buffer, "]")

-- if mode == M.motion_modes.LINE then
-- sln = { sln[1], 0 }
-- eln = { eln[1], vim.fn.getline(eln[1]):len() - 1 }
-- end
--
local region_m = require("coerce.region")
return region_m.region(region_m.modes.CHAR, sln, eln)
end
Expand Down
2 changes: 1 addition & 1 deletion tests/coerce/conversion_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe("coerce.conversion", function()
end)
test_helpers.execute_keys("e", "x")

assert.are.same(region.region(region.modes.CHAR, {0, 0}, {0, 4}), selected_region)
assert.are.same(region.region(region.modes.CHAR, { 0, 0 }, { 0, 4 }), selected_region)
end)
end)
describe("coerce_current_word", function()
Expand Down
14 changes: 13 additions & 1 deletion tests/coerce_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@ describe("coerce", function()
local buf = test_helpers.create_buf({ "myCase" })
c.setup({})
-- `ve` selects the keyword
-- `cru` runs the upper case coersion
-- `cru` runs upper case coercion
test_helpers.execute_keys("vecru", "x")

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

it("works with motion selection", function()
local buf = test_helpers.create_buf({ "myCase" })
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 a8fdd92

Please sign in to comment.