mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-27 20:13:32 +01:00
Evaluate Math Expression using new Emmet
This commit is contained in:
@@ -15,7 +15,7 @@ export class DocumentStreamReader {
|
||||
private document: TextDocument;
|
||||
private start: Position;
|
||||
private _eof: Position;
|
||||
private pos: Position;
|
||||
public pos: Position;
|
||||
private _eol: string;
|
||||
|
||||
/**
|
||||
|
||||
32
extensions/emmet/src/evaluateMathExpression.ts
Normal file
32
extensions/emmet/src/evaluateMathExpression.ts
Normal 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 it’s because of non-math expression
|
||||
console.warn('Math evaluation error', err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import { mergeLines } from './mergeLines';
|
||||
import { toggleComment } from './toggleComment';
|
||||
import { fetchEditPoint } from './editPoint';
|
||||
import { fetchSelectItem } from './selectItem';
|
||||
import { evaluateMathExpression } from './evaluateMathExpression';
|
||||
import { LANGUAGE_MODES, getMappedModes } from './util';
|
||||
import { updateExtensionsPath } from 'vscode-emmet-helper';
|
||||
|
||||
@@ -91,6 +92,10 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
fetchSelectItem('prev');
|
||||
}));
|
||||
|
||||
context.subscriptions.push(vscode.commands.registerCommand('emmet.evaluateMathExpression', () => {
|
||||
evaluateMathExpression();
|
||||
}));
|
||||
|
||||
updateExtensionsPath();
|
||||
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(() => {
|
||||
updateExtensionsPath();
|
||||
|
||||
Reference in New Issue
Block a user