Skip to content

Commit

Permalink
Revert "refactor: rename mode to selection mode"
Browse files Browse the repository at this point in the history
This reverts commit e8a047d.

In the end, I think the mode abstraction is on right level of
granularity.
  • Loading branch information
gregorias committed Feb 20, 2024
1 parent 677aa1d commit 7d90999
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ the bleeding edge though.
## 🚀 Usage

You can use Coerce to coerce [words][iskeyword] into various **cases** using
**selection modes**:
**modes**.

- A **case** is a function that changes a word into another word, e.g., the
word’s camel case version.
- A **selection mode** specifies how Coerce selects the word, e.g., select
whatever is currently visually selected.
- A **mode** specifies how Coerce triggers, selects and transforms the word,
e.g., select whatever is currently visually selected.

### Quick start

Expand All @@ -88,7 +88,7 @@ You can use Coerce to coerce [words][iskeyword] into various **cases** using
| UPPER_CASE | u |
| path/case | / |

### Built-in selection modes
### Built-in modes

| Vim mode | Keymap prefix | Selector |
| :-- | :-- | :-- |
Expand Down Expand Up @@ -136,10 +136,9 @@ require"coerce".setup{
keymap_registry = require("coerce.keymap").keymap_registry(),
-- The notification function used during error conditions.
notify = function(...) return vim.notify(...) end,
-- If you don’t like the default cases and selection modes,
-- you can override them.
-- If you don’t like the default cases and modes, you can override them.
cases = require"coerce".default_cases,
selection_modes = require"coerce".selection_modes,
modes = require"coerce".default_modes,
}
```

Expand All @@ -159,12 +158,12 @@ require"coerce".register_case{
}
```

### Register a new selection mode
### Register a new mode

You can register a new selection mode like so:
You can register a new mode like so:

```lua
require"coerce".register_selection mode{
require"coerce".register_mode{
vim_mode = "v",
keymap_prefix = "gc",
selector = function()
Expand Down
16 changes: 8 additions & 8 deletions lua/coerce.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ M.default_cases = {
{ keymap = "/", case = case_m.to_path_case, description = "path/case" },
}

M.default_selection_modes = {
M.default_modes = {
{ vim_mode = "n", keymap_prefix = "cr", selector = selector_m.select_current_word },
{ vim_mode = "n", keymap_prefix = "gcr", selector = selector_m.select_with_motion },
{ vim_mode = "v", keymap_prefix = "cr", selector = selector_m.select_current_visual_selection },
Expand All @@ -30,7 +30,7 @@ M.default_config = {
vim.notify(...)
end,
cases = M.default_cases,
selection_modes = M.default_selection_modes,
modes = M.default_modes,
}

--- The singleton Coercer object.
Expand All @@ -47,12 +47,12 @@ M.register_case = function(case)
coercer:register_case(case)
end

--- Registers a new selection mode.
--- Registers a new mode.
--
--@tparam table selection mode
M.register_selection_mode = function(selection_mode)
--@tparam table mode
M.register_mode = function(mode)
assert(coercer ~= nil, "Coercer is not initialized.")
coercer:register_selection_mode(selection_mode)
coercer:register_mode(mode)
end

--- Sets up the plugin.
Expand All @@ -63,8 +63,8 @@ M.setup = function(config)

coercer = conversion_m.Coercer(effective_config.keymap_registry, effective_config.notify)

for _, selection_mode in ipairs(effective_config.selection_modes) do
coercer:register_selection_mode(selection_mode)
for _, mode in ipairs(effective_config.modes) do
coercer:register_mode(mode)
end

for _, case in ipairs(effective_config.cases) do
Expand Down
45 changes: 20 additions & 25 deletions lua/coerce/conversion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
local M = {}

M.registered_cases = {}
M.registered_selection_modes = {}
M.registered_modes = {}

--- Constructs a Coercer object.
--
-- Coercer is meant to be a singleton object that handles registering new
-- cases and selection_modes, and actuating them.
-- cases and modes, and actuating them.
--
--@tparam table keymap_registry
--@tparam function notify The notification function to use.
Expand All @@ -19,23 +19,18 @@ M.Coercer = function(keymap_registry, notify)
keymap_registry = keymap_registry,
notify = notify,
registered_cases = {},
registered_selection_modes = {},
registered_modes = {},

_register_selection_mode_case = function(self, selection_mode, case)
self.keymap_registry.register_keymap(
selection_mode.vim_mode,
selection_mode.keymap_prefix .. case.keymap,
function()
local coroutine_m = require("coerce.coroutine")
coroutine_m.fire_and_forget(function()
local error = M.coerce(selection_mode.selector, case.case)
if type(error) == "string" then
self.notify(error, "error", { title = "Coerce" })
end
end)
end,
case.description
)
_register_mode_case = function(self, mode, case)
self.keymap_registry.register_keymap(mode.vim_mode, mode.keymap_prefix .. case.keymap, function()
local coroutine_m = require("coerce.coroutine")
coroutine_m.fire_and_forget(function()
local error = M.coerce(mode.selector, case.case)
if type(error) == "string" then
self.notify(error, "error", { title = "Coerce" })
end
end)
end, case.description)
end,

--- Registers a new case.
Expand All @@ -45,20 +40,20 @@ M.Coercer = function(keymap_registry, notify)
register_case = function(self, case)
table.insert(self.registered_cases, case)

for _, selection_mode in ipairs(self.registered_selection_modes) do
self:_register_selection_mode_case(selection_mode, case)
for _, mode in ipairs(self.registered_modes) do
self:_register_mode_case(mode, case)
end
end,

--- Registers a new selection_mode.
--- Registers a new mode.
--
--@tparam { keymap_prefix=string, selector=function }
register_selection_mode = function(self, selection_mode)
table.insert(self.registered_selection_modes, selection_mode)
self.keymap_registry.register_keymap_group(selection_mode.vim_mode, selection_mode.keymap_prefix, "+Coerce")
register_mode = function(self, mode)
table.insert(self.registered_modes, mode)
self.keymap_registry.register_keymap_group(mode.vim_mode, mode.keymap_prefix, "+Coerce")

for _, case in ipairs(self.registered_cases) do
self:_register_selection_mode_case(selection_mode, case)
self:_register_mode_case(mode, case)
end
end,
}
Expand Down

0 comments on commit 7d90999

Please sign in to comment.