-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_lib.lua
More file actions
250 lines (215 loc) · 7.65 KB
/
format_lib.lua
File metadata and controls
250 lines (215 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
-- Output formatting for code graph tools
-- ASCII trees, tables, Mermaid graphs
-- Entry kind: library.lua
---------------------------------------------------------------------------
-- ASCII tree rendering
---------------------------------------------------------------------------
--- Render a tree structure as ASCII art
-- @param root table {name, children: {tree_node}}
-- @param opts table|nil {prefix, is_last, max_depth, current_depth, seen}
-- @return {string} Lines of output
local function render_tree(root, opts)
opts = opts or {}
local prefix = opts.prefix or ""
local is_last = opts.is_last
local max_depth = opts.max_depth or 10
local depth = opts.current_depth or 0
local seen = opts.seen or {}
local lines = {}
if depth > max_depth then
table.insert(lines, prefix .. "... (max depth)")
return lines
end
-- Connector characters
local connector = ""
if depth > 0 then
connector = is_last and "└── " or "├── "
end
-- Cycle detection
local label = root.name or "?"
if root.annotation then
label = label .. " " .. root.annotation
end
if seen[root.name] then
table.insert(lines, prefix .. connector .. label .. " (↻ seen above)")
return lines
end
table.insert(lines, prefix .. connector .. label)
local children = root.children or {}
if #children > 0 then
seen[root.name] = true
local child_prefix = prefix
if depth > 0 then
child_prefix = prefix .. (is_last and " " or "│ ")
end
for i, child in ipairs(children) do
local child_lines = render_tree(child, {
prefix = child_prefix,
is_last = (i == #children),
max_depth = max_depth,
current_depth = depth + 1,
seen = seen,
})
for _, line in ipairs(child_lines) do
table.insert(lines, line)
end
end
end
return lines
end
---------------------------------------------------------------------------
-- Stats formatting
---------------------------------------------------------------------------
--- Format index statistics
-- @param result table {stats, errors, project, path}
-- @return string
local function format_index_stats(result)
local s = result.stats
local lines = {}
table.insert(lines, "# Index Complete")
table.insert(lines, "")
table.insert(lines, "Project: " .. (result.project or "default"))
if result.path and result.path ~= "" then
table.insert(lines, "Path: " .. result.path)
end
table.insert(lines, "")
table.insert(lines, string.format("Files: %d", s.files))
table.insert(lines, string.format("Namespaces: %d", s.namespaces))
table.insert(lines, string.format("Imports: %d", s.imports))
-- Symbol breakdown
local sym_parts = {}
local kind_order = {"class", "interface", "trait", "enum", "method", "function", "constant"}
for _, kind in ipairs(kind_order) do
local count = s.symbols_by_kind[kind]
if count and count > 0 then
table.insert(sym_parts, string.format("%d %ss", count, kind))
end
end
table.insert(lines, string.format(
"Symbols: %d (%s)",
s.symbols,
#sym_parts > 0 and table.concat(sym_parts, ", ") or "none"
))
-- Reference breakdown
local ref_parts = {}
for kind, count in pairs(s.refs_by_kind or {}) do
table.insert(ref_parts, {kind = kind, count = count})
end
table.sort(ref_parts, function(a, b) return a.count > b.count end)
local ref_strs = {}
for _, rp in ipairs(ref_parts) do
table.insert(ref_strs, string.format("%d %s", rp.count, rp.kind))
end
table.insert(lines, string.format(
"References: %d (%s)",
s.references,
#ref_strs > 0 and table.concat(ref_strs, ", ") or "none"
))
-- Errors
if result.errors and #result.errors > 0 then
table.insert(lines, "")
table.insert(lines, string.format("## Parse Errors (%d)", #result.errors))
for _, e in ipairs(result.errors) do
table.insert(lines, string.format(" %s: %s", e.file, e.error))
end
end
return table.concat(lines, "\n")
end
---------------------------------------------------------------------------
-- Reference formatting
---------------------------------------------------------------------------
--- Format references grouped by file
-- @param refs {Reference} Array of references
-- @param title string
-- @return string
local function format_refs_by_file(refs, title)
local lines = {}
local by_file = {}
local file_order = {}
for _, ref in ipairs(refs) do
if not by_file[ref.from_file] then
by_file[ref.from_file] = {}
table.insert(file_order, ref.from_file)
end
table.insert(by_file[ref.from_file], ref)
end
table.insert(lines, string.format("# %s — %d references", title, #refs))
table.insert(lines, "")
for _, file in ipairs(file_order) do
local file_refs = by_file[file]
table.sort(file_refs, function(a, b) return a.line < b.line end)
table.insert(lines, "## " .. file)
for _, ref in ipairs(file_refs) do
local extra = ""
if ref.to_name and ref.kind ~= "import" then
extra = " " .. ref.to_name
end
table.insert(lines, string.format(
" L%-5d %-16s%s",
ref.line, ref.kind, extra
))
end
table.insert(lines, "")
end
return table.concat(lines, "\n")
end
---------------------------------------------------------------------------
-- Mermaid graph rendering
---------------------------------------------------------------------------
--- Render a set of edges as a Mermaid graph
-- @param edges {{from, to, label?}}
-- @param title string|nil
-- @param highlight string|nil Node to highlight
-- @return string
local function render_mermaid(edges, title, highlight)
local lines = {}
table.insert(lines, "```mermaid")
table.insert(lines, "graph TD")
-- Collect unique nodes
local nodes = {}
local node_ids = {}
local id_counter = 0
local function node_id(name)
if not node_ids[name] then
id_counter = id_counter + 1
node_ids[name] = "N" .. id_counter
table.insert(nodes, {id = node_ids[name], name = name})
end
return node_ids[name]
end
for _, edge in ipairs(edges) do
local from_id = node_id(edge.from)
local to_id = node_id(edge.to)
if edge.label then
table.insert(lines, string.format(
' %s -->|"%s"| %s', from_id, edge.label, to_id
))
else
table.insert(lines, string.format(
" %s --> %s", from_id, to_id
))
end
end
-- Node labels (short names)
for _, node in ipairs(nodes) do
-- Use short name for display
local short = string.match(node.name, "([^\\]+)$") or node.name
local shape = '["' .. short .. '"]'
table.insert(lines, string.format(" %s%s", node.id, shape))
end
-- Highlight
if highlight and node_ids[highlight] then
table.insert(lines, string.format(
" style %s fill:#f9f,stroke:#333,stroke-width:2px",
node_ids[highlight]
))
end
table.insert(lines, "```")
return table.concat(lines, "\n")
end
return {
render_tree = render_tree,
format_index_stats = format_index_stats,
format_refs_by_file = format_refs_by_file,
render_mermaid = render_mermaid,
}