From 58228ffd6eaec0865dedc490151d0a5e28272859 Mon Sep 17 00:00:00 2001 From: expitau-dev Date: Sat, 30 Jul 2022 20:28:45 -0400 Subject: [PATCH] Added highlighting support --- forgscript/language/README.md | 7 ++- forgscript/language/extension.js | 75 ++++++++++++++++++++++---------- forgscript/language/package.json | 11 +++-- 3 files changed, 67 insertions(+), 26 deletions(-) diff --git a/forgscript/language/README.md b/forgscript/language/README.md index d4aa87a..c19afe9 100644 --- a/forgscript/language/README.md +++ b/forgscript/language/README.md @@ -15,10 +15,15 @@ For example: This extension contributes the following settings: -* `forgscript.highlightNextCommand`: Specifies whether to highlight the next step of the program +* `forgscript.underlineNextStep`: Specifies whether to underline the next step(s) of the program (where the forg will jump next) +* `forgscript.highlightNextAction`: Specifies whether to highlight the next action the program will take (the next command that is not a '.' or ignored) ## Release Notes +### 0.1.1 + +Support for highlighting next action + ### 0.1.0 Initial release diff --git a/forgscript/language/extension.js b/forgscript/language/extension.js index 30cd2fc..77de848 100644 --- a/forgscript/language/extension.js +++ b/forgscript/language/extension.js @@ -2,8 +2,12 @@ // Import the module and reference it with the alias vscode in your code below const vscode = require('vscode'); -let style = vscode.window.createTextEditorDecorationType( - { borderStyle: "solid", borderColor: new vscode.ThemeColor('textLink.activeForeground'), borderWidth: "0 0 2px 0" }); // The code window decoration style +let underline = vscode.window.createTextEditorDecorationType( + { borderStyle: "solid", borderColor: new vscode.ThemeColor('textLink.activeForeground'), borderWidth: "0 0 2px 0" }); + +let highlight = vscode.window.createTextEditorDecorationType( + // { borderStyle: "solid", borderColor: "#FF0000", borderWidth: "0 0 2px 0" }); + { backgroundColor: new vscode.ThemeColor('editor.findMatchHighlightBackground') }); /** * @param {vscode.ExtensionContext} context @@ -15,38 +19,61 @@ function activate(context) { console.log('Forgscript Language Extension Active'); // Set the background text color using the provided input - function updateDecorations(ranges, editor) { + function updateDecorations(underlined, highlighted, editor) { // Apply this highlight color (style) to the specified line (ranges) - editor.setDecorations(style, ranges); + editor.setDecorations(underline, underlined); + editor.setDecorations(highlight, highlighted); + } + + function collatz(x) { + return x % 2 == 0 ? collatz_even(x) : collatz_odd(x) + } + function collatz_even(x) { + return x / 2; + } + function collatz_odd(x) { + return 3 * x + 1 } function getDecoratorRanges(char, x, y) { - function collatz(x) { - return x % 2 == 0 ? collatz_even(x) : collatz_odd(x) - } - function collatz_even(x) { - return x / 2; - } - function collatz_odd(x) { - return 3 * x + 1 - } - function getRange(x, y){ + function getRange(x, y) { let pos = new vscode.Position(y - 1, x - 1); return new vscode.Range(pos, pos.translate(0, 1)) } - if (char == 'v'){ + if (char == 'v') { return [getRange(collatz(x), y + 1)] - } else if (char == '^'){ + } else if (char == '^') { return [getRange(collatz(x), y - 1)] - } else if (char == '*'){ + } else if (char == '*') { return x % 2 == 0 ? [getRange(collatz_even(x), y), getRange(collatz_odd(x), y)] : [getRange(collatz(x), y)] } else { return [getRange(collatz(x), y)] } } + function getNextSymbol(pos, editor) { + function getRange(pos) { + return new vscode.Range(pos, pos.translate(0, 1)) + } + let newPos = pos.with(); + function isNoop(char) { + return !char.match(/[v^<>\+\-\*]/) + } + for (let i = 0; i == 0 || i < 200 && isNoop(getCharAt(newPos, editor)); i++) { + newPos = newPos.with(undefined, collatz(newPos._character + 1) - 1) + } + if (!isNoop(getCharAt(newPos, editor))) { + return getRange(newPos); + } + return null; + } + + function getCharAt(pos, editor) { + return editor.document.getText(new vscode.Range(pos, pos.translate(0, 1))) + } + function isUnitSelection(selections) { if (selections.length != 1) return false; let selection = selections[0]; @@ -57,13 +84,17 @@ function activate(context) { // Event Handler: This event fires when the selected text changes vscode.window.onDidChangeTextEditorSelection(e => { - if (e.textEditor.document.languageId == "forgscript" && isUnitSelection(e.selections) && vscode.workspace.getConfiguration('forgscript').get('highlightNextCommand')) { + if (e.textEditor.document.languageId == "forgscript" && isUnitSelection(e.selections)) { let pos = e.selections[0]._start - let char = e.textEditor.document.getText(new vscode.Range(pos, pos.translate(0, 1))) - let ranges = getDecoratorRanges(char, pos._character + 1, pos._line + 1); - updateDecorations(ranges, e.textEditor); + let char = getCharAt(pos, e.textEditor) + let stepOptions = getDecoratorRanges(char, pos._character + 1, pos._line + 1); + let action = getNextSymbol(pos, e.textEditor); + let nextAction = action ? [action] : [] + updateDecorations( + vscode.workspace.getConfiguration('forgscript').get('underlineNextStep') ? stepOptions : [], + vscode.workspace.getConfiguration('forgscript').get('highlightNextAction') ? nextAction : [], e.textEditor); } else { - updateDecorations([], e.textEditor); + updateDecorations([], [], e.textEditor); } }, null, context.subscriptions); diff --git a/forgscript/language/package.json b/forgscript/language/package.json index 2e17eee..f32a190 100644 --- a/forgscript/language/package.json +++ b/forgscript/language/package.json @@ -2,7 +2,7 @@ "name": "forgscript-language-support", "displayName": "forgscript-language-support", "description": "Adds VSCode support for ForgScript", - "version": "0.1.0", + "version": "0.1.1", "engines": { "vscode": "^1.69.0" }, @@ -39,10 +39,15 @@ "configuration": { "title": "ForgScript", "properties": { - "forgscript.highlightNextCommand": { + "forgscript.underlineNextStep": { "type": "boolean", "default": true, - "description": "Specifies whether to highlight the next step of the program" + "description": "Specifies whether to underline the next step(s) of the program (where the forg will jump next)" + }, + "forgscript.highlightNextAction": { + "type": "boolean", + "default": false, + "description": "Specifies whether to highlight the next action the program will take (the next command that is not a '.' or ignored)" } } }