nvim-config

Log | Files | Refs | Submodules | README

commit bed8cf641c8e5dc301207c473c5e0d6029ef5564
parent 8fba898f01359e0bc32b125885ab5d7348725078
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date:   Sun, 14 May 2023 11:47:07 +0200

feat: enhance latex node change

Diffstat:
Mafter/ftplugin/tex.lua | 30+++++++++++++++++++++++-------
1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua @@ -21,22 +21,38 @@ local p = require 'nvim-treesitter.parsers' local query = vim.treesitter.query.parse("latex", [[ (generic_environment (begin (curly_group_text text: (_) @envbegin)) - (end (curly_group_text text: (_) @envend))) + (end (curly_group_text text: (_) @envend))) @_root (math_environment (begin (curly_group_text text: (_) @envbegin)) - (end (curly_group_text text: (_) @envend))) + (end (curly_group_text text: (_) @envend))) @_root ]]) -vim.keymap.set('n', '<LocalLeader>re', function() - local curbuf = vim.api.nvim_get_current_buf() - local curnode = ts_utils.get_node_at_cursor() - while curnode and curnode:type() ~= "generic_environment" and curnode:type() ~= "math_environment" do - curnode = curnode:parent() +local function find_smallest_match(line, col, query, bufnr) + local root = ts_utils.get_root_for_position(line, col) + + local smallest + for id, node, _ in query:iter_captures(root, bufnr, line, line) do + if query.captures[id] == "_root" then + if not smallest or ts_utils.node_length(smallest) > ts_utils.node_length(node) then + smallest = node + end + end end + return smallest +end + +vim.keymap.set('n', '<LocalLeader>re', function() + local curbuf = vim.api.nvim_get_current_buf() + local curwin = vim.api.nvim_get_current_win() + local cursor = vim.api.nvim_win_get_cursor(curwin) + local curnode = find_smallest_match(cursor[1] - 1, cursor[2], query, curbuf) + if not curnode then return end + -- TODO: get the content of the original captured node + local start_row, _, end_row, _ = curnode:range() vim.ui.input({ prompt = "Replacement" }, function(replacement) if replacement then