Evaluate Math Expression using new Emmet

This commit is contained in:
Ramya Achutha Rao
2017-06-21 19:05:10 -07:00
parent 09e94a709c
commit a38d58506a
6 changed files with 46 additions and 2 deletions

View File

@@ -0,0 +1,32 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import evaluate from '@emmetio/math-expression';
import { DocumentStreamReader } from './bufferStream';
export function evaluateMathExpression() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
return;
}
const stream = new DocumentStreamReader(editor.document);
editor.edit(editBuilder => {
editor.selections.forEach(selection => {
const pos = selection.isReversed ? selection.anchor : selection.active;
stream.pos = pos;
try {
const result = String(evaluate(stream, true));
editBuilder.replace(new vscode.Range(stream.pos, pos), result);
} catch (err) {
// Ignore error since most likely its because of non-math expression
console.warn('Math evaluation error', err);
}
});
});
}