56 lines
2.9 KiB
Lua
56 lines
2.9 KiB
Lua
local root = assert(vim.env.MODULUS_FORMAT_REPO)
|
|
vim.opt.rtp:append(assert(vim.env.MODULUS_CONFORM_PATH))
|
|
local conform = require("conform")
|
|
conform.setup({ formatters_by_ft = { cpp = { "clang_format" }, _ = { "original_fallback" } } })
|
|
vim.lsp.config("clangd", { cmd = { "clangd" }, filetypes = { "c", "cpp" } })
|
|
vim.lsp.enable = function() end
|
|
dofile(assert(vim.env.MODULUS_FORMAT_HELPER)).setup(root)
|
|
local function buffer(name, ft, lines)
|
|
local buf = vim.api.nvim_create_buf(true, false)
|
|
vim.api.nvim_set_current_buf(buf)
|
|
vim.api.nvim_buf_set_name(buf, name)
|
|
vim.bo[buf].filetype = ft
|
|
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
|
|
return buf
|
|
end
|
|
local function content(buf)
|
|
return table.concat(vim.api.nvim_buf_get_lines(buf, 0, -1, false), "\n")
|
|
end
|
|
local function check()
|
|
local outside = buffer("/tmp/not-modulus.cpp", "cpp", { "int x;" })
|
|
assert(vim.deep_equal(conform.list_formatters_for_buffer(outside), { "clang_format" }))
|
|
local python_outside = buffer("/tmp/not_modulus.py", "python", { "pass" })
|
|
assert(vim.deep_equal(conform.list_formatters_for_buffer(python_outside), { "original_fallback" }))
|
|
local cpp = buffer(root .. "/tests/modulus-format-probe.cpp", "cpp", { "void example(){", "", "int a=1;", "", "a++;", "}" })
|
|
assert(vim.deep_equal(conform.list_formatters_for_buffer(cpp), { "modulus" }))
|
|
local failure
|
|
conform.format({ bufnr = cpp, timeout_ms = 10000 }, function(err) failure = err end)
|
|
assert(not failure, vim.inspect(failure))
|
|
assert(content(cpp) == "void example() {\n int a = 1;\n a++;\n}", content(cpp))
|
|
local py = buffer(root .. "/tests/modulus_format_probe.py", "python", { "def example( x ): return x+1" })
|
|
vim.g.autoformat = false
|
|
vim.api.nvim_exec_autocmds("BufWritePre", { group = "ModulusFormat", buffer = py })
|
|
assert(content(py) == "def example( x ): return x+1")
|
|
vim.g.autoformat = true
|
|
vim.b[py].autoformat = false
|
|
vim.api.nvim_exec_autocmds("BufWritePre", { group = "ModulusFormat", buffer = py })
|
|
assert(content(py) == "def example( x ): return x+1")
|
|
vim.b[py].autoformat = true
|
|
vim.api.nvim_exec_autocmds("BufWritePre", { group = "ModulusFormat", buffer = py })
|
|
assert(content(py) == "def example(x): return x + 1", content(py))
|
|
vim.api.nvim_buf_set_lines(py, 0, -1, false, { "patterns = (", ' ".git",', ' ".tmp",', ")" })
|
|
vim.api.nvim_exec_autocmds("BufWritePre", { group = "ModulusFormat", buffer = py })
|
|
assert(content(py) == 'patterns = (\n ".git",\n ".tmp",\n)', content(py))
|
|
vim.api.nvim_buf_set_lines(py, 0, -1, false, { "def broken(" })
|
|
conform.format({ bufnr = py, timeout_ms = 10000, quiet = true }, function(err) failure = err end)
|
|
assert(failure, "invalid Python unexpectedly succeeded")
|
|
assert(content(py) == "def broken(", "failed formatter replaced the buffer")
|
|
end
|
|
local ok, err = pcall(check)
|
|
if not ok then
|
|
print("MODULUS_FORMAT_FAILURE " .. tostring(err))
|
|
vim.cmd("cquit 1")
|
|
end
|
|
print("MODULUS_FORMAT_OK")
|
|
vim.cmd("qa!")
|