Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion packages/language-server/src/lib/documents/Document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ export class Document extends WritableDocument {
if (config && !config.loadConfigError) {
update(config);
} else {
this.configPromise = configLoader.awaitConfig(this.getFilePath() || '');
update(undefined);
this.configPromise.then((c) => update(c));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class LSAndTSDocResolver {
async getSnapshot(pathOrDoc: string | Document) {
const filePath = typeof pathOrDoc === 'string' ? pathOrDoc : pathOrDoc.getFilePath() || '';
const tsService = await this.getTSService(filePath);
return tsService.updateDocument(pathOrDoc);
return tsService.updateSnapshot(pathOrDoc);
}

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

async deleteSnapshot(filePath: string) {
(await this.getTSService(filePath)).deleteDocument(filePath);
(await this.getTSService(filePath)).deleteSnapshot(filePath);
this.docManager.releaseDocument(pathToUrl(filePath));
}

Expand Down
52 changes: 31 additions & 21 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export interface LanguageServiceContainer {
readonly compilerOptions: ts.CompilerOptions;
readonly snapshotManager: SnapshotManager;
getService(): ts.LanguageService;
updateDocument(documentOrFilePath: Document | string): DocumentSnapshot;
deleteDocument(filePath: string): void;
updateSnapshot(documentOrFilePath: Document | string): DocumentSnapshot;
deleteSnapshot(filePath: string): void;
}

const services = new Map<string, Promise<LanguageServiceContainer>>();
Expand Down Expand Up @@ -125,36 +125,31 @@ async function createLanguageService(
tsconfigPath,
compilerOptions,
getService: () => languageService,
updateDocument,
deleteDocument,
updateSnapshot,
deleteSnapshot,
snapshotManager
};

function deleteDocument(filePath: string): void {
function deleteSnapshot(filePath: string): void {
svelteModuleLoader.deleteFromModuleCache(filePath);
snapshotManager.delete(filePath);
}

function updateDocument(documentOrFilePath: Document | string): DocumentSnapshot {
const filePath =
typeof documentOrFilePath === 'string'
? documentOrFilePath
: documentOrFilePath.getFilePath() || '';
const document = typeof documentOrFilePath === 'string' ? undefined : documentOrFilePath;
const prevSnapshot = snapshotManager.get(filePath);
function updateSnapshot(documentOrFilePath: Document | string): DocumentSnapshot {
return typeof documentOrFilePath === 'string'
? updateSnapshotFromFilePath(documentOrFilePath)
: updateSnapshotFromDocument(documentOrFilePath);
}

// Don't reinitialize document if no update needed.
if (document && prevSnapshot?.version === document.version) {
function updateSnapshotFromDocument(document: Document): DocumentSnapshot {
const filePath = document.getFilePath() || '';
const prevSnapshot = snapshotManager.get(filePath);
if (prevSnapshot?.version === document.version) {
return prevSnapshot;
}

const newSnapshot = document
? DocumentSnapshot.fromDocument(document, transformationConfig)
: DocumentSnapshot.fromFilePath(
filePath,
docContext.createDocument,
transformationConfig
);
const newSnapshot = DocumentSnapshot.fromDocument(document, transformationConfig);

snapshotManager.set(filePath, newSnapshot);
if (prevSnapshot && prevSnapshot.scriptKind !== newSnapshot.scriptKind) {
// Restart language service as it doesn't handle script kind changes.
Expand All @@ -165,6 +160,21 @@ async function createLanguageService(
return newSnapshot;
}

function updateSnapshotFromFilePath(filePath: string): DocumentSnapshot {
const prevSnapshot = snapshotManager.get(filePath);
if (prevSnapshot) {
return prevSnapshot;
}

const newSnapshot = DocumentSnapshot.fromFilePath(
filePath,
docContext.createDocument,
transformationConfig
);
snapshotManager.set(filePath, newSnapshot);
return newSnapshot;
}

function getSnapshot(fileName: string): DocumentSnapshot {
fileName = ensureRealSvelteFilePath(fileName);

Expand Down