Skip to content

Commit 009f53a

Browse files
committed
feat: add Graph rendering with CFG-metadata options only
Updated `updateMetadata` to support new logic We should note that I'm still fetching twice from GitHub..
1 parent 3ab9247 commit 009f53a

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

src/render/src/App.svelte

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ import {
3131
// Add state for panel and checkbox controls
3232
let isPanelOpen = false;
3333
let showMetadata = {
34-
language: false,
35-
functionName: false,
36-
lineCount: false,
34+
language: true,
35+
functionName: true,
36+
lineCount: true,
3737
nodeCount: false,
3838
edgeCount: false,
3939
cyclomaticComplexity: false,
@@ -44,32 +44,32 @@ const metadataFields = [
4444
{
4545
key: "language",
4646
label: "Language",
47-
value: () => functionAndCFGMetadata.functionData.language,
47+
value: () => functionAndCFGMetadata.functionData?.language,
4848
},
4949
{
5050
key: "functionName",
5151
label: "Function Name",
52-
value: () => functionAndCFGMetadata.functionData.name,
52+
value: () => functionAndCFGMetadata.functionData?.name,
5353
},
5454
{
5555
key: "lineCount",
5656
label: "Line Count",
57-
value: () => functionAndCFGMetadata.functionData.lineCount,
57+
value: () => functionAndCFGMetadata.functionData?.lineCount,
5858
},
5959
{
6060
key: "nodeCount",
6161
label: "Node Count",
62-
value: () => functionAndCFGMetadata.cfgGraphData.nodeCount,
62+
value: () => functionAndCFGMetadata.cfgGraphData?.nodeCount,
6363
},
6464
{
6565
key: "edgeCount",
6666
label: "Edge Count",
67-
value: () => functionAndCFGMetadata.cfgGraphData.edgeCount,
67+
value: () => functionAndCFGMetadata.cfgGraphData?.edgeCount,
6868
},
6969
{
7070
key: "cyclomaticComplexity",
7171
label: "Cyclomatic Complexity",
72-
value: () => functionAndCFGMetadata.cfgGraphData.cyclomaticComplexity,
72+
value: () => functionAndCFGMetadata.cfgGraphData?.cyclomaticComplexity,
7373
},
7474
];
7575
@@ -283,22 +283,23 @@ async function createCFG(params: Params): Promise<CFG> {
283283
284284
let functionAndCFGMetadata: FunctionAndCFGMetadata | undefined;
285285
286-
function updateMetadata(func: SyntaxNode, language: Language, CFG: CFG) {
287-
// Update function metadata
288-
const name: string | undefined = extractFunctionName(func, language);
289-
const lineCount: number = func.endPosition.row - func.startPosition.row + 1;
286+
function updateMetadata(CFG: CFG, func?: SyntaxNode, language?: Language) {
287+
// Update function metadata (GitHub only)
288+
let name: string | undefined = undefined;
289+
let lineCount: number | undefined = undefined;
290+
291+
if (func && language) {
292+
name = extractFunctionName(func, language);
293+
lineCount = func.endPosition.row - func.startPosition.row + 1;
294+
}
290295
291296
// Update CFG metadata
292297
const nodeCount: number = CFG.graph.order;
293298
const edgeCount: number = CFG.graph.size;
294-
const cyclomaticComplexity: number = CFG.graph.size - nodeCount + 2;
299+
const cyclomaticComplexity: number = CFG.graph.size - nodeCount + 2; // (https://en.wikipedia.org/wiki/Cyclomatic_complexity)
295300
296301
return {
297-
functionData: {
298-
name,
299-
lineCount,
300-
language,
301-
},
302+
functionData: name && lineCount ? { name, lineCount, language } : undefined,
302303
cfgGraphData: {
303304
nodeCount,
304305
edgeCount,
@@ -318,7 +319,10 @@ async function render() {
318319
if (params.type === "GitHub") {
319320
codeUrl = params.codeUrl;
320321
const { func, language } = await fetchFunctionAndLanguage(params);
321-
functionAndCFGMetadata = updateMetadata(func, language, cfg);
322+
functionAndCFGMetadata = updateMetadata(cfg, func, language);
323+
} else {
324+
// Graph
325+
functionAndCFGMetadata = updateMetadata(cfg);
322326
}
323327
324328
const graphviz = await Graphviz.load();
@@ -390,9 +394,9 @@ onMount(() => {
390394
</div>
391395
{#if functionAndCFGMetadata}
392396
<!-- Metadata display -->
393-
{#if Object.values(showMetadata).some(value => value)}
397+
{#if metadataFields.some(field => showMetadata[field.key] && field.value() !== undefined)}
394398
<div class="metadata" class:panel-open={isPanelOpen}>
395-
{#each metadataFields as { key, label, value }}
399+
{#each metadataFields.filter(field => field.value() !== undefined) as { key, label , value}}
396400
{#if showMetadata[key]}
397401
<span>{label}: {value()}</span>
398402
{/if}
@@ -405,8 +409,8 @@ onMount(() => {
405409
<!-- Control panel -->
406410
<div class="control-panel" class:open={isPanelOpen}>
407411
<h3>Display Options</h3>
408-
{#each metadataFields as { key, label }}
409-
<label>
412+
{#each metadataFields.filter(field => field.value() !== undefined) as { key, label }}
413+
<label>
410414
<input type="checkbox" bind:checked={showMetadata[key]} />
411415
{label}
412416
</label>
@@ -473,12 +477,9 @@ onMount(() => {
473477
color: var(--toggle-color);
474478
border: none;
475479
font-size: 1em;
480+
cursor: pointer;
476481
}
477482
478-
.panel-toggle:hover {
479-
cursor: pointer;
480-
}
481-
482483
.control-panel {
483484
position: fixed;
484485
top: 4em;

0 commit comments

Comments
 (0)