From 1c26936601d982a5ff8161076e4490768be81726 Mon Sep 17 00:00:00 2001 From: BHAKTISHAH Date: Wed, 10 Jul 2024 21:56:18 -0400 Subject: [PATCH] version bump + change in coq-lsp dependency. also can potentially add color, but for now the color dictionary is just set to white --- README.md | 2 +- package.json | 2 +- src/constants/variableconsts.ts | 26 ++++++++++++++++++++++++++ src/extension.ts | 6 ++++++ src/parsing/ast.ts | 2 ++ src/parsing/parser.ts | 24 ++++++++++++++++++++++-- src/parsing/sizes.ts | 6 +++--- src/rendering/render.ts | 20 ++++++++++++++------ 8 files changed, 75 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index d5795a2..018f486 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Install [here](https://marketplace.visualstudio.com/items?itemName=inQWIRE.vizx). -To render manually, use command `Render Expressions with ViZX`. +To render manually, use command `Render Expressions with ZXViz`. To render automatically, use command `Activate ZXViz automatic rendering`. To stop rendering automatically, use command `Deactivate ZXViz automatic rendering`. ## Requirements diff --git a/package.json b/package.json index a713929..59242ce 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vizx", "displayName": "ViZX", "description": "Visualizer for the ZX calculus", - "version": "0.1.3", + "version": "0.1.4", "repository": "https://github.com/inQWIRE/ViZX/", "publisher": "inQWIRE", "engines": { diff --git a/src/constants/variableconsts.ts b/src/constants/variableconsts.ts index 6ed6500..a44dec6 100644 --- a/src/constants/variableconsts.ts +++ b/src/constants/variableconsts.ts @@ -92,3 +92,29 @@ export function setCanvasWidthHeight(wh: [number, number]) { }, }; } + +export let COLOR_DICT: string[] = ["#FFFFFF"]; +// export let COLOR_DICT : string[] = [ +// "#d0d6e4", +// "#cfb77f", +// "#a48758", +// "#b5bccd", +// "#8b92a2" +// ]; + +export function updateColorDict(newDict: Array) { + console.log("pre update in update: ", COLOR_DICT); + if (COLOR_DICT !== undefined) { + while (COLOR_DICT.pop() !== undefined) {} + } + let w = JSON.parse(JSON.stringify(newDict.pop())); + while (w !== undefined) { + COLOR_DICT.push(JSON.parse(JSON.stringify(w))); + w = newDict.pop(); + } + console.log("post update in update: ", COLOR_DICT); +} + +export function getColorDictLength() { + return COLOR_DICT.length; +} diff --git a/src/extension.ts b/src/extension.ts index fea6b4c..82ce664 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -63,6 +63,11 @@ export function activate(context: vscode.ExtensionContext) { renderCallback(context, expr) ); context.subscriptions.push(disposable); + let coqLspApi = vscode.extensions.getExtension("ejgallego.coq-lsp")!.exports; + let hook = coqLspApi.onUserGoals((goals: any) => + vscode.commands.executeCommand("vizx.lspRender", goals) + ); + disposable = vscode.commands.registerCommand("vizx.activateRendering", () => { vscode.window.showInformationMessage( "Automatic rendering is now turned on." @@ -76,6 +81,7 @@ export function activate(context: vscode.ExtensionContext) { vscode.window.showInformationMessage( "Automatic rendering is now turned off." ); + hook.dispose(); } ); diff --git a/src/parsing/ast.ts b/src/parsing/ast.ts index 4462a14..3078883 100644 --- a/src/parsing/ast.ts +++ b/src/parsing/ast.ts @@ -77,12 +77,14 @@ export interface ASTStack extends ASTNode { kind: "stack"; left: ASTNode; right: ASTNode; + index: number; } export interface ASTCompose extends ASTNode { kind: "compose"; left: ASTNode; right: ASTNode; + index: number; } export interface ASTNStack extends ASTNode { diff --git a/src/parsing/parser.ts b/src/parsing/parser.ts index 292aed2..9fb3757 100644 --- a/src/parsing/parser.ts +++ b/src/parsing/parser.ts @@ -17,9 +17,19 @@ let _ = require("lodash"); import * as ast from "./ast"; import * as lex from "./lexer"; +import * as v from "../constants/variableconsts"; import { lexerPrettyPrinter } from "./lexer"; type Token = psec.Token; +let index = 0; + +function incrIndex() { + console.log("incrementing index: ", index + 1); + console.log("length of dict at incr index: ", v.getColorDictLength()); + let r = index; + index = ++index % v.getColorDictLength(); + return r; +} /*********************** NUMBERS ******************************/ @@ -441,10 +451,20 @@ function applyStackCompose( // console.log('calling applyStackCompose'); switch (args[0].kind) { case lex.TokenKind.Compose: { - return { kind: "compose", left: l, right: args[1] } as ast.ASTCompose; + return { + kind: "compose", + left: l, + right: args[1], + index: incrIndex(), + } as ast.ASTCompose; } case lex.TokenKind.Stack: { - return { kind: "stack", left: l, right: args[1] } as ast.ASTStack; + return { + kind: "stack", + left: l, + right: args[1], + index: incrIndex(), + } as ast.ASTStack; } default: { // throw new Error(`Unknown compose: ${args[0].text}`); diff --git a/src/parsing/sizes.ts b/src/parsing/sizes.ts index 3006128..e031a70 100644 --- a/src/parsing/sizes.ts +++ b/src/parsing/sizes.ts @@ -103,7 +103,7 @@ export function addSizesHappyRobot(node: ast.ASTNode): ast.ASTNode { break; } } - console.log("happy robot! ", node); + // console.log("happy robot! ", node); return node; } export function addSizes(node: ast.ASTNode): ast.ASTNode { @@ -276,9 +276,9 @@ export function addSizes(node: ast.ASTNode): ast.ASTNode { throw new Error(`Unknown kind: ${node.kind}`); } } - console.log("before happy robot: ", node); + // console.log("before happy robot: ", node); node = addSizesHappyRobot(node); - console.log("after happy robot: ", node); + // console.log("after happy robot: ", node); return node; } diff --git a/src/rendering/render.ts b/src/rendering/render.ts index 8f3a8f7..f22879d 100644 --- a/src/rendering/render.ts +++ b/src/rendering/render.ts @@ -36,6 +36,7 @@ import { SMALL_TEXT, ARIAL_FONT, REALLY_SMALL_TEXT, + COLOR_DICT, } from "../constants/variableconsts"; import { findCenter, findLeftCenter, findRightCenter } from "../parsing/coords"; import { quad } from "../constants/types"; @@ -173,18 +174,18 @@ function drawNWireNode(node: ast.ASTNode) { function drawStackNode(node: ast.ASTNode) { let stack = node; + drawBoundary(node.boundary!, STACK_DASH, COLOR_DICT[stack.index]); draw(stack.left); draw(stack.right); - drawBoundary(node.boundary!, STACK_DASH); } function drawNStackNode(node: ast.ASTNode) { let nstack = node; - draw(nstack.node); let label_bound = JSON.parse(JSON.stringify(nstack.boundary!)); label_bound.tr.x = label_bound.tl.x + FUNC_ARG_SIZE; label_bound.br.x = label_bound.bl.x + FUNC_ARG_SIZE; drawBoundary(label_bound, FUNCTION_DASH); + draw(nstack.node); let bound = JSON.parse(JSON.stringify(nstack.boundary!)); bound.tl.x += FUNC_ARG_SIZE; bound.bl.x += FUNC_ARG_SIZE; @@ -196,11 +197,11 @@ function drawNStackNode(node: ast.ASTNode) { function drawNStack1Node(node: ast.ASTNode) { let nstack = node; - draw(nstack.node); let label_bound = JSON.parse(JSON.stringify(nstack.boundary!)); label_bound.tr.x = label_bound.tl.x + FUNC_ARG_SIZE; label_bound.br.x = label_bound.bl.x + FUNC_ARG_SIZE; drawBoundary(label_bound, FUNCTION_DASH); + draw(nstack.node); let bound = JSON.parse(JSON.stringify(nstack.boundary!)); bound.tl.x += FUNC_ARG_SIZE; bound.bl.x += FUNC_ARG_SIZE; @@ -210,7 +211,11 @@ function drawNStack1Node(node: ast.ASTNode) { ctx.fillText(nstack.n.expr.concat(N_STACK_1_OP), cent.x, cent.y); } -function drawBoundary(boundary: quad, dash?: [number, number]) { +function drawBoundary( + boundary: quad, + dash?: [number, number], + color: string = white +) { if (dash !== undefined) { ctx.setLineDash(dash); } else { @@ -218,6 +223,8 @@ function drawBoundary(boundary: quad, dash?: [number, number]) { } ctx.lineWidth = LINE_WIDTH; ctx.strokeStyle = black; + console.log("setting fill style in draw boundary, ", color); + ctx.fillStyle = color; ctx.beginPath(); ctx.moveTo(boundary.tl.x, boundary.tl.y); ctx.lineTo(boundary.tr.x, boundary.tr.y); @@ -225,6 +232,7 @@ function drawBoundary(boundary: quad, dash?: [number, number]) { ctx.lineTo(boundary.bl.x, boundary.bl.y); ctx.closePath(); ctx.stroke(); + ctx.fill(); return; } @@ -250,15 +258,15 @@ function drawFuncBoundary(boundary: quad) { function drawComposeNode(node: ast.ASTNode) { let compose = node; + drawBoundary(node.boundary!, COMPOSE_DASH, COLOR_DICT[compose.index]); draw(compose.left); draw(compose.right); - drawBoundary(node.boundary!, COMPOSE_DASH); } function drawCastNode(node: ast.ASTNode) { let cast = node; - draw(cast.node); drawBoundary(cast.boundary!, CAST_DASH); + draw(cast.node); let lc = findLeftCenter(cast.boundary!); let rc = findRightCenter(cast.boundary!); ctx.save();