Skip to content

Commit

Permalink
refactor: move selectors to their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorias committed Feb 18, 2024
1 parent e8a047d commit f93e978
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 58 deletions.
7 changes: 4 additions & 3 deletions lua/coerce.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

local case_m = require("coerce.case")
local selector_m = require("coerce.selector")
local conversion_m = require("coerce.conversion")

--- The default cases to use.
Expand All @@ -16,9 +17,9 @@ M.default_cases = {
}

M.default_selection_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 },
{ 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 },
}

M.default_config = {
Expand Down
44 changes: 2 additions & 42 deletions lua/coerce/conversion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,47 +87,6 @@ M.substitute = function(selected_region, apply)
)
end

--- Selects the current word.
--
-- This is a fire-and-forget coroutine function.
--
--@treturn Region The selected region.
M.select_current_word = function()
local operator_m = require("coerce.operator")
return operator_m.operator("xn", "iw")
end

--- Selects with the user provided motion.
--
-- This is a fire-and-forget coroutine function.
--
--@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.
-- 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.
--
-- This plugin is only meant to work with keywords, so this function fails if
-- the selected region is multiline.
--
--@teturn Region The selected region or an error.
M.select_current_visual_selection = function()
local visual_m = require("coerce.visual")
local selected_region = visual_m.get_current_visual_selection()
local region = require("coerce.region")

local selected_line_count = region.lines(selected_region)
if selected_line_count > 1 then
return (selected_line_count .. " lines selected." .. " Coerce supports only single-line visual selections.")
end
return selected_region
end

--- Coerces selected text.
--
--@tparam function select_text The function that returns selected text (Region) or an error.
Expand All @@ -147,7 +106,8 @@ end
--@tparam function apply The function to apply to the current word.
--@treturn nil
M.coerce_current_word = function(apply)
M.coerce(M.select_current_word, apply)
local selector = require("coerce.selector")
M.coerce(selector.select_current_word, apply)
end

return M
49 changes: 49 additions & 0 deletions lua/coerce/selector.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--- A module with text selectors.
--
-- A text selector is a function that returns a region of selected text.
--
--@module coerce.selector
local M = {}

--- Selects the current word.
--
-- This is a fire-and-forget coroutine function.
--
--@treturn Region The selected region.
M.select_current_word = function()
local operator_m = require("coerce.operator")
return operator_m.operator("xn", "iw")
end

--- Selects with the user provided motion.
--
-- This is a fire-and-forget coroutine function.
--
--@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.
-- 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.
--
-- This plugin is only meant to work with keywords, so this function fails if
-- the selected region is multiline.
--
--@treturn Region The selected region or an error.
M.select_current_visual_selection = function()
local visual_m = require("coerce.visual")
local selected_region = visual_m.get_current_visual_selection()
local region = require("coerce.region")

local selected_line_count = region.lines(selected_region)
if selected_line_count > 1 then
return (selected_line_count .. " lines selected." .. " Coerce supports only single-line visual selections.")
end
return selected_region
end

return M
13 changes: 0 additions & 13 deletions tests/coerce/conversion_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ describe("coerce.conversion", function()
assert.are.same({ "Hello, Albert!" }, lines)
end)
end)
describe("select_with_motion", function()
it("select the word", function()
test_helpers.create_buf({ "Hello, world!" })

local selected_region = nil
cco.fire_and_forget(function()
selected_region = cc.select_with_motion()
end)
test_helpers.execute_keys("e", "x")

assert.are.same(region.region(region.modes.CHAR, { 0, 0 }, { 0, 4 }), selected_region)
end)
end)
describe("coerce_current_word", function()
it("coerces the current word", function()
local buf = test_helpers.create_buf({ "Hello, world!" })
Expand Down
20 changes: 20 additions & 0 deletions tests/coerce/selector_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local selector = require("coerce.selector")
local cco = require("coerce.coroutine")
local region = require("coerce.region")
local test_helpers = require("tests.helpers")

describe("coerce.selector", function()
describe("select_with_motion", function()
it("select the word", function()
test_helpers.create_buf({ "Hello, world!" })

local selected_region = nil
cco.fire_and_forget(function()
selected_region = selector.select_with_motion()
end)
test_helpers.execute_keys("e", "x")

assert.are.same(region.region(region.modes.CHAR, { 0, 0 }, { 0, 4 }), selected_region)
end)
end)
end)

0 comments on commit f93e978

Please sign in to comment.