refactor: collapse all {} blocks in declaration section, drop pattern filtering
This commit is contained in:
+25
-39
@@ -112,54 +112,44 @@ function M.expand_all_attr_blocks(opts)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Helper to make a pattern case-insensitive
|
-- Find the last line of the declaration section based on filetype
|
||||||
local function case_insensitive_pattern(pattern)
|
local function get_declaration_end(lines, ft)
|
||||||
return pattern:gsub("(%a)", function(c)
|
if ft == "udt" then
|
||||||
return "[" .. c:upper() .. c:lower() .. "]"
|
return #lines
|
||||||
end)
|
end
|
||||||
|
for i, line in ipairs(lines) do
|
||||||
|
if line:match("^%s*BEGIN%s*$") then
|
||||||
|
return i - 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return #lines
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Collapse all attribute blocks matching patterns in current buffer
|
-- Collapse all {...} blocks in the declaration section of current buffer
|
||||||
function M.collapse_all_attr_blocks(patterns)
|
function M.collapse_all_attr_blocks()
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
local bufnr = vim.api.nvim_get_current_buf()
|
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 lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
|
||||||
local count = 0
|
local count = 0
|
||||||
|
|
||||||
-- Initialize buffer state
|
local decl_end = get_declaration_end(lines, ft)
|
||||||
|
|
||||||
if not buffer_attr_state[bufnr] then
|
if not buffer_attr_state[bufnr] then
|
||||||
buffer_attr_state[bufnr] = {}
|
buffer_attr_state[bufnr] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
for line_idx, line in ipairs(lines) do
|
for line_idx = 1, decl_end do
|
||||||
|
local line = lines[line_idx]
|
||||||
local pos = 1
|
local pos = 1
|
||||||
while pos <= #line do
|
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
|
if not start_pos then
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
local content = line:sub(start_pos + 1, end_pos - 1)
|
local content = line:sub(start_pos + 1, end_pos - 1)
|
||||||
|
|
||||||
-- Check if content matches any pattern
|
if content ~= "..." then
|
||||||
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
|
|
||||||
local state_key = line_idx .. "_" .. start_pos
|
local state_key = line_idx .. "_" .. start_pos
|
||||||
buffer_attr_state[bufnr][state_key] = {
|
buffer_attr_state[bufnr][state_key] = {
|
||||||
collapsed = "...",
|
collapsed = "...",
|
||||||
@@ -168,21 +158,17 @@ function M.collapse_all_attr_blocks(patterns)
|
|||||||
line_num = line_idx,
|
line_num = line_idx,
|
||||||
start_col = start_pos,
|
start_col = start_pos,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Replace in line
|
|
||||||
line = line:sub(1, start_pos) .. "..." .. line:sub(end_pos)
|
line = line:sub(1, start_pos) .. "..." .. line:sub(end_pos)
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
|
||||||
-- Update position for next search
|
|
||||||
pos = start_pos + 4
|
pos = start_pos + 4
|
||||||
else
|
else
|
||||||
pos = end_pos + 1
|
pos = end_pos + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
lines[line_idx] = line
|
lines[line_idx] = line
|
||||||
end
|
end
|
||||||
|
|
||||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||||
vim.notify("Collapsed " .. count .. " attribute block(s)", vim.log.levels.INFO)
|
vim.notify("Collapsed " .. count .. " attribute block(s)", vim.log.levels.INFO)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user