-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Stylesheet partial parse #46376
Changes from 8 commits
787904e
8f796cb
dea5096
55a4ed3
e7dc7f8
e9089d4
f0c6efa
ae81a1d
7b4d979
3536668
a7a1b17
0dddad3
39fb6a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,97 @@ 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; | ||
let slash = 47; | ||
let star = 42; | ||
|
||
// Go forward until we found a closing brace. | ||
let stream = new DocumentStreamReader(document, position); | ||
while (!stream.eof()) { | ||
if (stream.eat(closeBrace)) { | ||
break; | ||
} else if (stream.eat(slash)) { | ||
if (stream.eat(slash) && document.languageId !== 'css') { | ||
// Single line Comment, we continue searching from next line. | ||
stream.pos = new vscode.Position(stream.pos.line + 1, 0); | ||
} else if (stream.eat(star)) { | ||
// Start of block comment, we need to find the closing '*/' | ||
let endCommentFound = false; | ||
while (!endCommentFound) { | ||
stream.eatWhile(char => { return char !== star; }); | ||
stream.eat(star); | ||
endCommentFound = stream.eat(slash); | ||
} | ||
} | ||
} else { | ||
stream.next(); | ||
} | ||
} | ||
|
||
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; | ||
let currentLine = position.line; | ||
let isCSS = document.languageId === 'css'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be pulled up as it can be used when determining the single line comments above |
||
|
||
while (openBracesRemaining > 0 && !stream.sof()) { | ||
if (position.line - stream.pos.line > 1000) { | ||
return parseStylesheet(new DocumentStreamReader(document, startPosition, new vscode.Range(startPosition, endPosition))); | ||
} else if (!isCSS && stream.pos.line !== currentLine) { | ||
// In not CSS stylesheets, we need to skip singleLine comments. | ||
currentLine = stream.pos.line; | ||
let startLineComment = document.lineAt(currentLine).text.indexOf('//'); | ||
if (startLineComment > -1) { | ||
stream.pos = new vscode.Position(currentLine, startLineComment); | ||
} | ||
} | ||
let ch = stream.backUp(1); | ||
if (ch === openBrace) { | ||
openBracesRemaining--; | ||
} else if (ch === closeBrace) { | ||
if (isCSS) { | ||
stream.next(); | ||
return parseStylesheet(new DocumentStreamReader(document, stream.pos, new vscode.Range(stream.pos, endPosition))); | ||
} | ||
openBracesRemaining++; | ||
} else if (ch === slash) { | ||
stream.backUp(1); | ||
if (!stream.eat(char => { return char !== star; })) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is emulating a |
||
// Closing block comment. We need to find the corresponding opening '*/' | ||
stream.pos = findOpeningComment(document, stream.pos); | ||
} | ||
} | ||
} | ||
// We are at an opening brace. We need to include its selector, but with one nonspace character is enough. | ||
while (!stream.sof()) { | ||
let ch = stream.backUp(1); | ||
if (ch === closeBrace || ch === openBrace) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} else if (!String.fromCharCode(ch).match(/\s/)) { | ||
break; | ||
} | ||
} | ||
startPosition = stream.pos; | ||
try { | ||
return parseStylesheet(new DocumentStreamReader(document, startPosition, new vscode.Range(startPosition, endPosition))); | ||
} catch (e) { | ||
} | ||
} | ||
|
||
function findOpeningComment(document: vscode.TextDocument, position: vscode.Position): vscode.Position { | ||
let text = document.getText(new vscode.Range(0, 0, position.line, position.character)); | ||
let offset = text.lastIndexOf('/*'); | ||
return document.positionAt(offset); | ||
} | ||
|
||
/** | ||
* Returns node corresponding to given position in the given root node | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we combine this
if
with thewhile
?while (!stream.eof() && !stream.eat(closeBrace))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can