Update to VS Code 1.52.1
This commit is contained in:
@ -138,6 +138,7 @@ function doWrapping(individualLines: boolean, args: any) {
|
||||
newText = newText.replace(/\$\{[\d]*(:[^}]*)?\}/g, (match) => { // Replacing Placeholders
|
||||
return match.replace(/^\$\{[\d]*:/, '').replace('}', '');
|
||||
});
|
||||
newText = newText.replace(/\\\$/g, '$'); // Remove backslashes before $
|
||||
builder.replace(oldPreviewRange, newText);
|
||||
|
||||
const expandedTextLines = newText.split('\n');
|
||||
|
@ -124,6 +124,10 @@ export function activateEmmetExtension(context: vscode.ExtensionContext) {
|
||||
return reflectCssValue();
|
||||
}));
|
||||
|
||||
context.subscriptions.push(vscode.commands.registerCommand('workbench.action.showEmmetCommands', () => {
|
||||
vscode.commands.executeCommand('workbench.action.quickOpen', '>Emmet: ');
|
||||
}));
|
||||
|
||||
updateEmmetExtensionsPath();
|
||||
|
||||
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration((e) => {
|
||||
|
@ -6,24 +6,42 @@
|
||||
/* Based on @sergeche's work in his emmet plugin */
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import evaluate from '@emmetio/math-expression';
|
||||
import evaluate, { extract } from '@emmetio/math-expression';
|
||||
import { DocumentStreamReader } from './bufferStream';
|
||||
|
||||
export function evaluateMathExpression() {
|
||||
export function evaluateMathExpression(): Thenable<boolean> {
|
||||
if (!vscode.window.activeTextEditor) {
|
||||
vscode.window.showInformationMessage('No editor is active');
|
||||
return;
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
const stream = new DocumentStreamReader(editor.document);
|
||||
editor.edit(editBuilder => {
|
||||
return editor.edit(editBuilder => {
|
||||
editor.selections.forEach(selection => {
|
||||
const pos = selection.isReversed ? selection.anchor : selection.active;
|
||||
stream.pos = pos;
|
||||
// startpos always comes before endpos
|
||||
const startpos = selection.isReversed ? selection.active : selection.anchor;
|
||||
const endpos = selection.isReversed ? selection.anchor : selection.active;
|
||||
const selectionText = stream.substring(startpos, endpos);
|
||||
|
||||
try {
|
||||
const result = String(evaluate(stream, true));
|
||||
editBuilder.replace(new vscode.Range(stream.pos, pos), result);
|
||||
if (selectionText) {
|
||||
// respect selections
|
||||
const result = String(evaluate(selectionText));
|
||||
editBuilder.replace(new vscode.Range(startpos, endpos), result);
|
||||
} else {
|
||||
// no selection made, extract expression from line
|
||||
const lineToSelectionEnd = stream.substring(new vscode.Position(selection.end.line, 0), endpos);
|
||||
const extractedIndices = extract(lineToSelectionEnd);
|
||||
if (!extractedIndices) {
|
||||
throw new Error('Invalid extracted indices');
|
||||
}
|
||||
const result = String(evaluate(lineToSelectionEnd.substr(extractedIndices[0], extractedIndices[1])));
|
||||
const rangeToReplace = new vscode.Range(
|
||||
new vscode.Position(selection.end.line, extractedIndices[0]),
|
||||
new vscode.Position(selection.end.line, extractedIndices[1])
|
||||
);
|
||||
editBuilder.replace(rangeToReplace, result);
|
||||
}
|
||||
} catch (err) {
|
||||
vscode.window.showErrorMessage('Could not evaluate expression');
|
||||
// Ignore error since most likely it’s because of non-math expression
|
||||
@ -31,5 +49,4 @@ export function evaluateMathExpression() {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,52 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import 'mocha';
|
||||
import * as assert from 'assert';
|
||||
import { Position, Selection } from 'vscode';
|
||||
import { withRandomFileEditor, closeAllEditors } from './testUtils';
|
||||
import { evaluateMathExpression } from '../evaluateMathExpression';
|
||||
|
||||
suite('Tests for Evaluate Math Expression', () => {
|
||||
teardown(closeAllEditors);
|
||||
|
||||
function testEvaluateMathExpression(fileContents: string, selection: [number, number] | number, expectedFileContents: string): Thenable<boolean> {
|
||||
return withRandomFileEditor(fileContents, 'html', async (editor, _doc) => {
|
||||
const selectionToUse = typeof selection === 'number' ?
|
||||
new Selection(new Position(0, selection), new Position(0, selection)) :
|
||||
new Selection(new Position(0, selection[0]), new Position(0, selection[1]));
|
||||
editor.selection = selectionToUse;
|
||||
|
||||
await evaluateMathExpression();
|
||||
|
||||
assert.strictEqual(editor.document.getText(), expectedFileContents);
|
||||
return Promise.resolve();
|
||||
});
|
||||
}
|
||||
|
||||
test('Selected sanity check', () => {
|
||||
return testEvaluateMathExpression('1 + 2', [0, 5], '3');
|
||||
});
|
||||
|
||||
test('Selected with surrounding text', () => {
|
||||
return testEvaluateMathExpression('test1 + 2test', [4, 9], 'test3test');
|
||||
});
|
||||
|
||||
test('Selected with number not part of selection', () => {
|
||||
return testEvaluateMathExpression('test3 1+2', [6, 9], 'test3 3');
|
||||
});
|
||||
|
||||
test('Non-selected sanity check', () => {
|
||||
return testEvaluateMathExpression('1 + 2', 5, '3');
|
||||
});
|
||||
|
||||
test('Non-selected midway', () => {
|
||||
return testEvaluateMathExpression('1 + 2', 1, '1 + 2');
|
||||
});
|
||||
|
||||
test('Non-selected with surrounding text', () => {
|
||||
return testEvaluateMathExpression('test1 + 3test', 9, 'test4test');
|
||||
});
|
||||
});
|
@ -1,13 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
declare module '@emmetio/math-expression' {
|
||||
import { BufferStream } from 'EmmetNode';
|
||||
|
||||
function index(stream: BufferStream, backward: boolean): number;
|
||||
|
||||
export default index;
|
||||
}
|
||||
|
Reference in New Issue
Block a user