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 171 172 173 174 175 176 177 178 179 | 1x 1x 1x 1x 33x 33x 30x 30x 30x 30x 30x 30x 49x 49x 49x 49x 30x 42x 42x 42x 42x 42x 42x 42x 30x 30x 30x 30x 33x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 34x 34x 34x 26x 26x 26x 26x 26x 26x 26x 26x 26x 34x 34x 34x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 42x 33x 42x 42x 42x 42x 42x 42x 42x 34x 34x 34x 34x 34x 34x 34x 42x 33x 21x 21x 21x 21x 21x 10x 10x 7x 10x 21x 21x 28x 9x 9x 9x 9x 9x 9x 9x 9x 9x 28x 19x 19x 19x 19x 19x 19x 19x 19x 19x 28x 21x 21x 33x 12x 12x 15x 12x 15x 3x 3x 15x 12x 12x 33x 5x 5x 33x 56x 68x 68x 26x 26x 68x 56x 33x 18x 18x 33x | import * as path from "path";
import { HighlightCodeLineData } from "../highlights/highlights-models";
import {
calculateSeverityCounts,
SeverityCounts,
} from "../highlights/severity-utils";
import { FolderTreeItem, FileTreeItem } from "./tree-item";
interface TreeNode {
name: string;
fsPath: string;
type: "folder" | "file";
children: Map<string, TreeNode>;
highlights: HighlightCodeLineData[];
severityCounts: SeverityCounts;
}
export class TreeBuilder {
private treeNodeCache = new Map<string, TreeNode>();
/** Builds tree and auto-caches for getNodeByPath() lookups. */
public buildTree(
highlights: HighlightCodeLineData[],
workspaceRoot: string
): Map<string, TreeNode> {
const rootNodes = new Map<string, TreeNode>();
const fileMap = new Map<string, HighlightCodeLineData[]>();
for (const highlight of highlights) {
if (!fileMap.has(highlight.filePath))
fileMap.set(highlight.filePath, []);
fileMap.get(highlight.filePath)!.push(highlight);
}
for (const [filePath, fileHighlights] of fileMap.entries()) {
this.addFileToTree(
rootNodes,
filePath,
fileHighlights,
workspaceRoot
);
}
this.treeNodeCache.clear();
this.cacheNodesRecursive(rootNodes);
return rootNodes;
}
private addFileToTree(
rootNodes: Map<string, TreeNode>,
filePath: string,
highlights: HighlightCodeLineData[],
workspaceRoot: string
): void {
const relativePath = path.relative(workspaceRoot, filePath);
const parts = relativePath.split(path.sep);
let currentLevel = rootNodes;
let currentPath = workspaceRoot;
for (let i = 0; i < parts.length - 1; i++) {
const folderName = parts[i];
currentPath = path.join(currentPath, folderName);
if (!currentLevel.has(folderName)) {
currentLevel.set(folderName, {
name: folderName,
fsPath: currentPath,
type: "folder",
children: new Map(),
highlights: [],
severityCounts: { hotspot: 0, warning: 0, info: 0 },
});
}
const node = currentLevel.get(folderName)!;
currentLevel = node.children;
}
const fileName = parts[parts.length - 1];
const severityCounts = calculateSeverityCounts(highlights);
currentLevel.set(fileName, {
name: fileName,
fsPath: filePath,
type: "file",
children: new Map(),
highlights,
severityCounts,
});
this.propagateCounts(rootNodes, relativePath, severityCounts);
}
private propagateCounts(
rootNodes: Map<string, TreeNode>,
relativePath: string,
counts: SeverityCounts
): void {
const parts = relativePath.split(path.sep);
let currentLevel = rootNodes;
for (let i = 0; i < parts.length - 1; i++) {
const folderName = parts[i];
const node = currentLevel.get(folderName)!;
node.severityCounts.hotspot += counts.hotspot;
node.severityCounts.warning += counts.warning;
node.severityCounts.info += counts.info;
currentLevel = node.children;
}
}
public convertToTreeItems(
nodes: Map<string, TreeNode>
): (FolderTreeItem | FileTreeItem)[] {
const items: (FolderTreeItem | FileTreeItem)[] = [];
const sortedEntries = Array.from(nodes.entries()).sort(
([nameA, nodeA], [nameB, nodeB]) => {
if (nodeA.type !== nodeB.type)
return nodeA.type === "folder" ? -1 : 1;
return nameA.localeCompare(nameB);
}
);
for (const [name, node] of sortedEntries) {
if (node.type === "folder") {
const fileCount = this.countFiles(node);
items.push(
new FolderTreeItem(
name,
node.fsPath,
node.severityCounts,
fileCount
)
);
} else {
items.push(
new FileTreeItem(
name,
node.fsPath,
node.severityCounts,
node.highlights
)
);
}
}
return items;
}
private countFiles(node: TreeNode): number {
let count = 0;
for (const child of node.children.values()) {
if (child.type === "file") {
count++;
} else {
count += this.countFiles(child);
}
}
return count;
}
public clearCache(): void {
this.treeNodeCache.clear();
}
private cacheNodesRecursive(nodes: Map<string, TreeNode>): void {
for (const node of nodes.values()) {
this.treeNodeCache.set(node.fsPath, node);
if (node.children.size > 0) {
this.cacheNodesRecursive(node.children);
}
}
}
public getNodeByPath(fsPath: string): TreeNode | undefined {
return this.treeNodeCache.get(fsPath);
}
}
|