@domainlang/language 0.7.0 → 0.9.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/out/domain-lang-module.d.ts +2 -0
- package/out/domain-lang-module.js +21 -2
- package/out/domain-lang-module.js.map +1 -1
- package/out/lsp/domain-lang-completion.d.ts +142 -1
- package/out/lsp/domain-lang-completion.js +620 -22
- package/out/lsp/domain-lang-completion.js.map +1 -1
- package/out/lsp/domain-lang-document-symbol-provider.d.ts +79 -0
- package/out/lsp/domain-lang-document-symbol-provider.js +210 -0
- package/out/lsp/domain-lang-document-symbol-provider.js.map +1 -0
- package/out/lsp/domain-lang-index-manager.d.ts +34 -5
- package/out/lsp/domain-lang-index-manager.js +66 -27
- package/out/lsp/domain-lang-index-manager.js.map +1 -1
- package/out/lsp/domain-lang-node-kind-provider.d.ts +27 -0
- package/out/lsp/domain-lang-node-kind-provider.js +87 -0
- package/out/lsp/domain-lang-node-kind-provider.js.map +1 -0
- package/out/lsp/domain-lang-scope-provider.d.ts +53 -20
- package/out/lsp/domain-lang-scope-provider.js +119 -44
- package/out/lsp/domain-lang-scope-provider.js.map +1 -1
- package/out/lsp/domain-lang-workspace-manager.d.ts +23 -2
- package/out/lsp/domain-lang-workspace-manager.js +51 -6
- package/out/lsp/domain-lang-workspace-manager.js.map +1 -1
- package/out/lsp/hover/domain-lang-hover.d.ts +16 -6
- package/out/lsp/hover/domain-lang-hover.js +160 -134
- package/out/lsp/hover/domain-lang-hover.js.map +1 -1
- package/out/lsp/hover/hover-builders.d.ts +57 -0
- package/out/lsp/hover/hover-builders.js +171 -0
- package/out/lsp/hover/hover-builders.js.map +1 -0
- package/out/main.js +2 -1
- package/out/main.js.map +1 -1
- package/out/sdk/index.d.ts +31 -11
- package/out/sdk/index.js +30 -11
- package/out/sdk/index.js.map +1 -1
- package/out/sdk/loader-node.d.ts +2 -0
- package/out/sdk/loader-node.js +3 -1
- package/out/sdk/loader-node.js.map +1 -1
- package/out/sdk/loader.d.ts +55 -2
- package/out/sdk/loader.js +87 -28
- package/out/sdk/loader.js.map +1 -1
- package/out/sdk/query.js +14 -11
- package/out/sdk/query.js.map +1 -1
- package/out/sdk/validator.d.ts +134 -0
- package/out/sdk/validator.js +249 -0
- package/out/sdk/validator.js.map +1 -0
- package/out/services/package-boundary-detector.d.ts +101 -0
- package/out/services/package-boundary-detector.js +211 -0
- package/out/services/package-boundary-detector.js.map +1 -0
- package/out/services/performance-optimizer.js +6 -2
- package/out/services/performance-optimizer.js.map +1 -1
- package/out/services/types.d.ts +24 -0
- package/out/services/types.js.map +1 -1
- package/out/services/workspace-manager.d.ts +73 -6
- package/out/services/workspace-manager.js +210 -57
- package/out/services/workspace-manager.js.map +1 -1
- package/out/utils/import-utils.d.ts +9 -6
- package/out/utils/import-utils.js +26 -15
- package/out/utils/import-utils.js.map +1 -1
- package/out/validation/constants.d.ts +7 -0
- package/out/validation/constants.js +21 -3
- package/out/validation/constants.js.map +1 -1
- package/out/validation/import.d.ts +11 -1
- package/out/validation/import.js +42 -14
- package/out/validation/import.js.map +1 -1
- package/out/validation/maps.js +50 -1
- package/out/validation/maps.js.map +1 -1
- package/package.json +8 -9
- package/src/domain-lang-module.ts +24 -3
- package/src/lsp/domain-lang-completion.ts +736 -27
- package/src/lsp/domain-lang-document-symbol-provider.ts +254 -0
- package/src/lsp/domain-lang-index-manager.ts +79 -27
- package/src/lsp/domain-lang-node-kind-provider.ts +119 -0
- package/src/lsp/domain-lang-scope-provider.ts +171 -55
- package/src/lsp/domain-lang-workspace-manager.ts +64 -6
- package/src/lsp/hover/domain-lang-hover.ts +189 -131
- package/src/lsp/hover/hover-builders.ts +208 -0
- package/src/main.ts +3 -1
- package/src/sdk/index.ts +33 -11
- package/src/sdk/loader-node.ts +6 -1
- package/src/sdk/loader.ts +125 -34
- package/src/sdk/query.ts +15 -11
- package/src/sdk/validator.ts +358 -0
- package/src/services/package-boundary-detector.ts +238 -0
- package/src/services/performance-optimizer.ts +6 -2
- package/src/services/types.ts +25 -0
- package/src/services/workspace-manager.ts +259 -62
- package/src/utils/import-utils.ts +27 -15
- package/src/validation/constants.ts +23 -6
- package/src/validation/import.ts +49 -14
- package/src/validation/maps.ts +59 -2
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Model validation utilities for Node.js environments.
|
|
3
|
+
*
|
|
4
|
+
* **WARNING: This module is NOT browser-compatible.**
|
|
5
|
+
*
|
|
6
|
+
* Provides validation capabilities that leverage the LSP infrastructure
|
|
7
|
+
* for workspace initialization, import resolution, and document building.
|
|
8
|
+
*
|
|
9
|
+
* @module sdk/validator
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { NodeFileSystem } from 'langium/node';
|
|
13
|
+
import { URI } from 'langium';
|
|
14
|
+
import { createDomainLangServices } from '../domain-lang-module.js';
|
|
15
|
+
import { ensureImportGraphFromDocument } from '../utils/import-utils.js';
|
|
16
|
+
import { isModel } from '../generated/ast.js';
|
|
17
|
+
import { dirname, resolve, join } from 'node:path';
|
|
18
|
+
import { existsSync } from 'node:fs';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Validation diagnostic with file context.
|
|
22
|
+
*/
|
|
23
|
+
export interface ValidationDiagnostic {
|
|
24
|
+
/** Diagnostic severity (1=error, 2=warning, 3=info, 4=hint) */
|
|
25
|
+
severity: number;
|
|
26
|
+
/** Diagnostic message */
|
|
27
|
+
message: string;
|
|
28
|
+
/** File path where diagnostic occurred */
|
|
29
|
+
file: string;
|
|
30
|
+
/** Line number (1-based) */
|
|
31
|
+
line: number;
|
|
32
|
+
/** Column number (1-based) */
|
|
33
|
+
column: number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Result of model validation.
|
|
38
|
+
*/
|
|
39
|
+
export interface ValidationResult {
|
|
40
|
+
/** Whether the model is valid (no errors) */
|
|
41
|
+
valid: boolean;
|
|
42
|
+
/** Number of files validated */
|
|
43
|
+
fileCount: number;
|
|
44
|
+
/** Number of domains in the model */
|
|
45
|
+
domainCount: number;
|
|
46
|
+
/** Number of bounded contexts in the model */
|
|
47
|
+
bcCount: number;
|
|
48
|
+
/** Validation errors */
|
|
49
|
+
errors: ValidationDiagnostic[];
|
|
50
|
+
/** Validation warnings */
|
|
51
|
+
warnings: ValidationDiagnostic[];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Options for validation.
|
|
56
|
+
*/
|
|
57
|
+
export interface ValidationOptions {
|
|
58
|
+
/** Workspace directory (defaults to file's directory) */
|
|
59
|
+
workspaceDir?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Convert Langium diagnostic to ValidationDiagnostic.
|
|
64
|
+
*/
|
|
65
|
+
function toValidationDiagnostic(
|
|
66
|
+
diagnostic: { severity?: number; message: string; range: { start: { line: number; character: number } } },
|
|
67
|
+
file: string
|
|
68
|
+
): ValidationDiagnostic {
|
|
69
|
+
return {
|
|
70
|
+
severity: diagnostic.severity ?? 1,
|
|
71
|
+
message: diagnostic.message,
|
|
72
|
+
file,
|
|
73
|
+
line: diagnostic.range.start.line + 1,
|
|
74
|
+
column: diagnostic.range.start.character + 1,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validates a DomainLang model file and all its imports.
|
|
80
|
+
*
|
|
81
|
+
* Uses the LSP infrastructure to:
|
|
82
|
+
* - Initialize the workspace
|
|
83
|
+
* - Resolve and load imports
|
|
84
|
+
* - Build and validate all documents
|
|
85
|
+
*
|
|
86
|
+
* @param filePath - Path to the entry .dlang file
|
|
87
|
+
* @param options - Validation options
|
|
88
|
+
* @returns Validation result with errors, warnings, and model statistics
|
|
89
|
+
* @throws Error if file doesn't exist or has invalid extension
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { validateFile } from '@domainlang/language/sdk';
|
|
94
|
+
*
|
|
95
|
+
* const result = await validateFile('./index.dlang');
|
|
96
|
+
*
|
|
97
|
+
* if (!result.valid) {
|
|
98
|
+
* for (const err of result.errors) {
|
|
99
|
+
* console.error(`${err.file}:${err.line}:${err.column}: ${err.message}`);
|
|
100
|
+
* }
|
|
101
|
+
* process.exit(1);
|
|
102
|
+
* }
|
|
103
|
+
*
|
|
104
|
+
* console.log(`✓ Validated ${result.fileCount} files`);
|
|
105
|
+
* console.log(` ${result.domainCount} domains, ${result.bcCount} bounded contexts`);
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
export async function validateFile(
|
|
109
|
+
filePath: string,
|
|
110
|
+
options: ValidationOptions = {}
|
|
111
|
+
): Promise<ValidationResult> {
|
|
112
|
+
// Resolve absolute path
|
|
113
|
+
const absolutePath = resolve(filePath);
|
|
114
|
+
|
|
115
|
+
// Check file exists
|
|
116
|
+
if (!existsSync(absolutePath)) {
|
|
117
|
+
throw new Error(`File not found: ${filePath}`);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Create services with workspace support
|
|
121
|
+
const servicesObj = createDomainLangServices(NodeFileSystem);
|
|
122
|
+
const shared = servicesObj.shared;
|
|
123
|
+
const services = servicesObj.DomainLang;
|
|
124
|
+
|
|
125
|
+
// Check file extension
|
|
126
|
+
const extensions = services.LanguageMetaData.fileExtensions;
|
|
127
|
+
if (!extensions.some(ext => absolutePath.endsWith(ext))) {
|
|
128
|
+
throw new Error(`Invalid file extension. Expected: ${extensions.join(', ')}`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Initialize workspace with the specified directory or file's directory
|
|
132
|
+
const workspaceDir = options.workspaceDir ?? dirname(absolutePath);
|
|
133
|
+
const workspaceManager = services.imports.WorkspaceManager;
|
|
134
|
+
await workspaceManager.initialize(workspaceDir);
|
|
135
|
+
|
|
136
|
+
// Load and parse the document
|
|
137
|
+
const uri = URI.file(absolutePath);
|
|
138
|
+
const document = await shared.workspace.LangiumDocuments.getOrCreateDocument(uri);
|
|
139
|
+
|
|
140
|
+
// Build document initially without validation to load imports
|
|
141
|
+
await shared.workspace.DocumentBuilder.build([document], { validation: false });
|
|
142
|
+
|
|
143
|
+
// Load all imported documents via the import graph
|
|
144
|
+
const importResolver = services.imports.ImportResolver;
|
|
145
|
+
await ensureImportGraphFromDocument(
|
|
146
|
+
document,
|
|
147
|
+
shared.workspace.LangiumDocuments,
|
|
148
|
+
importResolver
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
// Build all documents with validation enabled
|
|
152
|
+
const allDocuments = Array.from(shared.workspace.LangiumDocuments.all);
|
|
153
|
+
await shared.workspace.DocumentBuilder.build(allDocuments, { validation: true });
|
|
154
|
+
|
|
155
|
+
// Collect diagnostics from the entry document
|
|
156
|
+
const diagnostics = document.diagnostics ?? [];
|
|
157
|
+
const errors: ValidationDiagnostic[] = [];
|
|
158
|
+
const warnings: ValidationDiagnostic[] = [];
|
|
159
|
+
|
|
160
|
+
for (const diagnostic of diagnostics) {
|
|
161
|
+
const validationDiag = toValidationDiagnostic(diagnostic, absolutePath);
|
|
162
|
+
if (diagnostic.severity === 1) {
|
|
163
|
+
errors.push(validationDiag);
|
|
164
|
+
} else if (diagnostic.severity === 2) {
|
|
165
|
+
warnings.push(validationDiag);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Count model elements across all documents
|
|
170
|
+
let domainCount = 0;
|
|
171
|
+
let bcCount = 0;
|
|
172
|
+
|
|
173
|
+
for (const doc of allDocuments) {
|
|
174
|
+
const model = doc.parseResult?.value;
|
|
175
|
+
if (isModel(model)) {
|
|
176
|
+
for (const element of model.children ?? []) {
|
|
177
|
+
if (element.$type === 'Domain') {
|
|
178
|
+
domainCount++;
|
|
179
|
+
} else if (element.$type === 'BoundedContext') {
|
|
180
|
+
bcCount++;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
valid: errors.length === 0,
|
|
188
|
+
fileCount: allDocuments.length,
|
|
189
|
+
domainCount,
|
|
190
|
+
bcCount,
|
|
191
|
+
errors,
|
|
192
|
+
warnings,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Workspace validation result with diagnostics grouped by file.
|
|
198
|
+
*/
|
|
199
|
+
export interface WorkspaceValidationResult {
|
|
200
|
+
/** Whether the workspace is valid (no errors in any file) */
|
|
201
|
+
valid: boolean;
|
|
202
|
+
/** Number of files validated */
|
|
203
|
+
fileCount: number;
|
|
204
|
+
/** Number of domains across all files */
|
|
205
|
+
domainCount: number;
|
|
206
|
+
/** Number of bounded contexts across all files */
|
|
207
|
+
bcCount: number;
|
|
208
|
+
/** Validation errors grouped by file path */
|
|
209
|
+
errors: ValidationDiagnostic[];
|
|
210
|
+
/** Validation warnings grouped by file path */
|
|
211
|
+
warnings: ValidationDiagnostic[];
|
|
212
|
+
/** Total number of diagnostics across all files */
|
|
213
|
+
totalDiagnostics: number;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Validates an entire DomainLang workspace.
|
|
218
|
+
*
|
|
219
|
+
* Uses the LSP infrastructure to:
|
|
220
|
+
* - Initialize the workspace from a directory containing model.yaml
|
|
221
|
+
* - Load the entry file (from manifest or default index.dlang)
|
|
222
|
+
* - Resolve and load all imports
|
|
223
|
+
* - Build and validate all documents in the workspace
|
|
224
|
+
* - Collect diagnostics from ALL documents (like VS Code Problems pane)
|
|
225
|
+
*
|
|
226
|
+
* @param workspaceDir - Path to the workspace directory (containing model.yaml)
|
|
227
|
+
* @returns Validation result with diagnostics from all files
|
|
228
|
+
* @throws Error if workspace directory doesn't exist or cannot be loaded
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* import { validateWorkspace } from '@domainlang/language/sdk';
|
|
233
|
+
*
|
|
234
|
+
* const result = await validateWorkspace('./my-workspace');
|
|
235
|
+
*
|
|
236
|
+
* if (!result.valid) {
|
|
237
|
+
* console.error(`Found ${result.errors.length} errors in ${result.fileCount} files`);
|
|
238
|
+
*
|
|
239
|
+
* for (const err of result.errors) {
|
|
240
|
+
* console.error(`${err.file}:${err.line}:${err.column}: ${err.message}`);
|
|
241
|
+
* }
|
|
242
|
+
* process.exit(1);
|
|
243
|
+
* }
|
|
244
|
+
*
|
|
245
|
+
* console.log(`✓ Validated ${result.fileCount} files`);
|
|
246
|
+
* console.log(` ${result.domainCount} domains, ${result.bcCount} bounded contexts`);
|
|
247
|
+
* console.log(` 0 errors, ${result.warnings.length} warnings`);
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
export async function validateWorkspace(
|
|
251
|
+
workspaceDir: string
|
|
252
|
+
): Promise<WorkspaceValidationResult> {
|
|
253
|
+
// Resolve absolute path
|
|
254
|
+
const absolutePath = resolve(workspaceDir);
|
|
255
|
+
|
|
256
|
+
// Check directory exists
|
|
257
|
+
if (!existsSync(absolutePath)) {
|
|
258
|
+
throw new Error(`Workspace directory not found: ${workspaceDir}`);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Create services with workspace support
|
|
262
|
+
const servicesObj = createDomainLangServices(NodeFileSystem);
|
|
263
|
+
const shared = servicesObj.shared;
|
|
264
|
+
const services = servicesObj.DomainLang;
|
|
265
|
+
const workspaceManager = services.imports.WorkspaceManager;
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
// Initialize workspace - this will find and load model.yaml
|
|
269
|
+
await workspaceManager.initialize(absolutePath);
|
|
270
|
+
} catch (error) {
|
|
271
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
272
|
+
throw new Error(`Failed to initialize workspace at ${workspaceDir}: ${message}`);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Get the manifest to find the entry file
|
|
276
|
+
const manifest = await workspaceManager.getManifest();
|
|
277
|
+
let entryFile = 'index.dlang';
|
|
278
|
+
|
|
279
|
+
if (manifest?.model?.entry) {
|
|
280
|
+
entryFile = manifest.model.entry;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const entryPath = join(absolutePath, entryFile);
|
|
284
|
+
|
|
285
|
+
// Check if entry file exists
|
|
286
|
+
if (!existsSync(entryPath)) {
|
|
287
|
+
throw new Error(
|
|
288
|
+
`Entry file not found: ${entryFile}\n` +
|
|
289
|
+
`Expected at: ${entryPath}\n` +
|
|
290
|
+
(manifest ? `Specified in manifest` : `Using default entry file`)
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Load and parse the entry document
|
|
295
|
+
const uri = URI.file(entryPath);
|
|
296
|
+
const document = await shared.workspace.LangiumDocuments.getOrCreateDocument(uri);
|
|
297
|
+
|
|
298
|
+
// Build document initially without validation to load imports
|
|
299
|
+
await shared.workspace.DocumentBuilder.build([document], { validation: false });
|
|
300
|
+
|
|
301
|
+
// Load all imported documents via the import graph
|
|
302
|
+
const importResolver = services.imports.ImportResolver;
|
|
303
|
+
await ensureImportGraphFromDocument(
|
|
304
|
+
document,
|
|
305
|
+
shared.workspace.LangiumDocuments,
|
|
306
|
+
importResolver
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
// Build all documents with validation enabled
|
|
310
|
+
const allDocuments = Array.from(shared.workspace.LangiumDocuments.all);
|
|
311
|
+
await shared.workspace.DocumentBuilder.build(allDocuments, { validation: true });
|
|
312
|
+
|
|
313
|
+
// Collect diagnostics from ALL documents (not just entry)
|
|
314
|
+
const errors: ValidationDiagnostic[] = [];
|
|
315
|
+
const warnings: ValidationDiagnostic[] = [];
|
|
316
|
+
|
|
317
|
+
for (const doc of allDocuments) {
|
|
318
|
+
const diagnostics = doc.diagnostics ?? [];
|
|
319
|
+
const docPath = doc.uri.fsPath;
|
|
320
|
+
|
|
321
|
+
for (const diagnostic of diagnostics) {
|
|
322
|
+
const validationDiag = toValidationDiagnostic(diagnostic, docPath);
|
|
323
|
+
|
|
324
|
+
if (diagnostic.severity === 1) {
|
|
325
|
+
errors.push(validationDiag);
|
|
326
|
+
} else if (diagnostic.severity === 2) {
|
|
327
|
+
warnings.push(validationDiag);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Count model elements across all documents
|
|
333
|
+
let domainCount = 0;
|
|
334
|
+
let bcCount = 0;
|
|
335
|
+
|
|
336
|
+
for (const doc of allDocuments) {
|
|
337
|
+
const model = doc.parseResult?.value;
|
|
338
|
+
if (isModel(model)) {
|
|
339
|
+
for (const element of model.children ?? []) {
|
|
340
|
+
if (element.$type === 'Domain') {
|
|
341
|
+
domainCount++;
|
|
342
|
+
} else if (element.$type === 'BoundedContext') {
|
|
343
|
+
bcCount++;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return {
|
|
350
|
+
valid: errors.length === 0,
|
|
351
|
+
fileCount: allDocuments.length,
|
|
352
|
+
domainCount,
|
|
353
|
+
bcCount,
|
|
354
|
+
errors,
|
|
355
|
+
warnings,
|
|
356
|
+
totalDiagnostics: errors.length + warnings.length,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Package Boundary Detector
|
|
3
|
+
*
|
|
4
|
+
* Determines package boundaries for import scoping.
|
|
5
|
+
* Per ADR-003, package boundaries are defined by:
|
|
6
|
+
* - External packages: Files within .dlang/packages/ sharing the same model.yaml
|
|
7
|
+
* - Local files: Each file is its own boundary (non-transitive)
|
|
8
|
+
*
|
|
9
|
+
* Used by DomainLangScopeProvider to enable transitive imports within
|
|
10
|
+
* package boundaries while keeping local file imports non-transitive.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as path from 'node:path';
|
|
14
|
+
import * as fs from 'node:fs/promises';
|
|
15
|
+
import { URI } from 'langium';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Detects and caches package boundaries for efficient scope resolution.
|
|
19
|
+
*/
|
|
20
|
+
export class PackageBoundaryDetector {
|
|
21
|
+
/**
|
|
22
|
+
* Cache mapping document URI to its package root path.
|
|
23
|
+
* - External packages: path to directory containing model.yaml
|
|
24
|
+
* - Local files: null (no package boundary)
|
|
25
|
+
*/
|
|
26
|
+
private readonly packageRootCache = new Map<string, string | null>();
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Determines if a document is part of an external package.
|
|
30
|
+
*
|
|
31
|
+
* External packages are stored in .dlang/packages/owner/repo/commit/
|
|
32
|
+
*
|
|
33
|
+
* @param documentUri - The URI of the document to check
|
|
34
|
+
* @returns true if document is in an external package
|
|
35
|
+
*/
|
|
36
|
+
isExternalPackage(documentUri: URI | string): boolean {
|
|
37
|
+
const fsPath = this.toFsPath(documentUri);
|
|
38
|
+
const normalized = fsPath.split(path.sep);
|
|
39
|
+
|
|
40
|
+
// Check if path contains .dlang/packages/
|
|
41
|
+
const dlangIndex = normalized.indexOf('.dlang');
|
|
42
|
+
if (dlangIndex === -1) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return dlangIndex + 1 < normalized.length &&
|
|
47
|
+
normalized[dlangIndex + 1] === 'packages';
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Gets the package root for a document.
|
|
52
|
+
*
|
|
53
|
+
* For external packages (.dlang/packages/), walks up from the document
|
|
54
|
+
* to find the nearest model.yaml file within the package structure.
|
|
55
|
+
*
|
|
56
|
+
* For local files, returns null (no package boundary).
|
|
57
|
+
*
|
|
58
|
+
* @param documentUri - The URI of the document
|
|
59
|
+
* @returns Absolute path to package root, or null if not in a package
|
|
60
|
+
*/
|
|
61
|
+
async getPackageRoot(documentUri: URI | string): Promise<string | null> {
|
|
62
|
+
const uriString = documentUri.toString();
|
|
63
|
+
|
|
64
|
+
// Check cache first
|
|
65
|
+
if (this.packageRootCache.has(uriString)) {
|
|
66
|
+
return this.packageRootCache.get(uriString) ?? null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// If not an external package, it has no package boundary
|
|
70
|
+
if (!this.isExternalPackage(documentUri)) {
|
|
71
|
+
this.packageRootCache.set(uriString, null);
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const fsPath = this.toFsPath(documentUri);
|
|
76
|
+
const packageRoot = await this.findPackageRootForExternal(fsPath);
|
|
77
|
+
this.packageRootCache.set(uriString, packageRoot);
|
|
78
|
+
return packageRoot;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Checks if two documents are in the same package (synchronous heuristic).
|
|
83
|
+
*
|
|
84
|
+
* This is a fast, synchronous check that compares package commit directories
|
|
85
|
+
* without filesystem access. Documents are in the same package if:
|
|
86
|
+
* - Both are in .dlang/packages/ AND
|
|
87
|
+
* - They share the same owner/repo/commit path
|
|
88
|
+
*
|
|
89
|
+
* This is used by the scope provider which needs synchronous access.
|
|
90
|
+
*
|
|
91
|
+
* Structure: .dlang/packages/owner/repo/commit/...
|
|
92
|
+
*
|
|
93
|
+
* @param doc1Uri - URI of first document
|
|
94
|
+
* @param doc2Uri - URI of second document
|
|
95
|
+
* @returns true if both are in the same package commit directory
|
|
96
|
+
*/
|
|
97
|
+
areInSamePackageSync(doc1Uri: URI | string, doc2Uri: URI | string): boolean {
|
|
98
|
+
// Both must be external packages
|
|
99
|
+
if (!this.isExternalPackage(doc1Uri) || !this.isExternalPackage(doc2Uri)) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const path1 = this.toFsPath(doc1Uri);
|
|
104
|
+
const path2 = this.toFsPath(doc2Uri);
|
|
105
|
+
|
|
106
|
+
const root1 = this.getPackageCommitDirectory(path1);
|
|
107
|
+
const root2 = this.getPackageCommitDirectory(path2);
|
|
108
|
+
|
|
109
|
+
return root1 !== null && root1 === root2;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Gets the package commit directory (owner/repo/commit) from a path.
|
|
114
|
+
*
|
|
115
|
+
* @param fsPath - Filesystem path
|
|
116
|
+
* @returns Commit directory path or null
|
|
117
|
+
*/
|
|
118
|
+
private getPackageCommitDirectory(fsPath: string): string | null {
|
|
119
|
+
const normalized = fsPath.split(path.sep);
|
|
120
|
+
const dlangIndex = normalized.indexOf('.dlang');
|
|
121
|
+
|
|
122
|
+
if (dlangIndex === -1) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const packagesIndex = dlangIndex + 1;
|
|
127
|
+
if (packagesIndex >= normalized.length || normalized[packagesIndex] !== 'packages') {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Commit directory is at: .dlang/packages/owner/repo/commit
|
|
132
|
+
const commitIndex = packagesIndex + 3;
|
|
133
|
+
if (commitIndex >= normalized.length) {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Return the path up to and including the commit directory
|
|
138
|
+
return normalized.slice(0, commitIndex + 1).join(path.sep);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Checks if two documents are in the same package.
|
|
143
|
+
*
|
|
144
|
+
* Documents are in the same package if:
|
|
145
|
+
* - Both are external packages AND
|
|
146
|
+
* - They share the same package root (model.yaml location)
|
|
147
|
+
*
|
|
148
|
+
* Local files are never in the same package (each is isolated).
|
|
149
|
+
*
|
|
150
|
+
* @param doc1Uri - URI of first document
|
|
151
|
+
* @param doc2Uri - URI of second document
|
|
152
|
+
* @returns true if both documents are in the same package
|
|
153
|
+
*/
|
|
154
|
+
async areInSamePackage(doc1Uri: URI | string, doc2Uri: URI | string): Promise<boolean> {
|
|
155
|
+
const root1 = await this.getPackageRoot(doc1Uri);
|
|
156
|
+
const root2 = await this.getPackageRoot(doc2Uri);
|
|
157
|
+
|
|
158
|
+
// If either is not in a package, they can't be in the same package
|
|
159
|
+
if (!root1 || !root2) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return root1 === root2;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Finds the package root for an external package by walking up to find model.yaml.
|
|
168
|
+
*
|
|
169
|
+
* External packages have structure: .dlang/packages/owner/repo/commit/...
|
|
170
|
+
* The model.yaml should be at the commit level or just below it.
|
|
171
|
+
*
|
|
172
|
+
* @param fsPath - Filesystem path of the document
|
|
173
|
+
* @returns Path to directory containing model.yaml, or null
|
|
174
|
+
*/
|
|
175
|
+
private async findPackageRootForExternal(fsPath: string): Promise<string | null> {
|
|
176
|
+
const normalized = fsPath.split(path.sep);
|
|
177
|
+
const dlangIndex = normalized.indexOf('.dlang');
|
|
178
|
+
|
|
179
|
+
if (dlangIndex === -1) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Find the packages directory
|
|
184
|
+
const packagesIndex = dlangIndex + 1;
|
|
185
|
+
if (packagesIndex >= normalized.length || normalized[packagesIndex] !== 'packages') {
|
|
186
|
+
return null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Start from the commit directory level
|
|
190
|
+
// Structure: .dlang/packages/owner/repo/commit/
|
|
191
|
+
const commitIndex = packagesIndex + 3;
|
|
192
|
+
if (commitIndex >= normalized.length) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Walk up from the document path to the commit directory
|
|
197
|
+
let currentPath = path.dirname(fsPath);
|
|
198
|
+
const commitPath = normalized.slice(0, commitIndex + 1).join(path.sep);
|
|
199
|
+
|
|
200
|
+
// Search upward for model.yaml, but don't go above the commit directory
|
|
201
|
+
while (currentPath.length >= commitPath.length) {
|
|
202
|
+
const manifestPath = path.join(currentPath, 'model.yaml');
|
|
203
|
+
try {
|
|
204
|
+
await fs.access(manifestPath);
|
|
205
|
+
return currentPath;
|
|
206
|
+
} catch {
|
|
207
|
+
// model.yaml not found at this level, continue upward
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const parent = path.dirname(currentPath);
|
|
211
|
+
if (parent === currentPath) {
|
|
212
|
+
// Reached filesystem root without finding model.yaml
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
currentPath = parent;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Converts a URI to a filesystem path.
|
|
223
|
+
*/
|
|
224
|
+
private toFsPath(uri: URI | string): string {
|
|
225
|
+
if (typeof uri === 'string') {
|
|
226
|
+
uri = URI.parse(uri);
|
|
227
|
+
}
|
|
228
|
+
return uri.fsPath;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Clears the package root cache.
|
|
233
|
+
* Call this when packages are installed/removed.
|
|
234
|
+
*/
|
|
235
|
+
clearCache(): void {
|
|
236
|
+
this.packageRootCache.clear();
|
|
237
|
+
}
|
|
238
|
+
}
|
|
@@ -76,7 +76,8 @@ export class PerformanceOptimizer {
|
|
|
76
76
|
|
|
77
77
|
try {
|
|
78
78
|
const content = await fs.readFile(manifestPath, 'utf-8');
|
|
79
|
-
const
|
|
79
|
+
const { parse } = await import('yaml');
|
|
80
|
+
const manifest: unknown = parse(content);
|
|
80
81
|
|
|
81
82
|
this.manifestCache.set(cacheKey, {
|
|
82
83
|
value: manifest,
|
|
@@ -127,7 +128,10 @@ export class PerformanceOptimizer {
|
|
|
127
128
|
const stat = await fs.stat(lockPath);
|
|
128
129
|
const cached = this.lockFileCache.get(workspaceRoot);
|
|
129
130
|
|
|
130
|
-
|
|
131
|
+
// Floor mtimeMs to integer precision to match Date.now() —
|
|
132
|
+
// some filesystems (e.g. APFS) report sub-millisecond mtime,
|
|
133
|
+
// which can exceed the integer timestamp from Date.now().
|
|
134
|
+
if (cached && Math.floor(stat.mtimeMs) > cached.timestamp) {
|
|
131
135
|
stale.push(workspaceRoot);
|
|
132
136
|
}
|
|
133
137
|
} catch {
|
package/src/services/types.ts
CHANGED
|
@@ -42,6 +42,31 @@
|
|
|
42
42
|
// Core Building Blocks
|
|
43
43
|
// ============================================================================
|
|
44
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Information about an import statement tracked during indexing.
|
|
47
|
+
*
|
|
48
|
+
* Used by IndexManager to track both the import's resolved location
|
|
49
|
+
* and its alias (if any) for scope resolution.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```typescript
|
|
53
|
+
* // import "larsbaunwall/ddd-types" as ddd
|
|
54
|
+
* const info: ImportInfo = {
|
|
55
|
+
* specifier: "larsbaunwall/ddd-types",
|
|
56
|
+
* alias: "ddd",
|
|
57
|
+
* resolvedUri: "file:///.dlang/packages/larsbaunwall/ddd-types/abc123/index.dlang"
|
|
58
|
+
* };
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export interface ImportInfo {
|
|
62
|
+
/** The import specifier as written in source (e.g., "./file.dlang", "owner/repo") */
|
|
63
|
+
readonly specifier: string;
|
|
64
|
+
/** Optional alias from 'as' clause (e.g., "ddd" in 'import "pkg" as ddd') */
|
|
65
|
+
readonly alias?: string;
|
|
66
|
+
/** Resolved absolute URI of the imported document */
|
|
67
|
+
readonly resolvedUri: string;
|
|
68
|
+
}
|
|
69
|
+
|
|
45
70
|
/**
|
|
46
71
|
* Type of git reference for version pinning.
|
|
47
72
|
*
|