Skip to content

Commit

Permalink
virtual doc won't pass the project files check so we have to directly…
Browse files Browse the repository at this point in the history
… access the lsContainer
  • Loading branch information
jasonlyu123 committed Aug 16, 2024
1 parent fc0da19 commit 006e807
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,11 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {

const isImportFix = codeAction.data.fixName === FIX_IMPORT_FIX_NAME;
const virtualDocInfo = isImportFix
? await this.createVirtualDocumentForCombinedImportCodeFix(
? this.createVirtualDocumentForCombinedImportCodeFix(
document,
getDiagnostics(),
tsDoc,
lsContainer,
lang
)
: undefined;
Expand Down Expand Up @@ -259,10 +260,11 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
* Do not use this in regular code action
* This'll cause TypeScript to rebuild and invalidate caches every time. It'll be slow
*/
private async createVirtualDocumentForCombinedImportCodeFix(
private createVirtualDocumentForCombinedImportCodeFix(
document: Document,
diagnostics: Diagnostic[],
tsDoc: DocumentSnapshot,
lsContainer: LanguageServiceContainer,
lang: ts.LanguageService
) {
const virtualUri = document.uri + '.__virtual__.svelte';
Expand Down Expand Up @@ -314,10 +316,8 @@ export class CodeActionsProviderImpl implements CodeActionsProvider {
const virtualDoc = new Document(virtualUri, newText);
virtualDoc.openedByClient = true;
// let typescript know about the virtual document
// getLSAndTSDoc instead of getSnapshot so that project dirty state is correctly tracked by us
// otherwise, sometime the applied code fix might not be picked up by the language service
// because we think the project is still dirty and doesn't update the project version
await this.lsAndTsDocResolver.getLSAndTSDoc(virtualDoc);
lsContainer.openVirtualDocument(virtualDoc);
lsContainer.getService();

return {
virtualDoc,
Expand Down
12 changes: 12 additions & 0 deletions packages/language-server/src/plugins/typescript/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export interface LanguageServiceContainer {
onPackageJsonChange(packageJsonPath: string): void;
getTsConfigSvelteOptions(): { namespace: string };
getResolvedProjectReferences(): TsConfigInfo[];
openVirtualDocument(document: Document): void;
dispose(): void;
}

Expand Down Expand Up @@ -440,6 +441,7 @@ async function createLanguageService(
onPackageJsonChange,
getTsConfigSvelteOptions,
getResolvedProjectReferences,
openVirtualDocument,
dispose
};

Expand Down Expand Up @@ -1137,6 +1139,16 @@ async function createLanguageService(
.map((ref) => ensureTsConfigInfoUpToDate(normalizePath(ref.path)))
.filter(isNotNullOrUndefined);
}

function openVirtualDocument(document: Document) {
const filePath = document.getFilePath();
if (!filePath) {
return;
}
configFileForOpenFiles.set(filePath, tsconfigPath || workspacePath);
updateSnapshot(document);
scheduleUpdate(filePath);
}
}

/**
Expand Down

0 comments on commit 006e807

Please sign in to comment.