Add typescript.implementationsCodeLens.showOnInterfaceMethods setting (#136282) (#198419)

* Add `typescript.implementationsCodeLens.showOnInterfaceMethods` setting (#136282)

* Update codelenses when `typescript.referencesCodeLens.showOnAllFunctions` changes

* Improve handling of disposables
This commit is contained in:
John Murray 2023-11-16 22:46:41 +00:00 committed by GitHub
parent 678f6a544c
commit 77dc8793db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 5 deletions

View file

@ -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,

View file

@ -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...",

View file

@ -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<ReferencesCodeLens> {
export abstract class TypeScriptBaseCodeLensProvider extends Disposable implements vscode.CodeLensProvider<ReferencesCodeLens>{
protected changeEmitter = this._register(new vscode.EventEmitter<void>());
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<Proto.NavTreeResponse>
) { }
) {
super();
}
async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<ReferencesCodeLens[]> {
const filepath = this.client.toOpenTsFilePath(document);

View file

@ -17,6 +17,20 @@ import { ExecutionTarget } from '../../tsServer/server';
export default class TypeScriptImplementationsCodeLensProvider extends TypeScriptBaseCodeLensProvider {
public constructor(
client: ITypeScriptServiceClient,
protected _cachedResponse: CachedResponse<Proto.NavTreeResponse>,
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<boolean>('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));
});
}

View file

@ -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<vscode.CodeLens> {