Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,16 @@ require('toggle').setup({
### Keymap example

```lua
vim.keymap.set('n', '<leader>t', require('toggle').toggle, { desc = 'Toggle word' })
-- v for visual mode, if needed
vim.keymap.set({ 'n', 'v' }, '<leader>t', require('toggle').toggle, { desc = 'Toggle word' })
```

## How it works

1. Gets the **word** under the cursor and checks for a mapping → replaces with `ciw`
1. Gets the **word** under the cursor and checks for a mapping → replaces with `ciw` / `ciW`
2. Falls back to the single **character** under the cursor → replaces with `r`
3. Also works for visual mode, in such case selection is being checked
4. Also checks if end of word matches anything in mappings and toggles it

Mappings are circular: for a group like `{ 'public', 'protected', 'private' }`, each value cycles to the next, and the last wraps to the first.

Expand Down
1 change: 1 addition & 0 deletions lua/toggle/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return {
-- short words
{ 'or', 'and' },
{ 'on', 'off' },
{ 'in', 'out' },
{ 'up', 'down' },
{ 'get', 'set' },
{ 'yes', 'no' },
Expand Down
3 changes: 2 additions & 1 deletion lua/toggle/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ end

function M.toggle()
local replacers = {
replacer.__visual_mode_replacer,
replacer.__get_cWORD_replacer,
replacer.__get_cword_replacer,
replacer.__get_end_of_word_replacer,
replacer.__character_replacer,
replacer.__get_character_replacer,
}

local current_cursor_position = vim.fn.getcurpos()
Expand Down
25 changes: 25 additions & 0 deletions lua/toggle/replacer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,29 @@ M.__get_end_of_word_replacer = function()
}
end

M.__visual_mode_replacer = function()
local function is_visual_mode()
local mode = vim.api.nvim_get_mode().mode
return (mode == 'v') or (mode == 'V')
end

local selected_text = ''
if is_visual_mode() then
local vstart = vim.fn.getpos('.')
local vend = vim.fn.getpos('v')
local lines = vim.fn.getregion(vstart, vend)
selected_text = lines[1]
end

return {
can_handle = function()
return is_visual_mode() and mapping.__has_mapping(selected_text)
end,

replace = function()
vim.api.nvim_command('norm! c' .. mapping.__get_mapping(selected_text))
end,
}
end

return M
2 changes: 2 additions & 0 deletions test/test_files/test_1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ false
false,
<
goto_prev
thisisalongwordwithprotectedanotherwordinside
kaboomza
19 changes: 19 additions & 0 deletions test/toggle_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,23 @@ describe('toggle', function()
assert(vim.fn.expand('<cword>') == 'goto_prev')
end)
end)

it('toggle - no match', function()
open_file_at('test/test_files/test_1.txt', 6, 1, function()
toggle.toggle()
assert(vim.fn.expand('<cword>') == 'kaboomza')
toggle.toggle()
assert(vim.fn.expand('<cword>') == 'kaboomza')
end)
end)

-- TODO
it('toggle - visual mode', function()
open_file_at('test/test_files/test_1.txt', 4, 5, function()
toggle.toggle()
assert(vim.fn.expand('<cword>') == 'goto_next')
toggle.toggle()
assert(vim.fn.expand('<cword>') == 'goto_prev')
end)
end)
end)
Loading