提交 8d244787 编写于 作者: S SteVen Batten 提交者: Matt Bierner

updating the typescript extension codelens to cache requests for the navtree (#40402)

Fixes #28214
上级 f929c1bf
...@@ -19,12 +19,29 @@ export class ReferencesCodeLens extends CodeLens { ...@@ -19,12 +19,29 @@ export class ReferencesCodeLens extends CodeLens {
} }
} }
export class CachedNavTreeResponse {
response?: Promise<Proto.NavTreeResponse>;
version: number = -1;
document: string = '';
matches(document: TextDocument): boolean {
return this.version === document.version && this.document === document.uri.toString();
}
update(document: TextDocument, response: Promise<Proto.NavTreeResponse>) {
this.response = response;
this.version = document.version;
this.document = document.uri.toString();
}
}
export abstract class TypeScriptBaseCodeLensProvider implements CodeLensProvider { export abstract class TypeScriptBaseCodeLensProvider implements CodeLensProvider {
private enabled: boolean = true; private enabled: boolean = true;
private onDidChangeCodeLensesEmitter = new EventEmitter<void>(); private onDidChangeCodeLensesEmitter = new EventEmitter<void>();
public constructor( public constructor(
protected client: ITypeScriptServiceClient protected client: ITypeScriptServiceClient,
private cachedResponse: CachedNavTreeResponse
) { } ) { }
public get onDidChangeCodeLenses(): Event<void> { public get onDidChangeCodeLenses(): Event<void> {
...@@ -48,10 +65,15 @@ export abstract class TypeScriptBaseCodeLensProvider implements CodeLensProvider ...@@ -48,10 +65,15 @@ export abstract class TypeScriptBaseCodeLensProvider implements CodeLensProvider
return []; return [];
} }
try { try {
const response = await this.client.execute('navtree', { file: filepath }, token); if (!this.cachedResponse.matches(document)) {
this.cachedResponse.update(document, this.client.execute('navtree', { file: filepath }, token));
}
const response = await this.cachedResponse.response;
if (!response) { if (!response) {
return []; return [];
} }
const tree = response.body; const tree = response.body;
const referenceableSpans: Range[] = []; const referenceableSpans: Range[] = [];
if (tree && tree.childItems) { if (tree && tree.childItems) {
......
...@@ -7,7 +7,7 @@ import { CodeLens, CancellationToken, TextDocument, Range, Location, workspace } ...@@ -7,7 +7,7 @@ import { CodeLens, CancellationToken, TextDocument, Range, Location, workspace }
import * as Proto from '../protocol'; import * as Proto from '../protocol';
import * as PConst from '../protocol.const'; import * as PConst from '../protocol.const';
import { TypeScriptBaseCodeLensProvider, ReferencesCodeLens } from './baseCodeLensProvider'; import { TypeScriptBaseCodeLensProvider, ReferencesCodeLens, CachedNavTreeResponse } from './baseCodeLensProvider';
import { ITypeScriptServiceClient } from '../typescriptService'; import { ITypeScriptServiceClient } from '../typescriptService';
import { tsTextSpanToVsRange, vsPositionToTsFileLocation } from '../utils/convert'; import { tsTextSpanToVsRange, vsPositionToTsFileLocation } from '../utils/convert';
...@@ -17,9 +17,10 @@ const localize = nls.loadMessageBundle(); ...@@ -17,9 +17,10 @@ const localize = nls.loadMessageBundle();
export default class TypeScriptImplementationsCodeLensProvider extends TypeScriptBaseCodeLensProvider { export default class TypeScriptImplementationsCodeLensProvider extends TypeScriptBaseCodeLensProvider {
public constructor( public constructor(
client: ITypeScriptServiceClient, client: ITypeScriptServiceClient,
private readonly language: string private readonly language: string,
cachedResponse: CachedNavTreeResponse
) { ) {
super(client); super(client, cachedResponse);
} }
public updateConfiguration(): void { public updateConfiguration(): void {
......
...@@ -7,7 +7,7 @@ import { CodeLens, CancellationToken, TextDocument, Range, Location, workspace } ...@@ -7,7 +7,7 @@ import { CodeLens, CancellationToken, TextDocument, Range, Location, workspace }
import * as Proto from '../protocol'; import * as Proto from '../protocol';
import * as PConst from '../protocol.const'; import * as PConst from '../protocol.const';
import { TypeScriptBaseCodeLensProvider, ReferencesCodeLens } from './baseCodeLensProvider'; import { TypeScriptBaseCodeLensProvider, ReferencesCodeLens, CachedNavTreeResponse } from './baseCodeLensProvider';
import { ITypeScriptServiceClient } from '../typescriptService'; import { ITypeScriptServiceClient } from '../typescriptService';
import { tsTextSpanToVsRange, vsPositionToTsFileLocation } from '../utils/convert'; import { tsTextSpanToVsRange, vsPositionToTsFileLocation } from '../utils/convert';
...@@ -17,9 +17,10 @@ const localize = nls.loadMessageBundle(); ...@@ -17,9 +17,10 @@ const localize = nls.loadMessageBundle();
export default class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLensProvider { export default class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLensProvider {
public constructor( public constructor(
client: ITypeScriptServiceClient, client: ITypeScriptServiceClient,
private readonly language: string private readonly language: string,
cachedResponse: CachedNavTreeResponse
) { ) {
super(client); super(client, cachedResponse);
} }
public updateConfiguration(): void { public updateConfiguration(): void {
......
...@@ -37,6 +37,7 @@ import { CommandManager } from './utils/commandManager'; ...@@ -37,6 +37,7 @@ import { CommandManager } from './utils/commandManager';
import DiagnosticsManager from './features/diagnostics'; import DiagnosticsManager from './features/diagnostics';
import { LanguageDescription } from './utils/languageDescription'; import { LanguageDescription } from './utils/languageDescription';
import * as fileSchemes from './utils/fileSchemes'; import * as fileSchemes from './utils/fileSchemes';
import { CachedNavTreeResponse } from './features/baseCodeLensProvider';
const validateSetting = 'validate.enable'; const validateSetting = 'validate.enable';
...@@ -148,12 +149,14 @@ class LanguageProvider { ...@@ -148,12 +149,14 @@ class LanguageProvider {
this.disposables.push(languages.registerCodeActionsProvider(selector, new (await import('./features/refactorProvider')).default(client, this.formattingOptionsManager, commandManager))); this.disposables.push(languages.registerCodeActionsProvider(selector, new (await import('./features/refactorProvider')).default(client, this.formattingOptionsManager, commandManager)));
this.registerVersionDependentProviders(); this.registerVersionDependentProviders();
const referenceCodeLensProvider = new (await import('./features/referencesCodeLensProvider')).default(client, this.description.id); const cachedResponse = new CachedNavTreeResponse();
const referenceCodeLensProvider = new (await import('./features/referencesCodeLensProvider')).default(client, this.description.id, cachedResponse);
referenceCodeLensProvider.updateConfiguration(); referenceCodeLensProvider.updateConfiguration();
this.toUpdateOnConfigurationChanged.push(referenceCodeLensProvider); this.toUpdateOnConfigurationChanged.push(referenceCodeLensProvider);
this.disposables.push(languages.registerCodeLensProvider(selector, referenceCodeLensProvider)); this.disposables.push(languages.registerCodeLensProvider(selector, referenceCodeLensProvider));
const implementationCodeLensProvider = new (await import('./features/implementationsCodeLensProvider')).default(client, this.description.id); const implementationCodeLensProvider = new (await import('./features/implementationsCodeLensProvider')).default(client, this.description.id, cachedResponse);
implementationCodeLensProvider.updateConfiguration(); implementationCodeLensProvider.updateConfiguration();
this.toUpdateOnConfigurationChanged.push(implementationCodeLensProvider); this.toUpdateOnConfigurationChanged.push(implementationCodeLensProvider);
this.disposables.push(languages.registerCodeLensProvider(selector, implementationCodeLensProvider)); this.disposables.push(languages.registerCodeLensProvider(selector, implementationCodeLensProvider));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册