All files / src/highlights diagnostic-provider.ts

100% Statements 59/59
91.66% Branches 11/12
100% Functions 7/7
100% Lines 59/59

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1001x   1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x           1x 10x   10x 10x 10x 10x         10x 7x     7x 7x 8x 8x 8x 8x     7x 7x 7x 7x 7x         10x 8x 8x 8x 8x   8x 8x 8x   8x 8x 8x   8x 8x         10x     8x 8x 8x 8x 8x 8x 8x         10x 1x 1x         10x 11x 11x 10x  
import * as vscode from "vscode";
import { HighlightCodeLineData } from "./highlights-models";
import { classifyHighlight, Severity } from "./severity-utils";
 
/** Map severity to VS Code DiagnosticSeverity */
const SEVERITY_TO_DIAGNOSTIC: Record<Severity, vscode.DiagnosticSeverity> = {
    hotspot: vscode.DiagnosticSeverity.Error,
    warning: vscode.DiagnosticSeverity.Warning,
    info: vscode.DiagnosticSeverity.Information,
};
 
/** Map severity to diagnostic code */
const SEVERITY_TO_CODE: Record<Severity, string> = {
    hotspot: "hotspot",
    warning: "alert",
    info: "finding",
};
 
/**
 * Provides DataFlint highlights as VS Code diagnostics in the Problems panel.
 * This allows users to see issues even when folders are collapsed.
 */
export class DataFlintDiagnosticProvider {
    private diagnostics: vscode.DiagnosticCollection;
 
    constructor() {
        this.diagnostics =
            vscode.languages.createDiagnosticCollection("dataflint");
    }
 
    /**
     * Refresh diagnostics with new highlight data
     */
    refresh(highlights: HighlightCodeLineData[]): void {
        this.diagnostics.clear();
 
        // Group highlights by file
        const byFile = new Map<string, HighlightCodeLineData[]>();
        for (const h of highlights) {
            const existing = byFile.get(h.filePath) ?? [];
            existing.push(h);
            byFile.set(h.filePath, existing);
        }
 
        // Create diagnostics per file
        for (const [filePath, fileHighlights] of byFile) {
            const diags = fileHighlights.map((h) => this.createDiagnostic(h));
            this.diagnostics.set(vscode.Uri.file(filePath), diags);
        }
    }
 
    /**
     * Create a VS Code diagnostic from a highlight
     */
    private createDiagnostic(
        highlight: HighlightCodeLineData
    ): vscode.Diagnostic {
        const line = Math.max(0, highlight.codeLine - 1);
        const range = new vscode.Range(line, 0, line, Number.MAX_VALUE);
 
        const { severity, code } = this.mapSeverity(highlight);
        const message =
            highlight.topItemDisplay || highlight.text || "DataFlint issue";
 
        const diag = new vscode.Diagnostic(range, message, severity);
        diag.source = "DataFlint";
        diag.code = code;
 
        return diag;
    }
 
    /**
     * Map highlight to VS Code DiagnosticSeverity and code
     */
    private mapSeverity(highlight: HighlightCodeLineData): {
        severity: vscode.DiagnosticSeverity;
        code: string;
    } {
        const level = classifyHighlight(highlight);
        return {
            severity: SEVERITY_TO_DIAGNOSTIC[level],
            code: SEVERITY_TO_CODE[level],
        };
    }
 
    /**
     * Clear all diagnostics
     */
    clear(): void {
        this.diagnostics.clear();
    }
 
    /**
     * Dispose of resources
     */
    dispose(): void {
        this.diagnostics.dispose();
    }
}