Skip to content

Commit

Permalink
Added highlighting support
Browse files Browse the repository at this point in the history
  • Loading branch information
expitau committed Jul 31, 2022
1 parent 2faa2ad commit 58228ff
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
7 changes: 6 additions & 1 deletion forgscript/language/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
75 changes: 53 additions & 22 deletions forgscript/language/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
Expand All @@ -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);

Expand Down
11 changes: 8 additions & 3 deletions forgscript/language/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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)"
}
}
}
Expand Down

0 comments on commit 58228ff

Please sign in to comment.