From 5cfa03f2bdf312213b69cae329645f48da904ea1 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Mon, 20 Jul 2020 23:56:32 +0200 Subject: Textobjects: add swap feature --- lua/nvim-treesitter/textobjects.lua | 120 +++++++++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 3 deletions(-) (limited to 'lua/nvim-treesitter/textobjects.lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua index 7227b9e0..9c312f44 100644 --- a/lua/nvim-treesitter/textobjects.lua +++ b/lua/nvim-treesitter/textobjects.lua @@ -8,7 +8,7 @@ local ts_utils = require'nvim-treesitter.ts_utils' local M = {} -function M.select_textobject(query_string) +local function get_textobject_at_point(query_string) local bufnr = vim.api.nvim_get_current_buf() local lang = parsers.get_buf_lang(bufnr) if not lang then return end @@ -66,11 +66,93 @@ function M.select_textobject(query_string) if smallest_range.start then local start_range = {smallest_range.start.node:range()} local node_range = {smallest_range.node:range()} - ts_utils.update_selection(bufnr, {start_range[1], start_range[2], node_range[3], node_range[4]}) + return bufnr, {start_range[1], start_range[2], node_range[3], node_range[4]}, smallest_range.node else - ts_utils.update_selection(bufnr, smallest_range.node) + return bufnr, {smallest_range.node:range()}, smallest_range.node + end + end +end + +function M.select_textobject(query_string) + local bufnr, textobject = get_textobject_at_point(query_string) + if textobject then + ts_utils.update_selection(bufnr, textobject) + end +end + +local function swap_textobject(query_string, direction) + local bufnr, textobject_range, node = get_textobject_at_point(query_string) + local step = direction > 0 and 1 or -1 + if not node then return end + for _ = 1, math.abs(direction), step do + if direction > 0 then + ts_utils.swap_nodes(textobject_range, M.next_textobject(node, query_string, true, bufnr), bufnr, "yes, set cursor!") + else + ts_utils.swap_nodes(textobject_range, M.previous_textobject(node, query_string, true, bufnr), bufnr, "yes, set cursor!") + end + end +end + +function M.swap_textobject_next(query_string) + swap_textobject(query_string, 1) +end + +function M.swap_textobject_previous(query_string) + swap_textobject(query_string, -1) +end + +function M.next_textobject(node, query_string, same_parent, bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + + local matches = queries.get_capture_matches(bufnr, query_string, 'textobjects') + local _, _ , node_end = node:end_() + local next_node + local next_node_start + + for _, m in pairs(matches) do + local _, _, other_end = m.node:start() + if other_end > node_end then + if not same_parent or node:parent() == m.node:parent() then + if not next_node then + next_node = m + _, _, next_node_start = next_node.node:start() + end + if other_end < next_node_start then + next_node = m + _, _, next_node_start = next_node.node:start() + end + end end end + + return next_node and next_node.node +end + +function M.previous_textobject(node, query_string, same_parent, bufnr) + local bufnr = bufnr or api.nvim_get_current_buf() + + local matches = queries.get_capture_matches(bufnr, query_string, 'textobjects') + local _, _ , node_start = node:start() + local previous_node + local previous_node_end + + for _, m in pairs(matches) do + local _, _, other_end = m.node:end_() + if other_end < node_start then + if not same_parent or node:parent() == m.node:parent() then + if not previous_node then + previous_node = m + _, _, previous_node_end = previous_node.node:end_() + end + if other_end > previous_node_end then + previous_node = m + _, _, previous_node_end = previous_node.node:end_() + end + end + end + end + + return previous_node and previous_node.node end function M.attach(bufnr, lang) @@ -90,6 +172,28 @@ function M.attach(bufnr, lang) api.nvim_buf_set_keymap(buf, "v", mapping, cmd, {silent = true, noremap = true }) end end + for mapping, query in pairs(config.swap_next_keymaps) do + if type(query) == 'table' then + query = query[lang] + elseif not queries.get_query(lang, 'textobjects') then + query = nil + end + if query then + local cmd = ":lua require'nvim-treesitter.textobjects'.swap_textobject_next('"..query.."')" + api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true}) + end + end + for mapping, query in pairs(config.swap_previous_keymaps) do + if type(query) == 'table' then + query = query[lang] + elseif not queries.get_query(lang, 'textobjects') then + query = nil + end + if query then + local cmd = ":lua require'nvim-treesitter.textobjects'.swap_textobject_previous('"..query.."')" + api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true}) + end + end end function M.detach(bufnr) @@ -108,6 +212,16 @@ function M.detach(bufnr) api.nvim_buf_del_keymap(buf, "v", mapping) end end + for mapping, query in pairs(config.swap_next_keymaps) or pairs(config.swap_previous_keymaps) do + if type(query) == 'table' then + query = query[lang] + elseif not queries.get_query(lang, 'textobjects') then + query = nil + end + if query then + api.nvim_buf_del_keymap(buf, "n", mapping) + end + end end return M -- cgit v1.2.3 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/nvim-treesitter/textobjects.lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua index 9c312f44..56241e7f 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 From e629efafd8f529ff9b1297b947b4438bf4d2265c Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 2 Aug 2020 19:35:52 +0200 Subject: Textobjects: provide mappings for all swap/goto functions --- lua/nvim-treesitter/textobjects.lua | 110 ++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 50 deletions(-) (limited to 'lua/nvim-treesitter/textobjects.lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua index 56241e7f..af2c1fff 100644 --- a/lua/nvim-treesitter/textobjects.lua +++ b/lua/nvim-treesitter/textobjects.lua @@ -8,7 +8,7 @@ local ts_utils = require'nvim-treesitter.ts_utils' local M = {} -local function get_textobject_at_point(query_string) +local function textobject_at_point(query_string) local bufnr = vim.api.nvim_get_current_buf() local lang = parsers.get_buf_lang(bufnr) if not lang then return end @@ -74,59 +74,69 @@ local function get_textobject_at_point(query_string) end function M.select_textobject(query_string) - local bufnr, textobject = get_textobject_at_point(query_string) + local bufnr, textobject = textobject_at_point(query_string) if textobject then ts_utils.update_selection(bufnr, textobject) end end local function swap_textobject(query_string, direction) - local bufnr, textobject_range, node = get_textobject_at_point(query_string) + local bufnr, textobject_range, node = textobject_at_point(query_string) local step = direction > 0 and 1 or -1 if not node then return end for _ = 1, math.abs(direction), step do if direction > 0 then - ts_utils.swap_nodes(textobject_range, M.next_textobject(node, query_string, true, bufnr), bufnr, "yes, set cursor!") + ts_utils.swap_nodes(textobject_range, + M.next_textobject(node, query_string, true, bufnr), + bufnr, + "yes, set cursor!") else - ts_utils.swap_nodes(textobject_range, M.previous_textobject(node, query_string, true, bufnr), bufnr, "yes, set cursor!") + ts_utils.swap_nodes(textobject_range, + M.previous_textobject(node, query_string, true, bufnr), + bufnr, + "yes, set cursor!") end end end -function M.swap_textobject_next(query_string) +function M.swap_next(query_string) swap_textobject(query_string, 1) end -function M.swap_textobject_previous(query_string) +function M.swap_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 +function M.goto_adjacent(query_string, forward, start, same_parent) + local bufnr, _, node = textobject_at_point(query_string) + local adjacent_textobject if forward then - ajacent_textobject = M.next_textobject(node, query_string, same_parent, bufnr) + adjacent_textobject = M.next_textobject(node, query_string, same_parent, bufnr) else - ajacent_textobject = M.previous_textobject(node, query_string, same_parent, bufnr) + adjacent_textobject = M.previous_textobject(node, query_string, same_parent, bufnr) end - if ajacent_textobject then - local adjacent_textobject_range = {ajacent_textobject:range()} + if adjacent_textobject then + local adjacent_textobject_range = {adjacent_textobject:range()} if start then - api.nvim_win_set_cursor(api.nvim_get_current_win(), { adjacent_textobject_range[1] + 1, adjacent_textobject_range[2] }) + 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] }) + 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 +-- luacheck: push ignore 631 +M.goto_next_start = function(query_string) M.goto_adjacent(query_string, 'forward', 'start', false) end +M.goto_next_end = function(query_string) M.goto_adjacent(query_string, 'forward', false, false) end +M.goto_previous_start = function(query_string) M.goto_adjacent(query_string, false, 'start', false) end +M.goto_previous_end = function(query_string) M.goto_adjacent(query_string, false, false, false) end +-- luacheck: pop -function M.goto_next_textobject_end(query_string) - local bufnr, _, node = get_textobject_at_point(query_string) +function M.goto_next_end(query_string) + local bufnr, _, node = 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() @@ -189,6 +199,13 @@ function M.previous_textobject(node, query_string, same_parent, bufnr) return previous_node and previous_node.node end +local normal_mode_functions = { "swap_next", + "swap_previous", + "goto_next_start", + "goto_next_end", + "goto_previous_start", + "goto_previous_end"} + function M.attach(bufnr, lang) local buf = bufnr or api.nvim_get_current_buf() local config = configs.get_module("textobjects") @@ -206,26 +223,17 @@ function M.attach(bufnr, lang) api.nvim_buf_set_keymap(buf, "v", mapping, cmd, {silent = true, noremap = true }) end end - for mapping, query in pairs(config.swap_next_keymaps) do - if type(query) == 'table' then - query = query[lang] - elseif not queries.get_query(lang, 'textobjects') then - query = nil - end - if query then - local cmd = ":lua require'nvim-treesitter.textobjects'.swap_textobject_next('"..query.."')" - api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true}) - end - end - for mapping, query in pairs(config.swap_previous_keymaps) do - if type(query) == 'table' then - query = query[lang] - elseif not queries.get_query(lang, 'textobjects') then - query = nil - end - if query then - local cmd = ":lua require'nvim-treesitter.textobjects'.swap_textobject_previous('"..query.."')" - api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true}) + for _, function_call in pairs(normal_mode_functions) do + for mapping, query in pairs(config[function_call] or {}) do + if type(query) == 'table' then + query = query[lang] + elseif not queries.get_query(lang, 'textobjects') then + query = nil + end + if query then + local cmd = ":lua require'nvim-treesitter.textobjects'."..function_call.."('"..query.."')" + api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true, noremap = true }) + end end end end @@ -246,14 +254,16 @@ function M.detach(bufnr) api.nvim_buf_del_keymap(buf, "v", mapping) end end - for mapping, query in pairs(config.swap_next_keymaps) or pairs(config.swap_previous_keymaps) do - if type(query) == 'table' then - query = query[lang] - elseif not queries.get_query(lang, 'textobjects') then - query = nil - end - if query then - api.nvim_buf_del_keymap(buf, "n", mapping) + for _, function_call in pairs(normal_mode_functions) do + for mapping, query in pairs(config[function_call] or {}) do + if type(query) == 'table' then + query = query[lang] + elseif not queries.get_query(lang, 'textobjects') then + query = nil + end + if query then + api.nvim_buf_del_keymap(buf, "n", mapping) + end end end end -- cgit v1.2.3 From f6681c230f0eb8be0f3f9375b2da205681ea1fc9 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Mon, 3 Aug 2020 00:25:12 +0200 Subject: chore(textobject): use query.find_best_match to find next/previous textobject --- lua/nvim-treesitter/textobjects.lua | 70 ++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 40 deletions(-) (limited to 'lua/nvim-treesitter/textobjects.lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua index af2c1fff..0646a8ef 100644 --- a/lua/nvim-treesitter/textobjects.lua +++ b/lua/nvim-treesitter/textobjects.lua @@ -145,56 +145,46 @@ end function M.next_textobject(node, query_string, same_parent, bufnr) local node = node or ts_utils.get_node_at_cursor() + if not node then return end + local _, _, node_end = node:end_() local bufnr = bufnr or api.nvim_get_current_buf() - local matches = queries.get_capture_matches(bufnr, query_string, 'textobjects') - local _, _ , node_end = node:end_() - local next_node - local next_node_start - - for _, m in pairs(matches) do - local _, _, other_end = m.node:start() - if other_end > node_end then - if not same_parent or node:parent() == m.node:parent() then - if not next_node then - next_node = m - _, _, next_node_start = next_node.node:start() - end - if other_end < next_node_start then - next_node = m - _, _, next_node_start = next_node.node:start() - end - end - end - end + local next_node = queries.find_best_match(bufnr, + query_string, + 'textobjects', + function(match) + if not same_parent or node:parent() == match.node:parent() then + local _, _, start = match.node:start() + return start > node_end + end + end, + function(match) + local _, _, node_start = match.node:start() + return -node_start + end) return next_node and next_node.node end function M.previous_textobject(node, query_string, same_parent, bufnr) local node = node or ts_utils.get_node_at_cursor() + if not node then return end + local _, _, node_start = node:start() local bufnr = bufnr or api.nvim_get_current_buf() - local matches = queries.get_capture_matches(bufnr, query_string, 'textobjects') - local _, _ , node_start = node:start() - local previous_node - local previous_node_end - - for _, m in pairs(matches) do - local _, _, other_end = m.node:end_() - if other_end < node_start then - if not same_parent or node:parent() == m.node:parent() then - if not previous_node then - previous_node = m - _, _, previous_node_end = previous_node.node:end_() - end - if other_end > previous_node_end then - previous_node = m - _, _, previous_node_end = previous_node.node:end_() - end - end - end - end + local previous_node = queries.find_best_match(bufnr, + query_string, + 'textobjects', + function(match) + if not same_parent or node:parent() == match.node:parent() then + local _, _, end_ = match.node:end_() + return end_ < node_start + end + end, + function(match) + local _, _, node_end = match.node:end_() + return node_end + end) return previous_node and previous_node.node end -- cgit v1.2.3 From be2cfc1bca7194de4eb9dcdf9d3867be12c8b767 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Tue, 4 Aug 2020 19:11:59 +0200 Subject: Textobjects: Allow nested textobjects on goto_adjacent --- lua/nvim-treesitter/textobjects.lua | 60 ++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 24 deletions(-) (limited to 'lua/nvim-treesitter/textobjects.lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua index 0646a8ef..4ea9c4bb 100644 --- a/lua/nvim-treesitter/textobjects.lua +++ b/lua/nvim-treesitter/textobjects.lua @@ -84,15 +84,17 @@ local function swap_textobject(query_string, direction) local bufnr, textobject_range, node = textobject_at_point(query_string) local step = direction > 0 and 1 or -1 if not node then return end + local overlapping_range_ok = false + local same_parent = true for _ = 1, math.abs(direction), step do if direction > 0 then ts_utils.swap_nodes(textobject_range, - M.next_textobject(node, query_string, true, bufnr), + M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr), bufnr, "yes, set cursor!") else ts_utils.swap_nodes(textobject_range, - M.previous_textobject(node, query_string, true, bufnr), + M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr), bufnr, "yes, set cursor!") end @@ -107,13 +109,13 @@ function M.swap_previous(query_string) swap_textobject(query_string, -1) end -function M.goto_adjacent(query_string, forward, start, same_parent) +function M.goto_adjacent(query_string, forward, start, same_parent, overlapping_range_ok) local bufnr, _, node = textobject_at_point(query_string) local adjacent_textobject if forward then - adjacent_textobject = M.next_textobject(node, query_string, same_parent, bufnr) + adjacent_textobject = M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) else - adjacent_textobject = M.previous_textobject(node, query_string, same_parent, bufnr) + adjacent_textobject = M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) end if adjacent_textobject then @@ -129,33 +131,35 @@ function M.goto_adjacent(query_string, forward, start, same_parent) end -- luacheck: push ignore 631 -M.goto_next_start = function(query_string) M.goto_adjacent(query_string, 'forward', 'start', false) end -M.goto_next_end = function(query_string) M.goto_adjacent(query_string, 'forward', false, false) end -M.goto_previous_start = function(query_string) M.goto_adjacent(query_string, false, 'start', false) end -M.goto_previous_end = function(query_string) M.goto_adjacent(query_string, false, false, false) end +M.goto_next_start = function(query_string) M.goto_adjacent(query_string, 'forward', 'start', not 'same_parent', 'overlap ok') end +M.goto_next_end = function(query_string) M.goto_adjacent(query_string, 'forward', not 'start', not 'same_parent', 'overlap ok') end +M.goto_previous_start = function(query_string) M.goto_adjacent(query_string, not 'forward', 'start', not 'same_parent', 'overlap ok') end +M.goto_previous_end = function(query_string) M.goto_adjacent(query_string, not 'forward', not 'start', not 'same_parent', 'overlap ok') end -- luacheck: pop -function M.goto_next_end(query_string) - local bufnr, _, node = 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) +function M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) local node = node or ts_utils.get_node_at_cursor() + local bufnr = bufnr or api.nvim_get_current_buf() if not node then return end + local _, _, node_end = node:end_() - local bufnr = bufnr or api.nvim_get_current_buf() + local search_start, _ + if overlapping_range_ok then + _, _, search_start = node:start() + search_start = search_start - 1 + else + _, _, search_start = node:end_() + end local next_node = queries.find_best_match(bufnr, query_string, 'textobjects', function(match) + if match.node == node then return end if not same_parent or node:parent() == match.node:parent() then local _, _, start = match.node:start() - return start > node_end + local _, _, end_ = match.node:end_() + return start > search_start and end_ > node_end end end, function(match) @@ -166,19 +170,27 @@ function M.next_textobject(node, query_string, same_parent, bufnr) return next_node and next_node.node end -function M.previous_textobject(node, query_string, same_parent, bufnr) +function M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) local node = node or ts_utils.get_node_at_cursor() - if not node then return end - local _, _, node_start = node:start() local bufnr = bufnr or api.nvim_get_current_buf() + if not node then return end + local _, _, node_start = node:start() + local search_end, _ + if overlapping_range_ok then + _, _, search_end = node:end_() + search_end = search_end + 1 + else + _, _, search_end = node:start() + end local previous_node = queries.find_best_match(bufnr, query_string, 'textobjects', function(match) if not same_parent or node:parent() == match.node:parent() then local _, _, end_ = match.node:end_() - return end_ < node_start + local _, _, start = match.node:start() + return end_ < search_end and start < node_start end end, function(match) -- cgit v1.2.3 From f3ed370643dee45a3ea253a780cd30e74361d2bd Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Wed, 5 Aug 2020 18:11:01 +0200 Subject: Textobject goto: treat end differently that start --- lua/nvim-treesitter/textobjects.lua | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'lua/nvim-treesitter/textobjects.lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua index 4ea9c4bb..1220aced 100644 --- a/lua/nvim-treesitter/textobjects.lua +++ b/lua/nvim-treesitter/textobjects.lua @@ -87,17 +87,17 @@ local function swap_textobject(query_string, direction) local overlapping_range_ok = false local same_parent = true for _ = 1, math.abs(direction), step do - if direction > 0 then - ts_utils.swap_nodes(textobject_range, - M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr), - bufnr, - "yes, set cursor!") - else - ts_utils.swap_nodes(textobject_range, - M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr), - bufnr, - "yes, set cursor!") - end + if direction > 0 then + ts_utils.swap_nodes(textobject_range, + M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr), + bufnr, + "yes, set cursor!") + else + ts_utils.swap_nodes(textobject_range, + M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr), + bufnr, + "yes, set cursor!") + end end end @@ -146,7 +146,6 @@ function M.next_textobject(node, query_string, same_parent, overlapping_range_ok local search_start, _ if overlapping_range_ok then _, _, search_start = node:start() - search_start = search_start - 1 else _, _, search_start = node:end_() end @@ -159,7 +158,7 @@ function M.next_textobject(node, query_string, same_parent, overlapping_range_ok if not same_parent or node:parent() == match.node:parent() then local _, _, start = match.node:start() local _, _, end_ = match.node:end_() - return start > search_start and end_ > node_end + return start > search_start and end_ >= node_end end end, function(match) -- cgit v1.2.3 From 32271b26ef64433b2d9cc41392594db614449a4c Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Thu, 6 Aug 2020 09:01:38 +0200 Subject: Textobjects: set jump before going to adjacent_textobject --- lua/nvim-treesitter/textobjects.lua | 164 ++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 84 deletions(-) (limited to 'lua/nvim-treesitter/textobjects.lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua index 1220aced..95b46cab 100644 --- a/lua/nvim-treesitter/textobjects.lua +++ b/lua/nvim-treesitter/textobjects.lua @@ -5,6 +5,7 @@ local configs = require "nvim-treesitter.configs" local parsers = require "nvim-treesitter.parsers" local queries = require'nvim-treesitter.query' local ts_utils = require'nvim-treesitter.ts_utils' +local utils = require'nvim-treesitter.utils' local M = {} @@ -80,63 +81,11 @@ function M.select_textobject(query_string) end end -local function swap_textobject(query_string, direction) - local bufnr, textobject_range, node = textobject_at_point(query_string) - local step = direction > 0 and 1 or -1 - if not node then return end - local overlapping_range_ok = false - local same_parent = true - for _ = 1, math.abs(direction), step do - if direction > 0 then - ts_utils.swap_nodes(textobject_range, - M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr), - bufnr, - "yes, set cursor!") - else - ts_utils.swap_nodes(textobject_range, - M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr), - bufnr, - "yes, set cursor!") - end - end -end - -function M.swap_next(query_string) - swap_textobject(query_string, 1) +local function get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr) + local fn = forward and M.next_textobject or M.previous_textobject + return fn(node, query_string, same_parent, overlapping_range_ok, bufnr) end -function M.swap_previous(query_string) - swap_textobject(query_string, -1) -end - -function M.goto_adjacent(query_string, forward, start, same_parent, overlapping_range_ok) - local bufnr, _, node = textobject_at_point(query_string) - local adjacent_textobject - if forward then - adjacent_textobject = M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) - else - adjacent_textobject = M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) - end - - if adjacent_textobject then - local adjacent_textobject_range = {adjacent_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 - --- luacheck: push ignore 631 -M.goto_next_start = function(query_string) M.goto_adjacent(query_string, 'forward', 'start', not 'same_parent', 'overlap ok') end -M.goto_next_end = function(query_string) M.goto_adjacent(query_string, 'forward', not 'start', not 'same_parent', 'overlap ok') end -M.goto_previous_start = function(query_string) M.goto_adjacent(query_string, not 'forward', 'start', not 'same_parent', 'overlap ok') end -M.goto_previous_end = function(query_string) M.goto_adjacent(query_string, not 'forward', not 'start', not 'same_parent', 'overlap ok') end --- luacheck: pop - function M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) local node = node or ts_utils.get_node_at_cursor() local bufnr = bufnr or api.nvim_get_current_buf() @@ -149,22 +98,20 @@ function M.next_textobject(node, query_string, same_parent, overlapping_range_ok else _, _, search_start = node:end_() end + local function scoring_function(match) + if match.node == node then return end + if not same_parent or node:parent() == match.node:parent() then + local _, _, start = match.node:start() + local _, _, end_ = match.node:end_() + return start > search_start and end_ >= node_end + end + end + local function filter_function(match) + local _, _, node_start = match.node:start() + return -node_start + end - local next_node = queries.find_best_match(bufnr, - query_string, - 'textobjects', - function(match) - if match.node == node then return end - if not same_parent or node:parent() == match.node:parent() then - local _, _, start = match.node:start() - local _, _, end_ = match.node:end_() - return start > search_start and end_ >= node_end - end - end, - function(match) - local _, _, node_start = match.node:start() - return -node_start - end) + local next_node = queries.find_best_match(bufnr, query_string, 'textobjects', scoring_function, filter_function) return next_node and next_node.node end @@ -182,24 +129,73 @@ function M.previous_textobject(node, query_string, same_parent, overlapping_rang else _, _, search_end = node:start() end - local previous_node = queries.find_best_match(bufnr, - query_string, - 'textobjects', - function(match) - if not same_parent or node:parent() == match.node:parent() then - local _, _, end_ = match.node:end_() - local _, _, start = match.node:start() - return end_ < search_end and start < node_start - end - end, - function(match) - local _, _, node_end = match.node:end_() - return node_end - end) + + local function scoring_function(match) + if not same_parent or node:parent() == match.node:parent() then + local _, _, end_ = match.node:end_() + local _, _, start = match.node:start() + return end_ < search_end and start < node_start + end + end + + local function filter_function(match) + local _, _, node_end = match.node:end_() + return node_end + end + + local previous_node = queries.find_best_match(bufnr, query_string, 'textobjects', scoring_function, filter_function) return previous_node and previous_node.node end +function M.goto_adjacent(query_string, forward, start, same_parent, overlapping_range_ok) + local bufnr, _, node = textobject_at_point(query_string) + local adjacent_textobject = get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr) + + if adjacent_textobject then + utils.set_jump() + + local adjacent_textobject_range = {adjacent_textobject:range()} + local position + if start then + position = { adjacent_textobject_range[1] + 1, adjacent_textobject_range[2] } + else + position = { adjacent_textobject_range[3] + 1, adjacent_textobject_range[4] } + end + api.nvim_win_set_cursor(api.nvim_get_current_win(), position) + end +end + +local function swap_textobject(query_string, direction) + local bufnr, textobject_range, node = textobject_at_point(query_string) + if not node then return end + + local step = direction > 0 and 1 or -1 + local overlapping_range_ok = false + local same_parent = true + for _ = 1, math.abs(direction), step do + local forward = direction > 0 + local adjacent_textobject = get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr) + ts_utils.swap_nodes(textobject_range, adjacent_textobject, bufnr, "yes, set cursor!") + end +end + +function M.swap_next(query_string) + swap_textobject(query_string, 1) +end + +function M.swap_previous(query_string) + swap_textobject(query_string, -1) +end + + +-- luacheck: push ignore 631 +M.goto_next_start = function(query_string) M.goto_adjacent(query_string, 'forward', 'start', not 'same_parent', 'overlap ok') end +M.goto_next_end = function(query_string) M.goto_adjacent(query_string, 'forward', not 'start', not 'same_parent', 'overlap ok') end +M.goto_previous_start = function(query_string) M.goto_adjacent(query_string, not 'forward', 'start', not 'same_parent', 'overlap ok') end +M.goto_previous_end = function(query_string) M.goto_adjacent(query_string, not 'forward', not 'start', not 'same_parent', 'overlap ok') end +-- luacheck: pop + local normal_mode_functions = { "swap_next", "swap_previous", "goto_next_start", -- cgit v1.2.3 From 52168114594d791a3ae6092ab2489758da7b3ae8 Mon Sep 17 00:00:00 2001 From: Stephan Seitz Date: Sun, 16 Aug 2020 14:50:54 +0200 Subject: chore(textobjects): split up into submodules --- lua/nvim-treesitter/textobjects.lua | 268 ------------------------------------ 1 file changed, 268 deletions(-) delete mode 100644 lua/nvim-treesitter/textobjects.lua (limited to 'lua/nvim-treesitter/textobjects.lua') diff --git a/lua/nvim-treesitter/textobjects.lua b/lua/nvim-treesitter/textobjects.lua deleted file mode 100644 index 95b46cab..00000000 --- a/lua/nvim-treesitter/textobjects.lua +++ /dev/null @@ -1,268 +0,0 @@ -local api = vim.api -local ts = vim.treesitter - -local configs = require "nvim-treesitter.configs" -local parsers = require "nvim-treesitter.parsers" -local queries = require'nvim-treesitter.query' -local ts_utils = require'nvim-treesitter.ts_utils' -local utils = require'nvim-treesitter.utils' - -local M = {} - -local function textobject_at_point(query_string) - local bufnr = vim.api.nvim_get_current_buf() - local lang = parsers.get_buf_lang(bufnr) - if not lang then return end - - local row, col = unpack(vim.api.nvim_win_get_cursor(0)) - row = row - 1 - - local matches = {} - - if string.match(query_string, '^@.*') then - matches = queries.get_capture_matches(bufnr, query_string, 'textobjects') - else - local parser = parsers.get_parser(bufnr, lang) - local root = parser:parse():root() - - local start_row, _, end_row, _ = root:range() - - local query = ts.parse_query(lang, query_string) - for m in queries.iter_prepared_matches(query, root, bufnr, start_row, end_row) do - for _, n in pairs(m) do - if n.node then - table.insert(matches, n) - end - end - end - end - - local match_length - local smallest_range - local earliest_start - - for _, m in pairs(matches) do - if m.node and ts_utils.is_in_node_range(m.node, row, col) then - local length = ts_utils.node_length(m.node) - if not match_length or length < match_length then - smallest_range = m - match_length = length - end - -- for nodes with same length take the one with earliest start - if match_length and length == smallest_range then - local start = m.start - if start then - local _, _, start_byte = m.start.node:start() - if not earliest_start or start_byte < earliest_start then - smallest_range = m - match_length = length - earliest_start = start_byte - end - end - end - end - end - - if smallest_range then - if smallest_range.start then - local start_range = {smallest_range.start.node:range()} - local node_range = {smallest_range.node:range()} - return bufnr, {start_range[1], start_range[2], node_range[3], node_range[4]}, smallest_range.node - else - return bufnr, {smallest_range.node:range()}, smallest_range.node - end - end -end - -function M.select_textobject(query_string) - local bufnr, textobject = textobject_at_point(query_string) - if textobject then - ts_utils.update_selection(bufnr, textobject) - end -end - -local function get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr) - local fn = forward and M.next_textobject or M.previous_textobject - return fn(node, query_string, same_parent, overlapping_range_ok, bufnr) -end - -function M.next_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) - local node = node or ts_utils.get_node_at_cursor() - local bufnr = bufnr or api.nvim_get_current_buf() - if not node then return end - - local _, _, node_end = node:end_() - local search_start, _ - if overlapping_range_ok then - _, _, search_start = node:start() - else - _, _, search_start = node:end_() - end - local function scoring_function(match) - if match.node == node then return end - if not same_parent or node:parent() == match.node:parent() then - local _, _, start = match.node:start() - local _, _, end_ = match.node:end_() - return start > search_start and end_ >= node_end - end - end - local function filter_function(match) - local _, _, node_start = match.node:start() - return -node_start - end - - local next_node = queries.find_best_match(bufnr, query_string, 'textobjects', scoring_function, filter_function) - - return next_node and next_node.node -end - -function M.previous_textobject(node, query_string, same_parent, overlapping_range_ok, bufnr) - local node = node or ts_utils.get_node_at_cursor() - local bufnr = bufnr or api.nvim_get_current_buf() - if not node then return end - - local _, _, node_start = node:start() - local search_end, _ - if overlapping_range_ok then - _, _, search_end = node:end_() - search_end = search_end + 1 - else - _, _, search_end = node:start() - end - - local function scoring_function(match) - if not same_parent or node:parent() == match.node:parent() then - local _, _, end_ = match.node:end_() - local _, _, start = match.node:start() - return end_ < search_end and start < node_start - end - end - - local function filter_function(match) - local _, _, node_end = match.node:end_() - return node_end - end - - local previous_node = queries.find_best_match(bufnr, query_string, 'textobjects', scoring_function, filter_function) - - return previous_node and previous_node.node -end - -function M.goto_adjacent(query_string, forward, start, same_parent, overlapping_range_ok) - local bufnr, _, node = textobject_at_point(query_string) - local adjacent_textobject = get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr) - - if adjacent_textobject then - utils.set_jump() - - local adjacent_textobject_range = {adjacent_textobject:range()} - local position - if start then - position = { adjacent_textobject_range[1] + 1, adjacent_textobject_range[2] } - else - position = { adjacent_textobject_range[3] + 1, adjacent_textobject_range[4] } - end - api.nvim_win_set_cursor(api.nvim_get_current_win(), position) - end -end - -local function swap_textobject(query_string, direction) - local bufnr, textobject_range, node = textobject_at_point(query_string) - if not node then return end - - local step = direction > 0 and 1 or -1 - local overlapping_range_ok = false - local same_parent = true - for _ = 1, math.abs(direction), step do - local forward = direction > 0 - local adjacent_textobject = get_adjacent(forward, node, query_string, same_parent, overlapping_range_ok, bufnr) - ts_utils.swap_nodes(textobject_range, adjacent_textobject, bufnr, "yes, set cursor!") - end -end - -function M.swap_next(query_string) - swap_textobject(query_string, 1) -end - -function M.swap_previous(query_string) - swap_textobject(query_string, -1) -end - - --- luacheck: push ignore 631 -M.goto_next_start = function(query_string) M.goto_adjacent(query_string, 'forward', 'start', not 'same_parent', 'overlap ok') end -M.goto_next_end = function(query_string) M.goto_adjacent(query_string, 'forward', not 'start', not 'same_parent', 'overlap ok') end -M.goto_previous_start = function(query_string) M.goto_adjacent(query_string, not 'forward', 'start', not 'same_parent', 'overlap ok') end -M.goto_previous_end = function(query_string) M.goto_adjacent(query_string, not 'forward', not 'start', not 'same_parent', 'overlap ok') end --- luacheck: pop - -local normal_mode_functions = { "swap_next", - "swap_previous", - "goto_next_start", - "goto_next_end", - "goto_previous_start", - "goto_previous_end"} - -function M.attach(bufnr, lang) - local buf = bufnr or api.nvim_get_current_buf() - local config = configs.get_module("textobjects") - local lang = lang or parsers.get_buf_lang(buf) - - for mapping, query in pairs(config.keymaps) do - if type(query) == 'table' then - query = query[lang] - elseif not queries.get_query(lang, 'textobjects') then - query = nil - end - if query then - local cmd = ":lua require'nvim-treesitter.textobjects'.select_textobject('"..query.."')" - api.nvim_buf_set_keymap(buf, "o", mapping, cmd, {silent = true, noremap = true }) - api.nvim_buf_set_keymap(buf, "v", mapping, cmd, {silent = true, noremap = true }) - end - end - for _, function_call in pairs(normal_mode_functions) do - for mapping, query in pairs(config[function_call] or {}) do - if type(query) == 'table' then - query = query[lang] - elseif not queries.get_query(lang, 'textobjects') then - query = nil - end - if query then - local cmd = ":lua require'nvim-treesitter.textobjects'."..function_call.."('"..query.."')" - api.nvim_buf_set_keymap(buf, "n", mapping, cmd, {silent = true, noremap = true }) - end - end - end -end - -function M.detach(bufnr) - local buf = bufnr or api.nvim_get_current_buf() - local config = configs.get_module("textobjects") - local lang = parsers.get_buf_lang(bufnr) - - for mapping, query in pairs(config.keymaps) do - if type(query) == 'table' then - query = query[lang] - elseif not queries.get_query(lang, 'textobjects') then - query = nil - end - if query then - api.nvim_buf_del_keymap(buf, "o", mapping) - api.nvim_buf_del_keymap(buf, "v", mapping) - end - end - for _, function_call in pairs(normal_mode_functions) do - for mapping, query in pairs(config[function_call] or {}) do - if type(query) == 'table' then - query = query[lang] - elseif not queries.get_query(lang, 'textobjects') then - query = nil - end - if query then - api.nvim_buf_del_keymap(buf, "n", mapping) - end - end - end -end - -return M -- cgit v1.2.3