Skip to content

Commit

Permalink
Avoid emmet expansion inside html tag and css selectors. Fixes #28829
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Jun 19, 2017
1 parent 47de9b4 commit fadd172
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions extensions/emmet/src/emmetCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

import * as vscode from 'vscode';
import { expand, createSnippetsRegistry } from '@emmetio/expand-abbreviation';
import { getSyntax, getProfile, extractAbbreviation, isStyleSheet } from './util';
import { getSyntax, getProfile, extractAbbreviation, isStyleSheet, getNode } from './util';
import parseStylesheet from '@emmetio/css-parser';
import parse from '@emmetio/html-matcher';
import Node from '@emmetio/node';
import { DocumentStreamReader } from './bufferStream';

const field = (index, placeholder) => `\${${index}${placeholder ? ':' + placeholder : ''}}`;
const snippetCompletionsCache = new Map<string, vscode.CompletionItem[]>();
Expand All @@ -22,7 +26,11 @@ export class EmmetCompletionItemProvider implements vscode.CompletionItemProvide
let completionItems: vscode.CompletionItem[] = [];
let syntax = getSyntax(document);
let currentWord = getCurrentWord(document, position);
let expandedAbbr = this.getExpandedAbbreviation(document, position);

let parseContent = isStyleSheet(syntax) ? parseStylesheet : parse;
let rootNode: Node = parseContent(new DocumentStreamReader(document));
let currentNode = getNode(rootNode, position);
let expandedAbbr = this.getExpandedAbbreviation(document, position, syntax, currentNode);

if (!isStyleSheet(syntax)) {
if (expandedAbbr) {
Expand All @@ -39,15 +47,18 @@ export class EmmetCompletionItemProvider implements vscode.CompletionItemProvide
return Promise.resolve(new vscode.CompletionList(completionItems, true));
}

getExpandedAbbreviation(document: vscode.TextDocument, position: vscode.Position): vscode.CompletionItem {
getExpandedAbbreviation(document: vscode.TextDocument, position: vscode.Position, syntax: string, currentNode: Node): vscode.CompletionItem {
if (!vscode.workspace.getConfiguration('emmet')['showExpandedAbbreviation']) {
return;
}
let [rangeToReplace, wordToExpand] = extractAbbreviation(position);
if (!rangeToReplace || !wordToExpand) {
return;
}
let syntax = getSyntax(document);
if (!isValidLocationForEmmetAbbreviation(currentNode, syntax, position)) {
return;
}

let expandedWord = expand(wordToExpand, {
field: field,
syntax: syntax,
Expand Down Expand Up @@ -114,6 +125,17 @@ function removeTabStops(expandedWord: string): string {
return expandedWord.replace(/\$\{\d+\}/g, '').replace(/\$\{\d+:([^\}]+)\}/g, '$1');
}

function isValidLocationForEmmetAbbreviation(currentNode: Node, syntax: string, position: vscode.Position): boolean {
if (!currentNode) {
return true;
}

if (isStyleSheet(syntax)) {
return currentNode.type !== 'rule';
}

return position.isAfter(currentNode.open.end);
}



0 comments on commit fadd172

Please sign in to comment.