Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a command to replace selected text with it's math.js evaluation #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ./",
Expand Down
25 changes: 25 additions & 0 deletions src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
window,
workspace,
ExtensionContext,
Range
} from "vscode";
import MathDocument from "./document";
import { MathJsStatic } from 'mathjs';
Expand Down Expand Up @@ -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.
*/
Expand Down
7 changes: 4 additions & 3 deletions src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { defaultScope } from './math';
export default class MathDocument {
document: TextDocument;
results = new Map<number, any>();
scope: any

// Expression compiler cache.
private compileCache = new Map<string, math.EvalFunction>();
Expand All @@ -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);
Expand All @@ -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") {
Expand Down
3 changes: 3 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));
}