From 1642e37499e0d58952cb6b5e3bedc9126cc43e4a Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 2 Aug 2020 18:47:01 +0200 Subject: Textobjects: Add goto_adjacent --- lua/nvim-treesitter/textobjects.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua index 9c312f44a..56241e7f3 100644 --- a/lua/nvim-treesitter/textobjects.lua +++ b/lua/nvim-treesitter/textobjects.lua @@ -101,7 +101,40 @@ function M.swap_textobject_previous(query_string) swap_textobject(query_string, -1) end +function M.goto_adjacent_textobejct(query_string, forward, start, same_parent) + local bufnr, _, node = get_textobject_at_point(query_string) + local ajacent_textobject + if forward then + ajacent_textobject = M.next_textobject(node, query_string, same_parent, bufnr) + else + ajacent_textobject = M.previous_textobject(node, query_string, same_parent, bufnr) + end + + if ajacent_textobject then + local adjacent_textobject_range = {ajacent_textobject:range()} + if start then + api.nvim_win_set_cursor(api.nvim_get_current_win(), { adjacent_textobject_range[1] + 1, adjacent_textobject_range[2] }) + else + api.nvim_win_set_cursor(api.nvim_get_current_win(), { adjacent_textobject_range[3] + 1, adjacent_textobject_range[4] }) + end + end +end + +M.goto_next_textobject_start = function(query_string) M.goto_adjacent_textobejct(query_string, 'forward', 'start', false) end +M.goto_next_textobject_end = function(query_string) M.goto_adjacent_textobejct(query_string, 'forward', false, false) end +M.goto_previous_textobject_start = function(query_string) M.goto_adjacent_textobejct(query_string, false, 'start', false) end +M.goto_previous_textobject_end = function(query_string) M.goto_adjacent_textobejct(query_string, false, false, false) end + +function M.goto_next_textobject_end(query_string) + local bufnr, _, node = get_textobject_at_point(query_string) + if not node then return end + local next_textobject = M.next_textobject(node, query_string, false, bufnr) + local next_textobject_range = next_textobject:range() + api.nvim_win_set_cursor(api.nvim_get_current_win(), { next_textobject_range[3] + 1, next_textobject_range[4] + 1 }) +end + function M.next_textobject(node, query_string, same_parent, bufnr) + local node = node or ts_utils.get_node_at_cursor() local bufnr = bufnr or api.nvim_get_current_buf() local matches = queries.get_capture_matches(bufnr, query_string, 'textobjects') @@ -129,6 +162,7 @@ function M.next_textobject(node, query_string, same_parent, bufnr) end function M.previous_textobject(node, query_string, same_parent, bufnr) + local node = node or ts_utils.get_node_at_cursor() local bufnr = bufnr or api.nvim_get_current_buf() local matches = queries.get_capture_matches(bufnr, query_string, 'textobjects') -- cgit v1.2.3-70-g09d2