All files / src/highlights file-decoration-provider.ts

98.48% Statements 130/132
89.18% Branches 33/37
100% Functions 11/11
98.48% Lines 130/132

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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 1711x 1x   1x                   1x   21x 21x   21x 21x 21x   21x 21x 21x 21x   21x 22x 22x 22x   22x 22x 22x 22x   22x 22x 22x   22x 2x 1x 1x 2x   22x 22x 22x 22x   21x 1x 1x 1x   21x 25x 25x   25x 17x 17x 25x 3x 3x 5x 25x   21x 21x 21x   21x 23x 23x 23x 23x 23x   21x 22x 86x 86x 27x 27x 86x 86x 86x 22x   21x 86x 86x 59x 59x 59x   27x 27x 27x 86x   86x 40x 40x 40x 40x 40x   27x 86x   21x 20x 20x   21x 17x 17x 17x 17x 17x   17x 17x   17x 17x 17x 17x 17x 17x   17x 17x 17x   21x 3x 3x 3x 3x   3x 8x 8x 8x 8x 8x   3x   3x 3x   3x 3x   3x 3x 3x 3x 3x   3x 3x 3x 21x  
import * as vscode from "vscode";
import * as path from "path";
import { HighlightCodeLineData } from "./highlights-models";
import {
    calculateSeverityCounts,
    getHighestSeverity,
    SEVERITY_THEME_COLORS,
    buildSeverityBreakdown,
    pluralizeIssue,
    pluralizeFile,
    Severity,
} from "./severity-utils";
 
export class DataFlintFileDecorationProvider
    implements vscode.FileDecorationProvider
{
    private _onDidChangeFileDecorations = new vscode.EventEmitter<
        vscode.Uri | vscode.Uri[] | undefined
    >();
    readonly onDidChangeFileDecorations =
        this._onDidChangeFileDecorations.event;
 
    private highlightsByFile = new Map<string, HighlightCodeLineData[]>();
    private affectedFolders = new Set<string>();
    private parentFoldersCache = new Map<string, string[]>();
    private lastDecoratedPaths = new Set<string>();
 
    refresh(highlights: HighlightCodeLineData[]): void {
        const previousDecoratedPaths = new Set(this.lastDecoratedPaths);
        this.clearCaches();
        this.aggregateHighlights(highlights);
 
        const urisToRefresh: vscode.Uri[] = [
            ...[...this.highlightsByFile.keys()].map((p) => vscode.Uri.file(p)),
            ...[...this.affectedFolders].map((p) => vscode.Uri.file(p)),
        ];
 
        for (const p of this.highlightsByFile.keys())
            this.lastDecoratedPaths.add(p);
        for (const p of this.affectedFolders) this.lastDecoratedPaths.add(p);
 
        for (const prevPath of previousDecoratedPaths) {
            if (!this.lastDecoratedPaths.has(prevPath)) {
                urisToRefresh.push(vscode.Uri.file(prevPath));
            }
        }
 
        this._onDidChangeFileDecorations.fire(
            urisToRefresh.length > 0 ? urisToRefresh : undefined
        );
    }
 
    clear(): void {
        this.clearCaches();
        this._onDidChangeFileDecorations.fire(undefined);
    }
 
    provideFileDecoration(uri: vscode.Uri): vscode.FileDecoration | undefined {
        const fsPath = uri.fsPath;
        const fileHighlights = this.highlightsByFile.get(fsPath);
 
        if (fileHighlights?.length) {
            return this.buildFileDecoration(fileHighlights);
        }
        if (this.affectedFolders.has(fsPath)) {
            return this.getFolderDecoration(fsPath);
        }
        return undefined;
    }
 
    dispose(): void {
        this._onDidChangeFileDecorations.dispose();
    }
 
    private clearCaches(): void {
        this.highlightsByFile.clear();
        this.affectedFolders.clear();
        this.parentFoldersCache.clear();
        this.lastDecoratedPaths.clear();
    }
 
    private aggregateHighlights(highlights: HighlightCodeLineData[]): void {
        for (const highlight of highlights) {
            const filePath = highlight.filePath;
            if (!this.highlightsByFile.has(filePath)) {
                this.highlightsByFile.set(filePath, []);
            }
            this.highlightsByFile.get(filePath)!.push(highlight);
            this.addParentFolders(filePath);
        }
    }
 
    private addParentFolders(filePath: string): void {
        const cached = this.parentFoldersCache.get(filePath);
        if (cached) {
            cached.forEach((folder) => this.affectedFolders.add(folder));
            return;
        }
 
        const parents: string[] = [];
        let currentPath = path.dirname(filePath);
        const workspaceRoot =
            vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
 
        while (currentPath && currentPath !== path.dirname(currentPath)) {
            parents.push(currentPath);
            this.affectedFolders.add(currentPath);
            if (workspaceRoot && currentPath === workspaceRoot) break;
            currentPath = path.dirname(currentPath);
        }
 
        this.parentFoldersCache.set(filePath, parents);
    }
 
    private getSeverityColor(severity: Severity): vscode.ThemeColor {
        return new vscode.ThemeColor(SEVERITY_THEME_COLORS[severity]);
    }
 
    private buildFileDecoration(
        highlights: HighlightCodeLineData[]
    ): vscode.FileDecoration {
        const severity = getHighestSeverity(highlights);
        const counts = calculateSeverityCounts(highlights);
        const fileName = path.basename(highlights[0].filePath);
 
        const header = `${highlights.length} DataFlint ${pluralizeIssue(highlights.length)} in ${fileName}`;
        const breakdown = buildSeverityBreakdown(counts, { prefix: "• " });
 
        return {
            badge: "D",
            color: this.getSeverityColor(severity),
            tooltip:
                breakdown.length > 0
                    ? `${header}\n  ${breakdown.join("\n  ")}`
                    : header,
            propagate: false,
        };
    }
 
    private getFolderDecoration(
        folderPath: string
    ): vscode.FileDecoration | undefined {
        const allHighlights: HighlightCodeLineData[] = [];
        const affectedFiles = new Set<string>();
 
        for (const [filePath, highlights] of this.highlightsByFile.entries()) {
            if (filePath.startsWith(folderPath + path.sep)) {
                allHighlights.push(...highlights);
                affectedFiles.add(filePath);
            }
        }
 
        if (allHighlights.length === 0) return undefined;
 
        const severity = getHighestSeverity(allHighlights);
        const counts = calculateSeverityCounts(allHighlights);
 
        const header = `${allHighlights.length} DataFlint ${pluralizeIssue(allHighlights.length)} across ${affectedFiles.size} ${pluralizeFile(affectedFiles.size)}`;
        const breakdown = buildSeverityBreakdown(counts, { prefix: "• " });
 
        return {
            color: this.getSeverityColor(severity),
            tooltip:
                breakdown.length > 0
                    ? `${header}\n  ${breakdown.join("\n  ")}`
                    : header,
            propagate: true,
        };
    }
}