feat: improve formatter and attribute block handling

- Format multi-line assignments with proper alignment
- Format FB calls in multiline style with parameter alignment
- Auto-expand collapsed attribute blocks before save
- Rename command from LspSCLFormat to SCLFormat
- Condense AGENTS.md and add testing section with reference project
This commit is contained in:
2026-02-23 10:29:05 +01:00
parent 15f88d110b
commit 3dfca6bba3
4 changed files with 68 additions and 290 deletions
+27 -5
View File
@@ -80,23 +80,24 @@ function M.toggle_attr_block()
end
-- Expand all collapsed blocks in current buffer
function M.expand_all_attr_blocks()
function M.expand_all_attr_blocks(opts)
opts = opts or {}
local bufnr = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
local count = 0
if not buffer_attr_state[bufnr] then
vim.notify("No collapsed blocks to expand", vim.log.levels.INFO)
if not opts.silent then
vim.notify("No collapsed blocks to expand", vim.log.levels.INFO)
end
return
end
for state_key, state in pairs(buffer_attr_state[bufnr]) do
if state.is_collapsed then
-- state.line_num is 1-indexed, but nvim_buf_get_lines returns 0-indexed array
local line = lines[state.line_num]
if line then
-- Find the collapsed {...} and replace with expanded content
local new_line = line:sub(1, state.start_col) .. state.expanded .. line:sub(state.start_col + 4)
lines[state.line_num] = new_line
state.is_collapsed = false
@@ -106,7 +107,9 @@ function M.expand_all_attr_blocks()
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.notify("Expanded " .. count .. " attribute block(s)", vim.log.levels.INFO)
if not opts.silent and count > 0 then
vim.notify("Expanded " .. count .. " attribute block(s)", vim.log.levels.INFO)
end
end
-- Helper to make a pattern case-insensitive
@@ -193,6 +196,25 @@ end
function M.setup()
local augroup = vim.api.nvim_create_augroup("SCLAttrToggle", { clear = true })
vim.api.nvim_create_autocmd("BufWritePre", {
group = augroup,
pattern = { "*.scl", "*.udt", "*.db" },
callback = function(args)
if buffer_attr_state[args.buf] then
local has_collapsed = false
for _, state in pairs(buffer_attr_state[args.buf]) do
if state.is_collapsed then
has_collapsed = true
break
end
end
if has_collapsed then
M.expand_all_attr_blocks({ silent = true })
end
end
end,
})
vim.api.nvim_create_autocmd("BufUnload", {
group = augroup,
pattern = { "*.scl", "*.udt", "*.db" },
+2 -4
View File
@@ -34,9 +34,8 @@ function M.setup(opts)
end
if opts.lsp and opts.lsp.formatting then
vim.api.nvim_buf_create_user_command(b, "LspSCLFormat", function()
vim.api.nvim_buf_create_user_command(b, "SCLFormat", function()
vim.lsp.buf.format({ name = "scl_lsp" })
-- Clear attr_toggle state since line positions may have changed
local ok, at = pcall(require, "scl.attr_toggle")
if ok then
at.clear_buffer_state(vim.api.nvim_get_current_buf())
@@ -56,9 +55,8 @@ function M.setup(opts)
})
-- Create global format command (works on current buffer)
vim.api.nvim_create_user_command("LspSCLFormat", function()
vim.api.nvim_create_user_command("SCLFormat", function()
vim.lsp.buf.format({ name = "scl_lsp" })
-- Clear attr_toggle state since line positions may have changed
local ok, at = pcall(require, "scl.attr_toggle")
if ok then
at.clear_buffer_state(vim.api.nvim_get_current_buf())