commit f3a9a08e87deb1c96d1e2888ec0c405ddfcefd31
parent 1a6f5d1fab58323e4d26980fea4f3ed4f85e9b7a
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date: Fri, 26 May 2023 09:19:56 +0200
feat(tex): some rework for consistency
Diffstat:
1 file changed, 77 insertions(+), 55 deletions(-)
diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua
@@ -3,6 +3,7 @@ vim.opt_local.formatlistpat = "^\\s*\\\\item\\s*"
vim.opt_local.textwidth = 100
vim.opt_local.conceallevel = 0
vim.opt_local.foldmethod = "marker"
+vim.opt_local.spell = true
vim.opt_local.foldmarker = "%(,%)"
vim.opt_local.wrap = false
vim.opt_local.spellcapcheck = "\\(verb.\\+\\)\\@<![.?!]\\_[\\])'\"\t\n]\\+"
@@ -17,17 +18,6 @@ local edit = require "architext.edit"
local ts_utils = require 'nvim-treesitter.ts_utils'
local p = require 'nvim-treesitter.parsers'
--- Replace the surrounding environment
-local query = vim.treesitter.query.parse("latex", [[
- (generic_environment
- (begin (curly_group_text text: (_) @envbegin))
- (end (curly_group_text text: (_) @envend))) @_root
-
- (math_environment
- (begin (curly_group_text text: (_) @envbegin))
- (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
@@ -42,67 +32,99 @@ local function find_smallest_match(line, col, query, bufnr)
local smallest
local smallnode
local smalllen
- for _, match, _ in query:iter_matches(root, bufnr, line, line) do
+ for _, match, _ in query:iter_matches(root, bufnr, line, line + 1) 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
+ if vim.treesitter.node_contains(node, { line, col, line, col }) then
+ local thislen = ts_utils.node_length(node)
+ if not smallest or smalllen > thislen then
+ smallest = match
+ smalllen = thislen
+ smallnode = node
+ end
end
- break
end
end
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 match, node = find_smallest_match(cursor[1] - 1, cursor[2], query, curbuf)
+local function with_current_position(func)
+ return 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)
- if not match then
- print("Not in an environment")
- return
+ return func(curbuf, curwin, cursor)
end
+end
+
+do
+ -- Replace the surrounding environment
+ local query = vim.treesitter.query.parse("latex", [[
+ (generic_environment
+ (begin (curly_group_text text: (_) @envbegin))
+ (end (curly_group_text text: (_) @envend))) @_root
- local start_row, _, end_row, _ = node:range()
+ (math_environment
+ (begin (curly_group_text text: (_) @envbegin))
+ (end (curly_group_text text: (_) @envend))) @_root
+ ]])
- local default = vim.treesitter.get_node_text(index_by_name(query, match, "envbegin"), curbuf)
+ vim.keymap.set('n', '<LocalLeader>re', with_current_position(function(curbuf, _, cursor)
+ local match, node = find_smallest_match(cursor[1] - 1, cursor[2], query, curbuf)
- vim.ui.input({ prompt = "Replacement", default = default }, function(replacement)
- if replacement then
- edit.edit_match(curbuf, match, query, { envbegin = replacement, envend = replacement }, start_row, end_row)
+ if not match then
+ print("Not in an environment")
+ return
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)
+ local start_row, _, end_row, _ = node:range()
- if not match then
- print("Not in an environment")
- return
- end
+ 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_match(curbuf, match, query, { envbegin = replacement, envend = replacement }, start_row, end_row)
+ end
+ end)
+ end), { buffer = true })
- local start_row, _, end_row, _ = node:range()
+ vim.keymap.set('n', '<LocalLeader>se', with_current_position(function(curbuf, _, cursor)
+ local match, node = find_smallest_match(cursor[1] - 1, cursor[2], query, curbuf)
- local current = vim.treesitter.get_node_text(index_by_name(query, match, "envbegin"), curbuf)
+ if not match then
+ print("Not in an environment")
+ return
+ end
- if vim.endswith(current, "*") then
- current = current:sub(0, #current - 1)
- else
- current = current .. "*"
- 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 })
+ edit.edit_match(curbuf, match, query, { envbegin = current, envend = current }, start_row, end_row)
+ end), { buffer = true })
+end
+
+do
+ local query = vim.treesitter.query.parse("latex", [[
+ [
+ (inline_formula)
+ (displayed_equation)
+ ] @_root
+ ]])
+
+ vim.keymap.set({ 'o', 'v' }, 'am', with_current_position(function(curbuf, _, cursor)
+ local _, node = find_smallest_match(cursor[1] - 1, cursor[2], query, curbuf)
+ ts_utils.update_selection(curbuf, node)
+ end), { buffer = true })
+end
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
@@ -220,21 +242,21 @@ local function preffix_suffix_change(func)
end
-- Wrap the selection in a call
-vis_input_change('<LocalLeader>f', 'Function name', preffix_suffix_change(function(input)
+vis_input_change('<LocalLeader>f', 'Function name: ', preffix_suffix_change(function(input)
return string.format('\\%s{', input), '}'
end))
-- Wrap the selection in an inline environment
-vis_input_change('<LocalLeader>ie', 'Inline env name', preffix_suffix_change(function(input)
+vis_input_change('<LocalLeader>ie', 'Inline env name: ', preffix_suffix_change(function(input)
return '{\\' .. input, '}'
end))
-- Wrap the selection in a color block
-vis_input_change('<LocalLeader>c', 'Color name', preffix_suffix_change(function(input)
+vis_input_change('<LocalLeader>c', 'Color name: ', preffix_suffix_change(function(input)
return string.format('{\\color{%s}', input), '}'
end))
-- Wrap into an environment
-vis_input_change('<LocalLeader>e', 'Environment name', preffix_suffix_change(function(input)
+vis_input_change('<LocalLeader>e', 'Environment name: ', preffix_suffix_change(function(input)
return string.format('\\begin{%s}', input), string.format('\\end{%s}', input)
end))