Skip emmet when open tag is not closed Fixes #35128

This commit is contained in:
Ramya Achutha Rao
2017-12-03 18:14:07 -08:00
parent 526191661d
commit 8a12b294de
3 changed files with 95 additions and 38 deletions

View File

@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { HtmlNode } from 'EmmetNode';
import { HtmlNode, Node } from 'EmmetNode';
import { isValidLocationForEmmetAbbreviation } from './abbreviationActions';
import { getEmmetHelper, getNode, getInnerRange, getMappingForIncludedLanguages, parseDocument, getEmmetConfiguration, getEmmetMode, isStyleSheet } from './util';
@@ -23,10 +23,27 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi
const isSyntaxMapped = mappedLanguages[document.languageId] ? true : false;
let syntax = getEmmetMode((isSyntaxMapped ? mappedLanguages[document.languageId] : document.languageId), excludedLanguages);
const helper = getEmmetHelper();
const extractAbbreviationResults = helper.extractAbbreviation(document, position);
if (!extractAbbreviationResults) {
return;
}
// If document can be html/css parsed, validate syntax and location
if (document.languageId === 'html' || isStyleSheet(document.languageId)) {
// Document can be html/css parsed
// Use syntaxHelper to parse file, validate location and update sytnax if needed
syntax = this.syntaxHelper(syntax, document, position);
const rootNode = parseDocument(document, false);
if (!rootNode) {
return;
}
// Use syntaxHelper to update sytnax if needed
const currentNode = getNode(rootNode, position, true);
syntax = this.syntaxHelper(syntax, currentNode, position);
// Validate location
if (!syntax || !isValidLocationForEmmetAbbreviation(document, currentNode, syntax, position, extractAbbreviationResults.abbreviationRange)) {
return;
}
}
if (!syntax
@@ -35,23 +52,19 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi
return;
}
const helper = getEmmetHelper();
let noiseCheckPromise: Thenable<any> = Promise.resolve();
// Fix for https://github.com/Microsoft/vscode/issues/32647
// Check for document symbols in js/ts/jsx/tsx and avoid triggering emmet for abbreviations of the form symbolName.sometext
// Presence of > or * or + in the abbreviation denotes valid abbreviation that should trigger emmet
if (!isStyleSheet(syntax) && (document.languageId === 'javascript' || document.languageId === 'javascriptreact' || document.languageId === 'typescript' || document.languageId === 'typescriptreact')) {
let extractAbbreviationResults = helper.extractAbbreviation(document, position);
if (extractAbbreviationResults) {
let abbreviation: string = extractAbbreviationResults.abbreviation;
if (abbreviation.startsWith('this.')) {
noiseCheckPromise = Promise.resolve(true);
} else {
noiseCheckPromise = vscode.commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeDocumentSymbolProvider', document.uri).then((symbols: vscode.SymbolInformation[] | undefined) => {
return symbols && symbols.find(x => abbreviation === x.name || (abbreviation.startsWith(x.name + '.') && !/>|\*|\+/.test(abbreviation)));
});
}
let abbreviation: string = extractAbbreviationResults.abbreviation;
if (abbreviation.startsWith('this.')) {
noiseCheckPromise = Promise.resolve(true);
} else {
noiseCheckPromise = vscode.commands.executeCommand<vscode.SymbolInformation[]>('vscode.executeDocumentSymbolProvider', document.uri).then((symbols: vscode.SymbolInformation[] | undefined) => {
return symbols && symbols.find(x => abbreviation === x.name || (abbreviation.startsWith(x.name + '.') && !/>|\*|\+/.test(abbreviation)));
});
}
}
@@ -88,21 +101,11 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi
/**
* Parses given document to check whether given position is valid for emmet abbreviation and returns appropriate syntax
* @param syntax string language mode of current document
* @param document vscode.Textdocument
* @param currentNode node in the document that contains the position
* @param position vscode.Position position of the abbreviation that needs to be expanded
*/
private syntaxHelper(syntax: string | undefined, document: vscode.TextDocument, position: vscode.Position): string | undefined {
if (!syntax) {
return syntax;
}
let rootNode = parseDocument(document, false);
if (!rootNode) {
return;
}
let currentNode = getNode(rootNode, position, true);
if (!isStyleSheet(syntax)) {
private syntaxHelper(syntax: string | undefined, currentNode: Node | null, position: vscode.Position): string | undefined {
if (syntax && !isStyleSheet(syntax)) {
const currentHtmlNode = <HtmlNode>currentNode;
if (currentHtmlNode && currentHtmlNode.close) {
const innerRange = getInnerRange(currentHtmlNode);
@@ -121,9 +124,6 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi
}
}
if (!isValidLocationForEmmetAbbreviation(currentNode, syntax, position)) {
return;
}
return syntax;
}