|
| 1 | +import vscode from "vscode" |
| 2 | +import { OwnershipEngine } from "@snyk/github-codeowners/dist/lib/ownership" |
| 3 | + |
| 4 | +function updateDiagnostics( |
| 5 | + document: vscode.TextDocument, |
| 6 | + collection: vscode.DiagnosticCollection, |
| 7 | +): void { |
| 8 | + if (document.languageId !== "codeowners") { |
| 9 | + return |
| 10 | + } |
| 11 | + |
| 12 | + const engine = OwnershipEngine.FromCodeownersFile(document.uri.fsPath) |
| 13 | + |
| 14 | + const diagnostics = engine.errors.map((err) => { |
| 15 | + const line = document.lineAt(err.lineno) |
| 16 | + return new vscode.Diagnostic(line.range, err.message, vscode.DiagnosticSeverity.Error) |
| 17 | + }) |
| 18 | + |
| 19 | + collection.set(document.uri, diagnostics) |
| 20 | +} |
| 21 | + |
| 22 | +export function registerCodeownersDiagnostics( |
| 23 | + context: vscode.ExtensionContext, |
| 24 | +): void { |
| 25 | + const collection = vscode.languages.createDiagnosticCollection("codeowners") |
| 26 | + context.subscriptions.push(collection) |
| 27 | + |
| 28 | + vscode.workspace.textDocuments.forEach((doc) => |
| 29 | + updateDiagnostics(doc, collection), |
| 30 | + ) |
| 31 | + |
| 32 | + vscode.workspace.onDidOpenTextDocument( |
| 33 | + (doc) => updateDiagnostics(doc, collection), |
| 34 | + null, |
| 35 | + context.subscriptions, |
| 36 | + ) |
| 37 | + |
| 38 | + vscode.workspace.onDidChangeTextDocument( |
| 39 | + (e) => updateDiagnostics(e.document, collection), |
| 40 | + null, |
| 41 | + context.subscriptions, |
| 42 | + ) |
| 43 | + |
| 44 | + vscode.workspace.onDidCloseTextDocument( |
| 45 | + (doc) => collection.delete(doc.uri), |
| 46 | + null, |
| 47 | + context.subscriptions, |
| 48 | + ) |
| 49 | +} |
0 commit comments