-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathzmodel-preview.ts
More file actions
377 lines (333 loc) · 14 KB
/
zmodel-preview.ts
File metadata and controls
377 lines (333 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import * as vscode from 'vscode';
import * as path from 'path';
import * as os from 'os';
import { z } from 'zod';
import { LanguageClient } from 'vscode-languageclient/node';
import { URI } from 'vscode-uri';
import { DocumentationCache } from './documentation-cache';
import { requireAuth } from '../extension';
import { API_URL } from './zenstack-auth-provider';
import telemetry from './vscode-telemetry';
/**
* ZModelPreview class handles ZModel file preview functionality
*/
export class ZModelPreview implements vscode.Disposable {
private documentationCache: DocumentationCache;
private languageClient: LanguageClient;
private lastGeneratedMarkdown: string | null = null;
// use a zero-width space in the file name to make it non-colliding with user file
private readonly previewZModelFileName = `zmodel${'\u200B'}-preview.md`;
// Schema for validating the request body
private static DocRequestSchema = z.object({
models: z.array(
z.object({
path: z.string().optional(),
content: z.string(),
})
),
environments: z
.object({
vscodeAppName: z.string(),
vscodeVersion: z.string(),
vscodeAppHost: z.string(),
osRelease: z.string(),
osType: z.string(),
})
.optional(),
});
constructor(context: vscode.ExtensionContext, client: LanguageClient, cache: DocumentationCache) {
this.documentationCache = cache;
this.languageClient = client;
this.initialize(context);
}
/**
* Initialize and register commands
*/
initialize(context: vscode.ExtensionContext): void {
this.registerCommands(context);
context.subscriptions.push(
vscode.window.tabGroups.onDidChangeTabs(() => {
const activeTabLabels = vscode.window.tabGroups.all.filter((group) =>
group.activeTab?.label?.endsWith(this.previewZModelFileName)
);
if (activeTabLabels.length > 0) {
vscode.commands.executeCommand('setContext', 'zenstack.isMarkdownPreview', true);
} else {
vscode.commands.executeCommand('setContext', 'zenstack.isMarkdownPreview', false);
}
})
);
}
/**
* Register ZModel preview commands
*/
private registerCommands(context: vscode.ExtensionContext): void {
// Register the preview command for zmodel files
context.subscriptions.push(
vscode.commands.registerCommand('zenstack.preview-zmodel', async () => {
await this.previewZModelFile();
})
);
// Register the save documentation command for zmodel files
context.subscriptions.push(
vscode.commands.registerCommand('zenstack.save-zmodel-documentation', async () => {
await this.saveZModelDocumentation();
})
);
// Register cache management commands
context.subscriptions.push(
vscode.commands.registerCommand('zenstack.clear-documentation-cache', async () => {
await this.documentationCache.clearAllCache();
vscode.window.showInformationMessage('ZenStack documentation cache cleared');
})
);
}
/**
* Preview a ZModel file
*/
async previewZModelFile(): Promise<void> {
telemetry.track('extension:zmodel-preview');
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No active editor found.');
return;
}
const document = editor.document;
if (!document.fileName.endsWith('.zmodel')) {
vscode.window.showErrorMessage('The active file is not a ZModel file.');
return;
}
// Check authentication before proceeding
const session = await requireAuth();
if (!session) {
return;
}
try {
this.checkForMermaidExtensions();
// Show progress indicator
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: 'Generating ZModel documentation...',
cancellable: false,
},
async () => {
const markdownContent = await this.generateZModelDocumentation(document);
if (markdownContent) {
// Store the generated content for potential saving later
this.lastGeneratedMarkdown = markdownContent;
await this.openMarkdownPreview(markdownContent);
}
}
);
} catch (error) {
console.error('Error previewing ZModel:', error);
vscode.window.showErrorMessage(
`Failed to preview ZModel: ${error instanceof Error ? error.message : String(error)}`
);
}
}
/**
* Get all imported ZModel URIs using the language server
*/
private async getAllImportedZModelURIs(document: vscode.TextDocument): Promise<{
hasSyntaxErrors: boolean;
importedURIs: URI[];
}> {
if (!this.languageClient) {
throw new Error('Language client not initialized');
}
try {
// Ensure the language server is ready
await this.languageClient.start();
// Send the custom request to get all imported ZModel URIs
const result = await this.languageClient.sendRequest('zenstack/getAllImportedZModelURIs', {
textDocument: {
uri: document.uri.toString(),
},
});
return result as {
hasSyntaxErrors: boolean;
importedURIs: URI[];
};
} catch (error) {
console.error('Error getting AST from language server:', error);
throw error;
}
}
/**
* Generate documentation for ZModel
*/
private async generateZModelDocumentation(document: vscode.TextDocument): Promise<string> {
try {
const astInfo = await this.getAllImportedZModelURIs(document);
if (astInfo?.hasSyntaxErrors !== false) {
vscode.window.showWarningMessage('Please fix the errors in the ZModel first');
return '';
}
const importedURIs = astInfo?.importedURIs;
// get vscode document from importedURIs
const importedModels = await Promise.all(
importedURIs.map(async (uri) => {
try {
const fileUri = vscode.Uri.file(uri.path);
const fileContent = await vscode.workspace.fs.readFile(fileUri);
const filePath = fileUri.path;
return { content: Buffer.from(fileContent).toString('utf8').trim(), path: filePath };
} catch (error) {
throw new Error(
`Failed to read imported ZModel file at ${uri.path}: ${
error instanceof Error ? error.message : String(error)
}`
);
}
})
);
const allModels = [{ content: document.getText().trim(), path: document.uri.path }, ...importedModels];
const session = await requireAuth();
if (!session) {
throw new Error('Authentication required to generate documentation');
}
// Prepare request body
const requestBody: z.infer<typeof ZModelPreview.DocRequestSchema> = {
models: allModels,
environments: {
vscodeAppName: vscode.env.appName,
vscodeVersion: vscode.version,
vscodeAppHost: vscode.env.appHost,
osRelease: os.release(),
osType: os.type(),
},
};
const allModelsContent = allModels.map((m) => m.content);
// Check cache first
const cachedResponse = await this.documentationCache.getCachedResponse(allModelsContent);
if (cachedResponse) {
return cachedResponse;
}
// record the time spent
const startTime = Date.now();
const apiResponse = await fetch(`${API_URL}/api/doc`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authorization: session.accessToken,
},
body: JSON.stringify(requestBody),
});
console.log(`API request completed in ${Date.now() - startTime} ms`);
if (!apiResponse.ok) {
throw new Error(`API request failed: ${apiResponse.status} ${apiResponse.statusText}`);
}
const responseText = await apiResponse.text();
// Cache the response
await this.documentationCache.setCachedResponse(allModelsContent, responseText);
return responseText;
} catch (error) {
console.error('Error generating documentation:', error);
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(`Failed to generate documentation: ${errorMessage}`);
}
}
/**
* Open markdown preview
*/
private async openMarkdownPreview(markdownContent: string): Promise<void> {
// Create a temporary markdown file with a descriptive name in the system temp folder
const tempFilePath = path.join(os.tmpdir(), this.previewZModelFileName);
const tempFile = vscode.Uri.file(tempFilePath);
try {
// Write the markdown content to the temp file
await vscode.workspace.fs.writeFile(tempFile, new TextEncoder().encode(markdownContent));
// Open the markdown preview side by side
await vscode.commands.executeCommand('markdown.showPreviewToSide', tempFile);
} catch (error) {
console.error('Error creating markdown preview:', error);
throw new Error(
`Failed to create markdown preview: ${error instanceof Error ? error.message : String(error)}`
);
}
}
/**
* Save ZModel documentation to a user-selected file
*/
async saveZModelDocumentation(): Promise<void> {
telemetry.track('extension:zmodel-save');
// Check if we have cached content first
if (!this.lastGeneratedMarkdown) {
vscode.window.showErrorMessage(
'No documentation content available to save. Please generate the documentation first by running "Preview ZModel Documentation".'
);
return;
}
// Show save dialog
let defaultFilePath = `zmodel-doc.md`;
const workspaceFolders = vscode.workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
const workspacePath = workspaceFolders[0].uri.fsPath;
// If the workspace folder exists, use it
defaultFilePath = path.join(workspacePath, defaultFilePath);
}
const saveUri = await vscode.window.showSaveDialog({
defaultUri: vscode.Uri.file(defaultFilePath),
filters: {
Markdown: ['md'],
'All Files': ['*'],
},
saveLabel: 'Save Documentation',
});
if (!saveUri) {
return; // User cancelled
}
try {
// Write the markdown content to the selected file
await vscode.workspace.fs.writeFile(saveUri, new TextEncoder().encode(this.lastGeneratedMarkdown));
// Open and close the saved file to refresh the shown markdown preview
await vscode.commands.executeCommand('vscode.open', saveUri);
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
} catch (error) {
console.error('Error saving markdown file:', error);
vscode.window.showErrorMessage(
`Failed to save documentation: ${error instanceof Error ? error.message : String(error)}`
);
}
}
/**
* Check for Mermaid extensions
*/
private checkForMermaidExtensions(): void {
const setting = vscode.workspace.getConfiguration('zenstack').get('searchForExtensions');
if (setting !== false) {
const extensions = vscode.extensions.all.filter((extension) =>
['markdown-mermaid', 'vscode-mermaid-chart', 'vscode-mermaid-preview'].some((name) =>
extension.packageJSON.name?.toLowerCase().includes(name.toLowerCase())
)
);
if (extensions.length === 0) {
const searchAction = 'Search';
const stopShowing = "Don't show again";
vscode.window
.showInformationMessage(
'Search for extensions to view mermaid chart in ZModel preview doc?',
searchAction,
stopShowing
)
.then((selectedAction) => {
if (selectedAction === searchAction) {
vscode.commands.executeCommand('workbench.extensions.search', 'markdown-mermaid');
} else if (selectedAction === stopShowing) {
vscode.workspace
.getConfiguration('zenstack')
.update('searchForExtensions', false, vscode.ConfigurationTarget.Global);
}
});
}
}
}
/**
* Dispose of resources
*/
dispose(): void {
// Any cleanup if needed
}
}