scip-query 0.3.5 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/{chunk-6FKIA6EI.js → chunk-2F2WH5WQ.js} +12 -12
- package/dist/{chunk-C7H5WBTJ.js → chunk-2UISVZGQ.js} +2 -2
- package/dist/{chunk-EN2Z2CLO.js → chunk-4ZT7UGWW.js} +16 -19
- package/dist/chunk-5AJJGPZE.js +60 -0
- package/dist/chunk-5RKYZSQ6.js +75 -0
- package/dist/chunk-7YBLWIXY.js +115 -0
- package/dist/{chunk-O7Q7FDUJ.js → chunk-A4GWYETB.js} +2 -2
- package/dist/{chunk-CHDJXYBG.js → chunk-A5BGEBM7.js} +2 -2
- package/dist/{chunk-KKCHYLVI.js → chunk-A7YY7IDA.js} +2 -2
- package/dist/chunk-AS7N27JK.js +115 -0
- package/dist/{chunk-F7XU27LU.js → chunk-CQRYLK33.js} +26 -2
- package/dist/{chunk-NFS5W3PP.js → chunk-CQUNEJYM.js} +2 -2
- package/dist/chunk-D4I3ZMN5.js +38 -0
- package/dist/{chunk-J3JSOSUO.js → chunk-E7J7Q7UW.js} +2 -2
- package/dist/{chunk-GEXE2T6I.js → chunk-EOHPASDV.js} +22 -14
- package/dist/chunk-GIBETK3W.js +37 -0
- package/dist/{chunk-SMDCNPMK.js → chunk-H3FPW5YN.js} +2 -2
- package/dist/{chunk-VMM4SYV4.js → chunk-HNURMDF4.js} +16 -3
- package/dist/{chunk-7KIMF5PV.js → chunk-HRDPUTIQ.js} +2 -2
- package/dist/{chunk-OIDHN6GD.js → chunk-I2JM34UV.js} +146 -5
- package/dist/{chunk-EPWLXXBL.js → chunk-IV6NZ426.js} +27 -29
- package/dist/{chunk-VT4JBH6L.js → chunk-KDCQJTYW.js} +2 -2
- package/dist/{chunk-5OMVSV6E.js → chunk-LOVDB4C6.js} +2 -2
- package/dist/chunk-MA3B3IUT.js +75 -0
- package/dist/{chunk-26DOJ63W.js → chunk-N2LH3M2P.js} +13 -2
- package/dist/chunk-NWCJWA36.js +162 -0
- package/dist/{chunk-NG5F43OU.js → chunk-P3VCDYMJ.js} +70 -1
- package/dist/{chunk-P3E6L7KW.js → chunk-P42KQKJZ.js} +83 -3
- package/dist/{chunk-YDBXNPYU.js → chunk-PGHN5UTM.js} +2 -2
- package/dist/{chunk-UGQKAVCD.js → chunk-QCYR4S6T.js} +2 -2
- package/dist/chunk-QGCEAVJD.js +2529 -0
- package/dist/{chunk-KBOQX573.js → chunk-RIEA5DOB.js} +19 -2
- package/dist/{chunk-VIYSWZCO.js → chunk-SL674KAW.js} +31 -3
- package/dist/chunk-SRELHCMG.js +110 -0
- package/dist/chunk-UTRKBUCB.js +87 -0
- package/dist/{chunk-TRESG7OB.js → chunk-VCOJRQPP.js} +2 -2
- package/dist/chunk-VISMEWYP.js +34 -0
- package/dist/{chunk-LFJQVJYJ.js → chunk-VU7FDTWV.js} +2 -2
- package/dist/{chunk-HLUS2HEB.js → chunk-VUBLUTMU.js} +5 -4
- package/dist/{chunk-WGAD3GNR.js → chunk-WNPF2I25.js} +5 -5
- package/dist/{chunk-YY4QGUQ5.js → chunk-X3J4VPWM.js} +2 -2
- package/dist/{chunk-GSH2FPKV.js → chunk-XH56HXLC.js} +2 -2
- package/dist/{chunk-DH7G3DDV.js → chunk-ZU2AQQB5.js} +2 -2
- package/dist/cli.js +2607 -805
- package/dist/{db-viWlyVtv.d.ts → db-C4rPbKkI.d.ts} +6 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +371 -138
- package/dist/postinstall.js +9 -3
- package/dist/queries/affected.d.ts +1 -1
- package/dist/queries/affected.js +2 -2
- package/dist/queries/bottlenecks.d.ts +1 -1
- package/dist/queries/bottlenecks.js +2 -1
- package/dist/queries/by-kind.d.ts +1 -1
- package/dist/queries/by-kind.js +2 -1
- package/dist/queries/call-graph.d.ts +1 -1
- package/dist/queries/call-graph.js +2 -2
- package/dist/queries/change-surface.d.ts +1 -1
- package/dist/queries/change-surface.js +2 -2
- package/dist/queries/code.d.ts +1 -1
- package/dist/queries/code.js +2 -2
- package/dist/queries/complexity-hotspots.d.ts +1 -1
- package/dist/queries/complexity-hotspots.js +2 -2
- package/dist/queries/complexity.d.ts +1 -1
- package/dist/queries/complexity.js +2 -2
- package/dist/queries/convergence.d.ts +1 -1
- package/dist/queries/convergence.js +2 -2
- package/dist/queries/coupling.d.ts +1 -1
- package/dist/queries/coupling.js +2 -2
- package/dist/queries/cycles.d.ts +1 -1
- package/dist/queries/cycles.js +2 -2
- package/dist/queries/dataflow.d.ts +1 -1
- package/dist/queries/dataflow.js +2 -2
- package/dist/queries/dead.d.ts +1 -1
- package/dist/queries/dead.js +3 -3
- package/dist/queries/deep-chains.d.ts +1 -1
- package/dist/queries/deep-chains.js +2 -2
- package/dist/queries/deps.d.ts +1 -1
- package/dist/queries/deps.js +2 -2
- package/dist/queries/diff-impact.d.ts +1 -1
- package/dist/queries/drift.d.ts +1 -1
- package/dist/queries/drift.js +2 -2
- package/dist/queries/extract-candidates.d.ts +1 -1
- package/dist/queries/extract-candidates.js +2 -2
- package/dist/queries/fan.d.ts +1 -1
- package/dist/queries/fan.js +2 -2
- package/dist/queries/files.d.ts +1 -1
- package/dist/queries/health.d.ts +1 -1
- package/dist/queries/health.js +13 -13
- package/dist/queries/hierarchy.d.ts +1 -1
- package/dist/queries/hierarchy.js +2 -2
- package/dist/queries/hotspots.d.ts +1 -1
- package/dist/queries/hotspots.js +2 -1
- package/dist/queries/imports.d.ts +1 -1
- package/dist/queries/imports.js +2 -2
- package/dist/queries/index.d.ts +1 -1
- package/dist/queries/index.js +42 -42
- package/dist/queries/isolated.d.ts +3 -4
- package/dist/queries/isolated.js +3 -3
- package/dist/queries/members.d.ts +1 -1
- package/dist/queries/members.js +2 -2
- package/dist/queries/methods.d.ts +1 -1
- package/dist/queries/methods.js +2 -1
- package/dist/queries/outline.d.ts +1 -1
- package/dist/queries/outline.js +2 -2
- package/dist/queries/passthrough-candidates.d.ts +1 -1
- package/dist/queries/passthrough-candidates.js +2 -2
- package/dist/queries/redundant-reexports.d.ts +1 -1
- package/dist/queries/redundant-reexports.js +3 -3
- package/dist/queries/refs.d.ts +1 -1
- package/dist/queries/refs.js +2 -2
- package/dist/queries/similar-chains.d.ts +1 -1
- package/dist/queries/similar-chains.js +2 -2
- package/dist/queries/similar-files.d.ts +1 -1
- package/dist/queries/similar-files.js +2 -2
- package/dist/queries/similar-signatures.d.ts +5 -3
- package/dist/queries/similar-signatures.js +2 -1
- package/dist/queries/similar.d.ts +1 -1
- package/dist/queries/similar.js +2 -2
- package/dist/queries/slice.d.ts +1 -1
- package/dist/queries/slice.js +2 -2
- package/dist/queries/stale-abstractions.d.ts +1 -1
- package/dist/queries/stale-abstractions.js +2 -2
- package/dist/queries/stats.d.ts +1 -1
- package/dist/queries/surface.d.ts +1 -1
- package/dist/queries/surface.js +2 -2
- package/dist/queries/symbols.d.ts +1 -1
- package/dist/queries/symbols.js +2 -2
- package/dist/queries/system.d.ts +1 -1
- package/dist/queries/system.js +2 -2
- package/dist/queries/trace.d.ts +1 -1
- package/dist/queries/trace.js +2 -2
- package/dist/queries/wrapper-candidates.d.ts +1 -1
- package/dist/queries/wrapper-candidates.js +2 -2
- package/dist/reindex-worker.js +213 -62
- package/package.json +1 -1
- package/skills/scip-language-playbook/SKILL.md +371 -0
- package/dist/chunk-4JCSOF2O.js +0 -97
- package/dist/chunk-AXQKUYKF.js +0 -1442
- package/dist/chunk-CPVAQJEC.js +0 -46
- package/dist/chunk-EOROMIFO.js +0 -41
- package/dist/chunk-GU2H5QRN.js +0 -28
- package/dist/chunk-LQJUPXQY.js +0 -109
- package/dist/chunk-MPGIHELS.js +0 -39
- package/dist/chunk-TOIEB3LG.js +0 -78
- package/dist/chunk-UQEQ6AHX.js +0 -60
- package/dist/chunk-VJJKSGIX.js +0 -121
- package/dist/chunk-YGGFLMTM.js +0 -83
- package/dist/chunk-ZEUCXQBN.js +0 -71
|
@@ -0,0 +1,2529 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isFunctionLikeSymbol,
|
|
3
|
+
isModuleLikeSymbol,
|
|
4
|
+
leafName,
|
|
5
|
+
leafSuffix,
|
|
6
|
+
parseSymbol,
|
|
7
|
+
shortenSymbol
|
|
8
|
+
} from "./chunk-QIXNAB5K.js";
|
|
9
|
+
|
|
10
|
+
// src/source-analysis.ts
|
|
11
|
+
import {
|
|
12
|
+
existsSync,
|
|
13
|
+
readFileSync
|
|
14
|
+
} from "fs";
|
|
15
|
+
import {
|
|
16
|
+
basename,
|
|
17
|
+
dirname,
|
|
18
|
+
extname,
|
|
19
|
+
join,
|
|
20
|
+
relative,
|
|
21
|
+
resolve
|
|
22
|
+
} from "path";
|
|
23
|
+
var SOURCE_IMPORT_CACHE = /* @__PURE__ */ new WeakMap();
|
|
24
|
+
var SOURCE_EXPORT_CACHE = /* @__PURE__ */ new WeakMap();
|
|
25
|
+
var SOURCE_TEXT_CACHE = /* @__PURE__ */ new WeakMap();
|
|
26
|
+
var SOURCE_CALL_CACHE = /* @__PURE__ */ new WeakMap();
|
|
27
|
+
var SOURCE_BINDING_CACHE = /* @__PURE__ */ new WeakMap();
|
|
28
|
+
var INDEXED_PATH_CACHE = /* @__PURE__ */ new WeakMap();
|
|
29
|
+
var SOURCE_EXTENSIONS = [".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs"];
|
|
30
|
+
var PYTHON_SOURCE_EXTENSIONS = [".py", ".pyi"];
|
|
31
|
+
var JVM_SOURCE_EXTENSIONS = [".java", ".scala", ".kt", ".kts"];
|
|
32
|
+
var RUST_SOURCE_EXTENSIONS = [".rs"];
|
|
33
|
+
var RUBY_SOURCE_EXTENSIONS = [".rb"];
|
|
34
|
+
var C_LIKE_SOURCE_EXTENSIONS = [".c", ".h", ".cc", ".cpp", ".cxx", ".hpp", ".hh", ".hxx"];
|
|
35
|
+
var DOTNET_SOURCE_EXTENSIONS = [".cs", ".vb"];
|
|
36
|
+
var DART_SOURCE_EXTENSIONS = [".dart"];
|
|
37
|
+
var PHP_SOURCE_EXTENSIONS = [".php"];
|
|
38
|
+
function getSourceImports(db, relativePath) {
|
|
39
|
+
const cache = getCachedMap(SOURCE_IMPORT_CACHE, db);
|
|
40
|
+
const normalized = normalizePath(relativePath);
|
|
41
|
+
const cached = cache.get(normalized);
|
|
42
|
+
if (cached) {
|
|
43
|
+
return cached;
|
|
44
|
+
}
|
|
45
|
+
const fullPath = join(db.config.projectRoot, normalized);
|
|
46
|
+
if (!existsSync(fullPath)) {
|
|
47
|
+
cache.set(normalized, []);
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
const source = readFileSync(fullPath, "utf-8");
|
|
51
|
+
const parsed = isPythonSourcePath(normalized) ? parsePythonImports(db, normalized, source) : isJavaScriptSourcePath(normalized) ? parseJavaScriptImports(db, normalized, source) : isJvmSourcePath(normalized) ? parseJvmImports(db, normalized, source) : isRustSourcePath(normalized) ? parseRustImports(db, normalized, source) : isRubySourcePath(normalized) ? parseRubyImports(db, normalized, source) : isCLikeSourcePath(normalized) ? parseCLikeImports(db, normalized, source) : isDotNetSourcePath(normalized) ? parseDotNetImports(db, normalized, source) : isDartSourcePath(normalized) ? parseDartImports(db, normalized, source) : isPhpSourcePath(normalized) ? parsePhpImports(db, normalized, source) : [];
|
|
52
|
+
cache.set(normalized, parsed);
|
|
53
|
+
return parsed;
|
|
54
|
+
}
|
|
55
|
+
function getSourceExports(db, relativePath) {
|
|
56
|
+
const cache = getCachedMap(SOURCE_EXPORT_CACHE, db);
|
|
57
|
+
const normalized = normalizePath(relativePath);
|
|
58
|
+
const cached = cache.get(normalized);
|
|
59
|
+
if (cached) {
|
|
60
|
+
return cached;
|
|
61
|
+
}
|
|
62
|
+
const fullPath = join(db.config.projectRoot, normalized);
|
|
63
|
+
if (!existsSync(fullPath)) {
|
|
64
|
+
cache.set(normalized, []);
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
const source = readFileSync(fullPath, "utf-8");
|
|
68
|
+
const parsed = isDartSourcePath(normalized) ? parseDartExports(db, normalized, source) : isRustSourcePath(normalized) ? parseRustExports(db, normalized, source) : [];
|
|
69
|
+
cache.set(normalized, parsed);
|
|
70
|
+
return parsed;
|
|
71
|
+
}
|
|
72
|
+
function getSourceCalls(db, relativePath, opts = {}) {
|
|
73
|
+
const normalized = normalizePath(relativePath);
|
|
74
|
+
if (!isPythonSourcePath(normalized) && !isJavaScriptSourcePath(normalized)) {
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
const cache = getCachedMap(SOURCE_CALL_CACHE, db);
|
|
78
|
+
const key = `${normalized}:${opts.startLine ?? 0}:${opts.endLine ?? Number.MAX_SAFE_INTEGER}`;
|
|
79
|
+
const cached = cache.get(key);
|
|
80
|
+
if (cached) {
|
|
81
|
+
return cached;
|
|
82
|
+
}
|
|
83
|
+
const source = getSourceText(db, normalized);
|
|
84
|
+
if (!source) {
|
|
85
|
+
cache.set(key, []);
|
|
86
|
+
return [];
|
|
87
|
+
}
|
|
88
|
+
const lines = source.split("\n");
|
|
89
|
+
const startLine = Math.max(0, opts.startLine ?? 0);
|
|
90
|
+
const endLine = Math.min(lines.length - 1, opts.endLine ?? lines.length - 1);
|
|
91
|
+
const scopedLines = lines.slice(startLine, endLine + 1);
|
|
92
|
+
const calls = isPythonSourcePath(normalized) ? parsePythonCalls(scopedLines, startLine) : parseJavaScriptCalls(scopedLines, startLine);
|
|
93
|
+
cache.set(key, calls);
|
|
94
|
+
return calls;
|
|
95
|
+
}
|
|
96
|
+
function getSourceConstructorBindings(db, relativePath, opts = {}) {
|
|
97
|
+
const normalized = normalizePath(relativePath);
|
|
98
|
+
if (!isPythonSourcePath(normalized) && !isJavaScriptSourcePath(normalized)) {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
const cache = getCachedMap(SOURCE_BINDING_CACHE, db);
|
|
102
|
+
const key = `${normalized}:${opts.startLine ?? 0}:${opts.endLine ?? Number.MAX_SAFE_INTEGER}`;
|
|
103
|
+
const cached = cache.get(key);
|
|
104
|
+
if (cached) {
|
|
105
|
+
return cached;
|
|
106
|
+
}
|
|
107
|
+
const source = getSourceText(db, normalized);
|
|
108
|
+
if (!source) {
|
|
109
|
+
cache.set(key, []);
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
const lines = source.split("\n");
|
|
113
|
+
const startLine = Math.max(0, opts.startLine ?? 0);
|
|
114
|
+
const endLine = Math.min(lines.length - 1, opts.endLine ?? lines.length - 1);
|
|
115
|
+
const scopedLines = lines.slice(startLine, endLine + 1);
|
|
116
|
+
const bindings = isPythonSourcePath(normalized) ? parsePythonConstructorBindings(scopedLines) : parseJavaScriptConstructorBindings(scopedLines);
|
|
117
|
+
cache.set(key, bindings);
|
|
118
|
+
return bindings;
|
|
119
|
+
}
|
|
120
|
+
function findIdentifierLines(db, relativePath, identifier, opts = {}) {
|
|
121
|
+
if (!identifier) {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
const source = getSourceText(db, normalizePath(relativePath));
|
|
125
|
+
if (!source) {
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
const lines = stripCommentsAndStrings(source).split("\n");
|
|
129
|
+
const regex = new RegExp(`\\b${escapeRegex(identifier)}\\b`);
|
|
130
|
+
const results = [];
|
|
131
|
+
for (let line = 0; line < lines.length; line++) {
|
|
132
|
+
if (typeof opts.excludeStartLine === "number" && typeof opts.excludeEndLine === "number" && line >= opts.excludeStartLine && line <= opts.excludeEndLine) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (regex.test(lines[line] ?? "")) {
|
|
136
|
+
results.push(line);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return results;
|
|
140
|
+
}
|
|
141
|
+
function parseJavaScriptImports(db, importerPath, source) {
|
|
142
|
+
return parseJavaScriptImportStatements(source).flatMap((statement) => parseJavaScriptImportStatement(
|
|
143
|
+
db,
|
|
144
|
+
importerPath,
|
|
145
|
+
statement.clause,
|
|
146
|
+
statement.specifier,
|
|
147
|
+
statement.start,
|
|
148
|
+
statement.end,
|
|
149
|
+
source
|
|
150
|
+
));
|
|
151
|
+
}
|
|
152
|
+
function parseJavaScriptImportStatements(source) {
|
|
153
|
+
const statements = [];
|
|
154
|
+
const importFromRegex = /^[ \t]*import\s+([\s\S]*?)\s+from\s+['"]([^'"]+)['"]\s*;?/gm;
|
|
155
|
+
for (const match of source.matchAll(importFromRegex)) {
|
|
156
|
+
const full = match[0];
|
|
157
|
+
const clause = match[1];
|
|
158
|
+
const specifier = match[2];
|
|
159
|
+
if (!full || !specifier || typeof match.index !== "number") continue;
|
|
160
|
+
statements.push({
|
|
161
|
+
clause,
|
|
162
|
+
specifier,
|
|
163
|
+
start: match.index,
|
|
164
|
+
end: match.index + full.length
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const sideEffectRegex = /^[ \t]*import\s+['"]([^'"]+)['"]\s*;?/gm;
|
|
168
|
+
for (const match of source.matchAll(sideEffectRegex)) {
|
|
169
|
+
const full = match[0];
|
|
170
|
+
const specifier = match[1];
|
|
171
|
+
if (!full || !specifier || typeof match.index !== "number") continue;
|
|
172
|
+
statements.push({
|
|
173
|
+
clause: null,
|
|
174
|
+
specifier,
|
|
175
|
+
start: match.index,
|
|
176
|
+
end: match.index + full.length
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return statements.sort((a, b) => a.start - b.start);
|
|
180
|
+
}
|
|
181
|
+
function parseJavaScriptImportStatement(db, importerPath, clause, specifier, start, end, source) {
|
|
182
|
+
const resolvedSource = resolveImportPath(db, importerPath, specifier);
|
|
183
|
+
const body = buildUsageBody(source, start, end);
|
|
184
|
+
if (!clause) {
|
|
185
|
+
return [{
|
|
186
|
+
importedName: "*",
|
|
187
|
+
localName: null,
|
|
188
|
+
sourcePath: resolvedSource,
|
|
189
|
+
kind: "side-effect",
|
|
190
|
+
used: true,
|
|
191
|
+
usedMembers: []
|
|
192
|
+
}];
|
|
193
|
+
}
|
|
194
|
+
const bindings = parseImportClause(clause).map((binding) => ({
|
|
195
|
+
...binding,
|
|
196
|
+
sourcePath: resolvedSource
|
|
197
|
+
}));
|
|
198
|
+
return bindings.map((binding) => {
|
|
199
|
+
if (binding.kind === "namespace") {
|
|
200
|
+
const usedMembers = collectNamespaceMembers(body, binding.localName);
|
|
201
|
+
return {
|
|
202
|
+
...binding,
|
|
203
|
+
used: usedMembers.length > 0 || hasIdentifierUsage(body, binding.localName),
|
|
204
|
+
usedMembers
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
if (binding.kind === "side-effect") {
|
|
208
|
+
return { ...binding, used: true, usedMembers: [] };
|
|
209
|
+
}
|
|
210
|
+
return {
|
|
211
|
+
...binding,
|
|
212
|
+
used: binding.localName ? hasIdentifierUsage(body, binding.localName) : false,
|
|
213
|
+
usedMembers: []
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
function parseJvmImports(db, importerPath, source) {
|
|
218
|
+
const statements = [];
|
|
219
|
+
for (const match of source.matchAll(/^[ \t]*import\s+(?:static\s+)?(.+?)\s*;?$/gm)) {
|
|
220
|
+
const clause = match[1]?.trim();
|
|
221
|
+
const full = match[0];
|
|
222
|
+
if (!clause || !full || typeof match.index !== "number") continue;
|
|
223
|
+
const body = buildUsageBody(source, match.index, match.index + full.length);
|
|
224
|
+
statements.push(...parseJvmImportClause(db, importerPath, clause, body));
|
|
225
|
+
}
|
|
226
|
+
return statements;
|
|
227
|
+
}
|
|
228
|
+
function parseJvmImportClause(db, importerPath, clause, body) {
|
|
229
|
+
if (clause.includes("{") && clause.includes("}")) {
|
|
230
|
+
const prefix = clause.slice(0, clause.indexOf("{")).replace(/\.$/, "").trim();
|
|
231
|
+
const inner = clause.slice(clause.indexOf("{") + 1, clause.lastIndexOf("}")).trim();
|
|
232
|
+
return splitTopLevel(inner).flatMap((entry) => {
|
|
233
|
+
const cleaned = entry.trim();
|
|
234
|
+
if (!cleaned) return [];
|
|
235
|
+
const [importedPart, aliasPart] = cleaned.includes("=>") ? cleaned.split(/\s*=>\s*/) : cleaned.split(/\s+as\s+/);
|
|
236
|
+
const importedName = importedPart?.trim();
|
|
237
|
+
if (!importedName || importedName === "_") return [];
|
|
238
|
+
const localName = (aliasPart ?? importedName.split(".").pop() ?? importedName).trim();
|
|
239
|
+
const qualified = importedName === "_" ? prefix : `${prefix}.${importedName}`.replace(/\.\./g, ".");
|
|
240
|
+
return [buildSimpleImport(db, importerPath, body, qualified, importedName, localName)];
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return [buildSimpleImport(
|
|
244
|
+
db,
|
|
245
|
+
importerPath,
|
|
246
|
+
body,
|
|
247
|
+
clause,
|
|
248
|
+
clause.split(".").pop() ?? clause,
|
|
249
|
+
clause.split(".").pop() ?? clause
|
|
250
|
+
)];
|
|
251
|
+
}
|
|
252
|
+
function parseRustImports(db, importerPath, source) {
|
|
253
|
+
const statements = [];
|
|
254
|
+
for (const match of source.matchAll(/^[ \t]*use\s+(.+?)\s*;$/gm)) {
|
|
255
|
+
const clause = match[1]?.trim();
|
|
256
|
+
const full = match[0];
|
|
257
|
+
if (!clause || !full || typeof match.index !== "number") continue;
|
|
258
|
+
const body = buildUsageBody(source, match.index, match.index + full.length);
|
|
259
|
+
statements.push(...parseRustUseClause(db, importerPath, clause, body));
|
|
260
|
+
}
|
|
261
|
+
return statements;
|
|
262
|
+
}
|
|
263
|
+
function parseRustUseClause(db, importerPath, clause, body) {
|
|
264
|
+
const trimmed = clause.trim();
|
|
265
|
+
if (trimmed.includes("{") && trimmed.includes("}")) {
|
|
266
|
+
const prefix = trimmed.slice(0, trimmed.indexOf("{")).replace(/::$/, "").trim();
|
|
267
|
+
const inner = trimmed.slice(trimmed.indexOf("{") + 1, trimmed.lastIndexOf("}")).trim();
|
|
268
|
+
return splitTopLevel(inner).flatMap((entry) => {
|
|
269
|
+
const cleaned = entry.trim();
|
|
270
|
+
if (!cleaned || cleaned === "self") return [];
|
|
271
|
+
const [importedPart2, aliasPart2] = cleaned.split(/\s+as\s+/);
|
|
272
|
+
const importedName2 = importedPart2?.trim();
|
|
273
|
+
if (!importedName2) return [];
|
|
274
|
+
const localName2 = (aliasPart2 ?? importedName2.split("::").pop() ?? importedName2).trim();
|
|
275
|
+
const moduleSpecifier = `${prefix}::${importedName2}`.replace(/::::/g, "::");
|
|
276
|
+
return [buildSimpleImport(
|
|
277
|
+
db,
|
|
278
|
+
importerPath,
|
|
279
|
+
body,
|
|
280
|
+
moduleSpecifier,
|
|
281
|
+
importedName2.split("::").pop() ?? importedName2,
|
|
282
|
+
localName2,
|
|
283
|
+
resolveRustImportPath(db, importerPath, prefix)
|
|
284
|
+
)];
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
const [importedPart, aliasPart] = trimmed.split(/\s+as\s+/);
|
|
288
|
+
const importedName = importedPart?.trim() ?? trimmed;
|
|
289
|
+
const localName = (aliasPart ?? importedName.split("::").pop() ?? importedName).trim();
|
|
290
|
+
const resolved = resolveRustImportPath(db, importerPath, importedName) ?? resolveRustImportPath(db, importerPath, importedName.split("::").slice(0, -1).join("::"));
|
|
291
|
+
return [buildSimpleImport(
|
|
292
|
+
db,
|
|
293
|
+
importerPath,
|
|
294
|
+
body,
|
|
295
|
+
importedName,
|
|
296
|
+
importedName.split("::").pop() ?? importedName,
|
|
297
|
+
localName,
|
|
298
|
+
resolved
|
|
299
|
+
)];
|
|
300
|
+
}
|
|
301
|
+
function parseRustExports(db, importerPath, source) {
|
|
302
|
+
const statements = [];
|
|
303
|
+
for (const match of source.matchAll(/^[ \t]*pub\s+use\s+(.+?)\s*;$/gm)) {
|
|
304
|
+
const clause = match[1]?.trim();
|
|
305
|
+
if (!clause) continue;
|
|
306
|
+
statements.push(...parseRustExportClause(db, importerPath, clause));
|
|
307
|
+
}
|
|
308
|
+
return statements;
|
|
309
|
+
}
|
|
310
|
+
function parseRustExportClause(db, importerPath, clause) {
|
|
311
|
+
const trimmed = clause.trim();
|
|
312
|
+
if (trimmed.includes("{") && trimmed.includes("}")) {
|
|
313
|
+
const prefix = trimmed.slice(0, trimmed.indexOf("{")).replace(/::$/, "").trim();
|
|
314
|
+
const inner = trimmed.slice(trimmed.indexOf("{") + 1, trimmed.lastIndexOf("}")).trim();
|
|
315
|
+
return splitTopLevel(inner).flatMap((entry) => {
|
|
316
|
+
const cleaned = entry.trim();
|
|
317
|
+
if (!cleaned || cleaned === "self") return [];
|
|
318
|
+
const [qualifiedPart] = cleaned.split(/\s+as\s+/);
|
|
319
|
+
const qualified = `${prefix}::${qualifiedPart?.trim() ?? cleaned}`.replace(/::::/g, "::");
|
|
320
|
+
return [buildRustExport(db, importerPath, qualified)];
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
return [buildRustExport(db, importerPath, trimmed)];
|
|
324
|
+
}
|
|
325
|
+
function buildRustExport(db, importerPath, specifier) {
|
|
326
|
+
return {
|
|
327
|
+
specifier,
|
|
328
|
+
sourcePath: resolveRustImportPath(db, importerPath, specifier) ?? resolveRustImportPath(db, importerPath, specifier.split("::").slice(0, -1).join("::"))
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function parseRubyImports(db, importerPath, source) {
|
|
332
|
+
const statements = [];
|
|
333
|
+
for (const match of source.matchAll(/^[ \t]*(require_relative|require)\s+["']([^"']+)["']\s*$/gm)) {
|
|
334
|
+
const kind = match[1];
|
|
335
|
+
const specifier = match[2];
|
|
336
|
+
const full = match[0];
|
|
337
|
+
if (!kind || !specifier || !full || typeof match.index !== "number") continue;
|
|
338
|
+
const body = buildUsageBody(source, match.index, match.index + full.length);
|
|
339
|
+
const sourcePath = kind === "require_relative" ? resolveRubyImportPath(db, importerPath, specifier) : null;
|
|
340
|
+
if (sourcePath) {
|
|
341
|
+
const localName = rubyConstantName(specifier);
|
|
342
|
+
statements.push({
|
|
343
|
+
importedName: localName,
|
|
344
|
+
localName,
|
|
345
|
+
sourcePath,
|
|
346
|
+
kind: "named",
|
|
347
|
+
used: hasIdentifierUsage(body, localName),
|
|
348
|
+
usedMembers: []
|
|
349
|
+
});
|
|
350
|
+
continue;
|
|
351
|
+
}
|
|
352
|
+
statements.push({
|
|
353
|
+
importedName: specifier,
|
|
354
|
+
localName: null,
|
|
355
|
+
sourcePath,
|
|
356
|
+
kind: "side-effect",
|
|
357
|
+
used: true,
|
|
358
|
+
usedMembers: []
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
return statements;
|
|
362
|
+
}
|
|
363
|
+
function rubyConstantName(specifier) {
|
|
364
|
+
return basename(specifier).replace(/\.[^.]+$/, "").split("_").filter(Boolean).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join("");
|
|
365
|
+
}
|
|
366
|
+
function parseCLikeImports(db, importerPath, source) {
|
|
367
|
+
const statements = [];
|
|
368
|
+
for (const match of source.matchAll(/^[ \t]*#include\s+[<"]([^">]+)[">]\s*$/gm)) {
|
|
369
|
+
const specifier = match[1]?.trim();
|
|
370
|
+
const full = match[0];
|
|
371
|
+
if (!specifier || !full || typeof match.index !== "number") continue;
|
|
372
|
+
const body = buildUsageBody(source, match.index, match.index + full.length);
|
|
373
|
+
const localName = basename(specifier).replace(/\.[^.]+$/, "");
|
|
374
|
+
statements.push({
|
|
375
|
+
importedName: specifier,
|
|
376
|
+
localName,
|
|
377
|
+
sourcePath: resolveCLikeImportPath(db, importerPath, specifier),
|
|
378
|
+
kind: "named",
|
|
379
|
+
used: hasIdentifierUsage(body, localName),
|
|
380
|
+
usedMembers: []
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
return statements;
|
|
384
|
+
}
|
|
385
|
+
function parseDotNetImports(db, importerPath, source) {
|
|
386
|
+
const statements = [];
|
|
387
|
+
const lineRegex = isVisualBasicSourcePath(importerPath) ? /^[ \t]*Imports\s+(.+?)\s*$/gm : /^[ \t]*using\s+(.+?)\s*;$/gm;
|
|
388
|
+
for (const match of source.matchAll(lineRegex)) {
|
|
389
|
+
const clause = match[1]?.trim();
|
|
390
|
+
const full = match[0];
|
|
391
|
+
if (!clause || !full || typeof match.index !== "number") continue;
|
|
392
|
+
const body = buildUsageBody(source, match.index, match.index + full.length);
|
|
393
|
+
const [aliasPart, targetPart] = isVisualBasicSourcePath(importerPath) ? clause.split(/\s*=\s*/) : clause.split(/\s*=\s*/);
|
|
394
|
+
const hasAlias = Boolean(targetPart);
|
|
395
|
+
const qualified = (hasAlias ? targetPart : aliasPart)?.trim() ?? clause;
|
|
396
|
+
const importedName = qualified.split(".").pop() ?? qualified;
|
|
397
|
+
const localName = hasAlias ? aliasPart?.trim() ?? importedName : importedName;
|
|
398
|
+
statements.push(buildSimpleImport(
|
|
399
|
+
db,
|
|
400
|
+
importerPath,
|
|
401
|
+
body,
|
|
402
|
+
qualified,
|
|
403
|
+
importedName,
|
|
404
|
+
localName,
|
|
405
|
+
resolveQualifiedImportPath(db, qualified, DOTNET_SOURCE_EXTENSIONS)
|
|
406
|
+
));
|
|
407
|
+
}
|
|
408
|
+
return statements;
|
|
409
|
+
}
|
|
410
|
+
function parseDartImports(db, importerPath, source) {
|
|
411
|
+
const statements = [];
|
|
412
|
+
for (const match of source.matchAll(/^[ \t]*import\s+['"]([^'"]+)['"](?:\s+as\s+([A-Za-z_]\w*))?[\s\S]*?;$/gm)) {
|
|
413
|
+
const specifier = match[1]?.trim();
|
|
414
|
+
const alias = match[2]?.trim() ?? null;
|
|
415
|
+
const full = match[0];
|
|
416
|
+
if (!specifier || !full || typeof match.index !== "number") continue;
|
|
417
|
+
const body = buildUsageBody(source, match.index, match.index + full.length);
|
|
418
|
+
statements.push({
|
|
419
|
+
importedName: specifier,
|
|
420
|
+
localName: alias,
|
|
421
|
+
sourcePath: resolveDartImportPath(db, importerPath, specifier),
|
|
422
|
+
kind: alias ? "namespace" : "side-effect",
|
|
423
|
+
used: alias ? hasIdentifierUsage(body, alias) : true,
|
|
424
|
+
usedMembers: alias ? collectNamespaceMembers(body, alias) : []
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
return statements;
|
|
428
|
+
}
|
|
429
|
+
function parseDartExports(db, importerPath, source) {
|
|
430
|
+
const statements = [];
|
|
431
|
+
for (const match of source.matchAll(/^[ \t]*export\s+['"]([^'"]+)['"][\s\S]*?;$/gm)) {
|
|
432
|
+
const specifier = match[1]?.trim();
|
|
433
|
+
if (!specifier) continue;
|
|
434
|
+
statements.push({
|
|
435
|
+
specifier,
|
|
436
|
+
sourcePath: resolveDartImportPath(db, importerPath, specifier)
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
return statements;
|
|
440
|
+
}
|
|
441
|
+
function parsePhpImports(db, importerPath, source) {
|
|
442
|
+
const statements = [];
|
|
443
|
+
for (const match of source.matchAll(/^[ \t]*use\s+(.+?)\s*;$/gm)) {
|
|
444
|
+
const clause = match[1]?.trim();
|
|
445
|
+
const full = match[0];
|
|
446
|
+
if (!clause || !full || typeof match.index !== "number") continue;
|
|
447
|
+
const body = buildUsageBody(source, match.index, match.index + full.length);
|
|
448
|
+
for (const entry of splitTopLevel(clause)) {
|
|
449
|
+
const cleaned = entry.trim();
|
|
450
|
+
if (!cleaned) continue;
|
|
451
|
+
const [qualifiedPart, aliasPart] = cleaned.split(/\s+as\s+/i);
|
|
452
|
+
const qualified = qualifiedPart?.trim() ?? cleaned;
|
|
453
|
+
const importedName = qualified.split("\\").pop() ?? qualified;
|
|
454
|
+
const localName = (aliasPart ?? importedName).trim();
|
|
455
|
+
statements.push(buildSimpleImport(
|
|
456
|
+
db,
|
|
457
|
+
importerPath,
|
|
458
|
+
body,
|
|
459
|
+
qualified,
|
|
460
|
+
importedName,
|
|
461
|
+
localName,
|
|
462
|
+
resolveQualifiedImportPath(db, qualified.replace(/\\/g, "."), PHP_SOURCE_EXTENSIONS)
|
|
463
|
+
));
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
return statements;
|
|
467
|
+
}
|
|
468
|
+
function buildSimpleImport(db, importerPath, body, qualifiedName, importedName, localName, sourcePath) {
|
|
469
|
+
return {
|
|
470
|
+
importedName,
|
|
471
|
+
localName,
|
|
472
|
+
sourcePath: sourcePath ?? resolveQualifiedImportPath(db, qualifiedName, extensionFamilyFor(importerPath)),
|
|
473
|
+
kind: "named",
|
|
474
|
+
used: hasIdentifierUsage(body, localName),
|
|
475
|
+
usedMembers: []
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
function parsePythonCalls(lines, baseLine) {
|
|
479
|
+
const calls = [];
|
|
480
|
+
const controlKeywords = /* @__PURE__ */ new Set([
|
|
481
|
+
"if",
|
|
482
|
+
"for",
|
|
483
|
+
"while",
|
|
484
|
+
"with",
|
|
485
|
+
"except",
|
|
486
|
+
"elif",
|
|
487
|
+
"return",
|
|
488
|
+
"yield",
|
|
489
|
+
"assert",
|
|
490
|
+
"raise",
|
|
491
|
+
"lambda",
|
|
492
|
+
"class",
|
|
493
|
+
"def"
|
|
494
|
+
]);
|
|
495
|
+
for (let index = 0; index < lines.length; index++) {
|
|
496
|
+
const rawLine = lines[index] ?? "";
|
|
497
|
+
const stripped = stripCommentsAndStrings(rawLine);
|
|
498
|
+
if (!stripped.trim()) continue;
|
|
499
|
+
const attributeMatches = [...stripped.matchAll(/\b([A-Za-z_][\w]*)\s*\.\s*([A-Za-z_][\w]*)\s*\(/g)];
|
|
500
|
+
const attributeRanges = attributeMatches.map((match) => ({
|
|
501
|
+
start: match.index ?? -1,
|
|
502
|
+
end: (match.index ?? -1) + match[0].length
|
|
503
|
+
}));
|
|
504
|
+
for (const match of attributeMatches) {
|
|
505
|
+
const receiverName = match[1];
|
|
506
|
+
const calleeName = match[2];
|
|
507
|
+
if (!receiverName || !calleeName) continue;
|
|
508
|
+
calls.push({
|
|
509
|
+
receiverName,
|
|
510
|
+
calleeName,
|
|
511
|
+
line: baseLine + index
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
for (const match of stripped.matchAll(/\b([A-Za-z_][\w]*)\s*\(/g)) {
|
|
515
|
+
const calleeName = match[1];
|
|
516
|
+
const start = match.index ?? -1;
|
|
517
|
+
if (!calleeName || start < 0) continue;
|
|
518
|
+
if (controlKeywords.has(calleeName)) continue;
|
|
519
|
+
if (attributeRanges.some((range) => start >= range.start && start < range.end)) continue;
|
|
520
|
+
const prefix = stripped.slice(0, start).trimEnd();
|
|
521
|
+
if (prefix.endsWith("def") || prefix.endsWith("class") || prefix.endsWith("async def")) {
|
|
522
|
+
continue;
|
|
523
|
+
}
|
|
524
|
+
calls.push({
|
|
525
|
+
receiverName: null,
|
|
526
|
+
calleeName,
|
|
527
|
+
line: baseLine + index
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
return calls;
|
|
532
|
+
}
|
|
533
|
+
function parseJavaScriptCalls(lines, baseLine) {
|
|
534
|
+
const calls = [];
|
|
535
|
+
const controlKeywords = /* @__PURE__ */ new Set([
|
|
536
|
+
"if",
|
|
537
|
+
"for",
|
|
538
|
+
"while",
|
|
539
|
+
"switch",
|
|
540
|
+
"catch",
|
|
541
|
+
"function",
|
|
542
|
+
"class",
|
|
543
|
+
"return",
|
|
544
|
+
"typeof",
|
|
545
|
+
"import"
|
|
546
|
+
]);
|
|
547
|
+
for (let index = 0; index < lines.length; index++) {
|
|
548
|
+
const stripped = stripCommentsAndStrings(lines[index] ?? "");
|
|
549
|
+
if (!stripped.trim()) continue;
|
|
550
|
+
const attributeMatches = [
|
|
551
|
+
...stripped.matchAll(/\b([A-Za-z_$][\w$]*)\s*(?:\?\.|\.)\s*([A-Za-z_$][\w$]*)\s*\(/g),
|
|
552
|
+
...stripped.matchAll(/\bthis\s*(?:\?\.|\.)\s*([A-Za-z_$][\w$]*)\s*\(/g)
|
|
553
|
+
];
|
|
554
|
+
const attributeRanges = attributeMatches.map((match) => ({
|
|
555
|
+
start: match.index ?? -1,
|
|
556
|
+
end: (match.index ?? -1) + match[0].length
|
|
557
|
+
}));
|
|
558
|
+
for (const match of attributeMatches) {
|
|
559
|
+
const receiverName = match[2] ? match[1] : "this";
|
|
560
|
+
const calleeName = match[2] ?? match[1];
|
|
561
|
+
if (!receiverName || !calleeName) continue;
|
|
562
|
+
calls.push({
|
|
563
|
+
receiverName,
|
|
564
|
+
calleeName,
|
|
565
|
+
line: baseLine + index
|
|
566
|
+
});
|
|
567
|
+
}
|
|
568
|
+
for (const match of stripped.matchAll(/\b([A-Za-z_$][\w$]*)\s*\(/g)) {
|
|
569
|
+
const calleeName = match[1];
|
|
570
|
+
const start = match.index ?? -1;
|
|
571
|
+
if (!calleeName || start < 0) continue;
|
|
572
|
+
if (controlKeywords.has(calleeName)) continue;
|
|
573
|
+
if (attributeRanges.some((range) => start >= range.start && start < range.end)) continue;
|
|
574
|
+
const prefix = stripped.slice(0, start).trimEnd();
|
|
575
|
+
if (prefix.endsWith("function") || prefix.endsWith("class") || prefix.endsWith("new")) {
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
calls.push({
|
|
579
|
+
receiverName: null,
|
|
580
|
+
calleeName,
|
|
581
|
+
line: baseLine + index
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return calls;
|
|
586
|
+
}
|
|
587
|
+
function parsePythonConstructorBindings(lines) {
|
|
588
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
589
|
+
for (const rawLine of lines) {
|
|
590
|
+
const stripped = stripCommentsAndStrings(rawLine);
|
|
591
|
+
const constructorMatch = stripped.match(/\b([A-Za-z_][\w]*)\s*=\s*([A-Z][\w]*)\s*\(/);
|
|
592
|
+
if (constructorMatch?.[1] && constructorMatch[2]) {
|
|
593
|
+
bindings.set(constructorMatch[1], constructorMatch[2]);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
return [...bindings.entries()].map(([localName, typeName]) => ({ localName, typeName }));
|
|
597
|
+
}
|
|
598
|
+
function parseJavaScriptConstructorBindings(lines) {
|
|
599
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
600
|
+
for (const rawLine of lines) {
|
|
601
|
+
const stripped = stripCommentsAndStrings(rawLine);
|
|
602
|
+
for (const match of stripped.matchAll(/\b([A-Za-z_$][\w$]*)\s*:\s*([A-Z][A-Za-z0-9_$]*)\b/g)) {
|
|
603
|
+
const localName = match[1];
|
|
604
|
+
const typeName = match[2];
|
|
605
|
+
if (localName && typeName) {
|
|
606
|
+
bindings.set(localName, typeName);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
const constructorMatch = stripped.match(/\b(?:const|let|var)?\s*([A-Za-z_$][\w$]*)\s*(?::\s*[A-Z][A-Za-z0-9_$<> ,]*)?\s*=\s*new\s+([A-Z][A-Za-z0-9_$]*)\s*\(/);
|
|
610
|
+
if (constructorMatch?.[1] && constructorMatch[2]) {
|
|
611
|
+
bindings.set(constructorMatch[1], constructorMatch[2]);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return [...bindings.entries()].map(([localName, typeName]) => ({ localName, typeName }));
|
|
615
|
+
}
|
|
616
|
+
function parsePythonImports(db, importerPath, source) {
|
|
617
|
+
return collectPythonImportStatements(source).flatMap(
|
|
618
|
+
(statement) => parsePythonImportStatement(db, importerPath, statement, source)
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
function collectPythonImportStatements(source) {
|
|
622
|
+
const lines = source.split("\n");
|
|
623
|
+
const statements = [];
|
|
624
|
+
let offset = 0;
|
|
625
|
+
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
|
|
626
|
+
const line = lines[lineIndex];
|
|
627
|
+
const trimmed = line.trimStart();
|
|
628
|
+
const lineStart = offset;
|
|
629
|
+
offset += line.length + 1;
|
|
630
|
+
if (!trimmed.startsWith("import ") && !trimmed.startsWith("from ")) {
|
|
631
|
+
continue;
|
|
632
|
+
}
|
|
633
|
+
let statement = line;
|
|
634
|
+
let statementEnd = lineStart + line.length;
|
|
635
|
+
let balance = pythonParenBalance(line);
|
|
636
|
+
while (lineIndex + 1 < lines.length && (balance > 0 || statement.trimEnd().endsWith("\\"))) {
|
|
637
|
+
lineIndex++;
|
|
638
|
+
const nextLine = lines[lineIndex];
|
|
639
|
+
statement += `
|
|
640
|
+
${nextLine}`;
|
|
641
|
+
statementEnd += 1 + nextLine.length;
|
|
642
|
+
balance += pythonParenBalance(nextLine);
|
|
643
|
+
offset += nextLine.length + 1;
|
|
644
|
+
}
|
|
645
|
+
const parsed = parsePythonStatementHeader(statement);
|
|
646
|
+
if (parsed) {
|
|
647
|
+
statements.push({
|
|
648
|
+
...parsed,
|
|
649
|
+
start: lineStart,
|
|
650
|
+
end: statementEnd
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return statements;
|
|
655
|
+
}
|
|
656
|
+
function parsePythonStatementHeader(statement) {
|
|
657
|
+
const normalized = statement.replace(/\\\s*\n/g, " ").trim();
|
|
658
|
+
if (normalized.startsWith("import ")) {
|
|
659
|
+
return {
|
|
660
|
+
kind: "import",
|
|
661
|
+
module: null,
|
|
662
|
+
clause: normalized.slice("import ".length).trim()
|
|
663
|
+
};
|
|
664
|
+
}
|
|
665
|
+
const fromMatch = normalized.match(/^from\s+([.\w]+)\s+import\s+([\s\S]+)$/);
|
|
666
|
+
if (!fromMatch) {
|
|
667
|
+
return null;
|
|
668
|
+
}
|
|
669
|
+
let clause = fromMatch[2].trim();
|
|
670
|
+
if (clause.startsWith("(") && clause.endsWith(")")) {
|
|
671
|
+
clause = clause.slice(1, -1).trim();
|
|
672
|
+
}
|
|
673
|
+
return {
|
|
674
|
+
kind: "from",
|
|
675
|
+
module: fromMatch[1],
|
|
676
|
+
clause
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
function parsePythonImportStatement(db, importerPath, statement, source) {
|
|
680
|
+
const body = buildUsageBody(source, statement.start, statement.end);
|
|
681
|
+
const normalizedClause = statement.clause.replace(/\n/g, " ").trim();
|
|
682
|
+
if (statement.kind === "import") {
|
|
683
|
+
return splitTopLevel(normalizedClause).flatMap((entry) => {
|
|
684
|
+
const cleaned = entry.trim().replace(/,$/, "");
|
|
685
|
+
if (!cleaned) return [];
|
|
686
|
+
const [moduleName, alias] = cleaned.split(/\s+as\s+/);
|
|
687
|
+
const importedName = moduleName.trim();
|
|
688
|
+
const localName = (alias ?? importedName.split(".")[0] ?? importedName).trim();
|
|
689
|
+
const sourcePath2 = resolvePythonImportPath(db, importerPath, importedName);
|
|
690
|
+
const usedMembers = collectNamespaceMembers(body, localName);
|
|
691
|
+
return [{
|
|
692
|
+
importedName,
|
|
693
|
+
localName,
|
|
694
|
+
sourcePath: sourcePath2,
|
|
695
|
+
kind: "namespace",
|
|
696
|
+
used: hasIdentifierUsage(body, localName) || usedMembers.length > 0,
|
|
697
|
+
usedMembers
|
|
698
|
+
}];
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
const sourcePath = statement.module ? resolvePythonImportPath(db, importerPath, statement.module) : null;
|
|
702
|
+
const results = [];
|
|
703
|
+
for (const entry of splitTopLevel(normalizedClause)) {
|
|
704
|
+
const cleaned = entry.trim().replace(/,$/, "");
|
|
705
|
+
if (!cleaned) continue;
|
|
706
|
+
if (cleaned === "*") {
|
|
707
|
+
results.push({
|
|
708
|
+
importedName: "*",
|
|
709
|
+
localName: null,
|
|
710
|
+
sourcePath,
|
|
711
|
+
kind: "side-effect",
|
|
712
|
+
used: true,
|
|
713
|
+
usedMembers: []
|
|
714
|
+
});
|
|
715
|
+
continue;
|
|
716
|
+
}
|
|
717
|
+
const [importedName, alias] = cleaned.split(/\s+as\s+/);
|
|
718
|
+
const localName = (alias ?? importedName).trim();
|
|
719
|
+
results.push({
|
|
720
|
+
importedName: importedName.trim(),
|
|
721
|
+
localName,
|
|
722
|
+
sourcePath,
|
|
723
|
+
kind: "named",
|
|
724
|
+
used: hasIdentifierUsage(body, localName),
|
|
725
|
+
usedMembers: []
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
return results;
|
|
729
|
+
}
|
|
730
|
+
function parseImportClause(clause) {
|
|
731
|
+
const trimmed = clause.trim().replace(/^type\s+/, "");
|
|
732
|
+
const [first, second] = splitImportClause(trimmed);
|
|
733
|
+
const entries = [];
|
|
734
|
+
if (first) {
|
|
735
|
+
entries.push(...parseImportBinding(first));
|
|
736
|
+
}
|
|
737
|
+
if (second) {
|
|
738
|
+
entries.push(...parseImportBinding(second));
|
|
739
|
+
}
|
|
740
|
+
return entries;
|
|
741
|
+
}
|
|
742
|
+
function parseImportBinding(binding) {
|
|
743
|
+
const trimmed = binding.trim();
|
|
744
|
+
if (!trimmed) return [];
|
|
745
|
+
if (trimmed.startsWith("{")) {
|
|
746
|
+
const inner = trimmed.slice(1, -1).trim();
|
|
747
|
+
if (!inner) return [];
|
|
748
|
+
return splitTopLevel(inner).map((entry) => {
|
|
749
|
+
const cleaned = entry.trim().replace(/^type\s+/, "");
|
|
750
|
+
const [importedName, alias] = cleaned.split(/\s+as\s+/);
|
|
751
|
+
return {
|
|
752
|
+
importedName: importedName.trim(),
|
|
753
|
+
localName: (alias ?? importedName).trim(),
|
|
754
|
+
kind: "named"
|
|
755
|
+
};
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
if (trimmed.startsWith("* as ")) {
|
|
759
|
+
return [{
|
|
760
|
+
importedName: "*",
|
|
761
|
+
localName: trimmed.slice(5).trim(),
|
|
762
|
+
kind: "namespace"
|
|
763
|
+
}];
|
|
764
|
+
}
|
|
765
|
+
return [{
|
|
766
|
+
importedName: "default",
|
|
767
|
+
localName: trimmed,
|
|
768
|
+
kind: "default"
|
|
769
|
+
}];
|
|
770
|
+
}
|
|
771
|
+
function splitImportClause(clause) {
|
|
772
|
+
let depth = 0;
|
|
773
|
+
for (let i = 0; i < clause.length; i++) {
|
|
774
|
+
const char = clause[i];
|
|
775
|
+
if (char === "{") depth++;
|
|
776
|
+
if (char === "}") depth--;
|
|
777
|
+
if (char === "," && depth === 0) {
|
|
778
|
+
return [clause.slice(0, i).trim(), clause.slice(i + 1).trim()];
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
return [clause.trim(), null];
|
|
782
|
+
}
|
|
783
|
+
function splitTopLevel(input) {
|
|
784
|
+
const parts = [];
|
|
785
|
+
let depth = 0;
|
|
786
|
+
let start = 0;
|
|
787
|
+
for (let i = 0; i < input.length; i++) {
|
|
788
|
+
const char = input[i];
|
|
789
|
+
if (char === "{" || char === "[" || char === "(") depth++;
|
|
790
|
+
if (char === "}" || char === "]" || char === ")") depth--;
|
|
791
|
+
if (char === "," && depth === 0) {
|
|
792
|
+
parts.push(input.slice(start, i));
|
|
793
|
+
start = i + 1;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
parts.push(input.slice(start));
|
|
797
|
+
return parts;
|
|
798
|
+
}
|
|
799
|
+
function buildUsageBody(source, start, end) {
|
|
800
|
+
const masked = `${source.slice(0, start)}${" ".repeat(end - start)}${source.slice(end)}`;
|
|
801
|
+
return stripCommentsAndStrings(masked);
|
|
802
|
+
}
|
|
803
|
+
function stripCommentsAndStrings(source) {
|
|
804
|
+
return source.replace(/'''[\s\S]*?'''/g, maskPreservingLines).replace(/"""[\s\S]*?"""/g, maskPreservingLines).replace(/#.*$/gm, maskPreservingLines).replace(/\/\/.*$/gm, maskPreservingLines).replace(/\/\*[\s\S]*?\*\//g, maskPreservingLines).replace(/`(?:\\[\s\S]|[^`])*`/g, maskPreservingLines).replace(/'(?:\\.|[^'\\\r\n])*'/g, maskPreservingLines).replace(/"(?:\\.|[^"\\\r\n])*"/g, maskPreservingLines);
|
|
805
|
+
}
|
|
806
|
+
function maskPreservingLines(segment) {
|
|
807
|
+
return segment.replace(/[^\r\n]/g, " ");
|
|
808
|
+
}
|
|
809
|
+
function hasIdentifierUsage(body, identifier) {
|
|
810
|
+
return new RegExp(`\\b${escapeRegex(identifier)}\\b`, "m").test(body);
|
|
811
|
+
}
|
|
812
|
+
function collectNamespaceMembers(body, namespaceName) {
|
|
813
|
+
const members = /* @__PURE__ */ new Set();
|
|
814
|
+
const regex = new RegExp(`\\b${escapeRegex(namespaceName)}\\s*\\.\\s*([A-Za-z_$][\\w$]*)`, "g");
|
|
815
|
+
for (const match of body.matchAll(regex)) {
|
|
816
|
+
const member = match[1];
|
|
817
|
+
if (member) {
|
|
818
|
+
members.add(member);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
return [...members];
|
|
822
|
+
}
|
|
823
|
+
function resolveImportPath(db, importerPath, specifier) {
|
|
824
|
+
if (isPythonSourcePath(importerPath)) {
|
|
825
|
+
return resolvePythonImportPath(db, importerPath, specifier);
|
|
826
|
+
}
|
|
827
|
+
if (isRustSourcePath(importerPath)) {
|
|
828
|
+
return resolveRustImportPath(db, importerPath, specifier);
|
|
829
|
+
}
|
|
830
|
+
if (isRubySourcePath(importerPath)) {
|
|
831
|
+
return resolveRubyImportPath(db, importerPath, specifier);
|
|
832
|
+
}
|
|
833
|
+
if (isCLikeSourcePath(importerPath)) {
|
|
834
|
+
return resolveCLikeImportPath(db, importerPath, specifier);
|
|
835
|
+
}
|
|
836
|
+
if (isJvmSourcePath(importerPath) || isDotNetSourcePath(importerPath) || isPhpSourcePath(importerPath)) {
|
|
837
|
+
return resolveQualifiedImportPath(db, specifier.replace(/\\/g, "."), extensionFamilyFor(importerPath));
|
|
838
|
+
}
|
|
839
|
+
if (isDartSourcePath(importerPath)) {
|
|
840
|
+
return resolveDartImportPath(db, importerPath, specifier);
|
|
841
|
+
}
|
|
842
|
+
return resolveJavaScriptImportPath(db, importerPath, specifier);
|
|
843
|
+
}
|
|
844
|
+
function resolveJavaScriptImportPath(db, importerPath, specifier) {
|
|
845
|
+
if (!specifier.startsWith(".") && !specifier.startsWith("/")) {
|
|
846
|
+
return null;
|
|
847
|
+
}
|
|
848
|
+
const importerDir = dirname(join(db.config.projectRoot, importerPath));
|
|
849
|
+
const absolute = resolve(importerDir, specifier);
|
|
850
|
+
const indexedPaths = getIndexedPaths(db);
|
|
851
|
+
for (const candidate of candidateImportPaths(absolute)) {
|
|
852
|
+
const relativeCandidate = normalizePath(relative(db.config.projectRoot, candidate));
|
|
853
|
+
if (indexedPaths.has(relativeCandidate) || existsSync(candidate)) {
|
|
854
|
+
return relativeCandidate;
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return normalizePath(relative(db.config.projectRoot, absolute));
|
|
858
|
+
}
|
|
859
|
+
function resolvePythonImportPath(db, importerPath, specifier) {
|
|
860
|
+
const indexedPaths = getIndexedPaths(db);
|
|
861
|
+
let basePath;
|
|
862
|
+
if (specifier.startsWith(".")) {
|
|
863
|
+
const match = specifier.match(/^(\.+)(.*)$/);
|
|
864
|
+
if (!match) return null;
|
|
865
|
+
const dots = match[1].length;
|
|
866
|
+
const remainder = match[2].replace(/^\./, "");
|
|
867
|
+
let baseDir = dirname(join(db.config.projectRoot, importerPath));
|
|
868
|
+
for (let i = 1; i < dots; i++) {
|
|
869
|
+
baseDir = dirname(baseDir);
|
|
870
|
+
}
|
|
871
|
+
basePath = remainder ? resolve(baseDir, remainder.replace(/\./g, "/")) : baseDir;
|
|
872
|
+
} else {
|
|
873
|
+
basePath = resolve(db.config.projectRoot, specifier.replace(/\./g, "/"));
|
|
874
|
+
}
|
|
875
|
+
for (const candidate of pythonCandidateImportPaths(basePath)) {
|
|
876
|
+
const relativeCandidate = normalizePath(relative(db.config.projectRoot, candidate));
|
|
877
|
+
if (indexedPaths.has(relativeCandidate) || existsSync(candidate)) {
|
|
878
|
+
return relativeCandidate;
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
return null;
|
|
882
|
+
}
|
|
883
|
+
function resolveRustImportPath(db, importerPath, specifier) {
|
|
884
|
+
if (!specifier) return null;
|
|
885
|
+
const normalizedSpecifier = specifier.replace(/\s+as\s+.+$/, "").trim();
|
|
886
|
+
if (!normalizedSpecifier.startsWith("crate::") && !normalizedSpecifier.startsWith("self::") && !normalizedSpecifier.startsWith("super::")) {
|
|
887
|
+
return null;
|
|
888
|
+
}
|
|
889
|
+
const importerDir = dirname(join(db.config.projectRoot, importerPath));
|
|
890
|
+
let basePath;
|
|
891
|
+
if (normalizedSpecifier.startsWith("crate::")) {
|
|
892
|
+
basePath = resolve(db.config.projectRoot, "src", normalizedSpecifier.slice("crate::".length).replace(/::/g, "/"));
|
|
893
|
+
} else if (normalizedSpecifier.startsWith("self::")) {
|
|
894
|
+
basePath = resolve(importerDir, normalizedSpecifier.slice("self::".length).replace(/::/g, "/"));
|
|
895
|
+
} else {
|
|
896
|
+
basePath = resolve(dirname(importerDir), normalizedSpecifier.slice("super::".length).replace(/::/g, "/"));
|
|
897
|
+
}
|
|
898
|
+
for (const candidate of rustCandidateImportPaths(basePath)) {
|
|
899
|
+
const relativeCandidate = normalizePath(relative(db.config.projectRoot, candidate));
|
|
900
|
+
if (getIndexedPaths(db).has(relativeCandidate) || existsSync(candidate)) {
|
|
901
|
+
return relativeCandidate;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
return null;
|
|
905
|
+
}
|
|
906
|
+
function resolveRubyImportPath(db, importerPath, specifier) {
|
|
907
|
+
const importerDir = dirname(join(db.config.projectRoot, importerPath));
|
|
908
|
+
const absolute = resolve(importerDir, specifier);
|
|
909
|
+
for (const candidate of rubyCandidateImportPaths(absolute)) {
|
|
910
|
+
const relativeCandidate = normalizePath(relative(db.config.projectRoot, candidate));
|
|
911
|
+
if (getIndexedPaths(db).has(relativeCandidate) || existsSync(candidate)) {
|
|
912
|
+
return relativeCandidate;
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
return null;
|
|
916
|
+
}
|
|
917
|
+
function resolveCLikeImportPath(db, importerPath, specifier) {
|
|
918
|
+
const indexedPaths = getIndexedPaths(db);
|
|
919
|
+
const importerDir = dirname(join(db.config.projectRoot, importerPath));
|
|
920
|
+
const candidates = [
|
|
921
|
+
resolve(importerDir, specifier),
|
|
922
|
+
resolve(db.config.projectRoot, specifier),
|
|
923
|
+
resolve(db.config.projectRoot, "include", specifier),
|
|
924
|
+
resolve(db.config.projectRoot, "src", specifier)
|
|
925
|
+
];
|
|
926
|
+
for (const candidate of candidates) {
|
|
927
|
+
const relativeCandidate = normalizePath(relative(db.config.projectRoot, candidate));
|
|
928
|
+
if (indexedPaths.has(relativeCandidate) || existsSync(candidate)) {
|
|
929
|
+
return relativeCandidate;
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
return null;
|
|
933
|
+
}
|
|
934
|
+
function resolveQualifiedImportPath(db, specifier, extensions) {
|
|
935
|
+
const indexedPaths = getIndexedPaths(db);
|
|
936
|
+
const normalized = specifier.replace(/\\/g, ".").replace(/::/g, ".").replace(/^global::/, "");
|
|
937
|
+
const pathified = normalized.replace(/\./g, "/");
|
|
938
|
+
const basenameOnly = normalized.split(".").pop() ?? normalized;
|
|
939
|
+
for (const ext of extensions) {
|
|
940
|
+
const exactSuffix = `${pathified}${ext}`;
|
|
941
|
+
const exact = [...indexedPaths].find((relativePath) => relativePath.endsWith(exactSuffix));
|
|
942
|
+
if (exact) return exact;
|
|
943
|
+
}
|
|
944
|
+
for (const ext of extensions) {
|
|
945
|
+
const basenameMatch = [...indexedPaths].find((relativePath) => basename(relativePath) === `${basenameOnly}${ext}`);
|
|
946
|
+
if (basenameMatch) return basenameMatch;
|
|
947
|
+
}
|
|
948
|
+
const folderMatches = [...indexedPaths].filter((relativePath) => extensions.includes(extname(relativePath).toLowerCase())).filter((relativePath) => relativePath.includes(`/${pathified}/`) || relativePath.includes(`/${basenameOnly}/`)).sort((left, right) => left.localeCompare(right));
|
|
949
|
+
if (folderMatches.length === 1) {
|
|
950
|
+
return folderMatches[0];
|
|
951
|
+
}
|
|
952
|
+
return null;
|
|
953
|
+
}
|
|
954
|
+
function resolveDartImportPath(db, importerPath, specifier) {
|
|
955
|
+
const indexedPaths = getIndexedPaths(db);
|
|
956
|
+
if (specifier.startsWith("package:")) {
|
|
957
|
+
const withoutScheme = specifier.slice("package:".length);
|
|
958
|
+
const slashIndex = withoutScheme.indexOf("/");
|
|
959
|
+
if (slashIndex < 0) return null;
|
|
960
|
+
const packageRelative = withoutScheme.slice(slashIndex + 1);
|
|
961
|
+
const candidate = normalizePath(packageRelative.startsWith("lib/") ? packageRelative : `lib/${packageRelative}`);
|
|
962
|
+
if (indexedPaths.has(candidate)) return candidate;
|
|
963
|
+
return null;
|
|
964
|
+
}
|
|
965
|
+
const importerDir = dirname(join(db.config.projectRoot, importerPath));
|
|
966
|
+
const absolute = resolve(importerDir, specifier);
|
|
967
|
+
for (const candidate of dartCandidateImportPaths(absolute)) {
|
|
968
|
+
const relativeCandidate = normalizePath(relative(db.config.projectRoot, candidate));
|
|
969
|
+
if (indexedPaths.has(relativeCandidate) || existsSync(candidate)) {
|
|
970
|
+
return relativeCandidate;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
return null;
|
|
974
|
+
}
|
|
975
|
+
function pythonCandidateImportPaths(basePath) {
|
|
976
|
+
const ext = extname(basePath);
|
|
977
|
+
if (PYTHON_SOURCE_EXTENSIONS.includes(ext)) {
|
|
978
|
+
return [basePath];
|
|
979
|
+
}
|
|
980
|
+
return [
|
|
981
|
+
`${basePath}.py`,
|
|
982
|
+
`${basePath}.pyi`,
|
|
983
|
+
join(basePath, "__init__.py"),
|
|
984
|
+
join(basePath, "__init__.pyi")
|
|
985
|
+
];
|
|
986
|
+
}
|
|
987
|
+
function rustCandidateImportPaths(basePath) {
|
|
988
|
+
const ext = extname(basePath);
|
|
989
|
+
if (RUST_SOURCE_EXTENSIONS.includes(ext)) {
|
|
990
|
+
return [basePath];
|
|
991
|
+
}
|
|
992
|
+
return [
|
|
993
|
+
`${basePath}.rs`,
|
|
994
|
+
join(basePath, "mod.rs")
|
|
995
|
+
];
|
|
996
|
+
}
|
|
997
|
+
function rubyCandidateImportPaths(basePath) {
|
|
998
|
+
const ext = extname(basePath);
|
|
999
|
+
if (RUBY_SOURCE_EXTENSIONS.includes(ext)) {
|
|
1000
|
+
return [basePath];
|
|
1001
|
+
}
|
|
1002
|
+
return [
|
|
1003
|
+
`${basePath}.rb`,
|
|
1004
|
+
join(basePath, "index.rb")
|
|
1005
|
+
];
|
|
1006
|
+
}
|
|
1007
|
+
function dartCandidateImportPaths(basePath) {
|
|
1008
|
+
const ext = extname(basePath);
|
|
1009
|
+
if (DART_SOURCE_EXTENSIONS.includes(ext)) {
|
|
1010
|
+
return [basePath];
|
|
1011
|
+
}
|
|
1012
|
+
return [`${basePath}.dart`, basePath];
|
|
1013
|
+
}
|
|
1014
|
+
function candidateImportPaths(absolute) {
|
|
1015
|
+
const ext = extname(absolute);
|
|
1016
|
+
const candidates = /* @__PURE__ */ new Set();
|
|
1017
|
+
if (ext) {
|
|
1018
|
+
candidates.add(absolute);
|
|
1019
|
+
for (const sourceExt of SOURCE_EXTENSIONS) {
|
|
1020
|
+
candidates.add(absolute.slice(0, -ext.length) + sourceExt);
|
|
1021
|
+
}
|
|
1022
|
+
} else {
|
|
1023
|
+
for (const sourceExt of SOURCE_EXTENSIONS) {
|
|
1024
|
+
candidates.add(`${absolute}${sourceExt}`);
|
|
1025
|
+
candidates.add(join(absolute, `index${sourceExt}`));
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
return [...candidates];
|
|
1029
|
+
}
|
|
1030
|
+
function getIndexedPaths(db) {
|
|
1031
|
+
const cached = INDEXED_PATH_CACHE.get(db);
|
|
1032
|
+
if (cached) {
|
|
1033
|
+
return cached;
|
|
1034
|
+
}
|
|
1035
|
+
const paths = new Set(
|
|
1036
|
+
db.all(
|
|
1037
|
+
`SELECT relative_path
|
|
1038
|
+
FROM documents
|
|
1039
|
+
WHERE 1 = 1
|
|
1040
|
+
${db.pathExclusionsFor("documents")}`
|
|
1041
|
+
).map((row) => normalizePath(row.relative_path)).filter((relativePath) => !db.isIgnored(relativePath))
|
|
1042
|
+
);
|
|
1043
|
+
INDEXED_PATH_CACHE.set(db, paths);
|
|
1044
|
+
return paths;
|
|
1045
|
+
}
|
|
1046
|
+
function getCachedMap(cache, db) {
|
|
1047
|
+
let map = cache.get(db);
|
|
1048
|
+
if (!map) {
|
|
1049
|
+
map = /* @__PURE__ */ new Map();
|
|
1050
|
+
cache.set(db, map);
|
|
1051
|
+
}
|
|
1052
|
+
return map;
|
|
1053
|
+
}
|
|
1054
|
+
function normalizePath(path) {
|
|
1055
|
+
return path.replace(/\\/g, "/");
|
|
1056
|
+
}
|
|
1057
|
+
function isJavaScriptSourcePath(relativePath) {
|
|
1058
|
+
return SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1059
|
+
}
|
|
1060
|
+
function isPythonSourcePath(relativePath) {
|
|
1061
|
+
return PYTHON_SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1062
|
+
}
|
|
1063
|
+
function isJvmSourcePath(relativePath) {
|
|
1064
|
+
return JVM_SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1065
|
+
}
|
|
1066
|
+
function isRustSourcePath(relativePath) {
|
|
1067
|
+
return RUST_SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1068
|
+
}
|
|
1069
|
+
function isRubySourcePath(relativePath) {
|
|
1070
|
+
return RUBY_SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1071
|
+
}
|
|
1072
|
+
function isCLikeSourcePath(relativePath) {
|
|
1073
|
+
return C_LIKE_SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1074
|
+
}
|
|
1075
|
+
function isDotNetSourcePath(relativePath) {
|
|
1076
|
+
return DOTNET_SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1077
|
+
}
|
|
1078
|
+
function isVisualBasicSourcePath(relativePath) {
|
|
1079
|
+
return extname(relativePath).toLowerCase() === ".vb";
|
|
1080
|
+
}
|
|
1081
|
+
function isDartSourcePath(relativePath) {
|
|
1082
|
+
return DART_SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1083
|
+
}
|
|
1084
|
+
function isPhpSourcePath(relativePath) {
|
|
1085
|
+
return PHP_SOURCE_EXTENSIONS.includes(extname(relativePath).toLowerCase());
|
|
1086
|
+
}
|
|
1087
|
+
function extensionFamilyFor(relativePath) {
|
|
1088
|
+
if (isJvmSourcePath(relativePath)) return JVM_SOURCE_EXTENSIONS;
|
|
1089
|
+
if (isDotNetSourcePath(relativePath)) return DOTNET_SOURCE_EXTENSIONS;
|
|
1090
|
+
if (isPhpSourcePath(relativePath)) return PHP_SOURCE_EXTENSIONS;
|
|
1091
|
+
if (isDartSourcePath(relativePath)) return DART_SOURCE_EXTENSIONS;
|
|
1092
|
+
if (isCLikeSourcePath(relativePath)) return C_LIKE_SOURCE_EXTENSIONS;
|
|
1093
|
+
if (isRustSourcePath(relativePath)) return RUST_SOURCE_EXTENSIONS;
|
|
1094
|
+
if (isRubySourcePath(relativePath)) return RUBY_SOURCE_EXTENSIONS;
|
|
1095
|
+
return SOURCE_EXTENSIONS;
|
|
1096
|
+
}
|
|
1097
|
+
function getSourceText(db, relativePath) {
|
|
1098
|
+
const cache = getCachedMap(SOURCE_TEXT_CACHE, db);
|
|
1099
|
+
const normalized = normalizePath(relativePath);
|
|
1100
|
+
const cached = cache.get(normalized);
|
|
1101
|
+
if (typeof cached === "string") {
|
|
1102
|
+
return cached;
|
|
1103
|
+
}
|
|
1104
|
+
const fullPath = join(db.config.projectRoot, normalized);
|
|
1105
|
+
if (!existsSync(fullPath)) {
|
|
1106
|
+
cache.set(normalized, "");
|
|
1107
|
+
return "";
|
|
1108
|
+
}
|
|
1109
|
+
const source = readFileSync(fullPath, "utf-8");
|
|
1110
|
+
cache.set(normalized, source);
|
|
1111
|
+
return source;
|
|
1112
|
+
}
|
|
1113
|
+
function pythonParenBalance(value) {
|
|
1114
|
+
let balance = 0;
|
|
1115
|
+
for (const char of value) {
|
|
1116
|
+
if (char === "(") balance++;
|
|
1117
|
+
if (char === ")") balance--;
|
|
1118
|
+
}
|
|
1119
|
+
return balance;
|
|
1120
|
+
}
|
|
1121
|
+
function escapeRegex(value) {
|
|
1122
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
// src/query-support.ts
|
|
1126
|
+
import { basename as basename2 } from "path";
|
|
1127
|
+
var FILE_DEFINITION_CACHE = /* @__PURE__ */ new WeakMap();
|
|
1128
|
+
var TEST_FILE_PATTERNS = [
|
|
1129
|
+
"%/__tests__/%",
|
|
1130
|
+
"%.test.%",
|
|
1131
|
+
"%.spec.%",
|
|
1132
|
+
"%/test/%",
|
|
1133
|
+
"%/tests/%",
|
|
1134
|
+
"%_test.%",
|
|
1135
|
+
"%_spec.%",
|
|
1136
|
+
"%/test_%.%",
|
|
1137
|
+
"%/spec_%.%"
|
|
1138
|
+
];
|
|
1139
|
+
var TEST_SUPPORT_PATH_PATTERNS = [
|
|
1140
|
+
"%/test-utils/%"
|
|
1141
|
+
];
|
|
1142
|
+
function testFileExclusionSql(alias, extraPatterns = []) {
|
|
1143
|
+
const patterns = uniquePatterns([...TEST_FILE_PATTERNS, ...extraPatterns]);
|
|
1144
|
+
return patterns.map((pattern) => `${alias}.relative_path NOT LIKE '${pattern}'`).join("\n AND ");
|
|
1145
|
+
}
|
|
1146
|
+
function buildFileDepGraph(db, scope) {
|
|
1147
|
+
const scopeFilter = scope ? `AND d1.relative_path LIKE '%${scope}%'` : "";
|
|
1148
|
+
const edges = db.all(
|
|
1149
|
+
`SELECT DISTINCT
|
|
1150
|
+
d1.relative_path AS from_file,
|
|
1151
|
+
d2.relative_path AS to_file
|
|
1152
|
+
FROM mentions m
|
|
1153
|
+
JOIN chunks c ON m.chunk_id = c.id
|
|
1154
|
+
JOIN documents d1 ON c.document_id = d1.id
|
|
1155
|
+
JOIN global_symbols gs ON m.symbol_id = gs.id
|
|
1156
|
+
JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
|
|
1157
|
+
JOIN documents d2 ON der.document_id = d2.id
|
|
1158
|
+
WHERE d1.id != d2.id
|
|
1159
|
+
AND m.role != 1
|
|
1160
|
+
${db.pathExclusionsFor("d1", "d2")}
|
|
1161
|
+
${scopeFilter}`
|
|
1162
|
+
);
|
|
1163
|
+
const graph = /* @__PURE__ */ new Map();
|
|
1164
|
+
const indexedFiles = new Set(
|
|
1165
|
+
db.all(
|
|
1166
|
+
`SELECT relative_path
|
|
1167
|
+
FROM documents
|
|
1168
|
+
WHERE 1 = 1
|
|
1169
|
+
${db.pathExclusionsFor("documents")}
|
|
1170
|
+
ORDER BY relative_path`
|
|
1171
|
+
).map((row) => row.relative_path).filter((relativePath) => !db.isIgnored(relativePath))
|
|
1172
|
+
);
|
|
1173
|
+
const addEdge = (fromFile, toFile) => {
|
|
1174
|
+
if (fromFile === toFile) return;
|
|
1175
|
+
if (db.isIgnored(fromFile) || db.isIgnored(toFile)) return;
|
|
1176
|
+
if (!indexedFiles.has(toFile)) return;
|
|
1177
|
+
if (!graph.has(fromFile)) graph.set(fromFile, /* @__PURE__ */ new Set());
|
|
1178
|
+
graph.get(fromFile).add(toFile);
|
|
1179
|
+
};
|
|
1180
|
+
for (const edge of edges) {
|
|
1181
|
+
addEdge(edge.from_file, edge.to_file);
|
|
1182
|
+
}
|
|
1183
|
+
for (const relativePath of indexedFiles) {
|
|
1184
|
+
if (scope && !relativePath.includes(scope)) continue;
|
|
1185
|
+
for (const entry of getSourceImports(db, relativePath)) {
|
|
1186
|
+
if (!entry.sourcePath) continue;
|
|
1187
|
+
addEdge(relativePath, entry.sourcePath);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
return graph;
|
|
1191
|
+
}
|
|
1192
|
+
function findFirstSymbolMatch(db, symbolPattern) {
|
|
1193
|
+
const fileLineMatch = symbolPattern.match(/^(.+):(\d+)-(\d+)$/);
|
|
1194
|
+
if (fileLineMatch) {
|
|
1195
|
+
const [, filePath, startStr, endStr] = fileLineMatch;
|
|
1196
|
+
let row = db.get(
|
|
1197
|
+
`SELECT gs.id, gs.symbol, der.document_id, der.start_line, der.end_line, d.relative_path
|
|
1198
|
+
FROM global_symbols gs
|
|
1199
|
+
JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
|
|
1200
|
+
JOIN documents d ON der.document_id = d.id
|
|
1201
|
+
WHERE d.relative_path LIKE ?
|
|
1202
|
+
AND der.start_line <= ? AND der.end_line >= ?
|
|
1203
|
+
${db.pathExclusionsFor("d")}
|
|
1204
|
+
ORDER BY (der.end_line - der.start_line) ASC
|
|
1205
|
+
LIMIT 1`,
|
|
1206
|
+
`%${filePath}%`,
|
|
1207
|
+
parseInt(startStr, 10),
|
|
1208
|
+
parseInt(endStr, 10)
|
|
1209
|
+
);
|
|
1210
|
+
if (!row) {
|
|
1211
|
+
row = db.get(
|
|
1212
|
+
`SELECT gs.id, gs.symbol, c.document_id, MIN(c.start_line) AS start_line, MAX(c.end_line) AS end_line, d.relative_path
|
|
1213
|
+
FROM global_symbols gs
|
|
1214
|
+
JOIN mentions m ON m.symbol_id = gs.id
|
|
1215
|
+
JOIN chunks c ON m.chunk_id = c.id
|
|
1216
|
+
JOIN documents d ON c.document_id = d.id
|
|
1217
|
+
WHERE m.role = 1
|
|
1218
|
+
AND d.relative_path LIKE ?
|
|
1219
|
+
AND c.start_line <= ? AND c.end_line >= ?
|
|
1220
|
+
${db.pathExclusionsFor("d")}
|
|
1221
|
+
GROUP BY gs.id, gs.symbol, c.document_id, d.relative_path
|
|
1222
|
+
ORDER BY (MAX(c.end_line) - MIN(c.start_line)) ASC
|
|
1223
|
+
LIMIT 1`,
|
|
1224
|
+
`%${filePath}%`,
|
|
1225
|
+
parseInt(startStr, 10),
|
|
1226
|
+
parseInt(endStr, 10)
|
|
1227
|
+
);
|
|
1228
|
+
}
|
|
1229
|
+
if (row && !db.isIgnored(row.relative_path)) {
|
|
1230
|
+
return {
|
|
1231
|
+
symbolId: row.id,
|
|
1232
|
+
symbol: row.symbol,
|
|
1233
|
+
documentId: row.document_id,
|
|
1234
|
+
startLine: row.start_line,
|
|
1235
|
+
endLine: row.end_line,
|
|
1236
|
+
relativePath: row.relative_path
|
|
1237
|
+
};
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
const cleaned = normalizeLookupPattern(symbolPattern);
|
|
1241
|
+
const tokens = lookupTokens(symbolPattern);
|
|
1242
|
+
const candidates = getSymbolLookupCandidates(db, tokens);
|
|
1243
|
+
let best = null;
|
|
1244
|
+
for (const row of candidates) {
|
|
1245
|
+
if (db.isIgnored(row.relative_path)) continue;
|
|
1246
|
+
const score = scoreSymbolCandidate(row, symbolPattern, cleaned, tokens);
|
|
1247
|
+
if (score <= 0) continue;
|
|
1248
|
+
if (!best || score > best.score) {
|
|
1249
|
+
best = { row, score };
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
if (best) {
|
|
1253
|
+
return {
|
|
1254
|
+
symbolId: best.row.id,
|
|
1255
|
+
symbol: best.row.symbol,
|
|
1256
|
+
documentId: best.row.document_id,
|
|
1257
|
+
startLine: best.row.start_line,
|
|
1258
|
+
endLine: best.row.end_line,
|
|
1259
|
+
relativePath: best.row.relative_path
|
|
1260
|
+
};
|
|
1261
|
+
}
|
|
1262
|
+
return null;
|
|
1263
|
+
}
|
|
1264
|
+
function findExactSymbolMatch(db, symbol) {
|
|
1265
|
+
const row = db.get(
|
|
1266
|
+
`SELECT gs.id, gs.symbol, der.document_id, der.start_line, der.end_line, d.relative_path
|
|
1267
|
+
FROM global_symbols gs
|
|
1268
|
+
JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
|
|
1269
|
+
JOIN documents d ON der.document_id = d.id
|
|
1270
|
+
WHERE gs.symbol = ?
|
|
1271
|
+
${db.pathExclusionsFor("d")}
|
|
1272
|
+
ORDER BY d.relative_path, der.start_line
|
|
1273
|
+
LIMIT 1`,
|
|
1274
|
+
symbol
|
|
1275
|
+
);
|
|
1276
|
+
if (!row || db.isIgnored(row.relative_path)) {
|
|
1277
|
+
return null;
|
|
1278
|
+
}
|
|
1279
|
+
return {
|
|
1280
|
+
symbolId: row.id,
|
|
1281
|
+
symbol: row.symbol,
|
|
1282
|
+
documentId: row.document_id,
|
|
1283
|
+
startLine: row.start_line,
|
|
1284
|
+
endLine: row.end_line,
|
|
1285
|
+
relativePath: row.relative_path
|
|
1286
|
+
};
|
|
1287
|
+
}
|
|
1288
|
+
function resolveIndexedFile(db, filePattern) {
|
|
1289
|
+
return resolveDocumentCandidates(db, filePattern, { allowMultiple: false })[0]?.relativePath ?? null;
|
|
1290
|
+
}
|
|
1291
|
+
function resolveIndexedPaths(db, filePattern) {
|
|
1292
|
+
return resolveDocumentCandidates(db, filePattern, { allowMultiple: true }).map((candidate) => candidate.relativePath);
|
|
1293
|
+
}
|
|
1294
|
+
function normalizeLookupPattern(symbolPattern) {
|
|
1295
|
+
return symbolPattern.trim().replace(/\(\)$/, "").replace(/\(.*$/, "");
|
|
1296
|
+
}
|
|
1297
|
+
function lookupTokens(symbolPattern) {
|
|
1298
|
+
const cleaned = normalizeLookupPattern(symbolPattern);
|
|
1299
|
+
const tokens = cleaned.split(/[^A-Za-z0-9_]+/).map((token) => token.trim()).filter((token) => token.length > 0);
|
|
1300
|
+
return tokens.length > 0 ? [...new Set(tokens)] : [cleaned];
|
|
1301
|
+
}
|
|
1302
|
+
function getSymbolLookupCandidates(db, tokens) {
|
|
1303
|
+
const tokenClauses = tokens.map(
|
|
1304
|
+
() => `(gs.symbol LIKE ? OR d.relative_path LIKE ? OR COALESCE(gs.display_name, '') LIKE ?)`
|
|
1305
|
+
);
|
|
1306
|
+
const params = tokens.flatMap((token) => {
|
|
1307
|
+
const like = `%${token}%`;
|
|
1308
|
+
return [like, like, like];
|
|
1309
|
+
});
|
|
1310
|
+
const primary = db.all(
|
|
1311
|
+
`SELECT gs.id, gs.symbol, der.document_id, der.start_line, der.end_line, d.relative_path, gs.display_name
|
|
1312
|
+
FROM global_symbols gs
|
|
1313
|
+
JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
|
|
1314
|
+
JOIN documents d ON der.document_id = d.id
|
|
1315
|
+
WHERE ${tokenClauses.join("\n AND ")}
|
|
1316
|
+
${db.pathExclusionsFor("d")}
|
|
1317
|
+
LIMIT 200`,
|
|
1318
|
+
...params
|
|
1319
|
+
);
|
|
1320
|
+
if (primary.length > 0) {
|
|
1321
|
+
return primary;
|
|
1322
|
+
}
|
|
1323
|
+
return db.all(
|
|
1324
|
+
`SELECT
|
|
1325
|
+
gs.id,
|
|
1326
|
+
gs.symbol,
|
|
1327
|
+
c.document_id,
|
|
1328
|
+
MIN(c.start_line) AS start_line,
|
|
1329
|
+
MAX(c.end_line) AS end_line,
|
|
1330
|
+
d.relative_path,
|
|
1331
|
+
gs.display_name
|
|
1332
|
+
FROM global_symbols gs
|
|
1333
|
+
JOIN mentions m ON m.symbol_id = gs.id
|
|
1334
|
+
JOIN chunks c ON m.chunk_id = c.id
|
|
1335
|
+
JOIN documents d ON c.document_id = d.id
|
|
1336
|
+
WHERE m.role = 1
|
|
1337
|
+
AND ${tokenClauses.join("\n AND ")}
|
|
1338
|
+
${db.pathExclusionsFor("d")}
|
|
1339
|
+
GROUP BY gs.id, gs.symbol, c.document_id, d.relative_path, gs.display_name
|
|
1340
|
+
LIMIT 200`,
|
|
1341
|
+
...params
|
|
1342
|
+
);
|
|
1343
|
+
}
|
|
1344
|
+
function scoreSymbolCandidate(row, originalPattern, cleanedPattern, tokens) {
|
|
1345
|
+
const originalCase = originalPattern.trim();
|
|
1346
|
+
const cleanedCase = cleanedPattern;
|
|
1347
|
+
const noParensCase = cleanedCase.replace(/\(\)$/, "");
|
|
1348
|
+
const original = originalPattern.toLowerCase();
|
|
1349
|
+
const cleaned = cleanedPattern.toLowerCase();
|
|
1350
|
+
const noParens = cleaned.replace(/\(\)$/, "");
|
|
1351
|
+
const rawCase = row.symbol;
|
|
1352
|
+
const shortCase = shortenSymbol(row.symbol);
|
|
1353
|
+
const leafCase = leafName(row.symbol);
|
|
1354
|
+
const displayCase = row.display_name ?? "";
|
|
1355
|
+
const raw = row.symbol.toLowerCase();
|
|
1356
|
+
const short = shortCase.toLowerCase();
|
|
1357
|
+
const leaf = leafCase.toLowerCase();
|
|
1358
|
+
const display = displayCase.toLowerCase();
|
|
1359
|
+
const path = row.relative_path.toLowerCase();
|
|
1360
|
+
const looksPathLike = /[/:.]/.test(cleanedPattern);
|
|
1361
|
+
let score = 0;
|
|
1362
|
+
if (rawCase === originalCase || rawCase === cleanedCase) score += 1150;
|
|
1363
|
+
if (shortCase === originalCase || shortCase === cleanedCase) score += 1100;
|
|
1364
|
+
if (displayCase === noParensCase) score += 980;
|
|
1365
|
+
if (leafCase === noParensCase) score += 960;
|
|
1366
|
+
if (`${leafCase}()` === originalCase || `${leafCase}()` === cleanedCase) score += 955;
|
|
1367
|
+
if (raw === original || raw === cleaned) score += 1e3;
|
|
1368
|
+
if (short === original || short === cleaned) score += 950;
|
|
1369
|
+
if (path === original || path === cleaned) score += 925;
|
|
1370
|
+
if (path.endsWith(`/${cleaned}`) || path.endsWith(`/${original}`)) score += 875;
|
|
1371
|
+
if (display === noParens) score += 850;
|
|
1372
|
+
if (leaf === noParens) score += 825;
|
|
1373
|
+
if (`${leaf}()` === original || `${leaf}()` === cleaned) score += 820;
|
|
1374
|
+
if (short.endsWith(`:${cleaned}`) || short.endsWith(`:${noParens}`) || short.endsWith(`:${noParens}()`)) score += 800;
|
|
1375
|
+
if (raw.includes(cleaned)) score += 120;
|
|
1376
|
+
if (short.includes(cleaned)) score += 140;
|
|
1377
|
+
if (path.includes(cleaned)) score += 140;
|
|
1378
|
+
if (display.includes(cleaned)) score += 110;
|
|
1379
|
+
if (tokens.every((token) => {
|
|
1380
|
+
const lower = token.toLowerCase();
|
|
1381
|
+
return raw.includes(lower) || short.includes(lower) || path.includes(lower) || display.includes(lower);
|
|
1382
|
+
})) {
|
|
1383
|
+
score += 100 + tokens.length * 15;
|
|
1384
|
+
}
|
|
1385
|
+
if (isFunctionLikeSymbol(row.symbol) && leaf === noParens) {
|
|
1386
|
+
score += 60;
|
|
1387
|
+
}
|
|
1388
|
+
if (!looksPathLike && isModuleLikeSymbol(row.symbol)) {
|
|
1389
|
+
score -= 160;
|
|
1390
|
+
}
|
|
1391
|
+
score -= Math.min(50, Math.max(0, row.end_line - row.start_line));
|
|
1392
|
+
return score;
|
|
1393
|
+
}
|
|
1394
|
+
function getCalleeRowsForSymbol(db, symbol, opts = {}) {
|
|
1395
|
+
const mentions = db.all(
|
|
1396
|
+
`SELECT DISTINCT m.symbol_id, c.id AS chunk_id
|
|
1397
|
+
FROM mentions m
|
|
1398
|
+
JOIN chunks c ON m.chunk_id = c.id
|
|
1399
|
+
WHERE c.document_id = ?
|
|
1400
|
+
AND c.start_line >= ?
|
|
1401
|
+
AND c.end_line <= ?
|
|
1402
|
+
AND m.role != 1
|
|
1403
|
+
AND m.symbol_id != ?
|
|
1404
|
+
${opts.limit ? "LIMIT ?" : ""}`,
|
|
1405
|
+
...calleeQueryParams(symbol, opts.limit)
|
|
1406
|
+
);
|
|
1407
|
+
const primary = [];
|
|
1408
|
+
const directSeen = /* @__PURE__ */ new Set();
|
|
1409
|
+
for (const mention of mentions) {
|
|
1410
|
+
const callee = getFullSymbolMatch(db, {
|
|
1411
|
+
symbolId: mention.symbol_id,
|
|
1412
|
+
documentId: symbol.documentId,
|
|
1413
|
+
startLine: symbol.startLine,
|
|
1414
|
+
endLine: symbol.endLine
|
|
1415
|
+
});
|
|
1416
|
+
if (!callee || db.isIgnored(callee.relativePath)) continue;
|
|
1417
|
+
const key = `${callee.symbol}|${callee.relativePath}|${mention.chunk_id}`;
|
|
1418
|
+
if (directSeen.has(key)) continue;
|
|
1419
|
+
directSeen.add(key);
|
|
1420
|
+
primary.push({
|
|
1421
|
+
symbol: callee.symbol,
|
|
1422
|
+
file: callee.relativePath,
|
|
1423
|
+
chunkId: mention.chunk_id
|
|
1424
|
+
});
|
|
1425
|
+
}
|
|
1426
|
+
const sourceFallback = getSourceBackedCalleeRows(db, symbol, opts.limit);
|
|
1427
|
+
if (sourceFallback.length === 0) {
|
|
1428
|
+
return primary;
|
|
1429
|
+
}
|
|
1430
|
+
if (primary.length === 0) {
|
|
1431
|
+
return applyLimit(sourceFallback, opts.limit);
|
|
1432
|
+
}
|
|
1433
|
+
const merged = [...sourceFallback];
|
|
1434
|
+
const seen = new Set(merged.map((row) => `${row.symbol}|${row.file}`));
|
|
1435
|
+
const preferredFallbackLeaves = new Set(
|
|
1436
|
+
sourceFallback.filter((row) => !isLikelyTestPath(row.file)).map((row) => leafName(row.symbol))
|
|
1437
|
+
);
|
|
1438
|
+
for (const row of primary) {
|
|
1439
|
+
if (isLikelyTestPath(row.file) && preferredFallbackLeaves.has(leafName(row.symbol))) {
|
|
1440
|
+
continue;
|
|
1441
|
+
}
|
|
1442
|
+
const key = `${row.symbol}|${row.file}`;
|
|
1443
|
+
if (seen.has(key)) continue;
|
|
1444
|
+
seen.add(key);
|
|
1445
|
+
merged.push(row);
|
|
1446
|
+
}
|
|
1447
|
+
return applyLimit(merged, opts.limit);
|
|
1448
|
+
}
|
|
1449
|
+
function getCallerRowsForSymbol(db, symbol, opts = {}) {
|
|
1450
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1451
|
+
if (!match) {
|
|
1452
|
+
return [];
|
|
1453
|
+
}
|
|
1454
|
+
const primary = db.all(
|
|
1455
|
+
`SELECT DISTINCT ref_d.relative_path AS caller_file, c.start_line AS line
|
|
1456
|
+
FROM mentions m
|
|
1457
|
+
JOIN chunks c ON m.chunk_id = c.id
|
|
1458
|
+
JOIN documents ref_d ON c.document_id = ref_d.id
|
|
1459
|
+
WHERE m.symbol_id = ?
|
|
1460
|
+
AND m.role != 1
|
|
1461
|
+
${db.pathExclusionsFor("ref_d")}
|
|
1462
|
+
ORDER BY ref_d.relative_path
|
|
1463
|
+
${opts.limit ? "LIMIT ?" : ""}`,
|
|
1464
|
+
...referenceQueryParams(match, opts.limit)
|
|
1465
|
+
).filter((row) => !db.isIgnored(row.caller_file)).flatMap((row) => {
|
|
1466
|
+
const enclosing = findEnclosingDefinition(getDefinitionsForFile(db, row.caller_file), row.line);
|
|
1467
|
+
if (!enclosing || enclosing.symbolId === match.symbolId) {
|
|
1468
|
+
return [];
|
|
1469
|
+
}
|
|
1470
|
+
return [{
|
|
1471
|
+
symbol: enclosing.symbol,
|
|
1472
|
+
file: row.caller_file
|
|
1473
|
+
}];
|
|
1474
|
+
});
|
|
1475
|
+
const sourceFallback = dedupeCallerRows([
|
|
1476
|
+
...getPythonSourceCallerRows(db, match, opts.limit),
|
|
1477
|
+
...getGenericSourceCallerRows(db, match, opts.limit)
|
|
1478
|
+
]);
|
|
1479
|
+
if (sourceFallback.length === 0) {
|
|
1480
|
+
return primary;
|
|
1481
|
+
}
|
|
1482
|
+
const merged = [...sourceFallback];
|
|
1483
|
+
const seen = new Set(merged.map((row) => `${row.symbol}|${row.file}`));
|
|
1484
|
+
for (const row of primary) {
|
|
1485
|
+
const key = `${row.symbol}|${row.file}`;
|
|
1486
|
+
if (seen.has(key)) continue;
|
|
1487
|
+
if (isFunctionLikeSymbol(row.symbol) || merged.length === 0) {
|
|
1488
|
+
seen.add(key);
|
|
1489
|
+
merged.push(row);
|
|
1490
|
+
}
|
|
1491
|
+
}
|
|
1492
|
+
return applyLimit(merged, opts.limit);
|
|
1493
|
+
}
|
|
1494
|
+
function getGenericSourceCallerRows(db, symbol, limit) {
|
|
1495
|
+
return applyLimit(
|
|
1496
|
+
getSourceReferenceSites(db, symbol).filter((site) => site.enclosingSymbol && site.enclosingSymbol !== symbol.symbol).map((site) => ({
|
|
1497
|
+
symbol: site.enclosingSymbol,
|
|
1498
|
+
file: site.file
|
|
1499
|
+
})),
|
|
1500
|
+
limit
|
|
1501
|
+
);
|
|
1502
|
+
}
|
|
1503
|
+
function dedupeCallerRows(rows) {
|
|
1504
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1505
|
+
const unique = [];
|
|
1506
|
+
for (const row of rows) {
|
|
1507
|
+
const key = `${row.symbol}|${row.file}`;
|
|
1508
|
+
if (seen.has(key)) continue;
|
|
1509
|
+
seen.add(key);
|
|
1510
|
+
unique.push(row);
|
|
1511
|
+
}
|
|
1512
|
+
return unique;
|
|
1513
|
+
}
|
|
1514
|
+
function getSourceReferenceSites(db, symbol) {
|
|
1515
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1516
|
+
if (!match) {
|
|
1517
|
+
return [];
|
|
1518
|
+
}
|
|
1519
|
+
const identifier = leafName(match.symbol);
|
|
1520
|
+
if (!identifier || !hasUniqueLeafDefinition(db, identifier, match.symbolId)) {
|
|
1521
|
+
return [];
|
|
1522
|
+
}
|
|
1523
|
+
const documents = db.all(
|
|
1524
|
+
`SELECT relative_path
|
|
1525
|
+
FROM documents
|
|
1526
|
+
WHERE 1 = 1
|
|
1527
|
+
${db.pathExclusionsFor("documents")}
|
|
1528
|
+
ORDER BY relative_path`
|
|
1529
|
+
);
|
|
1530
|
+
const sites = [];
|
|
1531
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1532
|
+
for (const document of documents) {
|
|
1533
|
+
if (db.isIgnored(document.relative_path)) continue;
|
|
1534
|
+
const lines = findIdentifierLines(db, document.relative_path, identifier, document.relative_path === match.relativePath ? { excludeStartLine: match.startLine, excludeEndLine: match.endLine } : {});
|
|
1535
|
+
const definitions = getDefinitionsForFile(db, document.relative_path);
|
|
1536
|
+
for (const line of lines) {
|
|
1537
|
+
const enclosing = findEnclosingDefinition(definitions, line);
|
|
1538
|
+
const key = `${document.relative_path}|${line}|${enclosing?.symbol ?? ""}`;
|
|
1539
|
+
if (seen.has(key)) continue;
|
|
1540
|
+
seen.add(key);
|
|
1541
|
+
sites.push({
|
|
1542
|
+
file: document.relative_path,
|
|
1543
|
+
line,
|
|
1544
|
+
enclosingSymbol: enclosing?.symbol ?? null
|
|
1545
|
+
});
|
|
1546
|
+
}
|
|
1547
|
+
}
|
|
1548
|
+
return sites;
|
|
1549
|
+
}
|
|
1550
|
+
function calleeQueryParams(symbol, limit) {
|
|
1551
|
+
const params = [
|
|
1552
|
+
symbol.documentId,
|
|
1553
|
+
symbol.startLine,
|
|
1554
|
+
symbol.endLine,
|
|
1555
|
+
symbol.symbolId
|
|
1556
|
+
];
|
|
1557
|
+
if (typeof limit === "number") {
|
|
1558
|
+
params.push(limit);
|
|
1559
|
+
}
|
|
1560
|
+
return params;
|
|
1561
|
+
}
|
|
1562
|
+
function referenceQueryParams(symbol, limit) {
|
|
1563
|
+
const params = [symbol.symbolId];
|
|
1564
|
+
if (typeof limit === "number") {
|
|
1565
|
+
params.push(limit);
|
|
1566
|
+
}
|
|
1567
|
+
return params;
|
|
1568
|
+
}
|
|
1569
|
+
function findEnclosingDefinition(definitions, line) {
|
|
1570
|
+
let best = null;
|
|
1571
|
+
for (const definition of definitions) {
|
|
1572
|
+
if (definition.startLine > line || definition.endLine < line) continue;
|
|
1573
|
+
if (!best || definition.endLine - definition.startLine < best.endLine - best.startLine) {
|
|
1574
|
+
best = definition;
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
return best;
|
|
1578
|
+
}
|
|
1579
|
+
function getPythonSourceCalleeRows(db, symbol, limit) {
|
|
1580
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1581
|
+
if (!match || !isPythonDocument(db, match.relativePath)) {
|
|
1582
|
+
return [];
|
|
1583
|
+
}
|
|
1584
|
+
const definitions = getDefinitionsForFile(db, match.relativePath);
|
|
1585
|
+
const current = definitions.find((definition) => definition.symbolId === match.symbolId);
|
|
1586
|
+
if (!current) {
|
|
1587
|
+
return [];
|
|
1588
|
+
}
|
|
1589
|
+
const imports = getSourceImports(db, match.relativePath);
|
|
1590
|
+
const bindings = new Map(
|
|
1591
|
+
getSourceConstructorBindings(db, match.relativePath, {
|
|
1592
|
+
startLine: match.startLine,
|
|
1593
|
+
endLine: match.endLine
|
|
1594
|
+
}).map((binding) => [binding.localName, binding.typeName])
|
|
1595
|
+
);
|
|
1596
|
+
const rows = [];
|
|
1597
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1598
|
+
for (const call of getSourceCalls(db, match.relativePath, {
|
|
1599
|
+
startLine: match.startLine,
|
|
1600
|
+
endLine: match.endLine
|
|
1601
|
+
})) {
|
|
1602
|
+
const resolved = resolvePythonCallTarget(
|
|
1603
|
+
db,
|
|
1604
|
+
current,
|
|
1605
|
+
definitions,
|
|
1606
|
+
imports,
|
|
1607
|
+
bindings,
|
|
1608
|
+
call.receiverName,
|
|
1609
|
+
call.calleeName
|
|
1610
|
+
);
|
|
1611
|
+
if (!resolved || resolved.symbolId === match.symbolId || db.isIgnored(resolved.relativePath)) continue;
|
|
1612
|
+
const chunkId = 1e9 + call.line;
|
|
1613
|
+
const key = `${resolved.symbol}|${resolved.relativePath}|${chunkId}`;
|
|
1614
|
+
if (seen.has(key)) continue;
|
|
1615
|
+
seen.add(key);
|
|
1616
|
+
rows.push({
|
|
1617
|
+
symbol: resolved.symbol,
|
|
1618
|
+
file: resolved.relativePath,
|
|
1619
|
+
chunkId
|
|
1620
|
+
});
|
|
1621
|
+
}
|
|
1622
|
+
return applyLimit(rows, limit);
|
|
1623
|
+
}
|
|
1624
|
+
function getJavaScriptSourceCalleeRows(db, symbol, limit) {
|
|
1625
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1626
|
+
if (!match || !isJavaScriptDocument(db, match.relativePath)) {
|
|
1627
|
+
return [];
|
|
1628
|
+
}
|
|
1629
|
+
const definitions = getDefinitionsForFile(db, match.relativePath);
|
|
1630
|
+
const current = definitions.find((definition) => definition.symbolId === match.symbolId);
|
|
1631
|
+
if (!current) {
|
|
1632
|
+
return [];
|
|
1633
|
+
}
|
|
1634
|
+
const imports = getSourceImports(db, match.relativePath);
|
|
1635
|
+
const bindings = new Map(
|
|
1636
|
+
getSourceConstructorBindings(db, match.relativePath, {
|
|
1637
|
+
startLine: match.startLine,
|
|
1638
|
+
endLine: match.endLine
|
|
1639
|
+
}).map((binding) => [binding.localName, binding.typeName])
|
|
1640
|
+
);
|
|
1641
|
+
const rows = [];
|
|
1642
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1643
|
+
for (const call of getSourceCalls(db, match.relativePath, {
|
|
1644
|
+
startLine: match.startLine,
|
|
1645
|
+
endLine: match.endLine
|
|
1646
|
+
})) {
|
|
1647
|
+
const resolved = resolveJavaScriptCallTarget(
|
|
1648
|
+
db,
|
|
1649
|
+
current,
|
|
1650
|
+
definitions,
|
|
1651
|
+
imports,
|
|
1652
|
+
bindings,
|
|
1653
|
+
call.receiverName,
|
|
1654
|
+
call.calleeName
|
|
1655
|
+
);
|
|
1656
|
+
if (!resolved || resolved.symbolId === match.symbolId || db.isIgnored(resolved.relativePath)) continue;
|
|
1657
|
+
const chunkId = 1e9 + call.line;
|
|
1658
|
+
const key = `${resolved.symbol}|${resolved.relativePath}|${chunkId}`;
|
|
1659
|
+
if (seen.has(key)) continue;
|
|
1660
|
+
seen.add(key);
|
|
1661
|
+
rows.push({
|
|
1662
|
+
symbol: resolved.symbol,
|
|
1663
|
+
file: resolved.relativePath,
|
|
1664
|
+
chunkId
|
|
1665
|
+
});
|
|
1666
|
+
}
|
|
1667
|
+
return applyLimit(rows, limit);
|
|
1668
|
+
}
|
|
1669
|
+
function getJavaSourceCalleeRows(db, symbol, limit) {
|
|
1670
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1671
|
+
if (!match || !isJavaDocument(db, match.relativePath)) {
|
|
1672
|
+
return [];
|
|
1673
|
+
}
|
|
1674
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1675
|
+
const bindings = parseJavaFieldBindings(getSourceText(db, match.relativePath));
|
|
1676
|
+
return resolveSimpleSourceCallees(db, match, calls, bindings, limit);
|
|
1677
|
+
}
|
|
1678
|
+
function getKotlinSourceCalleeRows(db, symbol, limit) {
|
|
1679
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1680
|
+
if (!match || !isKotlinDocument(db, match.relativePath)) {
|
|
1681
|
+
return [];
|
|
1682
|
+
}
|
|
1683
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1684
|
+
const bindings = parseKotlinFieldBindings(getSourceText(db, match.relativePath));
|
|
1685
|
+
return resolveSimpleSourceCallees(db, match, calls, bindings, limit);
|
|
1686
|
+
}
|
|
1687
|
+
function getScalaSourceCalleeRows(db, symbol, limit) {
|
|
1688
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1689
|
+
if (!match || !isScalaDocument(db, match.relativePath)) {
|
|
1690
|
+
return [];
|
|
1691
|
+
}
|
|
1692
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1693
|
+
return resolveSimpleSourceCallees(db, match, calls, /* @__PURE__ */ new Map(), limit);
|
|
1694
|
+
}
|
|
1695
|
+
function getCSharpSourceCalleeRows(db, symbol, limit) {
|
|
1696
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1697
|
+
if (!match || !isCSharpDocument(db, match.relativePath)) {
|
|
1698
|
+
return [];
|
|
1699
|
+
}
|
|
1700
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1701
|
+
return resolveSimpleSourceCallees(db, match, calls, /* @__PURE__ */ new Map(), limit);
|
|
1702
|
+
}
|
|
1703
|
+
function getVisualBasicSourceCalleeRows(db, symbol, limit) {
|
|
1704
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1705
|
+
if (!match || !isVisualBasicDocument(db, match.relativePath)) {
|
|
1706
|
+
return [];
|
|
1707
|
+
}
|
|
1708
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1709
|
+
return resolveSimpleSourceCallees(db, match, calls, /* @__PURE__ */ new Map(), limit);
|
|
1710
|
+
}
|
|
1711
|
+
function getCppSourceCalleeRows(db, symbol, limit) {
|
|
1712
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1713
|
+
if (!match || !isCppDocument(db, match.relativePath)) {
|
|
1714
|
+
return [];
|
|
1715
|
+
}
|
|
1716
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1717
|
+
const bindings = parseCppReceiverBindings(getSourceText(db, match.relativePath));
|
|
1718
|
+
return resolveSimpleSourceCallees(db, match, calls, bindings, limit);
|
|
1719
|
+
}
|
|
1720
|
+
function getRustSourceCalleeRows(db, symbol, limit) {
|
|
1721
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1722
|
+
if (!match || !isRustDocument(db, match.relativePath)) {
|
|
1723
|
+
return [];
|
|
1724
|
+
}
|
|
1725
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1726
|
+
return resolveSimpleSourceCallees(db, match, calls, /* @__PURE__ */ new Map(), limit);
|
|
1727
|
+
}
|
|
1728
|
+
function getRubySourceCalleeRows(db, symbol, limit) {
|
|
1729
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1730
|
+
if (!match || !isRubyDocument(db, match.relativePath)) {
|
|
1731
|
+
return [];
|
|
1732
|
+
}
|
|
1733
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine, { allowInstanceVariables: true });
|
|
1734
|
+
const rubyCalls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine, {
|
|
1735
|
+
allowInstanceVariables: true,
|
|
1736
|
+
allowBareMemberCalls: true
|
|
1737
|
+
});
|
|
1738
|
+
const bindings = parseRubyReceiverBindings(db, getSourceText(db, match.relativePath));
|
|
1739
|
+
return resolveSimpleSourceCallees(db, match, rubyCalls.length > 0 ? rubyCalls : calls, bindings, limit);
|
|
1740
|
+
}
|
|
1741
|
+
function getDartSourceCalleeRows(db, symbol, limit) {
|
|
1742
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1743
|
+
if (!match || !isDartDocument(db, match.relativePath)) {
|
|
1744
|
+
return [];
|
|
1745
|
+
}
|
|
1746
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1747
|
+
return resolveSimpleSourceCallees(db, match, calls, /* @__PURE__ */ new Map(), limit);
|
|
1748
|
+
}
|
|
1749
|
+
function getPhpSourceCalleeRows(db, symbol, limit) {
|
|
1750
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1751
|
+
if (!match || !isPhpDocument(db, match.relativePath)) {
|
|
1752
|
+
return [];
|
|
1753
|
+
}
|
|
1754
|
+
const calls = getSimpleSourceCalls(db, match.relativePath, match.startLine, match.endLine);
|
|
1755
|
+
return resolveSimpleSourceCallees(db, match, calls, /* @__PURE__ */ new Map(), limit);
|
|
1756
|
+
}
|
|
1757
|
+
function getSourceBackedCalleeRows(db, symbol, limit) {
|
|
1758
|
+
const match = getFullSymbolMatch(db, symbol);
|
|
1759
|
+
if (!match) {
|
|
1760
|
+
return [];
|
|
1761
|
+
}
|
|
1762
|
+
if (isPythonDocument(db, match.relativePath)) {
|
|
1763
|
+
return getPythonSourceCalleeRows(db, match, limit);
|
|
1764
|
+
}
|
|
1765
|
+
if (isJavaScriptDocument(db, match.relativePath)) {
|
|
1766
|
+
return getJavaScriptSourceCalleeRows(db, match, limit);
|
|
1767
|
+
}
|
|
1768
|
+
if (isJavaDocument(db, match.relativePath)) {
|
|
1769
|
+
return getJavaSourceCalleeRows(db, match, limit);
|
|
1770
|
+
}
|
|
1771
|
+
if (isScalaDocument(db, match.relativePath)) {
|
|
1772
|
+
return getScalaSourceCalleeRows(db, match, limit);
|
|
1773
|
+
}
|
|
1774
|
+
if (isKotlinDocument(db, match.relativePath)) {
|
|
1775
|
+
return getKotlinSourceCalleeRows(db, match, limit);
|
|
1776
|
+
}
|
|
1777
|
+
if (isCSharpDocument(db, match.relativePath)) {
|
|
1778
|
+
return getCSharpSourceCalleeRows(db, match, limit);
|
|
1779
|
+
}
|
|
1780
|
+
if (isVisualBasicDocument(db, match.relativePath)) {
|
|
1781
|
+
return getVisualBasicSourceCalleeRows(db, match, limit);
|
|
1782
|
+
}
|
|
1783
|
+
if (isCppDocument(db, match.relativePath)) {
|
|
1784
|
+
return getCppSourceCalleeRows(db, match, limit);
|
|
1785
|
+
}
|
|
1786
|
+
if (isRustDocument(db, match.relativePath)) {
|
|
1787
|
+
return getRustSourceCalleeRows(db, match, limit);
|
|
1788
|
+
}
|
|
1789
|
+
if (isRubyDocument(db, match.relativePath)) {
|
|
1790
|
+
return getRubySourceCalleeRows(db, match, limit);
|
|
1791
|
+
}
|
|
1792
|
+
if (isDartDocument(db, match.relativePath)) {
|
|
1793
|
+
return getDartSourceCalleeRows(db, match, limit);
|
|
1794
|
+
}
|
|
1795
|
+
if (isPhpDocument(db, match.relativePath)) {
|
|
1796
|
+
return getPhpSourceCalleeRows(db, match, limit);
|
|
1797
|
+
}
|
|
1798
|
+
return [];
|
|
1799
|
+
}
|
|
1800
|
+
function getPythonSourceCallerRows(db, target, limit) {
|
|
1801
|
+
if (!isPythonDocument(db, target.relativePath)) {
|
|
1802
|
+
return [];
|
|
1803
|
+
}
|
|
1804
|
+
const rows = [];
|
|
1805
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1806
|
+
for (const candidate of getAllFunctionLikeDefinitions(db)) {
|
|
1807
|
+
if (candidate.symbolId === target.symbolId) continue;
|
|
1808
|
+
const callees = getPythonSourceCalleeRows(db, candidate);
|
|
1809
|
+
if (!callees.some((callee) => callee.symbol === target.symbol)) continue;
|
|
1810
|
+
const key = `${candidate.symbol}|${candidate.relativePath}`;
|
|
1811
|
+
if (seen.has(key) || db.isIgnored(candidate.relativePath)) continue;
|
|
1812
|
+
seen.add(key);
|
|
1813
|
+
rows.push({
|
|
1814
|
+
symbol: candidate.symbol,
|
|
1815
|
+
file: candidate.relativePath
|
|
1816
|
+
});
|
|
1817
|
+
if (typeof limit === "number" && rows.length >= limit) {
|
|
1818
|
+
break;
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
return rows;
|
|
1822
|
+
}
|
|
1823
|
+
function getDefinitionRowsForSymbolId(db, symbolId) {
|
|
1824
|
+
const primary = db.all(
|
|
1825
|
+
`SELECT gs.id, gs.symbol, der.document_id, der.start_line, der.end_line, d.relative_path, gs.display_name
|
|
1826
|
+
FROM global_symbols gs
|
|
1827
|
+
JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
|
|
1828
|
+
JOIN documents d ON der.document_id = d.id
|
|
1829
|
+
WHERE gs.id = ?
|
|
1830
|
+
ORDER BY der.start_line, der.end_line`,
|
|
1831
|
+
symbolId
|
|
1832
|
+
);
|
|
1833
|
+
if (primary.length > 0) {
|
|
1834
|
+
return primary;
|
|
1835
|
+
}
|
|
1836
|
+
return db.all(
|
|
1837
|
+
`SELECT
|
|
1838
|
+
gs.id,
|
|
1839
|
+
gs.symbol,
|
|
1840
|
+
c.document_id,
|
|
1841
|
+
MIN(c.start_line) AS start_line,
|
|
1842
|
+
MAX(c.end_line) AS end_line,
|
|
1843
|
+
d.relative_path,
|
|
1844
|
+
gs.display_name
|
|
1845
|
+
FROM global_symbols gs
|
|
1846
|
+
JOIN mentions m ON m.symbol_id = gs.id
|
|
1847
|
+
JOIN chunks c ON m.chunk_id = c.id
|
|
1848
|
+
JOIN documents d ON c.document_id = d.id
|
|
1849
|
+
WHERE gs.id = ?
|
|
1850
|
+
AND m.role = 1
|
|
1851
|
+
${db.pathExclusionsFor("d")}
|
|
1852
|
+
GROUP BY gs.id, gs.symbol, c.document_id, d.relative_path, gs.display_name
|
|
1853
|
+
ORDER BY start_line, end_line`,
|
|
1854
|
+
symbolId
|
|
1855
|
+
);
|
|
1856
|
+
}
|
|
1857
|
+
function getFullSymbolMatch(db, symbol) {
|
|
1858
|
+
if ("symbol" in symbol && "relativePath" in symbol) {
|
|
1859
|
+
return symbol;
|
|
1860
|
+
}
|
|
1861
|
+
const row = getDefinitionRowsForSymbolId(db, symbol.symbolId)[0];
|
|
1862
|
+
if (!row) {
|
|
1863
|
+
return null;
|
|
1864
|
+
}
|
|
1865
|
+
return {
|
|
1866
|
+
symbolId: row.id,
|
|
1867
|
+
documentId: row.document_id,
|
|
1868
|
+
startLine: row.start_line,
|
|
1869
|
+
endLine: row.end_line,
|
|
1870
|
+
symbol: row.symbol,
|
|
1871
|
+
relativePath: row.relative_path
|
|
1872
|
+
};
|
|
1873
|
+
}
|
|
1874
|
+
function getDefinitionsForFile(db, relativePath) {
|
|
1875
|
+
let cache = FILE_DEFINITION_CACHE.get(db);
|
|
1876
|
+
if (!cache) {
|
|
1877
|
+
cache = /* @__PURE__ */ new Map();
|
|
1878
|
+
FILE_DEFINITION_CACHE.set(db, cache);
|
|
1879
|
+
}
|
|
1880
|
+
const cached = cache.get(relativePath);
|
|
1881
|
+
if (cached) {
|
|
1882
|
+
return cached;
|
|
1883
|
+
}
|
|
1884
|
+
const primary = db.all(
|
|
1885
|
+
`SELECT
|
|
1886
|
+
gs.id,
|
|
1887
|
+
gs.symbol,
|
|
1888
|
+
der.document_id,
|
|
1889
|
+
der.start_line,
|
|
1890
|
+
der.end_line,
|
|
1891
|
+
d.relative_path,
|
|
1892
|
+
gs.kind,
|
|
1893
|
+
gs.documentation,
|
|
1894
|
+
gs.enclosing_symbol
|
|
1895
|
+
FROM global_symbols gs
|
|
1896
|
+
JOIN defn_enclosing_ranges der ON gs.id = der.symbol_id
|
|
1897
|
+
JOIN documents d ON der.document_id = d.id
|
|
1898
|
+
WHERE d.relative_path = ?
|
|
1899
|
+
${db.symbolNoiseFor("gs")}
|
|
1900
|
+
ORDER BY der.start_line, der.end_line`,
|
|
1901
|
+
relativePath
|
|
1902
|
+
);
|
|
1903
|
+
const fallback = primary.length > 0 ? [] : db.all(
|
|
1904
|
+
`SELECT
|
|
1905
|
+
gs.id,
|
|
1906
|
+
gs.symbol,
|
|
1907
|
+
c.document_id,
|
|
1908
|
+
MIN(c.start_line) AS start_line,
|
|
1909
|
+
MAX(c.end_line) AS end_line,
|
|
1910
|
+
d.relative_path,
|
|
1911
|
+
gs.kind,
|
|
1912
|
+
gs.documentation,
|
|
1913
|
+
gs.enclosing_symbol
|
|
1914
|
+
FROM global_symbols gs
|
|
1915
|
+
JOIN mentions m ON m.symbol_id = gs.id
|
|
1916
|
+
JOIN chunks c ON m.chunk_id = c.id
|
|
1917
|
+
JOIN documents d ON c.document_id = d.id
|
|
1918
|
+
WHERE d.relative_path = ?
|
|
1919
|
+
AND m.role = 1
|
|
1920
|
+
${db.symbolNoiseFor("gs")}
|
|
1921
|
+
GROUP BY gs.id, gs.symbol, c.document_id, d.relative_path
|
|
1922
|
+
ORDER BY start_line, end_line`,
|
|
1923
|
+
relativePath
|
|
1924
|
+
);
|
|
1925
|
+
const definitions = (primary.length > 0 ? primary : fallback).map((row) => ({
|
|
1926
|
+
symbolId: row.id,
|
|
1927
|
+
symbol: row.symbol,
|
|
1928
|
+
documentId: row.document_id,
|
|
1929
|
+
startLine: row.start_line,
|
|
1930
|
+
endLine: row.end_line,
|
|
1931
|
+
relativePath: row.relative_path,
|
|
1932
|
+
leaf: leafName(row.symbol),
|
|
1933
|
+
parentTypeName: parentTypeName(row.symbol),
|
|
1934
|
+
isFunctionLike: isFunctionLikeSymbol(row.symbol),
|
|
1935
|
+
isTypeLike: leafSuffix(row.symbol) === "type",
|
|
1936
|
+
kind: row.kind,
|
|
1937
|
+
documentation: row.documentation,
|
|
1938
|
+
enclosingSymbol: row.enclosing_symbol
|
|
1939
|
+
}));
|
|
1940
|
+
cache.set(relativePath, definitions);
|
|
1941
|
+
return definitions;
|
|
1942
|
+
}
|
|
1943
|
+
function getAllDefinitions(db, opts = {}) {
|
|
1944
|
+
const { scope } = opts;
|
|
1945
|
+
const rows = db.all(
|
|
1946
|
+
`SELECT relative_path
|
|
1947
|
+
FROM documents
|
|
1948
|
+
WHERE 1 = 1
|
|
1949
|
+
${db.pathExclusionsFor("documents")}
|
|
1950
|
+
${scope ? "AND relative_path LIKE ?" : ""}
|
|
1951
|
+
ORDER BY relative_path`,
|
|
1952
|
+
...scope ? [`%${scope}%`] : []
|
|
1953
|
+
);
|
|
1954
|
+
return rows.filter((row) => !db.isIgnored(row.relative_path)).flatMap((row) => getDefinitionsForFile(db, row.relative_path));
|
|
1955
|
+
}
|
|
1956
|
+
function getAllFunctionLikeDefinitions(db) {
|
|
1957
|
+
return getAllDefinitions(db).filter((definition) => definition.isFunctionLike);
|
|
1958
|
+
}
|
|
1959
|
+
function resolvePythonCallTarget(db, current, currentFileDefinitions, imports, constructorBindings, receiverName, calleeName) {
|
|
1960
|
+
if (receiverName === "self" || receiverName === "cls") {
|
|
1961
|
+
return findDefinitionByName(currentFileDefinitions, calleeName, current.parentTypeName, ["function"]);
|
|
1962
|
+
}
|
|
1963
|
+
if (receiverName) {
|
|
1964
|
+
const inferredType = constructorBindings.get(receiverName);
|
|
1965
|
+
if (inferredType) {
|
|
1966
|
+
const boundMethod = findDefinitionByName(currentFileDefinitions, calleeName, inferredType, ["function"]);
|
|
1967
|
+
if (boundMethod) {
|
|
1968
|
+
return boundMethod;
|
|
1969
|
+
}
|
|
1970
|
+
for (const entry of imports) {
|
|
1971
|
+
if (entry.localName !== inferredType || !entry.sourcePath) continue;
|
|
1972
|
+
const importedDefinitions = getDefinitionsForFile(db, entry.sourcePath);
|
|
1973
|
+
const importedMethod = findDefinitionByName(importedDefinitions, calleeName, entry.importedName, ["function"]);
|
|
1974
|
+
if (importedMethod) {
|
|
1975
|
+
return importedMethod;
|
|
1976
|
+
}
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
const localClassMethod = findDefinitionByName(currentFileDefinitions, calleeName, receiverName, ["function"]);
|
|
1980
|
+
if (localClassMethod) {
|
|
1981
|
+
return localClassMethod;
|
|
1982
|
+
}
|
|
1983
|
+
const namespaceImport = imports.find((entry) => entry.localName === receiverName && entry.sourcePath);
|
|
1984
|
+
if (namespaceImport?.sourcePath) {
|
|
1985
|
+
const importedDefinitions = getDefinitionsForFile(db, namespaceImport.sourcePath);
|
|
1986
|
+
const importedTypeMethod = namespaceImport.kind === "named" ? findDefinitionByName(importedDefinitions, calleeName, namespaceImport.importedName, ["function"]) : null;
|
|
1987
|
+
if (importedTypeMethod) {
|
|
1988
|
+
return importedTypeMethod;
|
|
1989
|
+
}
|
|
1990
|
+
return findDefinitionByName(importedDefinitions, calleeName, null, ["function", "type"]);
|
|
1991
|
+
}
|
|
1992
|
+
return null;
|
|
1993
|
+
}
|
|
1994
|
+
const importedBinding = imports.find((entry) => entry.localName === calleeName && entry.sourcePath);
|
|
1995
|
+
if (importedBinding?.sourcePath) {
|
|
1996
|
+
const importedDefinitions = getDefinitionsForFile(db, importedBinding.sourcePath);
|
|
1997
|
+
const importedName = importedBinding.importedName === "*" ? calleeName : importedBinding.importedName;
|
|
1998
|
+
const importedDefinition = findDefinitionByName(importedDefinitions, importedName, null, ["function", "type"]);
|
|
1999
|
+
if (importedDefinition) {
|
|
2000
|
+
return importedDefinition;
|
|
2001
|
+
}
|
|
2002
|
+
}
|
|
2003
|
+
return findDefinitionByName(currentFileDefinitions, calleeName, null, ["function", "type"]);
|
|
2004
|
+
}
|
|
2005
|
+
function resolveJavaScriptCallTarget(db, current, currentFileDefinitions, imports, constructorBindings, receiverName, calleeName) {
|
|
2006
|
+
if (receiverName === "this") {
|
|
2007
|
+
return findDefinitionByName(currentFileDefinitions, calleeName, current.parentTypeName, ["function"]);
|
|
2008
|
+
}
|
|
2009
|
+
if (receiverName) {
|
|
2010
|
+
const inferredType = constructorBindings.get(receiverName);
|
|
2011
|
+
if (inferredType) {
|
|
2012
|
+
const boundMethod = findDefinitionByName(currentFileDefinitions, calleeName, inferredType, ["function"]);
|
|
2013
|
+
if (boundMethod) {
|
|
2014
|
+
return boundMethod;
|
|
2015
|
+
}
|
|
2016
|
+
for (const entry of imports) {
|
|
2017
|
+
if (entry.localName !== inferredType || !entry.sourcePath) continue;
|
|
2018
|
+
const importedDefinitions = getDefinitionsForFile(db, entry.sourcePath);
|
|
2019
|
+
const importedMethod = findDefinitionByName(importedDefinitions, calleeName, entry.importedName, ["function"]);
|
|
2020
|
+
if (importedMethod) {
|
|
2021
|
+
return importedMethod;
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
const localClassMethod = findDefinitionByName(currentFileDefinitions, calleeName, receiverName, ["function"]);
|
|
2026
|
+
if (localClassMethod) {
|
|
2027
|
+
return localClassMethod;
|
|
2028
|
+
}
|
|
2029
|
+
const namespaceImport = imports.find((entry) => entry.localName === receiverName && entry.sourcePath);
|
|
2030
|
+
if (namespaceImport?.sourcePath) {
|
|
2031
|
+
const importedDefinitions = getDefinitionsForFile(db, namespaceImport.sourcePath);
|
|
2032
|
+
if (namespaceImport.kind === "named" || namespaceImport.kind === "default") {
|
|
2033
|
+
const importedTypeMethod = findDefinitionByName(
|
|
2034
|
+
importedDefinitions,
|
|
2035
|
+
calleeName,
|
|
2036
|
+
namespaceImport.importedName,
|
|
2037
|
+
["function"]
|
|
2038
|
+
);
|
|
2039
|
+
if (importedTypeMethod) {
|
|
2040
|
+
return importedTypeMethod;
|
|
2041
|
+
}
|
|
2042
|
+
}
|
|
2043
|
+
return findDefinitionByName(importedDefinitions, calleeName, null, ["function", "type"]);
|
|
2044
|
+
}
|
|
2045
|
+
return null;
|
|
2046
|
+
}
|
|
2047
|
+
const importedBinding = imports.find((entry) => entry.localName === calleeName && entry.sourcePath && entry.kind !== "namespace");
|
|
2048
|
+
if (importedBinding?.sourcePath) {
|
|
2049
|
+
const importedDefinitions = getDefinitionsForFile(db, importedBinding.sourcePath);
|
|
2050
|
+
const importedName = importedBinding.importedName === "default" ? importedBinding.localName ?? calleeName : importedBinding.importedName;
|
|
2051
|
+
const importedDefinition = findDefinitionByName(importedDefinitions, importedName, null, ["function", "type"]);
|
|
2052
|
+
if (importedDefinition) {
|
|
2053
|
+
return importedDefinition;
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
return findDefinitionByName(currentFileDefinitions, calleeName, null, ["function", "type"]);
|
|
2057
|
+
}
|
|
2058
|
+
function resolveSimpleSourceCallees(db, current, calls, bindings, limit) {
|
|
2059
|
+
const currentFileDefinitions = getDefinitionsForFile(db, current.relativePath);
|
|
2060
|
+
const currentDefinition = currentFileDefinitions.find((definition) => definition.symbolId === current.symbolId);
|
|
2061
|
+
if (!currentDefinition) {
|
|
2062
|
+
return [];
|
|
2063
|
+
}
|
|
2064
|
+
const rows = [];
|
|
2065
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2066
|
+
for (const call of calls) {
|
|
2067
|
+
const resolved = resolveSimpleSourceCallTarget(
|
|
2068
|
+
db,
|
|
2069
|
+
currentDefinition,
|
|
2070
|
+
currentFileDefinitions,
|
|
2071
|
+
bindings,
|
|
2072
|
+
call.receiverName,
|
|
2073
|
+
call.calleeName
|
|
2074
|
+
);
|
|
2075
|
+
if (!resolved || resolved.symbolId === current.symbolId || db.isIgnored(resolved.relativePath)) continue;
|
|
2076
|
+
const chunkId = 2e9 + call.line;
|
|
2077
|
+
const key = `${resolved.symbol}|${resolved.relativePath}|${chunkId}`;
|
|
2078
|
+
if (seen.has(key)) continue;
|
|
2079
|
+
seen.add(key);
|
|
2080
|
+
rows.push({
|
|
2081
|
+
symbol: resolved.symbol,
|
|
2082
|
+
file: resolved.relativePath,
|
|
2083
|
+
chunkId
|
|
2084
|
+
});
|
|
2085
|
+
}
|
|
2086
|
+
return applyLimit(rows, limit);
|
|
2087
|
+
}
|
|
2088
|
+
function resolveSimpleSourceCallTarget(db, current, currentFileDefinitions, bindings, receiverName, calleeName) {
|
|
2089
|
+
if (!receiverName) {
|
|
2090
|
+
const localMethod = findDefinitionByName(currentFileDefinitions, calleeName, current.parentTypeName, ["function"]);
|
|
2091
|
+
if (localMethod) {
|
|
2092
|
+
return localMethod;
|
|
2093
|
+
}
|
|
2094
|
+
if (current.parentTypeName) {
|
|
2095
|
+
return findProjectDefinitionByTypeAndLeaf(db, current.parentTypeName, calleeName);
|
|
2096
|
+
}
|
|
2097
|
+
return findDefinitionByName(currentFileDefinitions, calleeName, null, ["function", "type"]);
|
|
2098
|
+
}
|
|
2099
|
+
const normalizedReceiver = normalizeReceiverName(receiverName);
|
|
2100
|
+
const inferredType = bindings.get(normalizedReceiver) ?? inferTypeNameFromReceiver(db, normalizedReceiver);
|
|
2101
|
+
if (!inferredType) {
|
|
2102
|
+
return null;
|
|
2103
|
+
}
|
|
2104
|
+
return findProjectDefinitionByTypeAndLeaf(db, inferredType, calleeName);
|
|
2105
|
+
}
|
|
2106
|
+
function findProjectDefinitionByTypeAndLeaf(db, typeName, calleeName) {
|
|
2107
|
+
const definitions = getAllDefinitions(db).filter((definition) => definition.isFunctionLike || definition.symbol.endsWith("()."));
|
|
2108
|
+
const exact = definitions.find((definition) => definition.leaf === calleeName && (definition.parentTypeName === typeName || definition.symbol.includes(typeName)));
|
|
2109
|
+
if (exact) {
|
|
2110
|
+
return exact;
|
|
2111
|
+
}
|
|
2112
|
+
const normalizedType = normalizeLookupName(typeName);
|
|
2113
|
+
const normalizedMatch = definitions.find((definition) => definition.leaf === calleeName && normalizeLookupName(definition.parentTypeName ?? "").includes(normalizedType));
|
|
2114
|
+
if (normalizedMatch) {
|
|
2115
|
+
return normalizedMatch;
|
|
2116
|
+
}
|
|
2117
|
+
return findLooseProjectDefinitionByTypeAndLeaf(db, typeName, calleeName);
|
|
2118
|
+
}
|
|
2119
|
+
function inferTypeNameFromReceiver(db, receiverName) {
|
|
2120
|
+
const normalizedReceiver = normalizeLookupName(receiverName);
|
|
2121
|
+
if (!normalizedReceiver) {
|
|
2122
|
+
return null;
|
|
2123
|
+
}
|
|
2124
|
+
const candidates = getAllDefinitions(db).filter((definition) => definition.isTypeLike || definition.symbol.endsWith("#")).map((definition) => definition.leaf).filter((leaf) => leaf.length > 0);
|
|
2125
|
+
let best = null;
|
|
2126
|
+
for (const candidate of candidates) {
|
|
2127
|
+
const normalizedCandidate = normalizeLookupName(candidate);
|
|
2128
|
+
let score = 0;
|
|
2129
|
+
if (normalizedCandidate === normalizedReceiver) score += 100;
|
|
2130
|
+
if (normalizedCandidate.endsWith(normalizedReceiver)) score += 80;
|
|
2131
|
+
if (normalizedCandidate.includes(normalizedReceiver)) score += 40;
|
|
2132
|
+
if (normalizedReceiver.includes(normalizedCandidate)) score += 20;
|
|
2133
|
+
if (score > 0 && (!best || score > best.score || score === best.score && candidate.length < best.name.length)) {
|
|
2134
|
+
best = { name: candidate, score };
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
return best?.name ?? null;
|
|
2138
|
+
}
|
|
2139
|
+
function getSimpleSourceCalls(db, relativePath, startLine, endLine, opts = {}) {
|
|
2140
|
+
const source = getSourceText(db, relativePath);
|
|
2141
|
+
if (!source) {
|
|
2142
|
+
return [];
|
|
2143
|
+
}
|
|
2144
|
+
const lines = source.split("\n");
|
|
2145
|
+
const scoped = lines.slice(startLine, endLine + 1);
|
|
2146
|
+
const calls = [];
|
|
2147
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2148
|
+
const pattern = opts.allowInstanceVariables ? /(@?[A-Za-z_][\w]*)\s*(?:\.|::)\s*([A-Za-z_][\w!?=]*)\s*\(/g : /\b([A-Za-z_][\w]*)\s*(?:\.|::)\s*([A-Za-z_][\w]*)\s*\(/g;
|
|
2149
|
+
for (let index = 0; index < scoped.length; index++) {
|
|
2150
|
+
const rawLine = scoped[index] ?? "";
|
|
2151
|
+
for (const match of rawLine.matchAll(pattern)) {
|
|
2152
|
+
const receiverName = match[1];
|
|
2153
|
+
const calleeName = match[2];
|
|
2154
|
+
if (!receiverName || !calleeName) continue;
|
|
2155
|
+
const key = `${startLine + index}|${receiverName}|${calleeName}`;
|
|
2156
|
+
if (seen.has(key)) continue;
|
|
2157
|
+
seen.add(key);
|
|
2158
|
+
calls.push({
|
|
2159
|
+
receiverName,
|
|
2160
|
+
calleeName,
|
|
2161
|
+
line: startLine + index
|
|
2162
|
+
});
|
|
2163
|
+
}
|
|
2164
|
+
if (opts.allowBareMemberCalls) {
|
|
2165
|
+
for (const match of rawLine.matchAll(/(@?[A-Za-z_][\w]*)\s*\.\s*([A-Za-z_][\w!?=]*)\b(?!\s*[:=])/g)) {
|
|
2166
|
+
const receiverName = match[1];
|
|
2167
|
+
const calleeName = match[2];
|
|
2168
|
+
if (!receiverName || !calleeName) continue;
|
|
2169
|
+
const key = `${startLine + index}|${receiverName}|${calleeName}`;
|
|
2170
|
+
if (seen.has(key)) continue;
|
|
2171
|
+
seen.add(key);
|
|
2172
|
+
calls.push({
|
|
2173
|
+
receiverName,
|
|
2174
|
+
calleeName,
|
|
2175
|
+
line: startLine + index
|
|
2176
|
+
});
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
for (const match of rawLine.matchAll(/\b([A-Za-z_][\w]*)\s*\(/g)) {
|
|
2180
|
+
const calleeName = match[1];
|
|
2181
|
+
const start = match.index ?? -1;
|
|
2182
|
+
if (!calleeName || start < 0) continue;
|
|
2183
|
+
const prefix = rawLine.slice(0, start).trimEnd();
|
|
2184
|
+
if (prefix.endsWith("def") || prefix.endsWith("fun") || prefix.endsWith("fn") || /\b(?:class|interface|trait|module|object)\s*$/.test(prefix)) {
|
|
2185
|
+
continue;
|
|
2186
|
+
}
|
|
2187
|
+
if (rawLine.slice(Math.max(0, start - 3), start).includes(".")) {
|
|
2188
|
+
continue;
|
|
2189
|
+
}
|
|
2190
|
+
calls.push({
|
|
2191
|
+
receiverName: null,
|
|
2192
|
+
calleeName,
|
|
2193
|
+
line: startLine + index
|
|
2194
|
+
});
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
return calls;
|
|
2198
|
+
}
|
|
2199
|
+
function parseJavaFieldBindings(source) {
|
|
2200
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
2201
|
+
for (const match of source.matchAll(/\b(?:private|protected|public)\s+(?:final\s+)?([A-Z][A-Za-z0-9_$<>?, ]*)\s+([A-Za-z_][\w$]*)\s*;/g)) {
|
|
2202
|
+
const typeName = stripGenericType(match[1]);
|
|
2203
|
+
const localName = match[2];
|
|
2204
|
+
if (typeName && localName) {
|
|
2205
|
+
bindings.set(localName, typeName);
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
return bindings;
|
|
2209
|
+
}
|
|
2210
|
+
function parseKotlinFieldBindings(source) {
|
|
2211
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
2212
|
+
for (const match of source.matchAll(/\b(?:private|protected|public)?\s*(?:val|var)\s+([A-Za-z_][\w]*)\s*:\s*([A-Z][A-Za-z0-9_.<>?]*)/g)) {
|
|
2213
|
+
const localName = match[1];
|
|
2214
|
+
const typeName = stripGenericType(match[2]);
|
|
2215
|
+
if (typeName && localName) {
|
|
2216
|
+
bindings.set(localName, typeName);
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
return bindings;
|
|
2220
|
+
}
|
|
2221
|
+
function parseCppReceiverBindings(source) {
|
|
2222
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
2223
|
+
const constructorMatch = source.match(/RunCoordinator::RunCoordinator\s*\(([\s\S]*?)\)\s*(?::\s*([\s\S]*?))?\s*\{/);
|
|
2224
|
+
if (!constructorMatch) {
|
|
2225
|
+
return bindings;
|
|
2226
|
+
}
|
|
2227
|
+
const params = constructorMatch[1] ?? "";
|
|
2228
|
+
const initializers = constructorMatch[2] ?? "";
|
|
2229
|
+
const parameterTypes = /* @__PURE__ */ new Map();
|
|
2230
|
+
for (const param of params.split(",")) {
|
|
2231
|
+
const trimmed = param.trim();
|
|
2232
|
+
const match = trimmed.match(/(.+?)\s+([A-Za-z_][\w]*)$/);
|
|
2233
|
+
if (!match) continue;
|
|
2234
|
+
const [, rawType, rawName] = match;
|
|
2235
|
+
if (!rawType || !rawName) continue;
|
|
2236
|
+
const typeName = stripGenericType(rawType.replace(/[&*]+/g, " ").replace(/\bconst\b/g, " ").trim());
|
|
2237
|
+
if (!typeName) continue;
|
|
2238
|
+
parameterTypes.set(normalizeReceiverName(rawName), typeName);
|
|
2239
|
+
}
|
|
2240
|
+
for (const initializer of initializers.split(",")) {
|
|
2241
|
+
const trimmed = initializer.trim();
|
|
2242
|
+
const match = trimmed.match(/([A-Za-z_][\w]*)\s*\(\s*([A-Za-z_][\w]*)\s*\)$/);
|
|
2243
|
+
if (!match) continue;
|
|
2244
|
+
const [, fieldName, sourceName] = match;
|
|
2245
|
+
if (!fieldName || !sourceName) continue;
|
|
2246
|
+
const typeName = parameterTypes.get(normalizeReceiverName(sourceName));
|
|
2247
|
+
if (!typeName) continue;
|
|
2248
|
+
bindings.set(normalizeReceiverName(fieldName), typeName);
|
|
2249
|
+
}
|
|
2250
|
+
return bindings;
|
|
2251
|
+
}
|
|
2252
|
+
function parseRubyReceiverBindings(db, source) {
|
|
2253
|
+
const bindings = /* @__PURE__ */ new Map();
|
|
2254
|
+
for (const match of source.matchAll(/@([A-Za-z_][\w]*)\s*=\s*([A-Za-z_][\w]*)/g)) {
|
|
2255
|
+
const ivarName = `@${match[1]}`;
|
|
2256
|
+
const localName = match[2];
|
|
2257
|
+
const typeName = inferTypeNameFromReceiver(db, localName ?? match[1] ?? "");
|
|
2258
|
+
if (typeName) {
|
|
2259
|
+
bindings.set(ivarName, typeName);
|
|
2260
|
+
bindings.set(match[1], typeName);
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2263
|
+
return bindings;
|
|
2264
|
+
}
|
|
2265
|
+
function stripGenericType(typeName) {
|
|
2266
|
+
return (typeName ?? "").replace(/<.*$/, "").replace(/^.*\./, "").trim();
|
|
2267
|
+
}
|
|
2268
|
+
function normalizeReceiverName(receiverName) {
|
|
2269
|
+
return receiverName.replace(/^@/, "").replace(/^this(?:\.|->)/, "").replace(/^_+/, "").replace(/_+$/, "").trim();
|
|
2270
|
+
}
|
|
2271
|
+
function normalizeLookupName(value) {
|
|
2272
|
+
return value.replace(/[^A-Za-z0-9]+/g, "").toLowerCase();
|
|
2273
|
+
}
|
|
2274
|
+
function isLikelyTestPath(relativePath) {
|
|
2275
|
+
return /(^|\/)(tests?|__tests__)\//.test(relativePath) || /\.(?:spec|test)\./.test(relativePath) || /_test\./.test(relativePath);
|
|
2276
|
+
}
|
|
2277
|
+
function findDefinitionByName(definitions, leaf, parentType, preference) {
|
|
2278
|
+
const candidates = definitions.filter((definition) => definition.leaf === leaf && definition.parentTypeName === parentType);
|
|
2279
|
+
if (candidates.length === 0) {
|
|
2280
|
+
return null;
|
|
2281
|
+
}
|
|
2282
|
+
for (const preferred of preference) {
|
|
2283
|
+
const match = candidates.find((candidate) => preferred === "function" ? candidate.isFunctionLike : candidate.isTypeLike);
|
|
2284
|
+
if (match) {
|
|
2285
|
+
return match;
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
return candidates[0] ?? null;
|
|
2289
|
+
}
|
|
2290
|
+
function findLooseProjectDefinitionByTypeAndLeaf(db, typeName, calleeName) {
|
|
2291
|
+
const rows = db.all(
|
|
2292
|
+
`SELECT
|
|
2293
|
+
gs.id,
|
|
2294
|
+
gs.symbol,
|
|
2295
|
+
c.document_id,
|
|
2296
|
+
MIN(c.start_line) AS start_line,
|
|
2297
|
+
MAX(c.end_line) AS end_line,
|
|
2298
|
+
d.relative_path,
|
|
2299
|
+
gs.kind,
|
|
2300
|
+
gs.documentation,
|
|
2301
|
+
gs.enclosing_symbol
|
|
2302
|
+
FROM global_symbols gs
|
|
2303
|
+
JOIN mentions m ON m.symbol_id = gs.id
|
|
2304
|
+
JOIN chunks c ON c.id = m.chunk_id
|
|
2305
|
+
JOIN documents d ON d.id = c.document_id
|
|
2306
|
+
WHERE gs.symbol LIKE ?
|
|
2307
|
+
AND gs.symbol LIKE ?
|
|
2308
|
+
${db.pathExclusionsFor("d")}
|
|
2309
|
+
GROUP BY gs.id, gs.symbol, c.document_id, d.relative_path, gs.kind, gs.documentation, gs.enclosing_symbol`,
|
|
2310
|
+
`%${typeName}%`,
|
|
2311
|
+
`%${calleeName}%`
|
|
2312
|
+
);
|
|
2313
|
+
const normalizedType = normalizeLookupName(typeName);
|
|
2314
|
+
const candidates = rows.filter((row2) => leafName(row2.symbol) === calleeName).filter((row2) => {
|
|
2315
|
+
const parentType = parentTypeName(row2.symbol);
|
|
2316
|
+
return parentType === typeName || row2.symbol.includes(typeName) || normalizeLookupName(parentType ?? "").includes(normalizedType);
|
|
2317
|
+
}).sort((left, right) => {
|
|
2318
|
+
const leftTest = isLikelyTestPath(left.relative_path) ? 1 : 0;
|
|
2319
|
+
const rightTest = isLikelyTestPath(right.relative_path) ? 1 : 0;
|
|
2320
|
+
return leftTest - rightTest || left.relative_path.localeCompare(right.relative_path) || left.start_line - right.start_line;
|
|
2321
|
+
});
|
|
2322
|
+
const row = candidates[0];
|
|
2323
|
+
if (!row) {
|
|
2324
|
+
return null;
|
|
2325
|
+
}
|
|
2326
|
+
return {
|
|
2327
|
+
symbolId: row.id,
|
|
2328
|
+
symbol: row.symbol,
|
|
2329
|
+
documentId: row.document_id,
|
|
2330
|
+
startLine: row.start_line,
|
|
2331
|
+
endLine: row.end_line,
|
|
2332
|
+
relativePath: row.relative_path,
|
|
2333
|
+
leaf: leafName(row.symbol),
|
|
2334
|
+
parentTypeName: parentTypeName(row.symbol),
|
|
2335
|
+
isFunctionLike: isFunctionLikeSymbol(row.symbol),
|
|
2336
|
+
isTypeLike: leafSuffix(row.symbol) === "type",
|
|
2337
|
+
kind: row.kind,
|
|
2338
|
+
documentation: row.documentation,
|
|
2339
|
+
enclosingSymbol: row.enclosing_symbol
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
2342
|
+
function hasUniqueLeafDefinition(db, leaf, symbolId) {
|
|
2343
|
+
const rows = db.all(
|
|
2344
|
+
`SELECT id, symbol
|
|
2345
|
+
FROM global_symbols
|
|
2346
|
+
WHERE symbol LIKE ?
|
|
2347
|
+
LIMIT 50`,
|
|
2348
|
+
`%${leaf}%`
|
|
2349
|
+
);
|
|
2350
|
+
let count = 0;
|
|
2351
|
+
for (const row of rows) {
|
|
2352
|
+
if (leafName(row.symbol) !== leaf) continue;
|
|
2353
|
+
count++;
|
|
2354
|
+
if (count > 1 && row.id !== symbolId) {
|
|
2355
|
+
return false;
|
|
2356
|
+
}
|
|
2357
|
+
}
|
|
2358
|
+
return count === 1;
|
|
2359
|
+
}
|
|
2360
|
+
function parentTypeName(rawSymbol) {
|
|
2361
|
+
const parsed = parseSymbol(rawSymbol);
|
|
2362
|
+
if ("kind" in parsed) {
|
|
2363
|
+
return null;
|
|
2364
|
+
}
|
|
2365
|
+
for (let index = parsed.descriptors.length - 2; index >= 0; index--) {
|
|
2366
|
+
const descriptor = parsed.descriptors[index];
|
|
2367
|
+
if (descriptor?.suffix === "type") {
|
|
2368
|
+
return descriptor.name;
|
|
2369
|
+
}
|
|
2370
|
+
}
|
|
2371
|
+
return null;
|
|
2372
|
+
}
|
|
2373
|
+
function isPythonDocument(db, relativePath) {
|
|
2374
|
+
const row = db.get(
|
|
2375
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2376
|
+
relativePath
|
|
2377
|
+
);
|
|
2378
|
+
return row?.language === "python" || relativePath.endsWith(".py") || relativePath.endsWith(".pyi");
|
|
2379
|
+
}
|
|
2380
|
+
function isJavaScriptDocument(db, relativePath) {
|
|
2381
|
+
const row = db.get(
|
|
2382
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2383
|
+
relativePath
|
|
2384
|
+
);
|
|
2385
|
+
return row?.language === "typescript" || row?.language === "javascript" || /\.(?:[cm]?[jt]sx?)$/.test(relativePath);
|
|
2386
|
+
}
|
|
2387
|
+
function isJavaDocument(db, relativePath) {
|
|
2388
|
+
const row = db.get(
|
|
2389
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2390
|
+
relativePath
|
|
2391
|
+
);
|
|
2392
|
+
return row?.language === "java" || relativePath.endsWith(".java");
|
|
2393
|
+
}
|
|
2394
|
+
function isKotlinDocument(db, relativePath) {
|
|
2395
|
+
const row = db.get(
|
|
2396
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2397
|
+
relativePath
|
|
2398
|
+
);
|
|
2399
|
+
return row?.language === "kotlin" || relativePath.endsWith(".kt") || relativePath.endsWith(".kts");
|
|
2400
|
+
}
|
|
2401
|
+
function isScalaDocument(db, relativePath) {
|
|
2402
|
+
const row = db.get(
|
|
2403
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2404
|
+
relativePath
|
|
2405
|
+
);
|
|
2406
|
+
return row?.language === "scala" || relativePath.endsWith(".scala");
|
|
2407
|
+
}
|
|
2408
|
+
function isCSharpDocument(db, relativePath) {
|
|
2409
|
+
const row = db.get(
|
|
2410
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2411
|
+
relativePath
|
|
2412
|
+
);
|
|
2413
|
+
return row?.language === "C#" || relativePath.endsWith(".cs");
|
|
2414
|
+
}
|
|
2415
|
+
function isVisualBasicDocument(db, relativePath) {
|
|
2416
|
+
const row = db.get(
|
|
2417
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2418
|
+
relativePath
|
|
2419
|
+
);
|
|
2420
|
+
return row?.language === "Visual Basic" || relativePath.endsWith(".vb");
|
|
2421
|
+
}
|
|
2422
|
+
function isCppDocument(db, relativePath) {
|
|
2423
|
+
const row = db.get(
|
|
2424
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2425
|
+
relativePath
|
|
2426
|
+
);
|
|
2427
|
+
return row?.language === "CPP" || /\.(?:cc|cpp|cxx|hpp|hh|hxx)$/.test(relativePath);
|
|
2428
|
+
}
|
|
2429
|
+
function isRustDocument(db, relativePath) {
|
|
2430
|
+
const row = db.get(
|
|
2431
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2432
|
+
relativePath
|
|
2433
|
+
);
|
|
2434
|
+
return row?.language === "Rust" || relativePath.endsWith(".rs");
|
|
2435
|
+
}
|
|
2436
|
+
function isRubyDocument(db, relativePath) {
|
|
2437
|
+
const row = db.get(
|
|
2438
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2439
|
+
relativePath
|
|
2440
|
+
);
|
|
2441
|
+
return row?.language === "ruby" || relativePath.endsWith(".rb");
|
|
2442
|
+
}
|
|
2443
|
+
function isDartDocument(db, relativePath) {
|
|
2444
|
+
const row = db.get(
|
|
2445
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2446
|
+
relativePath
|
|
2447
|
+
);
|
|
2448
|
+
return row?.language === "Dart" || relativePath.endsWith(".dart");
|
|
2449
|
+
}
|
|
2450
|
+
function isPhpDocument(db, relativePath) {
|
|
2451
|
+
const row = db.get(
|
|
2452
|
+
`SELECT language FROM documents WHERE relative_path = ? LIMIT 1`,
|
|
2453
|
+
relativePath
|
|
2454
|
+
);
|
|
2455
|
+
return row?.language === "PHP" || relativePath.endsWith(".php");
|
|
2456
|
+
}
|
|
2457
|
+
function applyLimit(values, limit) {
|
|
2458
|
+
return typeof limit === "number" ? values.slice(0, limit) : values;
|
|
2459
|
+
}
|
|
2460
|
+
function resolveDocumentCandidates(db, filePattern, opts) {
|
|
2461
|
+
const normalizedPattern = normalizeLookupPath(filePattern);
|
|
2462
|
+
if (!normalizedPattern) {
|
|
2463
|
+
return [];
|
|
2464
|
+
}
|
|
2465
|
+
const rows = db.all(
|
|
2466
|
+
`SELECT relative_path
|
|
2467
|
+
FROM documents
|
|
2468
|
+
WHERE 1 = 1
|
|
2469
|
+
${db.pathExclusionsFor("documents")}
|
|
2470
|
+
ORDER BY relative_path`
|
|
2471
|
+
);
|
|
2472
|
+
const scored = rows.filter((row) => !db.isIgnored(row.relative_path)).map((row) => ({
|
|
2473
|
+
relativePath: row.relative_path,
|
|
2474
|
+
score: scoreDocumentPath(row.relative_path, normalizedPattern)
|
|
2475
|
+
})).filter((row) => row.score > 0).sort((a, b) => b.score - a.score || a.relativePath.localeCompare(b.relativePath));
|
|
2476
|
+
if (scored.length === 0) {
|
|
2477
|
+
const symbolMatch = findFirstSymbolMatch(db, filePattern);
|
|
2478
|
+
if (!symbolMatch || db.isIgnored(symbolMatch.relativePath)) {
|
|
2479
|
+
return [];
|
|
2480
|
+
}
|
|
2481
|
+
return [{
|
|
2482
|
+
relativePath: symbolMatch.relativePath,
|
|
2483
|
+
score: 700
|
|
2484
|
+
}];
|
|
2485
|
+
}
|
|
2486
|
+
const exactish = scored.filter((row) => row.score >= 800);
|
|
2487
|
+
if (exactish.length > 0) {
|
|
2488
|
+
return opts.allowMultiple ? exactish : [exactish[0]];
|
|
2489
|
+
}
|
|
2490
|
+
return opts.allowMultiple ? scored : [scored[0]];
|
|
2491
|
+
}
|
|
2492
|
+
function scoreDocumentPath(relativePath, rawPattern) {
|
|
2493
|
+
const normalizedPath = normalizeLookupPath(relativePath);
|
|
2494
|
+
const pathBase = basename2(normalizedPath);
|
|
2495
|
+
const patternBase = basename2(rawPattern);
|
|
2496
|
+
let score = 0;
|
|
2497
|
+
if (normalizedPath === rawPattern) score += 1200;
|
|
2498
|
+
if (normalizedPath.endsWith(`/${rawPattern}`)) score += 1100;
|
|
2499
|
+
if (pathBase === patternBase) score += 900;
|
|
2500
|
+
if (normalizedPath.startsWith(`${rawPattern}/`)) score += 850;
|
|
2501
|
+
if (normalizedPath.includes(rawPattern)) score += 250;
|
|
2502
|
+
return score;
|
|
2503
|
+
}
|
|
2504
|
+
function normalizeLookupPath(filePattern) {
|
|
2505
|
+
return filePattern.trim().replace(/\\/g, "/").replace(/^\.\//, "").replace(/^\/+/, "").replace(/\/+$/, "");
|
|
2506
|
+
}
|
|
2507
|
+
function uniquePatterns(patterns) {
|
|
2508
|
+
return [...new Set(patterns)];
|
|
2509
|
+
}
|
|
2510
|
+
|
|
2511
|
+
export {
|
|
2512
|
+
getSourceImports,
|
|
2513
|
+
getSourceExports,
|
|
2514
|
+
getSourceText,
|
|
2515
|
+
TEST_FILE_PATTERNS,
|
|
2516
|
+
TEST_SUPPORT_PATH_PATTERNS,
|
|
2517
|
+
testFileExclusionSql,
|
|
2518
|
+
buildFileDepGraph,
|
|
2519
|
+
findFirstSymbolMatch,
|
|
2520
|
+
findExactSymbolMatch,
|
|
2521
|
+
resolveIndexedFile,
|
|
2522
|
+
resolveIndexedPaths,
|
|
2523
|
+
getCalleeRowsForSymbol,
|
|
2524
|
+
getCallerRowsForSymbol,
|
|
2525
|
+
getSourceReferenceSites,
|
|
2526
|
+
getDefinitionsForFile,
|
|
2527
|
+
getAllDefinitions
|
|
2528
|
+
};
|
|
2529
|
+
//# sourceMappingURL=chunk-QGCEAVJD.js.map
|