Skip to content

Commit 26ad542

Browse files
committed
feat: separate keymap for n/v mode
1 parent 72d39a5 commit 26ad542

File tree

11 files changed

+350
-326
lines changed

11 files changed

+350
-326
lines changed

.editorconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ trim_trailing_whitespace = true
77

88
[*.md]
99
trim_trailing_whitespace = false
10+

doc/text-transform.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ Your plugin configuration with its default values.
77
Default values:
88
>
99
TextTransform.options = {
10-
-- Prints useful logs about what event are triggered, and reasons actions are executed.
11-
debug = false,
12-
-- Keymap to trigger the transform.
13-
keymap = {
14-
"<Leader>~",
15-
},
10+
-- Prints useful logs about what event are triggered, and reasons actions are executed.
11+
debug = false,
12+
-- Keymap to trigger the transform.
13+
keymap = {
14+
-- Normal mode keymap.
15+
["n"] = "<Leader>~",
16+
-- Visual mode keymap.
17+
["v"] = "<Leader>~",
18+
},
1619
}
1720
1821
<

lua/text-transform/config.lua

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ local TextTransform = {}
55
--- Default values:
66
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
77
TextTransform.options = {
8-
-- Prints useful logs about what event are triggered, and reasons actions are executed.
9-
debug = false,
10-
-- Keymap to trigger the transform.
11-
keymap = {
12-
"<Leader>~",
13-
},
8+
-- Prints useful logs about what event are triggered, and reasons actions are executed.
9+
debug = false,
10+
-- Keymap to trigger the transform.
11+
keymap = {
12+
-- Normal mode keymap.
13+
["n"] = "<Leader>~",
14+
-- Visual mode keymap.
15+
["v"] = "<Leader>~",
16+
},
1417
}
1518

1619
--- Define your text-transform setup.
@@ -19,22 +22,40 @@ TextTransform.options = {
1922
---
2023
---@usage `require("text-transform").setup()` (add `{}` with your |TextTransform.options| table)
2124
function TextTransform.setup(options)
22-
options = options or {}
23-
24-
TextTransform.options = vim.tbl_deep_extend("keep", options, TextTransform.options)
25-
-- use input from current word in editor
26-
vim.cmd("amenu Transforms.&camelCase :lua ReplaceCurrentWord(CamelCase)<CR>")
27-
vim.cmd("amenu Transforms.&snake_case :lua ReplaceCurrentWord(SnakeCase)<CR>")
28-
vim.cmd("amenu Transforms.&PascalCase :lua ReplaceCurrentWord(PascalCase)<CR>")
29-
vim.cmd("amenu Transforms.&kebab-case :lua ReplaceCurrentWord(KebabCase)<CR>")
30-
vim.cmd("amenu Transforms.&dot\\.case :lua ReplaceCurrentWord(DotCase)<CR>")
31-
vim.cmd("amenu Transforms.&Title\\ Case :lua ReplaceCurrentWord(TitleCase)<CR>")
32-
33-
for kmap in TextTransform.options.keymap do
34-
vim.keymap.set({ "n", "v" }, kmap, "<cmd>popup Transforms<CR>", { silent = true })
35-
end
36-
37-
return TextTransform.options
25+
options = options or {}
26+
27+
TextTransform.options = vim.tbl_deep_extend("keep", options, TextTransform.options)
28+
29+
-- use input from current word in editor
30+
vim.cmd("amenu TransformsSelection.&camelCase :lua ReplaceCurrentSelection(CamelCase)<CR>")
31+
vim.cmd("amenu TransformsSelection.&snake_case :lua ReplaceCurrentSelection(SnakeCase)<CR>")
32+
vim.cmd("amenu TransformsSelection.&PascalCase :lua ReplaceCurrentSelection(PascalCase)<CR>")
33+
vim.cmd("amenu TransformsSelection.&kebab-case :lua ReplaceCurrentSelection(KebabCase)<CR>")
34+
vim.cmd("amenu TransformsSelection.&dot\\.case :lua ReplaceCurrentSelection(DotCase)<CR>")
35+
vim.cmd("amenu TransformsSelection.&Title\\ Case :lua ReplaceCurrentSelection(TitleCase)<CR>")
36+
37+
-- use input from current word in editor
38+
vim.cmd("amenu TransformsWord.&camelCase :lua ReplaceCurrentWord(CamelCase)<CR>")
39+
vim.cmd("amenu TransformsWord.&snake_case :lua ReplaceCurrentWord(SnakeCase)<CR>")
40+
vim.cmd("amenu TransformsWord.&PascalCase :lua ReplaceCurrentWord(PascalCase)<CR>")
41+
vim.cmd("amenu TransformsWord.&kebab-case :lua ReplaceCurrentWord(KebabCase)<CR>")
42+
vim.cmd("amenu TransformsWord.&dot\\.case :lua ReplaceCurrentWord(DotCase)<CR>")
43+
vim.cmd("amenu TransformsWord.&Title\\ Case :lua ReplaceCurrentWord(TitleCase)<CR>")
44+
45+
vim.keymap.set(
46+
"n",
47+
TextTransform.options.keymap.n,
48+
"<cmd>popup TransformsWord<CR>",
49+
{ silent = true }
50+
)
51+
vim.keymap.set(
52+
"v",
53+
TextTransform.options.keymap.v,
54+
"<cmd>popup TransformsSelection<CR>",
55+
{ silent = true }
56+
)
57+
58+
return TextTransform.options
3859
end
3960

4061
return TextTransform

lua/text-transform/init.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@ local TextTransform = {}
33

44
-- Toggle the plugin by calling the `enable`/`disable` methods respectively.
55
function TextTransform.toggle()
6-
-- when the config is not set to the global object, we set it
7-
if _G.TextTransform.config == nil then
8-
_G.TextTransform.config = require("text-transform.config").options
9-
end
6+
-- when the config is not set to the global object, we set it
7+
if _G.TextTransform.config == nil then
8+
_G.TextTransform.config = require("text-transform.config").options
9+
end
1010

11-
_G.TextTransform.state = M.toggle()
11+
_G.TextTransform.state = M.toggle()
1212
end
1313

1414
-- starts TextTransform and set internal functions and state.
1515
function TextTransform.enable()
16-
if _G.TextTransform.config == nil then
17-
_G.TextTransform.config = require("text-transform.config").options
18-
end
16+
if _G.TextTransform.config == nil then
17+
_G.TextTransform.config = require("text-transform.config").options
18+
end
1919

20-
local state = M.enable()
20+
local state = M.enable()
2121

22-
if state ~= nil then
23-
_G.TextTransform.state = state
24-
end
22+
if state ~= nil then
23+
_G.TextTransform.state = state
24+
end
2525

26-
return state
26+
return state
2727
end
2828

2929
-- disables TextTransform and reset internal functions and state.
3030
function TextTransform.disable()
31-
_G.TextTransform.state = M.disable()
31+
_G.TextTransform.state = M.disable()
3232
end
3333

3434
-- setup TextTransform options and merge them with user provided ones.
3535
function TextTransform.setup(opts)
36-
_G.TextTransform.config = require("text-transform.config").setup(opts)
36+
_G.TextTransform.config = require("text-transform.config").setup(opts)
3737
end
3838

3939
_G.TextTransform = TextTransform

lua/text-transform/main.lua

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,45 @@ local TextTransform = {}
55

66
-- state
77
local S = {
8-
-- Boolean determining if the plugin is enabled or not.
9-
enabled = false,
8+
-- Boolean determining if the plugin is enabled or not.
9+
enabled = false,
1010
}
1111

1212
---Toggle the plugin by calling the `enable`/`disable` methods respectively.
1313
---@private
1414
function TextTransform.toggle()
15-
if S.enabled then
16-
return TextTransform.disable()
17-
end
15+
if S.enabled then
16+
return TextTransform.disable()
17+
end
1818

19-
return TextTransform.enable()
19+
return TextTransform.enable()
2020
end
2121

2222
---Initializes the plugin.
2323
---@private
2424
function TextTransform.enable()
25-
if S.enabled then
26-
return S
27-
end
25+
if S.enabled then
26+
return S
27+
end
2828

29-
S.enabled = true
29+
S.enabled = true
3030

31-
return S
31+
return S
3232
end
3333

3434
---Disables the plugin and reset the internal state.
3535
---@private
3636
function TextTransform.disable()
37-
if not S.enabled then
38-
return S
39-
end
37+
if not S.enabled then
38+
return S
39+
end
4040

41-
-- reset the state
42-
S = {
43-
enabled = false,
44-
}
41+
-- reset the state
42+
S = {
43+
enabled = false,
44+
}
4545

46-
return S
46+
return S
4747
end
4848

4949
return TextTransform

lua/text-transform/util/debug.lua

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ local D = {}
77
---@param ... any: the arguments of the formatted string.
88
---@private
99
function D.log(scope, str, ...)
10-
if _G.TextTransform.config ~= nil and not _G.TextTransform.config.debug then
11-
return
12-
end
13-
14-
local info = debug.getinfo(2, "Sl")
15-
local line = ""
16-
17-
if info then
18-
line = "L" .. info.currentline
19-
end
20-
21-
print(
22-
string.format(
23-
"[text-transform:%s %s in %s] > %s",
24-
os.date("%H:%M:%S"),
25-
line,
26-
scope,
27-
string.format(str, ...)
28-
)
10+
if _G.TextTransform.config ~= nil and not _G.TextTransform.config.debug then
11+
return
12+
end
13+
14+
local info = debug.getinfo(2, "Sl")
15+
local line = ""
16+
17+
if info then
18+
line = "L" .. info.currentline
19+
end
20+
21+
print(
22+
string.format(
23+
"[text-transform:%s %s in %s] > %s",
24+
os.date("%H:%M:%S"),
25+
line,
26+
scope,
27+
string.format(str, ...)
2928
)
29+
)
3030
end
3131

3232
---prints the table if debug is true.
@@ -35,27 +35,27 @@ end
3535
---@param indent number?: the default indent value, starts at 0.
3636
---@private
3737
function D.tprint(table, indent)
38-
if _G.TextTransform.config ~= nil and not _G.TextTransform.config.debug then
39-
return
40-
end
41-
42-
if not indent then
43-
indent = 0
44-
end
45-
46-
for k, v in pairs(table) do
47-
local formatting = string.rep(" ", indent) .. k .. ": "
48-
if type(v) == "table" then
49-
print(formatting)
50-
D.tprint(v, indent + 1)
51-
elseif type(v) == "boolean" then
52-
print(formatting .. tostring(v))
53-
elseif type(v) == "function" then
54-
print(formatting .. "FUNCTION")
55-
else
56-
print(formatting .. v)
57-
end
38+
if _G.TextTransform.config ~= nil and not _G.TextTransform.config.debug then
39+
return
40+
end
41+
42+
if not indent then
43+
indent = 0
44+
end
45+
46+
for k, v in pairs(table) do
47+
local formatting = string.rep(" ", indent) .. k .. ": "
48+
if type(v) == "table" then
49+
print(formatting)
50+
D.tprint(v, indent + 1)
51+
elseif type(v) == "boolean" then
52+
print(formatting .. tostring(v))
53+
elseif type(v) == "function" then
54+
print(formatting .. "FUNCTION")
55+
else
56+
print(formatting .. v)
5857
end
58+
end
5959
end
6060

6161
return D

0 commit comments

Comments
 (0)