Skip to content

Commit

Permalink
feat(typescript): add option to disable auto import cache (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk authored Aug 24, 2024
1 parent a718f11 commit 33be3bc
Showing 1 changed file with 22 additions and 10 deletions.
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

0 comments on commit 33be3bc

Please sign in to comment.