aboutsummaryrefslogtreecommitdiffstats
path: root/tests/runner_spec.lua
diff options
context:
space:
mode:
authorSteven Arcangeli <506791+stevearc@users.noreply.github.com>2023-08-30 18:34:13 -0700
committerGitHub <noreply@github.com>2023-08-30 18:34:13 -0700
commit92393f02efadfb1d9f97c74c8feb853c1caea9de (patch)
tree980a43e5f6b70c419e089f82074d177aca9da7aa /tests/runner_spec.lua
parentc100b8548fd7262a1275bdb867186d0cd94e8b45 (diff)
feat: apply changes as text edits using LSP utils (#18)
* feat: apply changes as text edits using LSP utils This means we can leverage all of the work that was done in the LSP client to preserve marks, cursor position, etc * log: add trace logging to debug performance * feat: use the same diff -> TextEdit technique for bad LSP servers Some LSP servers simply return a single TextEdit that replaces the whole buffer. This is bad for extmarks, cursor, and if the buffer is open in multiple windows the non-active window will jump to the top. We can detect that situation and apply the same vim.diff logic to convert it into more granular TextEdits.
Diffstat (limited to 'tests/runner_spec.lua')
-rw-r--r--tests/runner_spec.lua70
1 files changed, 58 insertions, 12 deletions
diff --git a/tests/runner_spec.lua b/tests/runner_spec.lua
index f054d9e..8807d2d 100644
--- a/tests/runner_spec.lua
+++ b/tests/runner_spec.lua
@@ -124,16 +124,21 @@ describe("runner", function()
vim.bo[bufnr].modified = false
local expected_lines = vim.split(expected, "\n", { plain = true })
test_util.set_formatter_output(expected_lines)
- conform.format(vim.tbl_extend("force", opts or {}, { formatters = { "test" } }))
+ conform.format(vim.tbl_extend("force", opts or {}, { formatters = { "test" }, quiet = true }))
+ -- We expect the last newline to be effectively "swallowed" by the formatter
+ -- because vim will use that as the EOL at the end of the file. The exception is that we always
+ -- expect at least one line in the output
+ if #expected_lines > 1 and expected_lines[#expected_lines] == "" then
+ table.remove(expected_lines)
+ end
return expected_lines
end
---@param buf_content string
---@param new_content string
- ---@param expected? string[]
- local function run_formatter_test(buf_content, new_content, expected)
+ local function run_formatter_test(buf_content, new_content)
local lines = run_formatter(buf_content, new_content)
- assert.are.same(expected or lines, vim.api.nvim_buf_get_lines(0, 0, -1, false))
+ assert.are.same(lines, vim.api.nvim_buf_get_lines(0, 0, -1, false))
end
it("sets the correct output", function()
@@ -181,15 +186,18 @@ print("b")
print("a")
]]
)
- run_formatter_test("hello\ngoodbye", "hello\n\n\ngoodbye", { "hello", "", "", "goodbye" })
- run_formatter_test("hello", "hello\ngoodbye", { "hello", "goodbye" })
- run_formatter_test("", "hello", { "hello" })
- run_formatter_test("\nfoo", "\nhello\nfoo", { "", "hello", "foo" })
- run_formatter_test("hello", "hello\n\n", { "hello", "" })
- run_formatter_test("hello", "hello\n", { "hello" })
+ run_formatter_test("hello\ngoodbye", "hello\n\n\ngoodbye")
+ run_formatter_test("hello", "hello\ngoodbye")
+ run_formatter_test("hello\ngoodbye", "hello")
+ run_formatter_test("", "hello")
+ run_formatter_test("\nfoo", "\nhello\nfoo")
+ run_formatter_test("hello", "hello\n")
+ run_formatter_test("hello", "hello\n\n")
+ run_formatter_test("hello", "hello\n")
+ -- This should generate no changes to the buffer
assert.falsy(vim.bo.modified)
- run_formatter_test("hello\n", "hello", { "hello" })
- run_formatter_test("hello\n ", "hello", { "hello" })
+ run_formatter_test("hello\n", "hello")
+ run_formatter_test("hello\n ", "hello")
end)
it("does not change output if formatter fails", function()
@@ -238,5 +246,43 @@ print("a")
vim.fn.delete("tests/testfile.txt")
assert.are.same({ "goodbye" }, lines)
end)
+
+ describe("range formatting", function()
+ it("applies edits that overlap the range start", function()
+ run_formatter(
+ "a\nb\nc",
+ "d\nb\nd",
+ { range = {
+ start = { 1, 0 },
+ ["end"] = { 2, 0 },
+ } }
+ )
+ assert.are.same({ "d", "b", "c" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
+ end)
+
+ it("applies edits that overlap the range end", function()
+ run_formatter(
+ "a\nb\nc",
+ "d\nb\nd",
+ { range = {
+ start = { 3, 0 },
+ ["end"] = { 3, 1 },
+ } }
+ )
+ assert.are.same({ "a", "b", "d" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
+ end)
+
+ it("applies edits that are completely contained by the range", function()
+ run_formatter(
+ "a\nb\nc",
+ "a\nd\nc",
+ { range = {
+ start = { 1, 0 },
+ ["end"] = { 3, 0 },
+ } }
+ )
+ assert.are.same({ "a", "d", "c" }, vim.api.nvim_buf_get_lines(0, 0, -1, false))
+ end)
+ end)
end)
end)