|
| 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 './goOutline'; |
| 15 | +import { getBenchmarkFunctions, getTestFunctions } from './testUtils'; |
| 16 | + |
| 17 | +const mainFuncRegx = /^main$/u; |
| 18 | + |
| 19 | +export class GoMainCodeLensProvider extends GoBaseCodeLensProvider { |
| 20 | + private readonly mainRegex = /^main.+/; |
| 21 | + |
| 22 | + public async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { |
| 23 | + if (!this.enabled) { |
| 24 | + return []; |
| 25 | + } |
| 26 | + const config = getGoConfig(document.uri); |
| 27 | + const codeLensConfig = config.get<{ [key: string]: any }>('enableCodeLens'); |
| 28 | + const codelensEnabled = codeLensConfig ? codeLensConfig['runmain'] : false; |
| 29 | + if (!codelensEnabled || !document.fileName.match('main.go')) { |
| 30 | + return []; |
| 31 | + } |
| 32 | + |
| 33 | + const codelenses = await Promise.all([ |
| 34 | + this.getCodeLensForMainFunc(document, token) |
| 35 | + ]); |
| 36 | + return ([] as CodeLens[]).concat(...codelenses); |
| 37 | + } |
| 38 | + |
| 39 | + // Return the first main function |
| 40 | + private async getMainFunc( |
| 41 | + doc: vscode.TextDocument, |
| 42 | + token: vscode.CancellationToken |
| 43 | + ): Promise<vscode.DocumentSymbol | undefined> { |
| 44 | + const documentSymbolProvider = new GoDocumentSymbolProvider(true); |
| 45 | + const symbols = await documentSymbolProvider.provideDocumentSymbols(doc, token); |
| 46 | + if (!symbols || symbols.length === 0) { |
| 47 | + return; |
| 48 | + } |
| 49 | + const symbol = symbols[0]; |
| 50 | + if (!symbol) { |
| 51 | + return; |
| 52 | + } |
| 53 | + const children = symbol.children; |
| 54 | + |
| 55 | + return children.find(sym => sym.kind === vscode.SymbolKind.Function && mainFuncRegx.test(sym.name)); |
| 56 | + } |
| 57 | + |
| 58 | + private async getCodeLensForMainFunc(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> { |
| 59 | + const mainPromise = async (): Promise<CodeLens[]> => { |
| 60 | + const mainFunc = await this.getMainFunc(document, token); |
| 61 | + if (!mainFunc) { |
| 62 | + return []; |
| 63 | + } |
| 64 | + |
| 65 | + return [ |
| 66 | + new CodeLens(mainFunc.range, { |
| 67 | + title: 'run', |
| 68 | + command: 'go.main.run', |
| 69 | + arguments: [{ functionName: mainFunc.name }] |
| 70 | + }), |
| 71 | + new CodeLens(mainFunc.range, { |
| 72 | + title: 'package run', |
| 73 | + command: 'go.main.package', |
| 74 | + arguments: [{ functionName: mainFunc.name }] |
| 75 | + }) |
| 76 | + ]; |
| 77 | + }; |
| 78 | + |
| 79 | + return await mainPromise(); |
| 80 | + } |
| 81 | +} |
0 commit comments