Created
June 9, 2023 12:06
-
-
Save mattrothenberg/d53e103cb7e3247cfee1dda1ed1af83c to your computer and use it in GitHub Desktop.
Copy diagnostics to clipboard
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
// The module 'vscode' contains the VS Code extensibility API | |
// Import the module and reference it with the alias vscode in your code below | |
import * as vscode from "vscode"; | |
// This method is called when your extension is activated | |
// Your extension is activated the very first time the command is executed | |
export function activate(context: vscode.ExtensionContext) { | |
// Add a command that when executed lists all of the diagnostics in the active editor | |
const disposable = vscode.commands.registerCommand( | |
"yourExtension.listDiagnostics", | |
() => { | |
// Get the active text editor | |
const editor = vscode.window.activeTextEditor; | |
if (!editor) { | |
return; | |
} | |
// Get a list of all diagnostic errors in the current file. | |
const diagnostics = vscode.languages.getDiagnostics(); | |
// Filter down to only diagnostics in the current file. | |
const currentFileDiagnostics = diagnostics.filter( | |
(diagnostic) => diagnostic[0].fsPath === editor.document.uri.fsPath | |
); | |
const transformedDiagnostics = currentFileDiagnostics | |
.map((entry) => { | |
const [uri, diagnostics] = entry; | |
return diagnostics.map((diagnostic) => { | |
const { range, message, severity } = diagnostic; | |
const { start, end } = range; | |
const { line, character } = start; | |
return `L${line + 1}: ${message}`; | |
}); | |
}) | |
.flat(); | |
// Copy the list of diagnostics to the clipboard | |
vscode.env.clipboard.writeText(transformedDiagnostics.join("\n\n")); | |
} | |
); | |
context.subscriptions.push(disposable); | |
} | |
// This method is called when your extension is deactivated | |
export function deactivate() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment