Skip to content

Commit

Permalink
Use proper TS traversal to indent correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Sep 3, 2023
1 parent e37eb1d commit b0c7f61
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 73 deletions.
46 changes: 24 additions & 22 deletions lua/nvim-paredit/api/barfing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,6 @@ function M.barf_forwards(opts)
{ text }
)

indentation.handle_indentation({
type = "barf",
from = range,
to = { end_pos[1], end_pos[2], end_pos[1], end_pos[2] },
child = child,
parent = form,

indent_behaviour = opts.indent_behaviour or config.config.indent_behaviour,
lang = lang,
})

local cursor_behaviour = opts.cursor_behaviour or config.config.cursor_behaviour
if cursor_behaviour == "auto" or cursor_behaviour == "follow" then
local cursor_pos = vim.api.nvim_win_get_cursor(0)
Expand All @@ -91,6 +80,18 @@ function M.barf_forwards(opts)
vim.api.nvim_win_set_cursor(0, { end_pos[1] + 1, end_pos[2] })
end
end

indentation.handle_indentation({
type = "barf-forwards",
-- stylua: ignore
parent_range = {
edges.left.range[1], edges.left.range[2],
end_pos[1], end_pos[2],
},
reversed = false,
indent_behaviour = opts.indent_behaviour or config.config.indent_behaviour,
lang = lang,
})
end

function M.barf_backwards(opts)
Expand Down Expand Up @@ -151,17 +152,6 @@ function M.barf_backwards(opts)
{}
)

indentation.handle_indentation({
type = "barf",
from = range,
to = { end_pos[1], end_pos[2], end_pos[1], end_pos[2] },
child = child,
parent = form,

indent_behaviour = opts.indent_behaviour or config.config.indent_behaviour,
lang = lang,
})

local cursor_behaviour = opts.cursor_behaviour or config.config.cursor_behaviour
if cursor_behaviour == "auto" or cursor_behaviour == "follow" then
local cursor_pos = vim.api.nvim_win_get_cursor(0)
Expand All @@ -170,6 +160,18 @@ function M.barf_backwards(opts)
vim.api.nvim_win_set_cursor(0, { end_pos[1] + 1, end_pos[2] })
end
end

indentation.handle_indentation({
type = "barf-backwards",
-- stylua: ignore
parent_range = {
end_pos[1], end_pos[2],
edges.right.range[1], edges.right.range[2],
},
reversed = true,
indent_behaviour = opts.indent_behaviour or config.config.indent_behaviour,
lang = lang,
})
end

return M
41 changes: 29 additions & 12 deletions lua/nvim-paredit/api/slurping.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ local function slurp(opts)

local offset = 0
if opts.reversed and row == left_or_right_edge.range[1] then
offset = string.len(left_or_right_edge.text)
offset = #left_or_right_edge.text
end

-- stylua: ignore
Expand All @@ -79,17 +79,6 @@ local function slurp(opts)
{}
)

indentation.handle_indentation({
type = "slurp",
from = left_or_right_edge.range,
to = { row, col, row, col },
child = sibling,
parent = form,

indent_behaviour = opts.indent_behaviour or config.config.indent_behaviour,
lang = lang,
})

local cursor_behaviour = opts.cursor_behaviour or config.config.cursor_behaviour
if cursor_behaviour == "follow" then
offset = 0
Expand All @@ -103,6 +92,34 @@ local function slurp(opts)
vim.api.nvim_win_set_cursor(0, cursor_pos)
end
end

local operation_type
local new_range
if not opts.reversed then
operation_type = "slurp-forwards"
new_range = {
form_edges.left.range[1],
form_edges.left.range[2],
row,
col,
}
else
operation_type = "slurp-backwards"
new_range = {
row,
col,
form_edges.right.range[1],
form_edges.right.range[2],
}
end

indentation.handle_indentation({
type = operation_type,
parent_range = new_range,
reversed = opts.reversed,
indent_behaviour = opts.indent_behaviour or config.config.indent_behaviour,
lang = lang,
})
end

function M.slurp_forwards(opts)
Expand Down
10 changes: 9 additions & 1 deletion lua/nvim-paredit/indentation/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ function M.handle_indentation(operation)
return
end

indent_fn(operation)
local tree = vim.treesitter.get_parser(0)

tree:parse()

local parent = tree:named_node_for_range(operation.parent_range)
indent_fn(vim.tbl_deep_extend("force", operation, {
tree = tree,
parent = parent,
}))
end

return M
Loading

0 comments on commit b0c7f61

Please sign in to comment.