Emmet flatten DocumentStreamReader (#113602)

This commit is contained in:
Raymond Zhao 2020-12-30 11:00:21 -08:00 committed by GitHub
parent b84858babe
commit 56c808a66a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 84 deletions

View file

@ -5,7 +5,7 @@
/* Based on @sergeche's work in his emmet plugin */ /* Based on @sergeche's work in his emmet plugin */
import { TextDocument, Position, Range, EndOfLine } from 'vscode'; import { TextDocument } from 'vscode';
/** /**
* A stream reader for VSCode's `TextDocument` * A stream reader for VSCode's `TextDocument`
@ -13,40 +13,37 @@ import { TextDocument, Position, Range, EndOfLine } from 'vscode';
*/ */
export class DocumentStreamReader { export class DocumentStreamReader {
private document: TextDocument; private document: TextDocument;
private start: Position; private start: number;
private _eof: Position; private _eof: number;
private _sof: Position; private _sof: number;
public pos: Position; public pos: number;
private _eol: string;
constructor(document: TextDocument, pos?: Position, limit?: Range) {
constructor(document: TextDocument, pos?: number, limit?: [number, number]) {
this.document = document; this.document = document;
this.start = this.pos = pos ? pos : new Position(0, 0); this.start = this.pos = pos ? pos : 0;
this._sof = limit ? limit.start : new Position(0, 0); this._sof = limit ? limit[0] : 0;
this._eof = limit ? limit.end : new Position(this.document.lineCount - 1, this._lineLength(this.document.lineCount - 1)); this._eof = limit ? limit[1] : document.getText().length;
this._eol = this.document.eol === EndOfLine.LF ? '\n' : '\r\n';
} }
/** /**
* Returns true only if the stream is at the start of the file. * Returns true only if the stream is at the start of the file.
*/ */
sof(): boolean { sof(): boolean {
return this.pos.isBeforeOrEqual(this._sof); return this.pos <= this._sof;
} }
/** /**
* Returns true only if the stream is at the end of the file. * Returns true only if the stream is at the end of the file.
*/ */
eof(): boolean { eof(): boolean {
return this.pos.isAfterOrEqual(this._eof); return this.pos >= this._eof;
} }
/** /**
* Creates a new stream instance which is limited to given range for given document * Creates a new stream instance which is limited to given range for given document
*/ */
limit(start: Position, end: Position): DocumentStreamReader { limit(start: number, end: number): DocumentStreamReader {
return new DocumentStreamReader(this.document, start, new Range(start, end)); return new DocumentStreamReader(this.document, start, [start, end]);
} }
/** /**
@ -57,8 +54,7 @@ export class DocumentStreamReader {
if (this.eof()) { if (this.eof()) {
return NaN; return NaN;
} }
const line = this.document.lineAt(this.pos.line).text; return this.document.getText().charCodeAt(this.pos);
return this.pos.character < line.length ? line.charCodeAt(this.pos.character) : this._eol.charCodeAt(this.pos.character - line.length);
} }
/** /**
@ -70,19 +66,12 @@ export class DocumentStreamReader {
return NaN; return NaN;
} }
const line = this.document.lineAt(this.pos.line).text; const code = this.document.getText().charCodeAt(this.pos);
let code: number; this.pos++;
if (this.pos.character < line.length) {
code = line.charCodeAt(this.pos.character);
this.pos = this.pos.translate(0, 1);
} else {
code = this._eol.charCodeAt(this.pos.character - line.length);
this.pos = new Position(this.pos.line + 1, 0);
}
if (this.eof()) { if (this.eof()) {
// restrict pos to eof, if in case it got moved beyond eof // restrict pos to eof, if in case it got moved beyond eof
this.pos = new Position(this._eof.line, this._eof.character); this.pos = this._eof;
} }
return code; return code;
@ -92,20 +81,11 @@ export class DocumentStreamReader {
* Backs up the stream n characters. Backing it up further than the * Backs up the stream n characters. Backing it up further than the
* start of the current token will cause things to break, so be careful. * start of the current token will cause things to break, so be careful.
*/ */
backUp(n: number) { backUp(n: number): number {
let row = this.pos.line; this.pos -= n;
let column = this.pos.character; if (this.pos < 0) {
column -= (n || 1); this.pos = 0;
while (row >= 0 && column < 0) {
row--;
column += this._lineLength(row);
} }
this.pos = row < 0 || column < 0
? new Position(0, 0)
: new Position(row, column);
return this.peek(); return this.peek();
} }
@ -120,29 +100,18 @@ export class DocumentStreamReader {
/** /**
* Returns contents for given range * Returns contents for given range
*/ */
substring(from: Position, to: Position): string { substring(from: number, to: number): string {
return this.document.getText(new Range(from, to)); return this.document.getText().substring(from, to);
} }
/** /**
* Creates error object with current stream state * Creates error object with current stream state
*/ */
error(message: string): Error { error(message: string): Error {
const err = new Error(`${message} at row ${this.pos.line}, column ${this.pos.character}`); const err = new Error(`${message} at offset ${this.pos}`);
return err; return err;
} }
/**
* Returns line length of given row, including line ending
*/
_lineLength(row: number): number {
if (row === this.document.lineCount - 1) {
return this.document.lineAt(row).text.length;
}
return this.document.lineAt(row).text.length + this._eol.length;
}
/** /**
* `match` can be a character code or a function that takes a character code * `match` can be a character code or a function that takes a character code
* and returns a boolean. If the next character in the stream 'matches' * and returns a boolean. If the next character in the stream 'matches'
@ -167,6 +136,6 @@ export class DocumentStreamReader {
eatWhile(match: number | Function): boolean { eatWhile(match: number | Function): boolean {
const start = this.pos; const start = this.pos;
while (!this.eof() && this.eat(match)) { } while (!this.eof() && this.eat(match)) { }
return !this.pos.isEqual(start); return this.pos !== start;
} }
} }

View file

@ -150,37 +150,39 @@ const star = 42;
*/ */
export function parsePartialStylesheet(document: vscode.TextDocument, position: vscode.Position): FlatStylesheet | undefined { export function parsePartialStylesheet(document: vscode.TextDocument, position: vscode.Position): FlatStylesheet | undefined {
const isCSS = document.languageId === 'css'; const isCSS = document.languageId === 'css';
let startPosition = new vscode.Position(0, 0); const positionOffset = document.offsetAt(position);
let endPosition = new vscode.Position(document.lineCount - 1, document.lineAt(document.lineCount - 1).text.length); let startOffset = 0;
const limitCharacter = document.offsetAt(position) - 5000; let endOffset = document.getText().length;
const limitPosition = limitCharacter > 0 ? document.positionAt(limitCharacter) : startPosition; const limitCharacter = positionOffset - 5000;
const stream = new DocumentStreamReader(document, position); const limitOffset = limitCharacter > 0 ? limitCharacter : startOffset;
const stream = new DocumentStreamReader(document, positionOffset);
function findOpeningCommentBeforePosition(pos: vscode.Position): vscode.Position | undefined { function findOpeningCommentBeforePosition(pos: number): number | undefined {
let text = document.getText(new vscode.Range(0, 0, pos.line, pos.character)); const text = document.getText().substring(0, pos);
let offset = text.lastIndexOf('/*'); let offset = text.lastIndexOf('/*');
if (offset === -1) { if (offset === -1) {
return; return;
} }
return document.positionAt(offset); return offset;
} }
function findClosingCommentAfterPosition(pos: vscode.Position): vscode.Position | undefined { function findClosingCommentAfterPosition(pos: number): number | undefined {
let text = document.getText(new vscode.Range(pos.line, pos.character, document.lineCount - 1, document.lineAt(document.lineCount - 1).text.length)); const text = document.getText().substring(pos);
let offset = text.indexOf('*/'); let offset = text.indexOf('*/');
if (offset === -1) { if (offset === -1) {
return; return;
} }
offset += 2 + document.offsetAt(pos); offset += 2 + pos;
return document.positionAt(offset); return offset;
} }
function consumeLineCommentBackwards() { function consumeLineCommentBackwards() {
if (!isCSS && currentLine !== stream.pos.line) { const posLineNumber = document.positionAt(stream.pos).line;
currentLine = stream.pos.line; if (!isCSS && currentLine !== posLineNumber) {
let startLineComment = document.lineAt(currentLine).text.indexOf('//'); currentLine = posLineNumber;
const startLineComment = document.lineAt(currentLine).text.indexOf('//');
if (startLineComment > -1) { if (startLineComment > -1) {
stream.pos = new vscode.Position(currentLine, startLineComment); stream.pos = document.offsetAt(new vscode.Position(currentLine, startLineComment));
} }
} }
} }
@ -188,7 +190,7 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
function consumeBlockCommentBackwards() { function consumeBlockCommentBackwards() {
if (stream.peek() === slash) { if (stream.peek() === slash) {
if (stream.backUp(1) === star) { if (stream.backUp(1) === star) {
stream.pos = findOpeningCommentBeforePosition(stream.pos) || startPosition; stream.pos = findOpeningCommentBeforePosition(stream.pos) ?? startOffset;
} else { } else {
stream.next(); stream.next();
} }
@ -198,9 +200,10 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
function consumeCommentForwards() { function consumeCommentForwards() {
if (stream.eat(slash)) { if (stream.eat(slash)) {
if (stream.eat(slash) && !isCSS) { if (stream.eat(slash) && !isCSS) {
stream.pos = new vscode.Position(stream.pos.line + 1, 0); const posLineNumber = document.positionAt(stream.pos).line;
stream.pos = document.offsetAt(new vscode.Position(posLineNumber + 1, 0));
} else if (stream.eat(star)) { } else if (stream.eat(star)) {
stream.pos = findClosingCommentAfterPosition(stream.pos) || endPosition; stream.pos = findClosingCommentAfterPosition(stream.pos) ?? endOffset;
} }
} }
} }
@ -215,10 +218,10 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
} }
if (!stream.eof()) { if (!stream.eof()) {
endPosition = stream.pos; endOffset = stream.pos;
} }
stream.pos = position; stream.pos = positionOffset;
let openBracesToFind = 1; let openBracesToFind = 1;
let currentLine = position.line; let currentLine = position.line;
let exit = false; let exit = false;
@ -234,7 +237,7 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
case closeBrace: case closeBrace:
if (isCSS) { if (isCSS) {
stream.next(); stream.next();
startPosition = stream.pos; startOffset = stream.pos;
exit = true; exit = true;
} else { } else {
openBracesToFind++; openBracesToFind++;
@ -247,17 +250,17 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
break; break;
} }
if (position.line - stream.pos.line > 100 || stream.pos.isBeforeOrEqual(limitPosition)) { if (position.line - document.positionAt(stream.pos).line > 100
|| stream.pos <= limitOffset) {
exit = true; exit = true;
} }
} }
// We are at an opening brace. We need to include its selector. // We are at an opening brace. We need to include its selector.
currentLine = stream.pos.line; currentLine = document.positionAt(stream.pos).line;
openBracesToFind = 0; openBracesToFind = 0;
let foundSelector = false; let foundSelector = false;
while (!exit && !stream.sof() && !foundSelector && openBracesToFind >= 0) { while (!exit && !stream.sof() && !foundSelector && openBracesToFind >= 0) {
consumeLineCommentBackwards(); consumeLineCommentBackwards();
const ch = stream.backUp(1); const ch = stream.backUp(1);
@ -283,13 +286,11 @@ export function parsePartialStylesheet(document: vscode.TextDocument, position:
} }
if (!stream.sof() && foundSelector) { if (!stream.sof() && foundSelector) {
startPosition = stream.pos; startOffset = stream.pos;
} }
} }
try { try {
const startOffset = document.offsetAt(startPosition);
const endOffset = document.offsetAt(endPosition);
const buffer = ' '.repeat(startOffset) + document.getText().substring(startOffset, endOffset); const buffer = ' '.repeat(startOffset) + document.getText().substring(startOffset, endOffset);
return parseStylesheet(buffer); return parseStylesheet(buffer);
} catch (e) { } catch (e) {