Skip to content

Commit 2bd9640

Browse files
authored
Improve cache invalidation on client side (#254)
1 parent 4e15882 commit 2bd9640

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/extension/typescriptContext/serverPlugin/src/common/api.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { CompilerOptionsRunnable } from './baseContextProviders';
1010
import { ClassContextProvider } from './classContextProvider';
1111
import { ContextProvider, ContextRunnableCollector, RequestContext, type ComputeContextSession, type ContextProviderFactory, type ContextResult, type ContextRunnable, type ProviderComputeContext } from './contextProvider';
1212
import { FunctionContextProvider } from './functionContextProvider';
13-
import { ConstructorContextProvider, MethodContextProvider } from './methodContextProvider';
13+
import { AccessorProvider, ConstructorContextProvider, MethodContextProvider } from './methodContextProvider';
1414
import { ModuleContextProvider } from './moduleContextProvider';
1515
import { type FilePath } from './protocol';
1616
import { SourceFileContextProvider } from './sourceFileContextProvider';
@@ -44,6 +44,8 @@ class ContextProviders {
4444
[ts.SyntaxKind.FunctionDeclaration, (node, tokenInfo, computeContext) => new FunctionContextProvider(node as tt.FunctionDeclaration, tokenInfo, computeContext)],
4545
[ts.SyntaxKind.ArrowFunction, (node, tokenInfo, computeContext) => new FunctionContextProvider(node as tt.ArrowFunction, tokenInfo, computeContext)],
4646
[ts.SyntaxKind.FunctionExpression, (node, tokenInfo, computeContext) => new FunctionContextProvider(node as tt.FunctionExpression, tokenInfo, computeContext)],
47+
[ts.SyntaxKind.GetAccessor, (node, tokenInfo, computeContext) => new AccessorProvider(node as tt.GetAccessorDeclaration, tokenInfo, computeContext)],
48+
[ts.SyntaxKind.SetAccessor, (node, tokenInfo, computeContext) => new AccessorProvider(node as tt.SetAccessorDeclaration, tokenInfo, computeContext)],
4749
[ts.SyntaxKind.ClassDeclaration, ClassContextProvider.create as unknown as ContextProviderFactory],
4850
[ts.SyntaxKind.Constructor, (node, tokenInfo, computeContext) => new ConstructorContextProvider(node as tt.ConstructorDeclaration, tokenInfo, computeContext)],
4951
[ts.SyntaxKind.MethodDeclaration, (node, tokenInfo, computeContext) => new MethodContextProvider(node as tt.MethodDeclaration, tokenInfo, computeContext)],

src/extension/typescriptContext/serverPlugin/src/common/methodContextProvider.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import type tt from 'typescript/lib/tsserverlibrary';
5+
import tt from 'typescript/lib/tsserverlibrary';
66
import TS from './typescript';
77
const ts = TS();
88

@@ -470,7 +470,7 @@ class SimilarMethodRunnable extends SimilarPropertyRunnable<tt.MethodDeclaration
470470
}
471471
}
472472

473-
abstract class ClassPropertyContextProvider<T extends tt.MethodDeclaration | tt.ConstructorDeclaration> extends FunctionLikeContextProvider {
473+
abstract class ClassPropertyContextProvider<T extends tt.MethodDeclaration | tt.ConstructorDeclaration | tt.GetAccessorDeclaration | tt.SetAccessorDeclaration> extends FunctionLikeContextProvider {
474474

475475
protected readonly declaration: T;
476476
public override readonly isCallableProvider: boolean;
@@ -508,9 +508,9 @@ abstract class ClassPropertyContextProvider<T extends tt.MethodDeclaration | tt.
508508

509509
class PropertiesTypeRunnable extends AbstractContextRunnable {
510510

511-
private readonly declaration: tt.MethodDeclaration | tt.ConstructorDeclaration;
511+
private readonly declaration: tt.MethodDeclaration | tt.ConstructorDeclaration | tt.GetAccessorDeclaration | tt.SetAccessorDeclaration;
512512

513-
constructor(session: ComputeContextSession, languageService: tt.LanguageService, context: RequestContext, declaration: tt.MethodDeclaration | tt.ConstructorDeclaration, priority: number = Priorities.Properties) {
513+
constructor(session: ComputeContextSession, languageService: tt.LanguageService, context: RequestContext, declaration: tt.MethodDeclaration | tt.ConstructorDeclaration | tt.GetAccessorDeclaration | tt.SetAccessorDeclaration, priority: number = Priorities.Properties) {
514514
super(session, languageService, context, 'PropertiesTypeRunnable', priority, ComputeCost.Medium);
515515
this.declaration = declaration;
516516
}
@@ -645,6 +645,18 @@ export class MethodContextProvider extends ClassPropertyContextProvider<tt.Metho
645645
}
646646
}
647647

648+
export class AccessorProvider extends ClassPropertyContextProvider<tt.GetAccessorDeclaration | tt.SetAccessorDeclaration> {
649+
650+
constructor(declaration: tt.GetAccessorDeclaration | tt.SetAccessorDeclaration, tokenInfo: TokenInfo, computeContext: ProviderComputeContext) {
651+
super(declaration, tokenInfo, computeContext);
652+
}
653+
654+
public override provide(result: ContextRunnableCollector, session: ComputeContextSession, languageService: tt.LanguageService, context: RequestContext, token: tt.CancellationToken): void {
655+
super.provide(result, session, languageService, context, token);
656+
result.addSecondary(new PropertiesTypeRunnable(session, languageService, context, this.declaration));
657+
}
658+
}
659+
648660
class ConstructorBlueprintSearch extends FindInSiblingClassSearch<tt.ConstructorDeclaration> {
649661

650662
constructor(program: tt.Program, symbols: Symbols, search: ConstructorBlueprintSearch);

src/extension/typescriptContext/vscode-node/languageContextService.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@ class RunnableResultManager implements vscode.Disposable {
724724
this.withInRangeRunnableResults.length = 0;
725725
this.outsideRangeRunnableResults.length = 0;
726726
this.neighborFileRunnableResults.length = 0;
727-
this.results = new Map();
727+
this.results.clear();
728728
this.cacheInfo = {
729729
version: version,
730730
state: CacheState.NotPopulated
@@ -742,7 +742,7 @@ class RunnableResultManager implements vscode.Disposable {
742742
path: body.path ?? [0]
743743
};
744744

745-
if (body.runnableResults === undefined || body.runnableResults.length === 0) {
745+
if (body.runnableResults === undefined || body.runnableResults.length === 0 || body.path === undefined || body.path.length === 0 || body.path[0] === 0) {
746746
return { resolved: [], cached: cachedItems, referenced: referencedItems, serverComputed: serverComputed };
747747
}
748748

@@ -865,7 +865,7 @@ class RunnableResultManager implements vscode.Disposable {
865865
if (this.requestInfo?.document !== document.uri.toString()) {
866866
return undefined;
867867
}
868-
if (this.cacheInfo.version !== document.version) {
868+
if (this.cacheInfo.version !== document.version || this.cacheInfo.state === CacheState.NotPopulated || this.requestInfo.path.length === 0 || this.requestInfo.path[0] === 0) {
869869
this.clear();
870870
return undefined;
871871
}

0 commit comments

Comments
 (0)