-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src/goCodeLens: show codelens for implementations of symbols
This change adds `implementations` as a codelens to abstract and concrete symbols Fixes #56695
- Loading branch information
Showing
5 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See LICENSE in the project root for license information. | ||
*--------------------------------------------------------*/ | ||
|
||
'use strict'; | ||
|
||
import vscode = require('vscode'); | ||
import { CancellationToken, CodeLens, TextDocument } from 'vscode'; | ||
import { getGoConfig } from './config'; | ||
import { GoBaseCodeLensProvider } from './goBaseCodelens'; | ||
import { GoDocumentSymbolProvider } from './goDocumentSymbols'; | ||
import { GoExtensionContext } from './context'; | ||
import { GO_MODE } from './goMode'; | ||
import { getSymbolImplementations } from './language/goLanguageServer'; | ||
|
||
export class GoCodeLensProvider extends GoBaseCodeLensProvider { | ||
static activate(ctx: vscode.ExtensionContext, goCtx: GoExtensionContext) { | ||
const codeLensProvider = new this(goCtx); | ||
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, codeLensProvider)); | ||
ctx.subscriptions.push( | ||
vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => { | ||
if (!e.affectsConfiguration('go')) { | ||
return; | ||
} | ||
const updatedGoConfig = getGoConfig(); | ||
if (updatedGoConfig['enableCodeLens']) { | ||
codeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['implementation']); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
constructor(private readonly goCtx: GoExtensionContext) { | ||
super(); | ||
} | ||
|
||
public async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { | ||
if (!this.enabled) { | ||
return []; | ||
} | ||
const config = getGoConfig(document.uri); | ||
const codeLensConfig = config.get<{ [key: string]: any }>('enableCodeLens'); | ||
const codelensEnabled = codeLensConfig ? codeLensConfig['implementation'] : false; | ||
if (!codelensEnabled || !document.fileName.endsWith('.go')) { | ||
return []; | ||
} | ||
|
||
const abstractCodelenses = this.getCodeLensForAbstractSymbols(document, token); | ||
const concreteCodelenses = this.getCodeLensForConcreteSymbols(document, token); | ||
|
||
const codeLenses = await Promise.all([abstractCodelenses, concreteCodelenses]); | ||
|
||
return codeLenses.flat(); | ||
} | ||
|
||
private async getCodeLensForConcreteSymbols(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { | ||
const concreteTypes = await this.getConcreteTypes(document); | ||
if (concreteTypes && concreteTypes.length) { | ||
const concreteTypesCodeLens = await this.mapSymbolsToCodeLenses(document, concreteTypes); | ||
return concreteTypesCodeLens; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private async getCodeLensForAbstractSymbols(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { | ||
const interfaces = await this.getInterfaces(document); | ||
if (interfaces && interfaces.length) { | ||
const interfacesCodeLens = this.mapSymbolsToCodeLenses(document, interfaces); | ||
|
||
const methodsCodeLens = this.mapSymbolsToCodeLenses( | ||
document, | ||
interfaces.flatMap((i) => i.children) | ||
); | ||
|
||
const codeLenses = await Promise.all([interfacesCodeLens, methodsCodeLens]); | ||
|
||
return codeLenses.flat(); | ||
} | ||
return []; | ||
} | ||
|
||
private async getInterfaces(document: TextDocument): Promise<vscode.DocumentSymbol[]> { | ||
const documentSymbolProvider = GoDocumentSymbolProvider(this.goCtx); | ||
const symbols = await documentSymbolProvider.provideDocumentSymbols(document); | ||
if (!symbols || symbols.length === 0) { | ||
return []; | ||
} | ||
const pkg = symbols[0]; | ||
if (!pkg) { | ||
return []; | ||
} | ||
const children = pkg.children; | ||
const interfaces = children.filter((s) => s.kind === vscode.SymbolKind.Interface); | ||
if (!interfaces) { | ||
return []; | ||
} | ||
|
||
return interfaces; | ||
} | ||
|
||
private async getConcreteTypes(document: TextDocument): Promise<vscode.DocumentSymbol[]> { | ||
const documentSymbolProvider = GoDocumentSymbolProvider(this.goCtx); | ||
const symbols = await documentSymbolProvider.provideDocumentSymbols(document); | ||
if (!symbols || symbols.length === 0) { | ||
return []; | ||
} | ||
const pkg = symbols[0]; | ||
if (!pkg) { | ||
return []; | ||
} | ||
const children = pkg.children; | ||
const concreteTypes = children.filter((s) => | ||
[vscode.SymbolKind.Struct, vscode.SymbolKind.Method].includes(s.kind) | ||
); | ||
if (!concreteTypes) { | ||
return []; | ||
} | ||
|
||
return concreteTypes; | ||
} | ||
|
||
private async mapSymbolsToCodeLenses( | ||
document: vscode.TextDocument, | ||
symbols: vscode.DocumentSymbol[] | ||
): Promise<vscode.CodeLens[]> { | ||
return Promise.all( | ||
symbols.map(async (s) => { | ||
const implementations = await this.getImplementations(document, s); | ||
if (implementations.length) { | ||
return new CodeLens(s.range, { | ||
title: `${implementations.length} implementation${implementations.length > 1 ? 's' : ''}`, | ||
command: 'editor.action.goToLocations', | ||
arguments: [document.uri, s.range.start, implementations, 'peek'] | ||
}); | ||
} | ||
|
||
return new CodeLens(s.range, { | ||
title: 'no implementation found', | ||
command: '' | ||
}); | ||
}) | ||
); | ||
} | ||
|
||
private async getImplementations( | ||
document: vscode.TextDocument, | ||
symbol: vscode.DocumentSymbol | ||
): Promise<vscode.Location[]> { | ||
return getSymbolImplementations(this.goCtx, document, symbol); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters