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
41 changes: 16 additions & 25 deletions lua/toggle/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local mapping = require('toggle.mapping')
local defaults = require('toggle.defaults')
local replacer = require('toggle.replacer')

local M = {}

Expand Down Expand Up @@ -35,34 +36,24 @@ function M.setup(opts)
end

function M.toggle()
local cWORD_under_cursor = vim.fn.expand('<cWORD>')
local cword_under_cursor = vim.fn.expand('<cword>')
local current_cursor_position = vim.fn.getcurpos()
local replacers = {
replacer.__get_cWORD_replacer,
replacer.__get_cword_replacer,
replacer.__get_end_of_word_replacer,
replacer.__character_replacer,
}

if mapping.__has_mapping(cWORD_under_cursor) then
vim.api.nvim_command('normal! ciW' .. mapping.__get_mapping(cWORD_under_cursor))
if config.keep_cursor_position then
vim.fn.setpos('.', current_cursor_position)
end
return
end
local current_cursor_position = vim.fn.getcurpos()

if mapping.__has_mapping(cword_under_cursor) then
vim.api.nvim_command('normal! ciw' .. mapping.__get_mapping(cword_under_cursor))
if config.keep_cursor_position then
vim.fn.setpos('.', current_cursor_position)
for _, get_replacer in ipairs(replacers) do
local r = get_replacer()
if r.can_handle() then
r.replace()
if config.keep_cursor_position then
vim.fn.setpos('.', current_cursor_position)
end
return
end
return
end

-- fall back to single character under cursor
local coords = vim.api.nvim_win_get_cursor(0)
local line = vim.api.nvim_get_current_line()
local character_under_cursor = line:sub(coords[2] + 1, coords[2] + 1)

if mapping.__has_mapping(character_under_cursor) then
vim.api.nvim_command('normal! r' .. mapping.__get_mapping(character_under_cursor))
return
end
end

Expand Down
69 changes: 69 additions & 0 deletions lua/toggle/replacer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
local mapping = require('toggle.mapping')

-- TODO provide luadocs for replacers
local M = {}

M.__get_cword_replacer = function()
local word = vim.fn.expand('<cword>')
return {
can_handle = function()
return mapping.__has_mapping(word)
end,

replace = function()
vim.api.nvim_command('normal! ciw' .. mapping.__get_mapping(word))
end,
}
end

M.__get_cWORD_replacer = function()
local word = vim.fn.expand('<cWORD>')
return {
can_handle = function()
return mapping.__has_mapping(word)
end,

replace = function()
vim.api.nvim_command('normal! ciW' .. mapping.__get_mapping(word))
end,
}
end

M.__get_character_replacer = function()
local coords = vim.api.nvim_win_get_cursor(0)
local line = vim.api.nvim_get_current_line()
local character = line:sub(coords[2] + 1, coords[2] + 1)

return {
can_handle = function()
return mapping.__has_mapping(character)
end,

replace = function()
vim.api.nvim_command('normal! r' .. mapping.__get_mapping(character))
end,
}
end

M.__get_end_of_word_replacer = function()
local current_cursor_position = vim.fn.getcurpos()
vim.api.nvim_command('normal! e') -- jump to the end of word

local end_of_word_column = vim.api.nvim_win_get_cursor(0)[2]
vim.fn.setpos('.', current_cursor_position) -- restore cursor position

local line = vim.api.nvim_get_current_line()
local end_of_word_under_cursor = line:sub(current_cursor_position[3], end_of_word_column + 1)

return {
can_handle = function()
return mapping.__has_mapping(end_of_word_under_cursor)
end,

replace = function()
vim.api.nvim_command('normal! ce' .. mapping.__get_mapping(end_of_word_under_cursor))
end,
}
end

return M
1 change: 1 addition & 0 deletions test/test_files/test_1.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
false
false,
<
goto_prev
21 changes: 18 additions & 3 deletions test/toggle_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,39 @@ local function get_character_under_cursor()
end

describe('toggle', function()
it('toggle cword', function()
it('toggle - cword', function()
open_file_at('test/test_files/test_1.txt', 1, 1, function()
toggle.toggle()
assert(vim.fn.expand('<cword>') == 'true')
toggle.toggle()
assert(vim.fn.expand('<cword>') == 'false')
end)
end)

it('toggle cWORD', function()
it('toggle - cWORD', function()
open_file_at('test/test_files/test_1.txt', 2, 1, function()
toggle.toggle()
assert(vim.fn.expand('<cword>') == 'true')
toggle.toggle()
assert(vim.fn.expand('<cword>') == 'false')
end)
end)

it('toggle char', function()
it('toggle - char', function()
open_file_at('test/test_files/test_1.txt', 3, 1, function()
toggle.toggle()
assert(get_character_under_cursor() == '>')
toggle.toggle()
assert(get_character_under_cursor() == '<')
end)
end)

it('toggle - end of word', 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)