All files / src/frontend/webviews CallGraphProvider.ts

0% Statements 0/21
100% Branches 0/0
0% Functions 0/1
0% Lines 0/21

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                                                                                                                                                                                               
/*
 * This file is released under the MIT license.
 * Copyright (c) 2017, 2022, Mike Lischke
 *
 * See LICENSE file for more info.
 */
 
import * as path from "path";
import { Uri, Webview } from "vscode";
 
import { WebviewProvider } from "./WebviewProvider";
import { FrontendUtils } from "../FrontendUtils";
import { ICallGraphEntry } from "../../webview-scripts/types";
 
export class CallGraphProvider extends WebviewProvider {
 
    public generateContent(webview: Webview, uri: Uri): string {
        const fileName = uri.fsPath;
        const baseName = path.basename(fileName, path.extname(fileName));
 
        const graph = this.backend.getReferenceGraph(fileName);
        const data: ICallGraphEntry[] = [];
        for (const entry of graph) {
            const references: string[] = [];
            for (const ref of entry[1].rules) {
                references.push(ref);
            }
 
            for (const ref of entry[1].tokens) {
                references.push(ref);
            }
 
            data.push({ name: entry[0], references });
        }
 
        const rendererScriptPath = FrontendUtils.getOutPath("src/webview-scripts/CallGraphRenderer.js", this.context,
            webview);
        const exportScriptPath = FrontendUtils.getOutPath("src/webview-scripts/GraphExport.js", this.context,
            webview);
        const graphLibPath = FrontendUtils.getNodeModulesPath(webview, "d3/dist/d3.js", this.context);
        const nonce = this.generateNonce();
 
        const diagram = `<!DOCTYPE html>
            <html>
                <head>
                    <meta http-equiv="Content-type" content="text/html;charset=UTF-8"/>
                    ${this.generateContentSecurityPolicy(webview, nonce)}
                    ${this.getStyles(webview)}
                    <base href="${uri.toString(true)}">
                    <script nonce="${nonce}" src="${graphLibPath}"></script>
                    <script nonce="${nonce}">
                        let callGraphRenderer;
                        let graphExport;
                    </script>
                </head>
 
            <body>
                <div class="header"><span class="call-graph-color"><span class="graph-initial">Ⓒ</span>all Graph</span>
                    <span class="action-box">
                        <a onClick="callGraphRenderer.changeDiameter(0.8);">
                            <span class="call-graph-color" style="font-size: 120%; font-weight: 800; cursor: pointer;
                                vertical-align: middle;">-</span>
                        </a>
                        <span style="margin-left: -5px; margin-right: -5px; cursor: default;">Change radius</span>
                        <a onClick="callGraphRenderer.changeDiameter(1.2);">
                            <span class="call-graph-color" style="font-size: 120%; font-weight: 800; cursor: pointer;
                                vertical-align: middle;">+</span>
                        </a>&nbsp;
                        Save to SVG
                        <a onClick="graphExport.exportToSVG('call-graph', '${baseName}');">
                            <span class="call-graph-save-image" />
                        </a>
                    </span>
                </div>
 
                <div id="container">
                    <svg></svg>
                </div>
                <script nonce="${nonce}" type="module">
                    import { CallGraphRenderer } from "${rendererScriptPath}";
                    import { GraphExport } from "${exportScriptPath}";
 
                    const data = ${JSON.stringify(data)};
                    callGraphRenderer = new CallGraphRenderer(data);
                    graphExport = new GraphExport();
 
                    callGraphRenderer.render();
                </script>
 
            </body>
        </html>`;
 
        return diagram;
    }
}