diff --git a/lua/toggle/init.lua b/lua/toggle/init.lua index 2f75eba..d801d9a 100644 --- a/lua/toggle/init.lua +++ b/lua/toggle/init.lua @@ -1,5 +1,6 @@ local mapping = require('toggle.mapping') local defaults = require('toggle.defaults') +local replacer = require('toggle.replacer') local M = {} @@ -35,34 +36,24 @@ function M.setup(opts) end function M.toggle() - local cWORD_under_cursor = vim.fn.expand('') - local cword_under_cursor = vim.fn.expand('') - 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 diff --git a/lua/toggle/replacer.lua b/lua/toggle/replacer.lua new file mode 100644 index 0000000..7b03b79 --- /dev/null +++ b/lua/toggle/replacer.lua @@ -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('') + 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('') + 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 diff --git a/test/test_files/test_1.txt b/test/test_files/test_1.txt index ba0004c..50b2a23 100644 --- a/test/test_files/test_1.txt +++ b/test/test_files/test_1.txt @@ -1,3 +1,4 @@ false false, < +goto_prev diff --git a/test/toggle_spec.lua b/test/toggle_spec.lua index 94c4041..ba1a413 100644 --- a/test/toggle_spec.lua +++ b/test/toggle_spec.lua @@ -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('') == 'true') + toggle.toggle() + assert(vim.fn.expand('') == '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('') == 'true') + toggle.toggle() + assert(vim.fn.expand('') == '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('') == 'goto_next') + toggle.toggle() + assert(vim.fn.expand('') == 'goto_prev') end) end) end)