Skip to content

Commit 7ddd029

Browse files
authored
(fix) update document logic (#971)
Due to the refactoring in #917, documents were reread from disk every time when getting a snapshot. This resulted in worse performance and introduced inconsistencies because "openDocument" of the DocumentManager was called over and over again with potentially outdated content, resulting in buggy, seemingly random behavior. This fix reverts that part of #917 which was accidentally introduced and only loads documents from disk if a snapshot doesn't already exist. #970
1 parent aa0e314 commit 7ddd029

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

packages/language-server/src/lib/documents/Document.ts

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export class Document extends WritableDocument {
5151
if (config && !config.loadConfigError) {
5252
update(config);
5353
} else {
54-
this.configPromise = configLoader.awaitConfig(this.getFilePath() || '');
5554
update(undefined);
5655
this.configPromise.then((c) => update(c));
5756
}

packages/language-server/src/plugins/typescript/LSAndTSDocResolver.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export class LSAndTSDocResolver {
8686
async getSnapshot(pathOrDoc: string | Document) {
8787
const filePath = typeof pathOrDoc === 'string' ? pathOrDoc : pathOrDoc.getFilePath() || '';
8888
const tsService = await this.getTSService(filePath);
89-
return tsService.updateDocument(pathOrDoc);
89+
return tsService.updateSnapshot(pathOrDoc);
9090
}
9191

9292
async updateSnapshotPath(oldPath: string, newPath: string): Promise<DocumentSnapshot> {
@@ -95,7 +95,7 @@ export class LSAndTSDocResolver {
9595
}
9696

9797
async deleteSnapshot(filePath: string) {
98-
(await this.getTSService(filePath)).deleteDocument(filePath);
98+
(await this.getTSService(filePath)).deleteSnapshot(filePath);
9999
this.docManager.releaseDocument(pathToUrl(filePath));
100100
}
101101

packages/language-server/src/plugins/typescript/service.ts

+31-21
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export interface LanguageServiceContainer {
1414
readonly compilerOptions: ts.CompilerOptions;
1515
readonly snapshotManager: SnapshotManager;
1616
getService(): ts.LanguageService;
17-
updateDocument(documentOrFilePath: Document | string): DocumentSnapshot;
18-
deleteDocument(filePath: string): void;
17+
updateSnapshot(documentOrFilePath: Document | string): DocumentSnapshot;
18+
deleteSnapshot(filePath: string): void;
1919
}
2020

2121
const services = new Map<string, Promise<LanguageServiceContainer>>();
@@ -125,36 +125,31 @@ async function createLanguageService(
125125
tsconfigPath,
126126
compilerOptions,
127127
getService: () => languageService,
128-
updateDocument,
129-
deleteDocument,
128+
updateSnapshot,
129+
deleteSnapshot,
130130
snapshotManager
131131
};
132132

133-
function deleteDocument(filePath: string): void {
133+
function deleteSnapshot(filePath: string): void {
134134
svelteModuleLoader.deleteFromModuleCache(filePath);
135135
snapshotManager.delete(filePath);
136136
}
137137

138-
function updateDocument(documentOrFilePath: Document | string): DocumentSnapshot {
139-
const filePath =
140-
typeof documentOrFilePath === 'string'
141-
? documentOrFilePath
142-
: documentOrFilePath.getFilePath() || '';
143-
const document = typeof documentOrFilePath === 'string' ? undefined : documentOrFilePath;
144-
const prevSnapshot = snapshotManager.get(filePath);
138+
function updateSnapshot(documentOrFilePath: Document | string): DocumentSnapshot {
139+
return typeof documentOrFilePath === 'string'
140+
? updateSnapshotFromFilePath(documentOrFilePath)
141+
: updateSnapshotFromDocument(documentOrFilePath);
142+
}
145143

146-
// Don't reinitialize document if no update needed.
147-
if (document && prevSnapshot?.version === document.version) {
144+
function updateSnapshotFromDocument(document: Document): DocumentSnapshot {
145+
const filePath = document.getFilePath() || '';
146+
const prevSnapshot = snapshotManager.get(filePath);
147+
if (prevSnapshot?.version === document.version) {
148148
return prevSnapshot;
149149
}
150150

151-
const newSnapshot = document
152-
? DocumentSnapshot.fromDocument(document, transformationConfig)
153-
: DocumentSnapshot.fromFilePath(
154-
filePath,
155-
docContext.createDocument,
156-
transformationConfig
157-
);
151+
const newSnapshot = DocumentSnapshot.fromDocument(document, transformationConfig);
152+
158153
snapshotManager.set(filePath, newSnapshot);
159154
if (prevSnapshot && prevSnapshot.scriptKind !== newSnapshot.scriptKind) {
160155
// Restart language service as it doesn't handle script kind changes.
@@ -165,6 +160,21 @@ async function createLanguageService(
165160
return newSnapshot;
166161
}
167162

163+
function updateSnapshotFromFilePath(filePath: string): DocumentSnapshot {
164+
const prevSnapshot = snapshotManager.get(filePath);
165+
if (prevSnapshot) {
166+
return prevSnapshot;
167+
}
168+
169+
const newSnapshot = DocumentSnapshot.fromFilePath(
170+
filePath,
171+
docContext.createDocument,
172+
transformationConfig
173+
);
174+
snapshotManager.set(filePath, newSnapshot);
175+
return newSnapshot;
176+
}
177+
168178
function getSnapshot(fileName: string): DocumentSnapshot {
169179
fileName = ensureRealSvelteFilePath(fileName);
170180

0 commit comments

Comments
 (0)