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

feat(typescript): add option to disable auto import cache #105

Merged
merged 4 commits into from
Aug 24, 2024
Merged
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
32 changes: 22 additions & 10 deletions packages/typescript/lib/plugins/semantic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@ function getDocumentRegistry(ts: typeof import('typescript'), useCaseSensitiveFi
export function create(
ts: typeof import('typescript'),
{
disableAutoImportCache = false,
isValidationEnabled = async (document, context) => {
return await context.env.getConfiguration?.<boolean>(getConfigTitle(document) + '.validate.enable') ?? true;
},
isSuggestionsEnabled = async (document, context) => {
return await context.env.getConfiguration?.<boolean>(getConfigTitle(document) + '.suggest.enabled') ?? true;
},
}: {
disableAutoImportCache?: boolean;
isValidationEnabled?(document: TextDocument, context: LanguageServiceContext): ProviderResult<boolean>;
isSuggestionsEnabled?(document: TextDocument, context: LanguageServiceContext): ProviderResult<boolean>;
} = {}
Expand Down Expand Up @@ -172,13 +174,23 @@ export function create(
return {};
}
const { sys, languageServiceHost, uriConverter, getExtraServiceScript } = context.project.typescript;
const created = tsWithImportCache.createLanguageService(
ts,
sys,
languageServiceHost,
proxiedHost => ts.createLanguageService(proxiedHost, getDocumentRegistry(ts, sys.useCaseSensitiveFileNames, languageServiceHost.getCurrentDirectory()))
);
const { languageService } = created;
let languageService: ts.LanguageService;
let created: ReturnType<typeof tsWithImportCache.createLanguageService> | undefined;
if (disableAutoImportCache) {
languageService = ts.createLanguageService(
languageServiceHost,
getDocumentRegistry(ts, sys.useCaseSensitiveFileNames, languageServiceHost.getCurrentDirectory())
);
}
else {
created = tsWithImportCache.createLanguageService(
ts,
sys,
languageServiceHost,
proxiedHost => ts.createLanguageService(proxiedHost, getDocumentRegistry(ts, sys.useCaseSensitiveFileNames, languageServiceHost.getCurrentDirectory()))
);
languageService = created.languageService;
}
const ctx: SharedContext = {
...context,
languageServiceHost,
Expand Down Expand Up @@ -232,20 +244,20 @@ export function create(

let formattingOptions: FormattingOptions | undefined;

if (created.setPreferences && context.env.getConfiguration) {
if (created?.setPreferences && context.env.getConfiguration) {

updatePreferences();
context.env.onDidChangeConfiguration?.(updatePreferences);

async function updatePreferences() {
const preferences = await context.env.getConfiguration?.<ts.UserPreferences>('typescript.preferences');
if (preferences) {
created.setPreferences?.(preferences);
created!.setPreferences?.(preferences);
}
}
}

if (created.projectUpdated) {
if (created?.projectUpdated) {

const sourceScriptNames = new Set<string>();
const normalizeFileName = sys.useCaseSensitiveFileNames
Expand Down