Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stylesheet partial parse #46376

Merged
merged 13 commits into from
Mar 27, 2018
4 changes: 2 additions & 2 deletions extensions/emmet/src/defaultCompletionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as vscode from 'vscode';
import { Node } from 'EmmetNode';
import { isValidLocationForEmmetAbbreviation } from './abbreviationActions';
import { getEmmetHelper, getNode, getMappingForIncludedLanguages, parseDocument, getEmmetConfiguration, getEmmetMode, isStyleSheet } from './util';
import { getEmmetHelper, getNode, getMappingForIncludedLanguages, parsePartialStylesheet, getEmmetConfiguration, getEmmetMode, isStyleSheet, parseDocument } from './util';

export class DefaultCompletionItemProvider implements vscode.CompletionItemProvider {

Expand Down Expand Up @@ -38,7 +38,7 @@ export class DefaultCompletionItemProvider implements vscode.CompletionItemProvi

// If document can be css parsed, get currentNode
if (isStyleSheet(document.languageId)) {
const rootNode = parseDocument(document, false);
const rootNode = document.lineCount > 1000 ? parsePartialStylesheet(document, position) : parseDocument(document, false);
if (!rootNode) {
return;
}
Expand Down
46 changes: 46 additions & 0 deletions extensions/emmet/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,52 @@ export function parseDocument(document: vscode.TextDocument, showError: boolean
return undefined;
}

export function parsePartialStylesheet(document: vscode.TextDocument, position: vscode.Position): Node | undefined {

let startPosition = new vscode.Position(0, 0);
let endPosition = new vscode.Position(document.lineCount - 1, document.lineAt(document.lineCount - 1).text.length);
const closeBrace = 125;
const openBrace = 123;

// Go forward until we found a closing or opening brace
let stream = new DocumentStreamReader(document, position);
stream.eatWhile(char => { return char !== closeBrace && char !== openBrace; });
stream.eat(closeBrace);
endPosition = stream.pos;

// Go back until we found an opening brace. If we find a closing one, we first find its opening brace and then we continue.
stream.pos = position;
let openBracesRemaining = 1;
while (openBracesRemaining > 0 && !stream.sof()) {
if (position.line - stream.pos.line > 1000) {
return parseStylesheet(new DocumentStreamReader(document, startPosition, new vscode.Range(startPosition, endPosition)));
}
let ch = stream.backUp(1);
if (ch === openBrace) {
openBracesRemaining--;
} else if (ch === closeBrace) {
if (document.languageId === 'css') {
stream.next();
return parseStylesheet(new DocumentStreamReader(document, stream.pos, new vscode.Range(stream.pos, endPosition)));
}
openBracesRemaining++;
}
}
// We are at an opening brace. We need to include its selector
while (stream.pos.character > 0) {
let ch = stream.backUp(1);
if (ch === closeBrace || ch === openBrace) {
Copy link
Contributor

@ramya-rao-a ramya-rao-a Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't really need the whole selector, we can stop when we find a non whitespace as well

stream.next();
break;
}
}
startPosition = stream.pos;
try {
return parseStylesheet(new DocumentStreamReader(document, startPosition, new vscode.Range(startPosition, endPosition)));
} catch (e) {
}
}

/**
* Returns node corresponding to given position in the given root node
*/
Expand Down