Skip to content

Commit 61944c2

Browse files
committed
refactor: simplify module redirection logic
1 parent a14fbc8 commit 61944c2

File tree

2 files changed

+21
-23
lines changed

2 files changed

+21
-23
lines changed

lua/various-textobjs/init.lua

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@ function M.setup(userConfig) require("various-textobjs.config.config").setup(use
88
setmetatable(M, {
99
__index = function(_, key)
1010
return function(...)
11-
local linewiseObjs = vim.tbl_keys(require("various-textobjs.textobjs.linewise"))
12-
13-
local module = "charwise"
14-
if vim.tbl_contains(linewiseObjs, key) then module = "linewise" end
15-
if key == "column" then module = "blockwise" end
16-
if key == "diagnostic" then module = "diagnostic" end
17-
if key == "subword" then module = "subword" end
18-
if key == "emoji" then module = "emoji" end
11+
local warn = require("various-textobjs.utils").warn
1912

13+
-- DEPRECATION (2025-04-24)
2014
if key == "pyTripleQuotes" then
2115
local msg = "The `pyTripleQuotes` textobj is deprecated. "
2216
.. "Please use `nvim-treesitter-teextobjects`, create a file "
@@ -27,11 +21,28 @@ setmetatable(M, {
2721
.. "(expression_statement (string (string_content) @docstring.inner) @docstring.outer)\n"
2822
.. "```\n"
2923
.. "Call the textobject via `:TSTextobjectSelect @docstring.outer`"
30-
require("various-textobjs.utils").warn(msg)
24+
warn(msg)
3125
return function() end -- empty function to prevent error
3226
end
3327

34-
require("various-textobjs.textobjs." .. module)[key](...)
28+
local linewiseObjs = vim.tbl_keys(require("various-textobjs.textobjs.linewise"))
29+
local charwiseObjs = vim.tbl_keys(require("various-textobjs.textobjs.charwise"))
30+
31+
local module
32+
if vim.tbl_contains(linewiseObjs, key) then module = "linewise" end
33+
if vim.tbl_contains(charwiseObjs, key) then module = "charwise" end
34+
if key == "column" then module = "blockwise" end
35+
if key == "diagnostic" then module = "diagnostic" end
36+
if key == "subword" then module = "subword" end
37+
if key == "emoji" then module = "emoji" end
38+
39+
if module then
40+
require("various-textobjs.textobjs." .. module)[key](...)
41+
else
42+
local msg = ("There is no text object called `%s`.\n\n"):format(key)
43+
.. "Make sure it exists in the list of text objects, and that you haven't misspelled it."
44+
warn(msg)
45+
end
3546
end
3647
end,
3748
})

lua/various-textobjs/textobjs/charwise.lua

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@ local core = require("various-textobjs.charwise-core")
33
local u = require("various-textobjs.utils")
44
--------------------------------------------------------------------------------
55

6-
-- Warn in case user tries to call a textobj that doesn't exist.
7-
-- (Only needed in the module for `charwise` text objects, since it is the
8-
-- catch-all for the `__index` redirect from this plugin's main `init.lua`.)
9-
setmetatable(M, {
10-
__index = function(_, key)
11-
return function()
12-
local msg = ("There is no text object called `%s`.\n\n"):format(key)
13-
.. "Make sure it exists in the list of text objects, and that you haven't misspelled it."
14-
u.warn(msg)
15-
end
16-
end,
17-
})
18-
196
---@return integer
207
---@nodiscard
218
local function smallForward()

0 commit comments

Comments
 (0)