nvim-config

Log | Files | Refs | Submodules | README

commit c321b41bfadb24a792422bf23bf44580b17bf0f8
parent 422ce9a1b2a18c7f5bf61e00ea8a8fe4e6543ab7
Author: Thomas Vigouroux <me@vigoux.eu>
Date:   Fri,  1 Mar 2024 11:09:59 +0100

feat(latex): better surrounds

Diffstat:
Mafter/ftplugin/tex.lua | 139++++++++-----------------------------------------------------------------------
Mafter/queries/latex/textobjects.scm | 2++
Mlua/plugins.lua | 12+-----------
3 files changed, 16 insertions(+), 137 deletions(-)

diff --git a/after/ftplugin/tex.lua b/after/ftplugin/tex.lua @@ -121,134 +121,21 @@ vim.b.miniai_config = { } } -vim.b.minioperators_config = { - funcwrap = { - prefix = "<LocalLeader>f", - func = function(content) - -- TODO: add support for linewise and blockwise - local newlines = content.lines - newlines[1] = "\\func{" .. newlines[1] - newlines[#newlines] = newlines[#newlines] .. "}" - return newlines - end +local surround = require 'mini.surround' +vim.b.minisurround_config = { + custom_surroundings = { + f = { + input = surround.gen_spec.input.treesitter { outer = "@call.outer", inner = "@call.inner" }, + output = function() + local name = surround.user_input("Function name") + if not name then return end + + return { left = string.format("\\%s{", name), right = "}" } + 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 --- end --- --- --- Utilities to wrap visual text into something --- local function charpos_to_bytepos(buf, row, col) --- local line = vim.api.nvim_buf_get_lines(buf, row, row + 1, true)[1] --- return vim.str_byteindex(line, col) --- end --- --- local function correct_position_wrap(ty, spos, epos, func) --- local curbuf = vim.api.nvim_get_current_buf() --- local start = vim.fn.getcharpos(spos) --- local stop = vim.fn.getcharpos(epos) --- --- if start[2] > stop[2] or (start[2] == stop[2] and start[3] > stop[3]) then --- -- Swap these if the order is wrong --- start, stop = stop, start --- end --- --- local start_line = start[2] - 1 --- local start_col = charpos_to_bytepos(curbuf, start_line, start[3] - 1) --- --- local end_line = stop[2] - 1 --- local end_col = charpos_to_bytepos(curbuf, end_line, stop[3]) --- --- -- Now if we are in line mode, correct the offsets to span the whole lines --- if ty == 'line' then --- start_col = 0 --- end_col = buf_line_length(curbuf, end_line) --- 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, vim.fn['repeat'](' ', indent)) --- end --- --- TexDispatch = function() --- end --- --- local function vis_mapping(map, func) --- -- First handle the visual mapping --- vim.keymap.set('v', map, function() --- -- First determine the current mode --- local vmode = vim.fn.mode() --- local ty --- if vmode == 'v' then --- ty = 'char' --- elseif vmode == 'V' then --- ty = 'line' --- else --- ty = 'block' --- end --- --- correct_position_wrap(ty, 'v', '.', func) --- end, { buffer = true }) --- --- -- Then create the operator mapping --- vim.keymap.set('n', map, function() --- -- Ensure that the operator func is this correct one --- local tmpopf = vim.o.operatorfunc --- vim.o.operatorfunc = 'v:lua.TexDispatch' --- --- _G.TexDispatch = function(ty) --- correct_position_wrap(ty, "'[", "']", func) --- vim.o.operatorfunc = tmpopf --- end --- --- return 'g@' --- end, { buffer = true, expr = true }) --- --- -- Doubling works on the current line --- vim.keymap.set('n', map .. map, function() --- correct_position_wrap('line', '.', '.', func) --- end) --- 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, 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, 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, indent) --- local preffix, suffix = func(input) --- --- if type == 'char' then --- -- Add an articial spaces when necessary --- if preffix:match "%S$" and preffix:sub(#preffix) ~= '{' then --- preffix = preffix .. ' ' --- end --- --- if suffix:match "^%S" and suffix:sub(1, 1) ~= '}' then --- suffix = ' ' .. suffix --- end --- --- text[1] = preffix .. text[1] --- text[#text] = text[#text] .. suffix --- elseif type == 'line' then --- table.insert(text, 1, indent .. preffix) --- text[#text + 1] = indent .. suffix --- elseif type == 'block' then --- error "Block type not supported" --- end --- end --- end --- + -- -- Wrap the selection in a call -- vis_input_change('<LocalLeader>f', 'Function name: ', preffix_suffix_change(function(input) -- return string.format('\\%s{', input), '}' diff --git a/after/queries/latex/textobjects.scm b/after/queries/latex/textobjects.scm @@ -18,3 +18,5 @@ ((enum_item . (_) @_ibegin) @item.outer (#make-range! "item.inner" @_ibegin @item.outer)) + +(generic_command arg: (curly_group (_) @call.inner)) @call.outer diff --git a/lua/plugins.lua b/lua/plugins.lua @@ -77,7 +77,7 @@ paq { require 'mini.surround'.setup { mappings = { add = 'gsa', - delete = 'gsdd', + delete = 'gsd', find = 'gsf', find_left = 'gsF', highlight = 'gsh', @@ -101,16 +101,6 @@ paq { }) require('mini.ai').setup {} - require("mini.operators").setup { - funcwrap = { - prefix = '', - func = nil - } - } - require("mini.operators").make_mappings( - 'funcwrap', - { textobject = '<LocalLeader>f', line = '<LocalLeader>ff', selection = '<LocalLeader>f' } - ) end } }