Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: VS Code command to dump diagnostics into a JSON file #928

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions packages/safe-ds-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,30 @@
},
"commands": [
{
"command": "safe-ds.runPipelineFile",
"title": "Run Pipeline",
"category": "Safe-DS",
"icon": "$(play)"
"command": "safe-ds.dumpDiagnostics",
"title": "Dump Diagnostics to JSON",
"category": "Safe-DS"
},
{
"command": "safe-ds.openDiagnosticsDumps",
"title": "Open Diagnostics Dumps in New VS Code Window",
"category": "Safe-DS"
},
{
"command": "safe-ds.refreshWebview",
"title": "Refresh Webview",
"category": "EDA"
"category": "Safe-DS"
},
{
"command": "safe-ds.runEdaFromContext",
"title": "Explore Table",
"category": "EDA"
"category": "Safe-DS"
},
{
"command": "safe-ds.runPipelineFile",
"title": "Run Pipeline",
"category": "Safe-DS",
"icon": "$(play)"
}
],
"snippets": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import vscode, { ExtensionContext, Uri } from 'vscode';

export const dumpDiagnostics = (context: ExtensionContext) => async () => {
// Get the current document
const currentDocument = vscode.window.activeTextEditor?.document;
if (!currentDocument) {
vscode.window.showErrorMessage('No active document.');
return;
} else if (currentDocument.languageId !== 'safe-ds') {
vscode.window.showErrorMessage('The active document is not a Safe-DS document.');
return;
}

// Get diagnostics for the current document
const diagnostics = vscode.languages.getDiagnostics(currentDocument.uri);
if (diagnostics.length === 0) {
vscode.window.showErrorMessage('The active document has no diagnostics.');
return;
}

// Dump diagnostics to a file
const machineId = vscode.env.machineId;
const timestamp = new Date();
const fileName = `${machineId}-${toBasicISOString(timestamp)}.json`;
const uri = vscode.Uri.joinPath(diagnosticsDumpsUri(context), fileName);
const content = JSON.stringify(
{
machineId,
timestamp: timestamp.toISOString(),
text: currentDocument.getText(),
diagnostics,
},
null,
2,
);

await vscode.workspace.fs.writeFile(uri, Buffer.from(content));

// Inform the user and ask for further action
const item = await vscode.window.showInformationMessage(`Diagnostics successfully dumped.`, 'Open file', 'Close');

if (item === 'Open file') {
vscode.window.showTextDocument(uri);
}
};

export const diagnosticsDumpsUri = (context: ExtensionContext): Uri => {
return vscode.Uri.joinPath(context.globalStorageUri, 'diagnosticsDumps');
};

const toBasicISOString = (date: Date): string => {
return date.toISOString().replaceAll(/[-:]/gu, '');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import vscode, { ExtensionContext } from 'vscode';
import { diagnosticsDumpsUri } from './dumpDiagnostics.js';

export const openDiagnosticsDumps = (context: ExtensionContext) => async () => {
const uri = diagnosticsDumpsUri(context);
vscode.commands.executeCommand('vscode.openFolder', uri, { forceNewWindow: true });
};
7 changes: 7 additions & 0 deletions packages/safe-ds-vscode/src/extension/mainClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { getSafeDSOutputChannel, initializeLog, logError, logOutput, printOutput
import crypto from 'crypto';
import { LangiumDocument, URI } from 'langium';
import { EDAPanel, undefinedPanelIdentifier } from './eda/edaPanel.ts';
import { dumpDiagnostics } from './commands/dumpDiagnostics.js';
import { openDiagnosticsDumps } from './commands/openDiagnosticsDumps.js';

let client: LanguageClient;
let services: SafeDsServices;
Expand Down Expand Up @@ -153,6 +155,11 @@ const registerVSCodeCommands = function (context: vscode.ExtensionContext) {
});
};

context.subscriptions.push(vscode.commands.registerCommand('safe-ds.dumpDiagnostics', dumpDiagnostics(context)));
context.subscriptions.push(
vscode.commands.registerCommand('safe-ds.openDiagnosticsDumps', openDiagnosticsDumps(context)),
);

context.subscriptions.push(vscode.commands.registerCommand('safe-ds.runPipelineFile', commandRunPipelineFile));

context.subscriptions.push(
Expand Down