nvim-config

Log | Files | Refs | Submodules | README

commit b37a007cf09e6908c3392d0a1746c1e97bd6bae4
parent 5f8866ff32f300ce2452c0b6d8faf7d44639ff71
Author: Thomas Vigouroux <thomas.vigouroux@protonmail.com>
Date:   Wed,  5 Oct 2022 11:51:49 +0200

tex: correctly indent when inserting linewise

Diffstat:
Mafter/ftplugin/tex.lua | 36++++++++++++++++++++++--------------
1 file changed, 22 insertions(+), 14 deletions(-)

diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua @@ -1,4 +1,4 @@ -vim.opt_local.formatoptions:append { t = true, c = true, n = true} +vim.opt_local.formatoptions:append { t = true, c = true, n = true } vim.opt_local.formatlistpat = "^\\s*\\\\item\\s*" vim.opt_local.textwidth = 100 vim.opt_local.conceallevel = 0 @@ -11,12 +11,12 @@ vim.opt_local.spellcapcheck = "\\(verb.\\+\\)\\@<![.?!]\\_[\\])'\"\t\n]\\+" vim.cmd [[normal zx]] if #vim.fs.find({ "Makefile", "makefile" }, { type = "file" }) == 0 then - vim.cmd[[compiler latexmk]] + vim.cmd [[compiler latexmk]] end -local edit = require"architext.edit" -local ts_utils = require'nvim-treesitter.ts_utils' -local p = require'nvim-treesitter.parsers' +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.parse_query("latex", [[ @@ -24,7 +24,7 @@ local query = vim.treesitter.parse_query("latex", [[ (begin (curly_group_text text: (_) @envbegin)) (end (curly_group_text text: (_) @envend))) ]]) -vim.keymap.set('n', 'ge', function() +vim.keymap.set('n', '<LocalLeader>re', function() local curbuf = vim.api.nvim_get_current_buf() local curnode = ts_utils.get_node_at_cursor() @@ -75,11 +75,13 @@ local function correct_position_wrap(ty, spos, epos, func) end local text = vim.api.nvim_buf_get_text(curbuf, start_line, start_col, end_line, end_col, {}) + local indent = vim.fn.indent(start[2]) - func(ty, curbuf, start_line, start_col, end_line, end_col, text) + -- TODO(vigoux): the indentation is wrong after inserting, maybe reformat the range too ? + func(ty, curbuf, start_line, start_col, end_line, end_col, text, indent) end -TexDispatch=function() end +TexDispatch = function() end local function vis_mapping(map, func) -- First handle the visual mapping @@ -120,19 +122,19 @@ local function vis_mapping(map, func) end local function vis_input_change(map, input_name, func) - vis_mapping(map, function(type, curbuf, start_line, start_col, end_line, end_col, text) + vis_mapping(map, function(type, curbuf, start_line, start_col, end_line, end_col, text, indent) vim.ui.input({ prompt = input_name }, function(input) if not input then return end -- Func must modify the text array - func(type, text, input) + func(type, text, input, indent) vim.api.nvim_buf_set_text(curbuf, start_line, start_col, end_line, end_col, text) end) end) end local function preffix_suffix_change(func) - return function(type, text, input) + return function(type, text, input, indent) local preffix, suffix = func(input) @@ -145,8 +147,8 @@ local function preffix_suffix_change(func) text[1] = preffix .. text[1] text[#text] = text[#text] .. suffix elseif type == 'line' then - table.insert(text, 1, preffix) - text[#text + 1] = suffix + table.insert(text, 1, vim.fn['repeat'](' ', indent) .. preffix) + text[#text + 1] = vim.fn['repeat'](' ', indent) .. suffix elseif type == 'block' then error "Block type not supported" end @@ -159,7 +161,7 @@ vis_input_change('<LocalLeader>f', 'Function name', preffix_suffix_change(functi end)) -- Wrap the selection in an inline environment -vis_input_change('<LocalLeader>e', 'Inline env name', preffix_suffix_change(function(input) +vis_input_change('<LocalLeader>ie', 'Inline env name', preffix_suffix_change(function(input) return '{\\' .. input, '}' end)) @@ -167,3 +169,9 @@ end)) 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) + return string.format('\\begin{%s}', input), string.format('\\end{%s}', input) +end)) +