Skip to content

Commit

Permalink
Add typescript.implementationsCodeLens.showOnInterfaceMethods setti…
Browse files Browse the repository at this point in the history
…ng (#136282) (#198419)

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

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

* Improve handling of disposables
  • Loading branch information
gjsjohnmurray authored Nov 16, 2023
1 parent 678f6a5 commit 77dc879
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
6 changes: 6 additions & 0 deletions extensions/typescript-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions extensions/typescript-language-features/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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...",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down

0 comments on commit 77dc879

Please sign in to comment.