Skip to content

Commit b604398

Browse files
author
Sasha Gurevich
committed
feat(replacer): end of word replacer implementation
1 parent 19e91f4 commit b604398

File tree

4 files changed

+53
-20
lines changed

4 files changed

+53
-20
lines changed

lua/toggle/init.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ end
3737

3838
function M.toggle()
3939
local replacers = {
40-
replacer.__cWORD_replacer(),
41-
replacer.__cword_replacer(),
42-
replacer.__character_replacer(),
40+
replacer.__get_cWORD_replacer,
41+
replacer.__get_cword_replacer,
42+
replacer.__get_end_of_word_replacer,
43+
replacer.__character_replacer,
4344
}
4445

4546
local current_cursor_position = vim.fn.getcurpos()
4647

47-
for _, r in ipairs(replacers) do
48+
for _, get_replacer in ipairs(replacers) do
49+
local r = get_replacer()
4850
if r.can_handle() then
4951
r.replace()
5052
if config.keep_cursor_position then

lua/toggle/replacer.lua

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,69 @@
11
local mapping = require('toggle.mapping')
22

3+
-- TODO provide luadocs for replacers
34
local M = {}
45

5-
M.__cword_replacer = function()
6+
M.__get_cword_replacer = function()
7+
local word = vim.fn.expand('<cword>')
68
return {
79
can_handle = function()
8-
local word = vim.fn.expand('<cword>')
910
return mapping.__has_mapping(word)
1011
end,
1112

1213
replace = function()
13-
local word = vim.fn.expand('<cword>')
1414
vim.api.nvim_command('normal! ciw' .. mapping.__get_mapping(word))
1515
end,
1616
}
1717
end
1818

19-
M.__cWORD_replacer = function()
19+
M.__get_cWORD_replacer = function()
20+
local word = vim.fn.expand('<cWORD>')
2021
return {
2122
can_handle = function()
22-
local word = vim.fn.expand('<cWORD>')
2323
return mapping.__has_mapping(word)
2424
end,
2525

2626
replace = function()
27-
local word = vim.fn.expand('<cWORD>')
2827
vim.api.nvim_command('normal! ciW' .. mapping.__get_mapping(word))
2928
end,
3029
}
3130
end
3231

33-
M.__character_replacer = function()
32+
M.__get_character_replacer = function()
33+
local coords = vim.api.nvim_win_get_cursor(0)
34+
local line = vim.api.nvim_get_current_line()
35+
local character = line:sub(coords[2] + 1, coords[2] + 1)
36+
3437
return {
3538
can_handle = function()
36-
local character = M.__get_character_under_cursor()
3739
return mapping.__has_mapping(character)
3840
end,
3941

4042
replace = function()
41-
local character = M.__get_character_under_cursor()
4243
vim.api.nvim_command('normal! r' .. mapping.__get_mapping(character))
4344
end,
4445
}
4546
end
4647

47-
-- TODO write tests
48-
M.__get_character_under_cursor = function()
49-
local coords = vim.api.nvim_win_get_cursor(0)
48+
M.__get_end_of_word_replacer = function()
49+
local current_cursor_position = vim.fn.getcurpos()
50+
vim.api.nvim_command('normal! e') -- jump to the end of word
51+
52+
local end_of_word_column = vim.api.nvim_win_get_cursor(0)[2]
53+
vim.fn.setpos('.', current_cursor_position) -- restore cursor position
54+
5055
local line = vim.api.nvim_get_current_line()
51-
return line:sub(coords[2] + 1, coords[2] + 1)
56+
local end_of_word_under_cursor = line:sub(current_cursor_position[3], end_of_word_column + 1)
57+
58+
return {
59+
can_handle = function()
60+
return mapping.__has_mapping(end_of_word_under_cursor)
61+
end,
62+
63+
replace = function()
64+
vim.api.nvim_command('normal! ce' .. mapping.__get_mapping(end_of_word_under_cursor))
65+
end,
66+
}
5267
end
5368

5469
return M

test/test_files/test_1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
false
22
false,
33
<
4+
goto_prev

test/toggle_spec.lua

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,39 @@ local function get_character_under_cursor()
1515
end
1616

1717
describe('toggle', function()
18-
it('toggle cword', function()
18+
it('toggle - cword', function()
1919
open_file_at('test/test_files/test_1.txt', 1, 1, function()
2020
toggle.toggle()
2121
assert(vim.fn.expand('<cword>') == 'true')
22+
toggle.toggle()
23+
assert(vim.fn.expand('<cword>') == 'false')
2224
end)
2325
end)
2426

25-
it('toggle cWORD', function()
27+
it('toggle - cWORD', function()
2628
open_file_at('test/test_files/test_1.txt', 2, 1, function()
2729
toggle.toggle()
2830
assert(vim.fn.expand('<cword>') == 'true')
31+
toggle.toggle()
32+
assert(vim.fn.expand('<cword>') == 'false')
2933
end)
3034
end)
3135

32-
it('toggle char', function()
36+
it('toggle - char', function()
3337
open_file_at('test/test_files/test_1.txt', 3, 1, function()
3438
toggle.toggle()
3539
assert(get_character_under_cursor() == '>')
40+
toggle.toggle()
41+
assert(get_character_under_cursor() == '<')
42+
end)
43+
end)
44+
45+
it('toggle - end of word', function()
46+
open_file_at('test/test_files/test_1.txt', 4, 5, function()
47+
toggle.toggle()
48+
assert(vim.fn.expand('<cword>') == 'goto_next')
49+
toggle.toggle()
50+
assert(vim.fn.expand('<cword>') == 'goto_prev')
3651
end)
3752
end)
3853
end)

0 commit comments

Comments
 (0)