commit a88dd80e3a75301fb40123f9000a7ab02f699206
parent bed8cf641c8e5dc301207c473c5e0d6029ef5564
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date: Sun, 14 May 2023 16:00:00 +0200
feat: enhance latex
Diffstat:
1 file changed, 54 insertions(+), 11 deletions(-)
diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua
@@ -28,39 +28,82 @@ local query = vim.treesitter.query.parse("latex", [[
(end (curly_group_text text: (_) @envend))) @_root
]])
+local function index_by_name(query, match, key)
+ for id, node, _ in pairs(match) do
+ if query.captures[id] == key then
+ return node
+ end
+ end
+end
+
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
+ local smallnode
+ local smalllen
+ for _, match, _ in query:iter_matches(root, bufnr, line, line) do
+ local node = index_by_name(query, match, "_root")
+ if node then
+ local thislen = ts_utils.node_length(node)
+ if not smallest or smalllen > thislen then
+ smallest = match
+ smalllen = thislen
+ smallnode = node
end
+ break
end
end
- return smallest
+ return smallest, smallnode
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)
+ local match, node = find_smallest_match(cursor[1] - 1, cursor[2], query, curbuf)
- if not curnode then return end
+ if not match then
+ print("Not in an environment")
+ return
+ end
- -- TODO: get the content of the original captured node
+ local start_row, _, end_row, _ = node:range()
- local start_row, _, end_row, _ = curnode:range()
- vim.ui.input({ prompt = "Replacement" }, function(replacement)
+ local default = vim.treesitter.get_node_text(index_by_name(query, match, "envbegin"), curbuf)
+
+ vim.ui.input({ prompt = "Replacement", default = default }, function(replacement)
if replacement then
- edit.edit(curbuf, p.get_parser(curbuf), query, { envbegin = replacement, envend = replacement }, start_row, end_row)
+ edit.edit_match(curbuf, match, query, { envbegin = replacement, envend = replacement }, start_row, end_row)
end
end)
end, { buffer = true })
+vim.keymap.set('n', '<LocalLeader>se', 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 match, node = find_smallest_match(cursor[1] - 1, cursor[2], query, curbuf)
+
+ if not match then
+ print("Not in an environment")
+ return
+ end
+
+ local start_row, _, end_row, _ = node:range()
+
+ local current = vim.treesitter.get_node_text(index_by_name(query, match, "envbegin"), curbuf)
+
+ if vim.endswith(current, "*") then
+ current = current:sub(0, #current - 1)
+ else
+ current = current .. "*"
+ end
+
+ edit.edit_match(curbuf, match, query, { envbegin = current, envend = current }, start_row, end_row)
+end, { buffer = true })
+
local function buf_line_length(buf, row)
return vim.api.nvim_buf_get_offset(buf, row + 1) - vim.api.nvim_buf_get_offset(buf, row) - 1
end