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 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,6 +21,7 @@ import {
isSvelteFilePath,
getTsCheckComment
} from './utils';
import { performance } from 'perf_hooks';

/**
* An error which occured while trying to parse/preprocess the svelte file contents.
Expand Down Expand Up @@ -163,6 +164,7 @@ function preprocessSvelteFile(document: Document, options: SvelteSnapshotOptions
: ts.ScriptKind.JSX;

try {
performance.mark('svelte2tsx');
const tsx = svelte2tsx(text, {
filename: document.getFilePath() ?? undefined,
isTsFile: scriptKind === ts.ScriptKind.TSX,
Expand All @@ -172,6 +174,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;
exportedNames = tsx.exportedNames;
Expand Down Expand Up @@ -248,8 +252,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 @@ -332,8 +343,8 @@ export class JSOrTSDocumentSnapshot
return this.text;
}

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

positionAt(offset: number) {
Expand Down Expand Up @@ -437,3 +448,42 @@ export class SvelteSnapshotFragment implements SnapshotFragment {
}
}
}

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)) {
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;
}
}