diff --git a/lua/nvim-surround/buffer.lua b/lua/nvim-surround/buffer.lua index ebdedc0..48ea205 100644 --- a/lua/nvim-surround/buffer.lua +++ b/lua/nvim-surround/buffer.lua @@ -150,17 +150,6 @@ M.get_line = function(line_num) return M.get_lines(line_num, line_num)[1] end --- Formats a set of lines from the buffer, inclusive and 1-indexed. ----@param start integer The starting line. ----@param stop integer The final line. -M.format_lines = function(start, stop) - local b = vim.bo - -- Only format if a formatter is set up already - if start <= stop and (b.equalprg ~= "" or b.indentexpr ~= "" or b.cindent or b.smartindent or b.lisp) then - vim.cmd(string.format("silent normal! %dG=%dG", start, stop)) - end -end - -- Returns whether a position comes before another in a buffer, true if the positions are the same. ---@param pos1 integer[] The first position. ---@param pos2 integer[] The second position. diff --git a/lua/nvim-surround/config.lua b/lua/nvim-surround/config.lua index 98ddf14..24a11a9 100644 --- a/lua/nvim-surround/config.lua +++ b/lua/nvim-surround/config.lua @@ -258,6 +258,13 @@ M.default_opts = { duration = 0, }, move_cursor = "begin", + indent_lines = function(start, stop) + local b = vim.bo + -- Only re-indent the selection if a formatter is set up already + if start <= stop and (b.equalprg ~= "" or b.indentexpr ~= "" or b.cindent or b.smartindent or b.lisp) then + vim.cmd(string.format("silent normal! %dG=%dG", start, stop)) + end + end, } --[====================================================================================================================[ diff --git a/lua/nvim-surround/init.lua b/lua/nvim-surround/init.lua index 8514951..336255d 100644 --- a/lua/nvim-surround/init.lua +++ b/lua/nvim-surround/init.lua @@ -18,6 +18,7 @@ ---@field aliases table ---@field highlight { duration: boolean|integer } ---@field move_cursor boolean|string +---@field indent_lines boolean|function local buffer = require("nvim-surround.buffer") local cache = require("nvim-surround.cache") @@ -40,8 +41,8 @@ end -- Add delimiters around the cursor, in insert mode. M.insert_surround = function(line_mode) - local char = utils.get_char() local curpos = buffer.get_curpos() + local char = utils.get_char() local delimiters = utils.get_delimiters(char) if not delimiters then return @@ -55,14 +56,19 @@ M.insert_surround = function(line_mode) buffer.insert_text(curpos, delimiters[2]) buffer.insert_text(curpos, delimiters[1]) - buffer.format_lines(curpos[1], curpos[1] + #delimiters[1] + #delimiters[2] - 2) buffer.set_curpos({ curpos[1] + #delimiters[1] - 1, curpos[2] + #delimiters[1][#delimiters[1]] }) -- Indent the cursor to the correct level, if added line-wise - if line_mode then - local lnum = buffer.get_curpos()[1] - - vim.cmd(lnum .. "left " .. vim.fn.indent(lnum + 1) + vim.fn.shiftwidth()) - buffer.set_curpos({ lnum, #buffer.get_line(lnum) + 1 }) + local indent_lines = config.get_opts().indent_lines + if indent_lines then + curpos = buffer.get_curpos() + indent_lines(curpos[1], curpos[1] + #delimiters[1] + #delimiters[2] - 2) + buffer.set_curpos(curpos) + if line_mode then + local lnum = buffer.get_curpos()[1] + vim.pretty_print(lnum) + vim.cmd(lnum .. "left " .. vim.fn.indent(lnum + 1) + vim.fn.shiftwidth()) + buffer.set_curpos({ lnum, #buffer.get_line(lnum) + 1 }) + end end end @@ -149,7 +155,9 @@ M.visual_surround = function(line_mode) buffer.insert_text(first_pos, delimiters[1]) end - buffer.format_lines(first_pos[1], last_pos[1] + #delimiters[1] + #delimiters[2] - 2) + if config.get_opts().indent_lines then + config.get_opts().indent_lines(first_pos[1], last_pos[1] + #delimiters[1] + #delimiters[2] - 2) + end buffer.reset_curpos(curpos) end @@ -173,10 +181,12 @@ M.delete_surround = function(args) -- Delete the right selection first to ensure selection positions are correct buffer.delete_selection(selections.right) buffer.delete_selection(selections.left) - buffer.format_lines( - selections.left.first_pos[1], - selections.left.first_pos[1] + selections.right.first_pos[1] - selections.left.last_pos[1] - ) + if config.get_opts().indent_lines then + config.get_opts().indent_lines( + selections.left.first_pos[1], + selections.left.first_pos[1] + selections.right.first_pos[1] - selections.left.last_pos[1] + ) + end buffer.set_curpos(selections.left.first_pos) end