Skip to content

Commit

Permalink
Save and restore cursor pos after indentation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Sep 11, 2023
1 parent e2eb1dd commit 603dc8a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
30 changes: 20 additions & 10 deletions lua/nvim-paredit/indentation/native.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local traversal = require("nvim-paredit.utils.traversal")
local utils = require("nvim-paredit.indentation.utils")
local common = require("nvim-paredit.utils.common")
local langs = require("nvim-paredit.lang")

local M = {}
Expand Down Expand Up @@ -34,26 +35,35 @@ local function dedent_lines(lines, delta, opts)
{}
)
end

return smallest_distance
end

local function indent_lines(lines, delta, opts)
if delta == 0 then
return
end

local cursor_pos = vim.api.nvim_win_get_cursor(opts.buf or 0)
local cursor_delta = delta

if delta < 0 then
return dedent_lines(lines, delta * -1, opts)
cursor_delta = dedent_lines(lines, delta * -1, opts) * -1
else
local chars = string.rep(" ", delta)
for _, line in ipairs(lines) do
-- stylua: ignore
vim.api.nvim_buf_set_text(
opts.buf or 0,
line, 0,
line, 0,
{chars}
)
end
end

local chars = string.rep(" ", delta)
for _, line in ipairs(lines) do
-- stylua: ignore
vim.api.nvim_buf_set_text(
opts.buf or 0,
line, 0,
line, 0,
{chars}
)
if common.included_in_table(lines, cursor_pos[1] - 1) then
vim.api.nvim_win_set_cursor(opts.buf or 0, { cursor_pos[1], cursor_pos[2] + cursor_delta })
end
end

Expand Down
9 changes: 8 additions & 1 deletion tests/nvim-paredit/indentation_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,14 @@ describe("backward barfing indentation", function()
before_content = { "(a", " b)" },
before_cursor = { 1, 0 },
after_content = { "a", "(b)" },
after_cursor = { 2, 1 },
after_cursor = { 2, 0 },
},
{
"should keep the cursor in the same place",
before_content = { "((a", " bc", " de))" },
before_cursor = { 2, 3 },
after_content = { "(a", " (bc", " de))" },
after_cursor = { 2, 3 },
},
})
end)

0 comments on commit 603dc8a

Please sign in to comment.