refactor: collapse all {} blocks in declaration section, drop pattern filtering
This commit is contained in:
+20
-34
@@ -112,54 +112,44 @@ function M.expand_all_attr_blocks(opts)
|
||||
end
|
||||
end
|
||||
|
||||
-- Helper to make a pattern case-insensitive
|
||||
local function case_insensitive_pattern(pattern)
|
||||
return pattern:gsub("(%a)", function(c)
|
||||
return "[" .. c:upper() .. c:lower() .. "]"
|
||||
end)
|
||||
-- Find the last line of the declaration section based on filetype
|
||||
local function get_declaration_end(lines, ft)
|
||||
if ft == "udt" then
|
||||
return #lines
|
||||
end
|
||||
for i, line in ipairs(lines) do
|
||||
if line:match("^%s*BEGIN%s*$") then
|
||||
return i - 1
|
||||
end
|
||||
end
|
||||
return #lines
|
||||
end
|
||||
|
||||
-- Collapse all attribute blocks matching patterns in current buffer
|
||||
function M.collapse_all_attr_blocks(patterns)
|
||||
patterns = patterns or {
|
||||
case_insensitive_pattern("^%s*EXTERNAL"), -- EXTERNALACCESSIBLE, EXTERNALVISIBLE
|
||||
case_insensitive_pattern("^%s*S7_"), -- S7_Optimized_Access
|
||||
case_insensitive_pattern("^%s*NonRetain"), -- NonRetain
|
||||
case_insensitive_pattern("^%s*Retain"), -- Retain (but not NonRetain)
|
||||
"^%s*%w+%s*:=", -- key := value
|
||||
"^%s*%w+%s*$", -- single word attribute
|
||||
}
|
||||
|
||||
-- Collapse all {...} blocks in the declaration section of current buffer
|
||||
function M.collapse_all_attr_blocks()
|
||||
local bufnr = vim.api.nvim_get_current_buf()
|
||||
local ft = vim.bo[bufnr].filetype
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||
local count = 0
|
||||
|
||||
-- Initialize buffer state
|
||||
local decl_end = get_declaration_end(lines, ft)
|
||||
|
||||
if not buffer_attr_state[bufnr] then
|
||||
buffer_attr_state[bufnr] = {}
|
||||
end
|
||||
|
||||
for line_idx, line in ipairs(lines) do
|
||||
for line_idx = 1, decl_end do
|
||||
local line = lines[line_idx]
|
||||
local pos = 1
|
||||
while pos <= #line do
|
||||
local start_pos, end_pos, capture = line:find("{(.-)}", pos)
|
||||
local start_pos, end_pos = line:find("{(.-)}", pos)
|
||||
if not start_pos then
|
||||
break
|
||||
end
|
||||
|
||||
local content = line:sub(start_pos + 1, end_pos - 1)
|
||||
|
||||
-- Check if content matches any pattern
|
||||
local should_collapse = false
|
||||
for _, pattern in ipairs(patterns) do
|
||||
if content:match(pattern) then
|
||||
should_collapse = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
-- Don't collapse if already collapsed
|
||||
if should_collapse and content ~= "..." then
|
||||
if content ~= "..." then
|
||||
local state_key = line_idx .. "_" .. start_pos
|
||||
buffer_attr_state[bufnr][state_key] = {
|
||||
collapsed = "...",
|
||||
@@ -169,17 +159,13 @@ function M.collapse_all_attr_blocks(patterns)
|
||||
start_col = start_pos,
|
||||
}
|
||||
|
||||
-- Replace in line
|
||||
line = line:sub(1, start_pos) .. "..." .. line:sub(end_pos)
|
||||
count = count + 1
|
||||
|
||||
-- Update position for next search
|
||||
pos = start_pos + 4
|
||||
else
|
||||
pos = end_pos + 1
|
||||
end
|
||||
end
|
||||
|
||||
lines[line_idx] = line
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user