From 77dc8793db05c024e719b278533ed2ae9148b5ed Mon Sep 17 00:00:00 2001 From: John Murray Date: Thu, 16 Nov 2023 22:46:41 +0000 Subject: [PATCH] Add `typescript.implementationsCodeLens.showOnInterfaceMethods` setting (#136282) (#198419) * Add `typescript.implementationsCodeLens.showOnInterfaceMethods` setting (#136282) * Update codelenses when `typescript.referencesCodeLens.showOnAllFunctions` changes * Improve handling of disposables --- .../typescript-language-features/package.json | 6 ++++++ .../package.nls.json | 1 + .../codeLens/baseCodeLensProvider.ts | 10 ++++++--- .../codeLens/implementationsCodeLens.ts | 21 +++++++++++++++++-- .../codeLens/referencesCodeLens.ts | 7 +++++++ 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/extensions/typescript-language-features/package.json b/extensions/typescript-language-features/package.json index f8cf80d7d4e..2867f543ccd 100644 --- a/extensions/typescript-language-features/package.json +++ b/extensions/typescript-language-features/package.json @@ -259,6 +259,12 @@ "description": "%typescript.implementationsCodeLens.enabled%", "scope": "window" }, + "typescript.implementationsCodeLens.showOnInterfaceMethods": { + "type": "boolean", + "default": false, + "description": "%typescript.implementationsCodeLens.showOnInterfaceMethods%", + "scope": "window" + }, "typescript.tsserver.enableTracing": { "type": "boolean", "default": false, diff --git a/extensions/typescript-language-features/package.nls.json b/extensions/typescript-language-features/package.nls.json index d233e9614de..0bd09aa8d55 100644 --- a/extensions/typescript-language-features/package.nls.json +++ b/extensions/typescript-language-features/package.nls.json @@ -58,6 +58,7 @@ "typescript.referencesCodeLens.enabled": "Enable/disable references CodeLens in TypeScript files.", "typescript.referencesCodeLens.showOnAllFunctions": "Enable/disable references CodeLens on all functions in TypeScript files.", "typescript.implementationsCodeLens.enabled": "Enable/disable implementations CodeLens. This CodeLens shows the implementers of an interface.", + "typescript.implementationsCodeLens.showOnInterfaceMethods": "Enable/disable implementations CodeLens on interface methods.", "typescript.openTsServerLog.title": "Open TS Server log", "typescript.restartTsServer": "Restart TS Server", "typescript.selectTypeScriptVersion.title": "Select TypeScript Version...", diff --git a/extensions/typescript-language-features/src/languageFeatures/codeLens/baseCodeLensProvider.ts b/extensions/typescript-language-features/src/languageFeatures/codeLens/baseCodeLensProvider.ts index 7c970e4212c..38166ff27a8 100644 --- a/extensions/typescript-language-features/src/languageFeatures/codeLens/baseCodeLensProvider.ts +++ b/extensions/typescript-language-features/src/languageFeatures/codeLens/baseCodeLensProvider.ts @@ -9,6 +9,7 @@ import type * as Proto from '../../tsServer/protocol/protocol'; import * as typeConverters from '../../typeConverters'; import { ITypeScriptServiceClient } from '../../typescriptService'; import { escapeRegExp } from '../../utils/regexp'; +import { Disposable } from '../../utils/dispose'; export class ReferencesCodeLens extends vscode.CodeLens { @@ -21,7 +22,9 @@ export class ReferencesCodeLens extends vscode.CodeLens { } } -export abstract class TypeScriptBaseCodeLensProvider implements vscode.CodeLensProvider { +export abstract class TypeScriptBaseCodeLensProvider extends Disposable implements vscode.CodeLensProvider{ + protected changeEmitter = this._register(new vscode.EventEmitter()); + public onDidChangeCodeLenses = this.changeEmitter.event; public static readonly cancelledCommand: vscode.Command = { // Cancellation is not an error. Just show nothing until we can properly re-compute the code lens @@ -37,8 +40,9 @@ export abstract class TypeScriptBaseCodeLensProvider implements vscode.CodeLensP public constructor( protected client: ITypeScriptServiceClient, private readonly cachedResponse: CachedResponse - ) { } - + ) { + super(); + } async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise { const filepath = this.client.toOpenTsFilePath(document); diff --git a/extensions/typescript-language-features/src/languageFeatures/codeLens/implementationsCodeLens.ts b/extensions/typescript-language-features/src/languageFeatures/codeLens/implementationsCodeLens.ts index ca71495b345..6088292f8d0 100644 --- a/extensions/typescript-language-features/src/languageFeatures/codeLens/implementationsCodeLens.ts +++ b/extensions/typescript-language-features/src/languageFeatures/codeLens/implementationsCodeLens.ts @@ -17,6 +17,20 @@ import { ExecutionTarget } from '../../tsServer/server'; export default class TypeScriptImplementationsCodeLensProvider extends TypeScriptBaseCodeLensProvider { + public constructor( + client: ITypeScriptServiceClient, + protected _cachedResponse: CachedResponse, + private readonly language: LanguageDescription + ) { + super(client, _cachedResponse); + this._register( + vscode.workspace.onDidChangeConfiguration(evt => { + if (evt.affectsConfiguration(`${language.id}.implementationsCodeLens.showOnInterfaceMethods`)) { + this.changeEmitter.fire(); + } + }) + ); + } public async resolveCodeLens( codeLens: ReferencesCodeLens, @@ -71,8 +85,11 @@ export default class TypeScriptImplementationsCodeLensProvider extends TypeScrip protected extractSymbol( document: vscode.TextDocument, item: Proto.NavigationTree, - _parent: Proto.NavigationTree | undefined + parent: Proto.NavigationTree | undefined ): vscode.Range | undefined { + if (item.kind === PConst.Kind.method && parent && parent.kind === PConst.Kind.interface && vscode.workspace.getConfiguration(this.language.id).get('implementationsCodeLens.showOnInterfaceMethods')) { + return getSymbolRange(document, item); + } switch (item.kind) { case PConst.Kind.interface: return getSymbolRange(document, item); @@ -102,6 +119,6 @@ export function register( requireSomeCapability(client, ClientCapability.Semantic), ], () => { return vscode.languages.registerCodeLensProvider(selector.semantic, - new TypeScriptImplementationsCodeLensProvider(client, cachedResponse)); + new TypeScriptImplementationsCodeLensProvider(client, cachedResponse, language)); }); } diff --git a/extensions/typescript-language-features/src/languageFeatures/codeLens/referencesCodeLens.ts b/extensions/typescript-language-features/src/languageFeatures/codeLens/referencesCodeLens.ts index d76db9f9b18..73e6335dbad 100644 --- a/extensions/typescript-language-features/src/languageFeatures/codeLens/referencesCodeLens.ts +++ b/extensions/typescript-language-features/src/languageFeatures/codeLens/referencesCodeLens.ts @@ -23,6 +23,13 @@ export class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLens private readonly language: LanguageDescription ) { super(client, _cachedResponse); + this._register( + vscode.workspace.onDidChangeConfiguration(evt => { + if (evt.affectsConfiguration(`${language.id}.referencesCodeLens.showOnAllFunctions`)) { + this.changeEmitter.fire(); + } + }) + ); } public async resolveCodeLens(codeLens: ReferencesCodeLens, token: vscode.CancellationToken): Promise {