Formatting
publish / publish (push) Successful in 11m55s

This commit is contained in:
khodowany
2026-09-14 23:54:30 -05:00
parent b9d30fb975
commit 097cb61d2d
33 changed files with 950 additions and 302 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ def header_roots(config):
for driver in config["query_drivers"]:
for language in ("c", "c++"):
result = subprocess.run([driver, "-E", "-v", "-x", language, "-"], input="",
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
listing = False
for line in result.stderr.splitlines():
if line == "#include <...> search starts here:":
+5
View File
@@ -44,6 +44,11 @@ ENV PATH=/opt/modulus-python/bin:${PATH}
RUN python -m pip install --no-cache-dir \
conan==2.26.2
# The path is shared by source builds and exported consumer tooling.
ARG MODULUS_TOOLS=tools
COPY ${MODULUS_TOOLS}/docker/format-requirements.txt /tmp/modulus-format-requirements.txt
RUN python -m pip install --no-cache-dir -r /tmp/modulus-format-requirements.txt
WORKDIR /workspace
RUN mkdir -p /opt && touch /opt/modulus-build-image
+5
View File
@@ -0,0 +1,5 @@
clang-format==19.1.7
autopep8==2.3.2
pycodestyle==2.14.0
tree-sitter==0.25.2
tree-sitter-cpp==0.23.4
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env python3
import modulus_tool
if __name__ == "__main__":
modulus_tool.run("format")
+67 -1
View File
@@ -4,6 +4,71 @@ local function within(path, root)
return path == root or path:sub(1, #root + 1) == root .. "/"
end
local function setup_formatting(state)
if state.formatting then
return
end
local ok, conform = pcall(require, "conform")
if not ok then
vim.notify("Modulus formatting requires conform.nvim", vim.log.levels.WARN)
return
end
state.formatting = true
local function project(buf)
local ft = vim.bo[buf].filetype
if ft ~= "c" and ft ~= "cpp" and ft ~= "python" then
return nil
end
return state.project(vim.api.nvim_buf_get_name(buf))
end
local fallback = conform.formatters_by_ft["_"]
for _, ft in ipairs({ "c", "cpp", "python", "*" }) do
local original = conform.formatters_by_ft[ft]
if ft ~= "*" and (not original or (type(original) == "table" and vim.tbl_isempty(original))) then
original = fallback
end
conform.formatters_by_ft[ft] = function(buf)
if project(buf) then
if ft == "*" then
return {}
end
return { "modulus", lsp_format = "never", timeout_ms = 10000 }
end
if type(original) == "function" then
return original(buf) or {}
end
return original or {}
end
end
conform.formatters.modulus = {
command = "python3",
env = { MODULUS_DIRECT = "0" },
args = function(_, context)
local root = assert(project(context.buf))
local args = vim.uv.fs_stat(root .. "/modulus/cli.py")
and { root .. "/tools/format.py" }
or { root .. "/modulus.py", "format" }
vim.list_extend(args, { "--stdin-filename", context.filename })
return args
end,
cwd = function(_, context) return project(context.buf) end,
stdin = true,
}
-- LazyVim already owns save/manual formatting and its global/buffer toggles.
local has_save, save_hooks = pcall(vim.api.nvim_get_autocmds, { event = "BufWritePre", group = "Conform" })
if not rawget(_G, "LazyVim") and (not has_save or #save_hooks == 0) then
vim.api.nvim_create_autocmd("BufWritePre", {
group = vim.api.nvim_create_augroup("ModulusFormat", { clear = true }),
callback = function(event)
if project(event.buf) and vim.g.autoformat ~= false and vim.b[event.buf].autoformat ~= false
and not vim.g.disable_autoformat and not vim.b[event.buf].disable_autoformat then
conform.format({ bufnr = event.buf, lsp_format = "never", timeout_ms = 10000 })
end
end,
})
end
end
function M.setup(root)
if vim.fn.has("nvim-0.11") ~= 1 then
vim.notify("Modulus container clangd requires Neovim 0.11+", vim.log.levels.ERROR)
@@ -13,7 +78,7 @@ function M.setup(root)
-- Finish LazyVim's server setup before changing only the command and root routing.
local has_lazy, lazy = pcall(require, "lazy")
if has_lazy then
lazy.load({ plugins = { "nvim-lspconfig" } })
lazy.load({ plugins = { "nvim-lspconfig", "conform.nvim" } })
end
local state = rawget(_G, "modulus_clangd")
if not state then
@@ -85,6 +150,7 @@ function M.setup(root)
command = { "python3", root .. "/modulus.py", "clangd" }
end
state.roots[root] = command
setup_formatting(state)
for _, client in ipairs(vim.lsp.get_clients({ name = "clangd" })) do
if not client.config._modulus_container and client.config.root_dir and within(client.config.root_dir, root) then
client:stop()