-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_lib.lua
More file actions
260 lines (230 loc) · 8.41 KB
/
index_lib.lua
File metadata and controls
260 lines (230 loc) · 8.41 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
251
252
253
254
255
256
257
258
259
260
-- Indexing orchestration: scan files, parse, extract, build graph
-- Entry kind: library.lua
local fs = require("fs")
local logger = require("logger")
local registry = require("registry")
local extractor = require("extractor")
local resolver = require("resolver")
local graph = require("graph")
---------------------------------------------------------------------------
-- Project resolution (same pattern as code-tools)
---------------------------------------------------------------------------
local function discover_projects()
local entries, err = registry.find({kind = "fs.directory"})
if err then return nil, err end
local projects = {}
for _, entry in ipairs(entries) do
local meta = entry.meta
if meta and meta["mcp.project"] == true then
local name = meta["mcp.project.name"] or entry.id
projects[name] = {
entry_id = entry.id,
name = name,
is_default = meta["mcp.project.default"] == true,
}
end
end
return projects, nil
end
local function resolve_project(project_name)
local projects, err = discover_projects()
if err then return nil, nil, err end
local target
if project_name and project_name ~= "" then
target = projects[project_name]
if not target then
local names = {}
for name, _ in pairs(projects) do
table.insert(names, name)
end
table.sort(names)
return nil, nil, "Unknown project: " .. project_name
.. ". Available: " .. table.concat(names, ", ")
end
else
for _, p in pairs(projects) do
if p.is_default then target = p; break end
end
if not target then
for _, p in pairs(projects) do target = p; break end
end
if not target then return nil, nil, "No projects configured" end
end
local vol, fs_err = fs.get(target.entry_id)
if fs_err then
return nil, nil, "Failed to access project: " .. tostring(fs_err)
end
return vol, target.name, nil
end
---------------------------------------------------------------------------
-- File scanning
---------------------------------------------------------------------------
--- Recursively find all PHP files under a path
-- @param vol Filesystem volume
-- @param base_path string Starting directory (e.g. "src/")
-- @return {string} Array of file paths
local function scan_php_files(vol, base_path)
local files = {}
base_path = base_path or ""
local function scan_dir(dir)
local ok_dir = vol:isdir(dir == "" and "/" or dir)
if not ok_dir then return end
for entry in vol:readdir(dir == "" and "/" or dir) do
local name = entry.name
local path = dir == "" and name or (dir .. "/" .. name)
if entry.type == "directory" then
-- Skip hidden dirs and common non-code dirs
if string.sub(name, 1, 1) ~= "."
and name ~= "vendor"
and name ~= "node_modules"
and name ~= "storage"
and name ~= "cache" then
scan_dir(path)
end
elseif entry.type == "file" and string.match(name, "%.php$") then
table.insert(files, path)
end
end
end
scan_dir(base_path)
table.sort(files)
return files
end
---------------------------------------------------------------------------
-- Index builder
---------------------------------------------------------------------------
-- Forward declaration
local resolve_cross_file_refs
--- Index a project: scan, parse, extract, resolve, build graph
-- @param state IndexState State to populate (will be cleared first)
-- @param project_name string|nil Project identifier
-- @param base_path string|nil Subdirectory to scan
-- @return table {stats, errors}, error
local function index_project(state, project_name, base_path)
local vol, proj_name, err = resolve_project(project_name)
if err then return nil, err end
-- Clear previous state
graph.clear(state)
state.project = proj_name
-- Scan files
local files = scan_php_files(vol, base_path or "")
if #files == 0 then
return nil, "No PHP files found"
.. (base_path and (" in " .. base_path) or "")
end
logger:info("Indexing project", {
project = proj_name,
path = base_path or "/",
files = #files,
})
-- Parse and extract each file
local parse_errors = {}
local indexed_count = 0
for _, file_path in ipairs(files) do
local content, read_err = vol:readfile(file_path)
if read_err then
table.insert(parse_errors, {
file = file_path,
error = "read: " .. tostring(read_err),
})
else
local extraction, ext_err = extractor.extract_file(content, file_path)
if ext_err then
table.insert(parse_errors, {
file = file_path,
error = ext_err,
})
else
-- Resolve namespace references
resolver.resolve_references(
extraction.references,
extraction.imports,
extraction.namespace
)
resolver.resolve_symbol_relations(
extraction.symbols,
extraction.imports,
extraction.namespace
)
-- Add to graph
graph.add_file(state, file_path, extraction)
indexed_count = indexed_count + 1
end
end
end
-- Rebuild derived indexes
graph.rebuild_indexes(state)
state.indexed_at = os.time()
-- Cross-file resolution pass:
-- Resolve import FQNs that reference symbols we now know about
-- (This helps with references where the target wasn't yet parsed)
resolve_cross_file_refs(state)
logger:info("Indexing complete", {
project = proj_name,
files = indexed_count,
symbols = state.symbol_count,
references = state.ref_count,
errors = #parse_errors,
})
return {
stats = graph.stats(state),
errors = parse_errors,
project = proj_name,
path = base_path,
}, nil
end
---------------------------------------------------------------------------
-- Cross-file resolution
---------------------------------------------------------------------------
function resolve_cross_file_refs(state)
for _, ref in ipairs(state.references) do
if not ref.to_resolved and ref.to_name then
-- Try to find a matching symbol
local matches = graph.find_symbol(state, ref.to_name)
if #matches == 1 then
ref.to_resolved = matches[1].id
elseif #matches > 1 then
-- Ambiguous: try to disambiguate by file's imports
local file_info = state.files[ref.from_file]
if file_info then
for _, imp in ipairs(file_info.imports or {}) do
for _, m in ipairs(matches) do
if m.id == imp.fqn then
ref.to_resolved = m.id
break
end
end
if ref.to_resolved then break end
end
end
-- Still ambiguous? Pick first match
if not ref.to_resolved then
ref.to_resolved = matches[1].id
end
end
end
end
-- Rebuild ref indexes after resolution changes
state.refs_to = {}
state.refs_from = {}
for _, ref in ipairs(state.references) do
local target = ref.to_resolved or ref.to_name
if target then
if not state.refs_to[target] then
state.refs_to[target] = {}
end
table.insert(state.refs_to[target], ref)
end
if ref.from_symbol then
if not state.refs_from[ref.from_symbol] then
state.refs_from[ref.from_symbol] = {}
end
table.insert(state.refs_from[ref.from_symbol], ref)
end
end
end
return {
index_project = index_project,
scan_php_files = scan_php_files,
resolve_project = resolve_project,
}