Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve native indent impl under some edge cases #77

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lua/nvim-paredit/indentation/native.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ local function indent_barf(event)
local lhs_range = { lhs:range() }
local node_range = { node:range() }

if not utils.node_is_first_on_line(node, opts) or lhs_range[1] == node_range[1] then
if lhs_range[3] == node_range[1] then
return
end

if not utils.node_is_first_on_line(node, opts) then
return
end

Expand Down Expand Up @@ -136,13 +140,25 @@ local function indent_slurp(event)
if event.type == "slurp-forwards" then
child = parent:named_child(parent:named_child_count() - 1)
else
child = parent:named_child(1)
local first = parent:named_child(0)
if not first then
return
end
child = traversal.get_next_sibling_ignoring_comments(first, opts)
end

if not child then
return
end

local parent_range = { parent:range() }
local child_range = { child:range() }

if not utils.node_is_first_on_line(child, opts) or parent_range[1] == child_range[1] then
if parent_range[1] == child_range[1] then
return
end

if not utils.node_is_first_on_line(child, opts) then
return
end

Expand Down
16 changes: 13 additions & 3 deletions lua/nvim-paredit/indentation/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@ local common = require("nvim-paredit.utils.common")

local M = {}

local function get_corrected_line_range(range)
-- if the node is at the zeroth position on the last line, it's not actually
-- part of the line.
if range[4] == 0 then
return range[1], range[3] - 1
end
return range[1], range[3]
end

function M.get_node_line_range(range)
local start, _end = get_corrected_line_range(range)
local lines = {}
for i = range[1], range[3], 1 do
for i = start, _end, 1 do
table.insert(lines, i)
end
return lines
Expand Down Expand Up @@ -57,8 +67,8 @@ function M.node_is_first_on_line(node, opts)
return true
end

local sibling_range = { sibling:range() }
return sibling_range[3] ~= node_range[1]
local _, sibling_end = get_corrected_line_range({ sibling:range() })
return sibling_end ~= node_range[1]
end

-- This functions finds the closest sibling to a given `node` which is:
Expand Down
28 changes: 14 additions & 14 deletions tests/nvim-paredit/barf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ describe("barfing ::", function()
before_cursor = { 1, 3 },
after_content = "#?(:cljs a :clj) b",
after_cursor = { 1, 3 },
}
},
})
end)

Expand Down Expand Up @@ -147,15 +147,15 @@ describe("barfing ::", function()
before_content = "(aa bb)",
before_cursor = { 1, 5 },
after_content = "(aa) bb",
after_cursor = { 1, 3 }
after_cursor = { 1, 3 },
},
{
"multi line",
before_content = { "(aa", "bb)" },
before_cursor = { 2, 1 },
after_content = { "(aa)", "bb" },
after_cursor = { 1, 3 }
}
after_cursor = { 1, 3 },
},
})
end)

Expand All @@ -170,15 +170,15 @@ describe("barfing ::", function()
before_content = "(aa bb cc)",
before_cursor = { 1, 4 },
after_content = "(aa bb) cc",
after_cursor = { 1, 6 }
after_cursor = { 1, 6 },
},
{
"multi line",
before_content = { "(aa", "bb", "cc)" },
before_cursor = { 1, 1 },
after_content = { "(aa", "bb)", "cc" },
after_cursor = { 2, 2 }
}
after_cursor = { 2, 2 },
},
})
end)
end)
Expand Down Expand Up @@ -248,7 +248,7 @@ describe("barfing ::", function()
})
paredit.barf_backwards()
expect({
content = {"a ;; comment", "()"},
content = { "a ;; comment", "()" },
cursor = { 2, 0 },
})
end)
Expand Down Expand Up @@ -314,15 +314,15 @@ describe("barfing ::", function()
before_content = "(aa bb)",
before_cursor = { 1, 1 },
after_content = "aa (bb)",
after_cursor = { 1, 4 }
after_cursor = { 1, 4 },
},
{
"multi line",
before_content = { "(aa", "bb)" },
before_cursor = { 1, 1 },
after_content = { "aa", "(bb)" },
after_cursor = { 2, 0 }
}
after_cursor = { 2, 0 },
},
})
end)

Expand All @@ -337,15 +337,15 @@ describe("barfing ::", function()
before_content = "(aa bb cc)",
before_cursor = { 1, 1 },
after_content = "aa (bb cc)",
after_cursor = { 1, 4 }
after_cursor = { 1, 4 },
},
{
"multi line",
before_content = { "(aa", "bb", "cc)" },
before_cursor = { 1, 1 },
after_content = { "aa", "(bb", "cc)" },
after_cursor = { 2, 0 }
}
after_cursor = { 2, 0 },
},
})
end)
end)
Expand Down
6 changes: 3 additions & 3 deletions tests/nvim-paredit/deletions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer
local expect_all = require("tests.nvim-paredit.utils").expect_all
local expect = require("tests.nvim-paredit.utils").expect

describe("form deletions", function()
describe("form deletions ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})
Expand Down Expand Up @@ -93,7 +93,7 @@ describe("form deletions", function()
end)
end)

describe("form inner deletions", function()
describe("form inner deletions ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})
Expand Down Expand Up @@ -182,7 +182,7 @@ describe("form inner deletions", function()
end)
end)

describe("element deletions", function()
describe("element deletions ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})
Expand Down
3 changes: 2 additions & 1 deletion tests/nvim-paredit/element_drag_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer
local expect_all = require("tests.nvim-paredit.utils").expect_all
local expect = require("tests.nvim-paredit.utils").expect

describe("element-dragging", function()
describe("element dragging ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})

local parser = vim.treesitter.get_parser(0)
if not parser then
return error("Failed to get parser for language")
Expand Down
2 changes: 1 addition & 1 deletion tests/nvim-paredit/element_raise_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local paredit = require("nvim-paredit.api")
local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer
local expect = require("tests.nvim-paredit.utils").expect

describe("element raising", function()
describe("element raising ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})
Expand Down
2 changes: 1 addition & 1 deletion tests/nvim-paredit/form_and_element_wrap_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local paredit = require("nvim-paredit")
local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer
local expect = require("tests.nvim-paredit.utils").expect

describe("element and form wrap", function()
describe("form and element wrap ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})
Expand Down
2 changes: 1 addition & 1 deletion tests/nvim-paredit/form_drag_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer
local expect_all = require("tests.nvim-paredit.utils").expect_all
local expect = require("tests.nvim-paredit.utils").expect

describe("form-dragging", function()
describe("form dragging ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})
Expand Down
2 changes: 1 addition & 1 deletion tests/nvim-paredit/form_raise_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer
local expect_all = require("tests.nvim-paredit.utils").expect_all
local expect = require("tests.nvim-paredit.utils").expect

describe("form raising", function()
describe("form raising ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})
Expand Down
3 changes: 2 additions & 1 deletion tests/nvim-paredit/form_unwrap_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ local paredit = require("nvim-paredit")
local prepare_buffer = require("tests.nvim-paredit.utils").prepare_buffer
local expect = require("tests.nvim-paredit.utils").expect

describe("form uwrap (e.g. splice)", function()
describe("form uwrap ::", function()
vim.api.nvim_set_option_value("filetype", "clojure", {
buf = 0,
})

local unwrap = paredit.unwrap

it("should uwrap list under cursor", function()
Expand Down
Loading
Loading