-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: VS Code command to dump diagnostics into a JSON file (#928)
Closes #927 ### Summary of Changes Add two new commands: * Dump Diagnostics to JSON * Open Diagnostics Dumps in New VS Code Window (to quickly discover them)
- Loading branch information
1 parent
610604f
commit 34fa884
Showing
4 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/safe-ds-vscode/src/extension/commands/dumpDiagnostics.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); | ||
}; |
7 changes: 7 additions & 0 deletions
7
packages/safe-ds-vscode/src/extension/commands/openDiagnosticsDumps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters