From 3757643e2977f34382f6e334cb4c8b6a77944e98 Mon Sep 17 00:00:00 2001 From: Colin Holzman Date: Mon, 21 Sep 2020 11:30:00 -0400 Subject: [PATCH] add a command to replace selected text with it's math.js evaluation --- package.json | 8 ++++++++ src/decorator.ts | 25 +++++++++++++++++++++++++ src/document.ts | 7 ++++--- src/extension.ts | 3 +++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 969c6b5..8540ec0 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,14 @@ "onLanguage:markdown" ], "main": "./out/extension.js", + "contributes": { + "commands": [ + { + "command": "Mathpad.replace", + "title": "Mathpad: Replace" + } + ] + }, "scripts": { "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", diff --git a/src/decorator.ts b/src/decorator.ts index 40fcef5..fe40826 100644 --- a/src/decorator.ts +++ b/src/decorator.ts @@ -11,6 +11,7 @@ import { window, workspace, ExtensionContext, + Range } from "vscode"; import MathDocument from "./document"; import { MathJsStatic } from 'mathjs'; @@ -80,6 +81,30 @@ export default class EditorDecorator implements Disposable { window.visibleTextEditors.forEach(editor => this.renderEditor(editor)); } + renderSelection() { + window.visibleTextEditors.forEach(editor => { + editor.selections.forEach(selection => { + editor.edit(editBuilder => { + const range = new Range(selection.start, selection.end); + const text = editor.document.getText(range); + const scope = this.getMathDocument(editor.document).scope; + let evaluated = ''; + try { + evaluated = this.math.evaluate(text, scope); + if (evaluated === undefined) { + evaluated = ''; + } + evaluated = evaluated.toString(); + } catch (e) { + evaluated = e.toString(); + } + editBuilder.replace(selection, evaluated); + }); + }); + }); + this.renderAll(); + } + /** * Re-render all math decorations on the given editor. */ diff --git a/src/document.ts b/src/document.ts index 8b5986e..d8ee91c 100644 --- a/src/document.ts +++ b/src/document.ts @@ -8,6 +8,7 @@ import { defaultScope } from './math'; export default class MathDocument { document: TextDocument; results = new Map(); + scope: any // Expression compiler cache. private compileCache = new Map(); @@ -21,7 +22,7 @@ export default class MathDocument { */ evaluate() { this.results.clear(); - let scope = defaultScope(); + this.scope = defaultScope(); for (let lineNumber = 0; lineNumber < this.document.lineCount; lineNumber++) { const line = this.document.lineAt(lineNumber); @@ -32,8 +33,8 @@ export default class MathDocument { if (compiled) { try { - const result = compiled.evaluate(scope); - scope["last"] = result; + const result = compiled.evaluate(this.scope); + this.scope["last"] = result; // Only display value results. if (typeof result !== "function" && typeof result !== "undefined") { diff --git a/src/extension.ts b/src/extension.ts index fa498c9..79a1431 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -4,4 +4,7 @@ import EditorDecorator from './decorator'; export function activate(context: vscode.ExtensionContext) { let decorator = new EditorDecorator(context); context.subscriptions.push(decorator); + context.subscriptions.push(vscode.commands.registerCommand('Mathpad.replace', () => { + decorator.renderSelection(); + })); }