|
7 | 7 | import type Parser from "web-tree-sitter"; |
8 | 8 | import { type SyntaxNode } from "web-tree-sitter"; |
9 | 9 | import { type Language, newCFGBuilder } from "../../control-flow/cfg"; |
10 | | - import { type CFG, mergeNodeAttrs } from "../../control-flow/cfg-defs"; |
| 10 | + import { |
| 11 | + type CFG, |
| 12 | + type GraphEdge, |
| 13 | + type GraphNode, |
| 14 | + mergeNodeAttrs, |
| 15 | + } from "../../control-flow/cfg-defs"; |
11 | 16 | import { simplifyCFG, trimFor } from "../../control-flow/graph-ops"; |
12 | 17 | import { Graphviz } from "@hpcc-js/wasm-graphviz"; |
13 | 18 | import { graphToDot } from "../../control-flow/render"; |
14 | 19 | import { |
| 20 | + type ColorScheme, |
15 | 21 | getDarkColorList, |
16 | 22 | getLightColorList, |
17 | 23 | listToScheme, |
18 | 24 | } from "../../control-flow/colors"; |
19 | 25 | import Panzoom, { type PanzoomObject } from "@panzoom/panzoom"; |
20 | 26 | import { onMount } from "svelte"; |
| 27 | + import { MultiDirectedGraph } from "graphology"; |
| 28 | +
|
| 29 | + let codeUrl: string | undefined; |
21 | 30 |
|
22 | 31 | /** |
23 | 32 | * A reference to a function on GitHub |
|
26 | 35 | /** |
27 | 36 | * The URL for the raw file on GitHub |
28 | 37 | */ |
29 | | - rawURL: string; |
| 38 | + rawUrl: string; |
30 | 39 | /** |
31 | 40 | * The line-number for the function |
32 | 41 | */ |
|
45 | 54 | throw new Error("Missing line number."); |
46 | 55 | } |
47 | 56 |
|
48 | | - const rawURL = githubURL.replace( |
| 57 | + const rawUrl = githubURL.replace( |
49 | 58 | /(?<host>https:\/\/github.com\/)(?<project>\w+\/\w+\/)(blob\/)(?<path>.*)(#L\d+)/, |
50 | 59 | "https://raw.githubusercontent.com/$<project>$<path>", |
51 | 60 | ); |
52 | 61 |
|
53 | | - return { line, rawURL }; |
| 62 | + return { line, rawUrl }; |
54 | 63 | } |
55 | 64 |
|
56 | 65 | /** |
|
106 | 115 |
|
107 | 116 | let rawSVG: string | undefined; |
108 | 117 |
|
109 | | - async function render() { |
110 | | - const urlSearchParams = new URLSearchParams(window.location.search); |
111 | | - const githubUrl = urlSearchParams.get("github") ?? ""; |
| 118 | + type GithubParams = { |
| 119 | + type: "GitHub"; |
| 120 | + rawUrl: string; |
| 121 | + codeUrl: string; |
| 122 | + line: number; |
| 123 | + }; |
| 124 | + type GraphParams = { |
| 125 | + type: "Graph"; |
| 126 | + rawUrl: string; |
| 127 | + }; |
| 128 | + type Params = (GithubParams | GraphParams) & { |
| 129 | + colorScheme: ColorScheme; |
| 130 | + colors: "light" | "dark"; |
| 131 | + }; |
| 132 | +
|
| 133 | + function parseUrlSearchParams(urlSearchParams: URLSearchParams): Params { |
| 134 | + const githubUrl = urlSearchParams.get("github"); |
112 | 135 | const colors = urlSearchParams.get("colors") ?? "dark"; |
113 | | - if (colors !== "light" && colors !== "dark") { |
114 | | - throw new Error(`Unsupported color scheme ${colors}`); |
| 136 | + const graphUrl = urlSearchParams.get("graph"); |
| 137 | +
|
| 138 | + if (colors !== "dark" && colors !== "light") { |
| 139 | + throw new Error("Invalid color scheme"); |
| 140 | + } |
| 141 | + if (!(githubUrl || graphUrl)) { |
| 142 | + throw new Error("No URL provided"); |
115 | 143 | } |
| 144 | + if (githubUrl && graphUrl) { |
| 145 | + throw new Error("Too many URLs provided"); |
| 146 | + } |
| 147 | +
|
116 | 148 | const colorScheme = getColorScheme(colors); |
117 | | - setBackgroundColor(colors); |
118 | 149 |
|
119 | | - const { line, rawURL } = parseGithubUrl(githubUrl); |
120 | | - const response = await fetch(rawURL); |
| 150 | + if (githubUrl) { |
| 151 | + const { line, rawUrl } = parseGithubUrl(githubUrl); |
| 152 | + return { type: "GitHub", rawUrl, line, colorScheme, colors, codeUrl }; |
| 153 | + } |
| 154 | + return { |
| 155 | + type: "Graph", |
| 156 | + rawUrl: graphUrl, |
| 157 | + colorScheme: colorScheme, |
| 158 | + colors, |
| 159 | + }; |
| 160 | + } |
| 161 | +
|
| 162 | + async function createGitHubCFG(ghParams: GithubParams): Promise<CFG> { |
| 163 | + const { rawUrl, line } = ghParams; |
| 164 | + const response = await fetch(rawUrl); |
121 | 165 | const code = await response.text(); |
122 | 166 | // We assume that the raw URL always ends with the file extension |
123 | | - const language = getLanguage(rawURL); |
| 167 | + const language = getLanguage(rawUrl); |
124 | 168 |
|
125 | 169 | const func = await getFunctionByLine(code, language, line); |
126 | 170 | if (!func) { |
127 | 171 | throw new Error(`Unable to find function on line ${line}`); |
128 | 172 | } |
129 | 173 |
|
130 | | - const cfg = buildCFG(func, language); |
| 174 | + return buildCFG(func, language); |
| 175 | + } |
| 176 | +
|
| 177 | + async function createGraphCFG(graphParams: GraphParams): Promise<CFG> { |
| 178 | + const { rawUrl } = graphParams; |
| 179 | + const response = await fetch(rawUrl); |
| 180 | + const jsonData = await response.json(); |
| 181 | + const graph = new MultiDirectedGraph<GraphNode, GraphEdge>(); |
| 182 | + graph.import(jsonData); |
| 183 | +
|
| 184 | + const entry = graph.findNode( |
| 185 | + (node, _attributes) => graph.inDegree(node) === 0, |
| 186 | + ); |
| 187 | + if (!entry) { |
| 188 | + throw new Error("No entry found"); |
| 189 | + } |
| 190 | + return { graph, entry, offsetToNode: [] }; |
| 191 | + } |
| 192 | +
|
| 193 | + async function createCFG(params: Params): Promise<CFG> { |
| 194 | + switch (params.type) { |
| 195 | + case "GitHub": |
| 196 | + return createGitHubCFG(params); |
| 197 | + case "Graph": |
| 198 | + return createGraphCFG(params); |
| 199 | + } |
| 200 | + } |
| 201 | +
|
| 202 | + async function render() { |
| 203 | + const urlSearchParams = new URLSearchParams(window.location.search); |
| 204 | + const params = parseUrlSearchParams(urlSearchParams); |
| 205 | + setBackgroundColor(params.colors); |
| 206 | + if (params.type === "GitHub") { |
| 207 | + codeUrl = params.codeUrl; |
| 208 | + } |
| 209 | +
|
| 210 | + const cfg = await createCFG(params); |
131 | 211 | const graphviz = await Graphviz.load(); |
132 | | - rawSVG = graphviz.dot(graphToDot(cfg, false, undefined, colorScheme)); |
| 212 | + rawSVG = graphviz.dot( |
| 213 | + graphToDot(cfg, false, undefined, params.colorScheme), |
| 214 | + ); |
133 | 215 | return rawSVG; |
134 | 216 | } |
135 | 217 |
|
|
150 | 232 | } |
151 | 233 |
|
152 | 234 | function openCode() { |
153 | | - const urlSearchParams = new URLSearchParams(window.location.search); |
154 | | - const githubUrl = urlSearchParams.get("github") ?? ""; |
155 | | -
|
156 | | - if (!githubUrl) return; |
157 | | -
|
158 | | - window.open(githubUrl, "_blank").focus(); |
| 235 | + window.open(codeUrl, "_blank").focus(); |
159 | 236 | } |
160 | 237 |
|
161 | 238 | function saveSVG() { |
|
190 | 267 | <div class="controlsContainer"> |
191 | 268 | <div class="controls"> |
192 | 269 | <button on:click={resetView}>Reset View</button> |
193 | | - <button on:click={openCode}>Open Code</button> |
| 270 | + <button |
| 271 | + on:click={openCode} |
| 272 | + disabled={!Boolean(codeUrl)} |
| 273 | + title={Boolean(codeUrl) ? "" : "Only available for GitHub code"} |
| 274 | + >Open Code</button |
| 275 | + > |
194 | 276 | <button on:click={saveSVG}>Download SVG</button> |
195 | 277 | </div> |
196 | 278 | </div> |
|
0 commit comments