All files / src/tree-view tree-item.ts

100% Statements 102/102
100% Branches 13/13
100% Functions 10/10
100% Lines 102/102

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 1271x   1x                 1x       1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x   1x 28x 28x 28x   1x 28x 28x 28x 28x 28x 28x 28x         1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x   1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x   1x 9x 9x 9x 9x 9x 9x   1x 9x 9x 1x   1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   1x 19x 19x   1x 19x 19x 1x  
import * as vscode from "vscode";
import { HighlightCodeLineData } from "../highlights/highlights-models";
import {
    SeverityCounts,
    SEVERITY_THEME_COLORS,
    SEVERITY_EMOJIS,
    getHighestSeverityFromCounts,
    buildSeverityBreakdown,
    pluralizeIssue,
    pluralizeFile,
} from "../highlights/severity-utils";
import { ExtensionCommands } from "../extension-commands";
 
export type TreeItemType = "folder" | "file";
 
export abstract class DataFlintTreeItem extends vscode.TreeItem {
    constructor(
        public readonly label: string,
        public readonly itemType: TreeItemType,
        public readonly fsPath: string,
        public readonly severityCounts: SeverityCounts,
        collapsibleState: vscode.TreeItemCollapsibleState
    ) {
        super(label, collapsibleState);
        this.contextValue = itemType;
    }
 
    protected getSeverityColor(): vscode.ThemeColor {
        const severity = getHighestSeverityFromCounts(this.severityCounts);
        return new vscode.ThemeColor(SEVERITY_THEME_COLORS[severity]);
    }
 
    getDescription(): string {
        const { hotspot, warning, info } = this.severityCounts;
        const badges: string[] = [];
        if (hotspot > 0) badges.push(`${SEVERITY_EMOJIS.hotspot}${hotspot}`);
        if (warning > 0) badges.push(`${SEVERITY_EMOJIS.warning}${warning}`);
        if (info > 0) badges.push(`${SEVERITY_EMOJIS.info}${info}`);
        return badges.join(" ");
    }
 
    protected abstract getTooltipHeader(): string;
    protected abstract getTooltipFooter(): string;
 
    getTooltip(): string {
        const breakdown = buildSeverityBreakdown(this.severityCounts, {
            includeEmoji: true,
            includeDescription: true,
        });
        return [
            this.getTooltipHeader(),
            ...breakdown,
            "",
            this.getTooltipFooter(),
        ].join("\n");
    }
}
 
export class FolderTreeItem extends DataFlintTreeItem {
    constructor(
        label: string,
        fsPath: string,
        severityCounts: SeverityCounts,
        public readonly fileCount: number
    ) {
        super(
            label,
            "folder",
            fsPath,
            severityCounts,
            vscode.TreeItemCollapsibleState.Collapsed
        );
        this.iconPath = new vscode.ThemeIcon("folder", this.getSeverityColor());
        this.tooltip = this.getTooltip();
        this.description = this.getDescription();
    }
 
    protected getTooltipHeader(): string {
        const total =
            this.severityCounts.hotspot +
            this.severityCounts.warning +
            this.severityCounts.info;
        return `${total} ${pluralizeIssue(total)} in ${this.fileCount} ${pluralizeFile(this.fileCount)}`;
    }
 
    protected getTooltipFooter(): string {
        return "Expand to see affected files";
    }
}
 
export class FileTreeItem extends DataFlintTreeItem {
    constructor(
        label: string,
        fsPath: string,
        severityCounts: SeverityCounts,
        public readonly highlights: HighlightCodeLineData[]
    ) {
        super(
            label,
            "file",
            fsPath,
            severityCounts,
            vscode.TreeItemCollapsibleState.None
        );
        this.resourceUri = vscode.Uri.file(fsPath);
        this.iconPath = new vscode.ThemeIcon(
            "file-code",
            this.getSeverityColor()
        );
        this.tooltip = this.getTooltip();
        this.description = this.getDescription();
        this.command = {
            command: ExtensionCommands.TREE_ITEM_CLICKED,
            title: "Open File",
            arguments: [this.resourceUri, this.highlights[0]?.codeLine],
        };
    }
 
    protected getTooltipHeader(): string {
        return `${this.highlights.length} ${pluralizeIssue(this.highlights.length)} found`;
    }
 
    protected getTooltipFooter(): string {
        return "Click to open • Right-click for more options";
    }
}