Skip to content

Commit

Permalink
feat: Allow custom indentation via indent_lines key. (#138)
Browse files Browse the repository at this point in the history
* feat: Allow custom formatting via `format_lines` key.

* fix: Insert surrounds also depend on format key.

* refactor!: Rename `format_lines` to `indent_lines`.
  • Loading branch information
kylechui authored Aug 13, 2022
1 parent c8d4fb0 commit 3cf62f2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
11 changes: 0 additions & 11 deletions lua/nvim-surround/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions lua/nvim-surround/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

--[====================================================================================================================[
Expand Down
34 changes: 22 additions & 12 deletions lua/nvim-surround/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
---@field aliases table<string, boolean|string|string[]>
---@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")
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 3cf62f2

Please sign in to comment.