|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | +/*--------------------------------------------------------- |
| 4 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 5 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 6 | + *--------------------------------------------------------*/ |
| 7 | + |
| 8 | +'use strict'; |
| 9 | + |
| 10 | +import vscode = require('vscode'); |
| 11 | +import { CancellationToken, CodeLens, TextDocument } from 'vscode'; |
| 12 | +import { getGoConfig } from './config'; |
| 13 | +import { GoBaseCodeLensProvider } from './goBaseCodelens'; |
| 14 | +import { GoDocumentSymbolProvider } from './goDocumentSymbols'; |
| 15 | +import { GoExtensionContext } from './context'; |
| 16 | +import { GO_MODE } from './goMode'; |
| 17 | +import { getSymbolImplementations } from './language/goLanguageServer'; |
| 18 | + |
| 19 | +export class GoCodeLensProvider extends GoBaseCodeLensProvider { |
| 20 | + static activate(ctx: vscode.ExtensionContext, goCtx: GoExtensionContext) { |
| 21 | + const codeLensProvider = new this(goCtx); |
| 22 | + ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, codeLensProvider)); |
| 23 | + ctx.subscriptions.push( |
| 24 | + vscode.workspace.onDidChangeConfiguration(async (e: vscode.ConfigurationChangeEvent) => { |
| 25 | + if (!e.affectsConfiguration('go')) { |
| 26 | + return; |
| 27 | + } |
| 28 | + const updatedGoConfig = getGoConfig(); |
| 29 | + if (updatedGoConfig['enableCodeLens']) { |
| 30 | + codeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['implementation']); |
| 31 | + } |
| 32 | + }) |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + constructor(private readonly goCtx: GoExtensionContext) { |
| 37 | + super(); |
| 38 | + } |
| 39 | + |
| 40 | + public async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { |
| 41 | + if (!this.enabled) { |
| 42 | + return []; |
| 43 | + } |
| 44 | + const config = getGoConfig(document.uri); |
| 45 | + const codeLensConfig = config.get<{ [key: string]: any }>('enableCodeLens'); |
| 46 | + const codelensEnabled = codeLensConfig ? codeLensConfig['implementation'] : false; |
| 47 | + if (!codelensEnabled || !document.fileName.endsWith('.go')) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + |
| 51 | + const abstractCodelenses = this.getCodeLensForAbstractSymbols(document, token); |
| 52 | + const concreteCodelenses = this.getCodeLensForConcreteSymbols(document, token); |
| 53 | + |
| 54 | + const codeLenses = await Promise.all([abstractCodelenses, concreteCodelenses]); |
| 55 | + |
| 56 | + return codeLenses.flat(); |
| 57 | + } |
| 58 | + |
| 59 | + private async getCodeLensForConcreteSymbols(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { |
| 60 | + const concreteTypes = await this.getConcreteTypes(document); |
| 61 | + if (concreteTypes && concreteTypes.length) { |
| 62 | + const concreteTypesCodeLens = await this.mapSymbolsToCodeLenses(document, concreteTypes); |
| 63 | + return concreteTypesCodeLens; |
| 64 | + } |
| 65 | + |
| 66 | + return []; |
| 67 | + } |
| 68 | + |
| 69 | + private async getCodeLensForAbstractSymbols(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { |
| 70 | + const interfaces = await this.getInterfaces(document); |
| 71 | + if (interfaces && interfaces.length) { |
| 72 | + const interfacesCodeLens = this.mapSymbolsToCodeLenses(document, interfaces); |
| 73 | + |
| 74 | + const methodsCodeLens = this.mapSymbolsToCodeLenses( |
| 75 | + document, |
| 76 | + interfaces.flatMap((i) => i.children) |
| 77 | + ); |
| 78 | + |
| 79 | + const codeLenses = await Promise.all([interfacesCodeLens, methodsCodeLens]); |
| 80 | + |
| 81 | + return codeLenses.flat(); |
| 82 | + } |
| 83 | + return []; |
| 84 | + } |
| 85 | + |
| 86 | + private async getInterfaces(document: TextDocument): Promise<vscode.DocumentSymbol[]> { |
| 87 | + const documentSymbolProvider = GoDocumentSymbolProvider(this.goCtx); |
| 88 | + const symbols = await documentSymbolProvider.provideDocumentSymbols(document); |
| 89 | + if (!symbols || symbols.length === 0) { |
| 90 | + return []; |
| 91 | + } |
| 92 | + const pkg = symbols[0]; |
| 93 | + if (!pkg) { |
| 94 | + return []; |
| 95 | + } |
| 96 | + const children = pkg.children; |
| 97 | + const interfaces = children.filter((s) => s.kind === vscode.SymbolKind.Interface); |
| 98 | + if (!interfaces) { |
| 99 | + return []; |
| 100 | + } |
| 101 | + |
| 102 | + return interfaces; |
| 103 | + } |
| 104 | + |
| 105 | + private async getConcreteTypes(document: TextDocument): Promise<vscode.DocumentSymbol[]> { |
| 106 | + const documentSymbolProvider = GoDocumentSymbolProvider(this.goCtx); |
| 107 | + const symbols = await documentSymbolProvider.provideDocumentSymbols(document); |
| 108 | + if (!symbols || symbols.length === 0) { |
| 109 | + return []; |
| 110 | + } |
| 111 | + const pkg = symbols[0]; |
| 112 | + if (!pkg) { |
| 113 | + return []; |
| 114 | + } |
| 115 | + const children = pkg.children; |
| 116 | + const concreteTypes = children.filter((s) => |
| 117 | + [vscode.SymbolKind.Struct, vscode.SymbolKind.Method].includes(s.kind) |
| 118 | + ); |
| 119 | + if (!concreteTypes) { |
| 120 | + return []; |
| 121 | + } |
| 122 | + |
| 123 | + return concreteTypes; |
| 124 | + } |
| 125 | + |
| 126 | + private async mapSymbolsToCodeLenses( |
| 127 | + document: vscode.TextDocument, |
| 128 | + symbols: vscode.DocumentSymbol[] |
| 129 | + ): Promise<vscode.CodeLens[]> { |
| 130 | + return Promise.all( |
| 131 | + symbols.map(async (s) => { |
| 132 | + const implementations = await this.getImplementations(document, s); |
| 133 | + if (implementations.length) { |
| 134 | + return new CodeLens(s.range, { |
| 135 | + title: `${implementations.length} implementation${implementations.length > 1 ? 's' : ''}`, |
| 136 | + command: 'editor.action.goToLocations', |
| 137 | + arguments: [document.uri, s.range.start, implementations, 'peek'] |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + return new CodeLens(s.range, { |
| 142 | + title: 'no implementation found', |
| 143 | + command: '' |
| 144 | + }); |
| 145 | + }) |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + private async getImplementations( |
| 150 | + document: vscode.TextDocument, |
| 151 | + symbol: vscode.DocumentSymbol |
| 152 | + ): Promise<vscode.Location[]> { |
| 153 | + return getSymbolImplementations(this.goCtx, document, symbol); |
| 154 | + } |
| 155 | +} |
0 commit comments