Skip to content

Commit

Permalink
Emmet Error handling Fixes #29897
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Jun 30, 2017
1 parent e7de8b2 commit 0ee00cb
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 106 deletions.
6 changes: 3 additions & 3 deletions extensions/emmet/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion extensions/emmet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@
"@emmetio/html-matcher": "^0.3.1",
"@emmetio/css-parser": "^0.3.0",
"@emmetio/math-expression": "^0.1.1",
"vscode-emmet-helper":"0.0.16"
"vscode-emmet-helper":"0.0.17"
}
}
24 changes: 15 additions & 9 deletions extensions/emmet/src/abbreviationActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

import * as vscode from 'vscode';
import { expand } from '@emmetio/expand-abbreviation';
import parseStylesheet from '@emmetio/css-parser';
import parse from '@emmetio/html-matcher';
import { Node, HtmlNode, Rule } from 'EmmetNode';
import { getNode, getInnerRange, getMappingForIncludedLanguages } from './util';
import { getNode, getInnerRange, getMappingForIncludedLanguages, parse, validate } from './util';
import { getExpandOptions, extractAbbreviation, isStyleSheet, isAbbreviationValid, getEmmetMode } from 'vscode-emmet-helper';
import { DocumentStreamReader } from './bufferStream';

interface ExpandAbbreviationInput {
syntax: string;
Expand All @@ -22,7 +19,7 @@ interface ExpandAbbreviationInput {

export function wrapWithAbbreviation(args) {
const syntax = getSyntaxFromArgs(args);
if (!syntax) {
if (!syntax || !validate()) {
return;
}

Expand Down Expand Up @@ -79,14 +76,16 @@ export function wrapWithAbbreviation(args) {

export function expandAbbreviation(args) {
const syntax = getSyntaxFromArgs(args);
if (!syntax) {
if (!syntax || !validate()) {
return;
}

const editor = vscode.window.activeTextEditor;

let parseContent = isStyleSheet(syntax) ? parseStylesheet : parse;
let rootNode: Node = parseContent(new DocumentStreamReader(editor.document));
let rootNode = parse(editor.document);
if (!rootNode) {
return;
}

let abbreviationList: ExpandAbbreviationInput[] = [];
let firstAbbreviation: string;
Expand All @@ -100,6 +99,7 @@ export function expandAbbreviation(args) {
[rangeToReplace, abbreviation] = extractAbbreviation(editor.document, position);
}
if (!isAbbreviationValid(syntax, abbreviation)) {
vscode.window.showErrorMessage('Emmet: Invalid abbreviation');
return;
}

Expand Down Expand Up @@ -193,7 +193,13 @@ function expandAbbreviationInRange(editor: vscode.TextEditor, expandAbbrList: Ex
*/
function expandAbbr(input: ExpandAbbreviationInput, newLine: string): string {
// Expand the abbreviation
let expandedText = expand(input.abbreviation, getExpandOptions(input.syntax, input.textToWrap));
let expandedText;
try {
expandedText = expand(input.abbreviation, getExpandOptions(input.syntax, input.textToWrap));
} catch (e) {
vscode.window.showErrorMessage('Failed to expand abbreviation');
}

if (!expandedText) {
return;
}
Expand Down
15 changes: 4 additions & 11 deletions extensions/emmet/src/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import parse from '@emmetio/html-matcher';
import { HtmlNode } from 'EmmetNode';
import { DocumentStreamReader } from './bufferStream';
import { isStyleSheet } from 'vscode-emmet-helper';
import { getNode } from './util';
import { getNode, parse, validate } from './util';

export function balanceOut() {
balance(true);
Expand All @@ -20,20 +17,16 @@ export function balanceIn() {

function balance(out: boolean) {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
if (!validate(false)) {
return;
}
if (isStyleSheet(editor.document.languageId)) {
return;
}
let getRangeFunction = out ? getRangeToBalanceOut : getRangeToBalanceIn;

let rootNode: HtmlNode = parse(new DocumentStreamReader(editor.document));
let rootNode = <HtmlNode>parse(editor.document);
if (!rootNode) {
return;
}

let getRangeFunction = out ? getRangeToBalanceOut : getRangeToBalanceIn;
let newSelections: vscode.Selection[] = [];
editor.selections.forEach(selection => {
let range = getRangeFunction(editor.document, selection, rootNode);
Expand Down
14 changes: 7 additions & 7 deletions extensions/emmet/src/defaultCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import parseStylesheet from '@emmetio/css-parser';
import parse from '@emmetio/html-matcher';
import { Node, HtmlNode } from 'EmmetNode';
import { DocumentStreamReader } from './bufferStream';
import { HtmlNode } from 'EmmetNode';
import { EmmetCompletionItemProvider, isStyleSheet, getEmmetMode } from 'vscode-emmet-helper';
import { isValidLocationForEmmetAbbreviation } from './abbreviationActions';
import { getNode, getInnerRange, getMappingForIncludedLanguages } from './util';
import { getNode, getInnerRange, getMappingForIncludedLanguages, parse } from './util';

export class DefaultCompletionItemProvider implements vscode.CompletionItemProvider {

Expand Down Expand Up @@ -46,8 +43,11 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi
if (!syntax) {
return syntax;
}
let parseContent = isStyleSheet(syntax) ? parseStylesheet : parse;
let rootNode: Node = parseContent(new DocumentStreamReader(document));
let rootNode = parse(document, false);
if (!rootNode) {
return;
}

let currentNode = getNode(rootNode, position);

if (!isStyleSheet(syntax)) {
Expand Down
13 changes: 7 additions & 6 deletions extensions/emmet/src/matchTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import parse from '@emmetio/html-matcher';
import { HtmlNode } from 'EmmetNode';
import { DocumentStreamReader } from './bufferStream';
import { getNode } from './util';
import { getNode, parse, validate } from './util';

export function matchTag() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
if (!validate(false)) {
return;
}

let rootNode: HtmlNode = parse(new DocumentStreamReader(editor.document));
let rootNode = <HtmlNode>parse(editor.document);
if (!rootNode) {
return;
}

let updatedSelections = [];
editor.selections.forEach(selection => {
let updatedSelection = getUpdatedSelections(editor, selection.start, rootNode);
Expand All @@ -35,6 +33,9 @@ export function matchTag() {

function getUpdatedSelections(editor: vscode.TextEditor, position: vscode.Position, rootNode: HtmlNode): vscode.Selection {
let currentNode = <HtmlNode>getNode(rootNode, position, true);
if (!currentNode) {
return;
}

// If no closing tag or cursor is between open and close tag, then no-op
if (!currentNode.close || (position.isAfter(currentNode.open.end) && position.isBefore(currentNode.close.start))) {
Expand Down
22 changes: 11 additions & 11 deletions extensions/emmet/src/mergeLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,26 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import parse from '@emmetio/html-matcher';
import { Node } from 'EmmetNode';
import { DocumentStreamReader } from './bufferStream';
import { isStyleSheet } from 'vscode-emmet-helper';
import { getNode } from './util';
import { getNode, parse, validate } from './util';

export function mergeLines() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
return;
}
if (isStyleSheet(editor.document.languageId)) {
if (!validate(false)) {
return;
}

let rootNode: Node = parse(new DocumentStreamReader(editor.document));
let rootNode = parse(editor.document);
if (!rootNode) {
return;
}

editor.edit(editBuilder => {
editor.selections.reverse().forEach(selection => {
let [rangeToReplace, textToReplaceWith] = getRangesToReplace(editor.document, selection, rootNode);
editBuilder.replace(rangeToReplace, textToReplaceWith);
if (rangeToReplace && textToReplaceWith) {
editBuilder.replace(rangeToReplace, textToReplaceWith);
}
});
});
}
Expand All @@ -43,6 +39,10 @@ function getRangesToReplace(document: vscode.TextDocument, selection: vscode.Sel
endNodeToUpdate = getNode(rootNode, selection.end, true);
}

if (!startNodeToUpdate || !endNodeToUpdate) {
return [null, null];
}

let rangeToReplace = new vscode.Range(startNodeToUpdate.start, endNodeToUpdate.end);
let textToReplaceWith = document.getText(rangeToReplace).replace(/\r\n|\n/g, '').replace(/>\s*</g, '><');

Expand Down
29 changes: 22 additions & 7 deletions extensions/emmet/src/removeTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { getOpenCloseRange } from './util';
import { parse, validate, getNode } from './util';
import { HtmlNode } from 'EmmetNode';

export function removeTag() {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('No editor is active');
if (!validate(false)) {
return;
}

let rootNode = <HtmlNode>parse(editor.document);
if (!rootNode) {
return;
}

Expand All @@ -20,7 +25,7 @@ export function removeTag() {

let rangesToRemove = [];
editor.selections.reverse().forEach(selection => {
rangesToRemove = rangesToRemove.concat(getRangeToRemove(editor, selection, indentInSpaces));
rangesToRemove = rangesToRemove.concat(getRangeToRemove(editor, rootNode, selection, indentInSpaces));
});

editor.edit(editBuilder => {
Expand All @@ -30,8 +35,19 @@ export function removeTag() {
});
}

function getRangeToRemove(editor: vscode.TextEditor, selection: vscode.Selection, indentInSpaces: string): vscode.Range[] {
let [openRange, closeRange] = getOpenCloseRange(editor.document, selection.start);
function getRangeToRemove(editor: vscode.TextEditor, rootNode: HtmlNode, selection: vscode.Selection, indentInSpaces: string): vscode.Range[] {

let nodeToUpdate = <HtmlNode>getNode(rootNode, selection.start);
if (!nodeToUpdate) {
return [];
}

let openRange = new vscode.Range(nodeToUpdate.open.start, nodeToUpdate.open.end);
let closeRange = null;
if (nodeToUpdate.close) {
closeRange = new vscode.Range(nodeToUpdate.close.start, nodeToUpdate.close.end);
}

if (!openRange.contains(selection.start) && !closeRange.contains(selection.start)) {
return [];
}
Expand All @@ -50,4 +66,3 @@ function getRangeToRemove(editor: vscode.TextEditor, selection: vscode.Selection
return ranges;
}


12 changes: 3 additions & 9 deletions extensions/emmet/src/selectItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
*--------------------------------------------------------------------------------------------*/

import * as vscode from 'vscode';
import { validate } from './util';
import { validate, parse } from './util';
import { nextItemHTML, prevItemHTML } from './selectItemHTML';
import { nextItemStylesheet, prevItemStylesheet } from './selectItemStylesheet';
import parseStylesheet from '@emmetio/css-parser';
import parse from '@emmetio/html-matcher';
import { Node } from 'EmmetNode';
import { DocumentStreamReader } from './bufferStream';
import { isStyleSheet } from 'vscode-emmet-helper';


Expand All @@ -22,22 +18,20 @@ export function fetchSelectItem(direction: string): void {

let nextItem;
let prevItem;
let parseContent;

if (isStyleSheet(editor.document.languageId)) {
nextItem = nextItemStylesheet;
prevItem = prevItemStylesheet;
parseContent = parseStylesheet;
} else {
nextItem = nextItemHTML;
prevItem = prevItemHTML;
parseContent = parse;
}

let rootNode: Node = parseContent(new DocumentStreamReader(editor.document));
let rootNode = parse(editor.document);
if (!rootNode) {
return;
}

let newSelections: vscode.Selection[] = [];
editor.selections.forEach(selection => {
const selectionStart = selection.isReversed ? selection.active : selection.anchor;
Expand Down
8 changes: 8 additions & 0 deletions extensions/emmet/src/selectItemHTML.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function nextItemHTML(selectionStart: vscode.Position, selectionEnd: vsco
let currentNode = <HtmlNode>getNode(rootNode, selectionEnd);
let nextNode: HtmlNode;

if (!currentNode) {
return;
}

if (currentNode.type !== 'comment') {
// If cursor is in the tag name, select tag
if (selectionEnd.isBefore(currentNode.open.start.translate(0, currentNode.name.length))) {
Expand Down Expand Up @@ -53,6 +57,10 @@ export function prevItemHTML(selectionStart: vscode.Position, selectionEnd: vsco
let currentNode = <HtmlNode>getNode(rootNode, selectionStart);
let prevNode: HtmlNode;

if (!currentNode) {
return;
}

if (currentNode.type !== 'comment' && selectionStart.translate(0, -1).isAfter(currentNode.open.start)) {

if (selectionStart.isBefore(currentNode.open.end) || !currentNode.firstChild) {
Expand Down
7 changes: 6 additions & 1 deletion extensions/emmet/src/selectItemStylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export function nextItemStylesheet(startOffset: vscode.Position, endOffset: vsco
if (!currentNode) {
currentNode = <CssNode>rootNode;
}

if (!currentNode) {
return;
}
// Full property is selected, so select full property value next
if (currentNode.type === 'property' && startOffset.isEqual(currentNode.start) && endOffset.isEqual(currentNode.end)) {
return getSelectionFromProperty(currentNode, editor.document, startOffset, endOffset, true, 'next');
Expand Down Expand Up @@ -53,6 +55,9 @@ export function prevItemStylesheet(startOffset: vscode.Position, endOffset: vsco
if (!currentNode) {
currentNode = rootNode;
}
if (!currentNode) {
return;
}

// Full property value is selected, so select the whole property next
if (currentNode.type === 'property' && startOffset.isEqual((<Property>currentNode).valueToken.start) && endOffset.isEqual((<Property>currentNode).valueToken.end)) {
Expand Down
Loading

0 comments on commit 0ee00cb

Please sign in to comment.