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

(perf) WIP: TS incremental parsing #1192

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
isSvelteFilePath,
getTsCheckComment
} from './utils';
import { performance } from 'perf_hooks';
import { Logger } from '../../logger';
import { dirname, resolve } from 'path';
import { URI } from 'vscode-uri';
Expand Down Expand Up @@ -200,6 +201,7 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions
: ts.ScriptKind.JS;

try {
performance.mark('svelte2tsx');
const tsx = svelte2tsx(text, {
parse: options.parse,
version: options.version,
Expand All @@ -213,6 +215,8 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions
document.config?.compilerOptions?.accessors ??
document.config?.compilerOptions?.customElement
});
performance.mark('svelte2tsx-end');
performance.measure('svelte2tsx', 'svelte2tsx', 'svelte2tsx-end');
text = tsx.code;
tsxMap = tsx.map as EncodedSourceMap;
exportedNames = tsx.exportedNames;
Expand Down Expand Up @@ -311,8 +315,15 @@ export class SvelteDocumentSnapshot implements DocumentSnapshot {
return this.text;
}

getChangeRange() {
return undefined;
getChangeRange(oldSnapshot: SvelteDocumentSnapshot) {
if (
(oldSnapshot.parserError && !this.parserError) ||
(!oldSnapshot.parserError && this.parserError)
) {
return undefined;
}

return getChangeRange(oldSnapshot, this);
}

positionAt(offset: number) {
Expand Down Expand Up @@ -476,8 +487,8 @@ export class JSOrTSDocumentSnapshot extends IdentityMapper implements DocumentSn
return this.text;
}

getChangeRange() {
return undefined;
getChangeRange(oldSnapshot: JSOrTSDocumentSnapshot) {
return getChangeRange(oldSnapshot, this);
}

positionAt(offset: number) {
Expand Down Expand Up @@ -786,3 +797,42 @@ function tryParseRawSourceMap(text: string) {

return undefined;
}

function getChangeRange(oldSnapshot: DocumentSnapshot, currentSnapshot: DocumentSnapshot) {
try {
if (oldSnapshot.version >= currentSnapshot.version) {
return undefined;
}

const oldText = oldSnapshot.getFullText();
const currentText = currentSnapshot.getFullText();

let diffStart = oldText.length - 1;
for (let i = 0; i < currentText.length; i++) {
if (oldText.charAt(i) !== currentText.charAt(i)) {

Choose a reason for hiding this comment

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

It's probably better to use charCodeAt. chatAt will likely allocate intermediate strings.

diffStart = i;
break;
}
}

let lengthDiff = currentText.length - oldText.length;
let diffEnd = diffStart + 1;
for (let i = currentText.length - 1; i > diffStart; i--) {
if (oldText.charAt(i - lengthDiff) !== currentText.charAt(i)) {
diffEnd = i + 1;
break;
}
}

if (diffStart === 0 && diffEnd >= currentText.length - 1) {
return undefined;
} else {
return ts.createTextChangeRange(
ts.createTextSpan(diffStart, diffEnd - diffStart - lengthDiff),
diffEnd - diffStart
);
}
} catch (e) {
return undefined;
}
}
Loading