All files / src/highlights highlight-presentation.ts

100% Statements 91/91
95.23% Branches 20/21
100% Functions 4/4
100% Lines 91/91

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 1121x 1x     1x 1x               1x 1x 17x 17x 17x   17x 17x 17x 3x 3x 3x 3x 3x   14x     14x 14x 15x 1x 14x     15x 15x 16x 15x 15x   15x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 15x 14x 14x   14x 14x 14x 17x   1x 17x 17x 22x 18x 18x 22x 22x 17x 17x   1x 3x 3x 4x 3x 3x 4x 4x 3x 3x   1x 4x 4x 4x 4x 5x 5x 5x 5x 2x 2x 5x 4x 4x 1x  
import * as vscode from "vscode";
import * as path from "node:path";
import { HighlightDTO } from "@dataflint/mcp-server";
import { HighlightCodeLineData } from "./highlights-models";
import { readLineZeroBased } from "../file-utils";
import { Logger } from "../logger";
 
/**
 * HighlightPresentationService - simplified for server-computed data.
 *
 * Server now computes ALL display fields (displayText, tooltip, queryPrefix).
 * Extension just resolves file paths and passes through server data.
 */
export class HighlightPresentationService {
    async displayCodeHighlights(
        highlightDtos: HighlightDTO[]
    ): Promise<HighlightCodeLineData[]> {
        const logger = Logger.getInstance();
 
        const workspaceRoot =
            vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
        if (!workspaceRoot) {
            logger.warn(
                "Skipping highlight presentation: no workspace folder open"
            );
            return [];
        }
 
        const dtosByFile = this.groupByFile(highlightDtos);
 
        // Process all files in parallel
        const fileResults = await Promise.all(
            Object.entries(dtosByFile).map(async ([filePath, fileDtos]) => {
                const resolvedFilePath = path.isAbsolute(filePath)
                    ? filePath
                    : path.join(workspaceRoot, filePath);
 
                // Read all anchor texts for this file in parallel
                const anchorTexts = await Promise.all(
                    fileDtos.map((dto) =>
                        readLineZeroBased(resolvedFilePath, dto.codeLine - 1)
                    )
                );
 
                return fileDtos.map((dto, i) => ({
                    filePath: resolvedFilePath,
                    codeLine: dto.codeLine,
                    text: dto.displayText,
                    sql_id: dto.sqlIds?.[0],
                    sql_ids: dto.sqlIds,
                    node_id: dto.nodeIds?.[0],
                    node_ids: dto.nodeIds,
                    url: dto.url,
                    anchorText: anchorTexts[i],
                    queryPrefix: dto.queryPrefix,
                    tooltip: dto.tooltip,
                    structuredTooltip: dto.structuredTooltip,
                    contributions: dto.contributions,
                    isHotspot: dto.isHotspot,
                    alertCount: dto.alertCount,
                    findingCount: dto.findingCount,
                    jobId: dto.jobId,
                    topItemDisplay: dto.topItemDisplay,
                    hasFix: dto.hasFix,
                    additionalCount: dto.additionalCount,
                }));
            })
        );
 
        const result = fileResults.flat();
        logger.info(`Presented ${result.length} highlights for display`);
        return result;
    }
 
    groupByFile(dtos: HighlightDTO[]): Record<string, HighlightDTO[]> {
        const result: Record<string, HighlightDTO[]> = {};
        for (const dto of dtos) {
            if (!result[dto.filePath]) {
                result[dto.filePath] = [];
            }
            result[dto.filePath].push(dto);
        }
        return result;
    }
 
    groupByLine(dtos: HighlightDTO[]): Map<number, HighlightDTO[]> {
        const result = new Map<number, HighlightDTO[]>();
        for (const dto of dtos) {
            if (!result.has(dto.codeLine)) {
                result.set(dto.codeLine, []);
            }
            result.get(dto.codeLine)!.push(dto);
        }
        return result;
    }
 
    detectLinesWithMultipleQueries(
        dtosByLine: Map<number, HighlightDTO[]>
    ): Set<number> {
        const linesWithMultipleQueries = new Set<number>();
        for (const [line, lineDtos] of dtosByLine) {
            const uniqueQueryIds = new Set(
                lineDtos.flatMap((dto) => dto.sqlIds || [])
            );
            if (uniqueQueryIds.size > 1) {
                linesWithMultipleQueries.add(line);
            }
        }
        return linesWithMultipleQueries;
    }
}