Skip to content

Commit 8984ac8

Browse files
committed
WIP: Add codelens for main func (but still implementing the cmds)
1 parent 2222461 commit 8984ac8

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/goMain.ts

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { GO111MODULE, goModInit, isModSupported } from './goModules';
6161
import { playgroundCommand } from './goPlayground';
6262
import { GoReferencesCodeLensProvider } from './goReferencesCodelens';
6363
import { GoRunTestCodeLensProvider } from './goRunTestCodelens';
64+
import { GoMainCodeLensProvider } from './goMainCodelens';
6465
import { disposeGoStatusBar, expandGoStatusBar, outputChannel, updateGoStatusBar } from './goStatus';
6566
import {
6667
debugPrevious,
@@ -210,9 +211,11 @@ If you would like additional configuration for diagnostics from gopls, please se
210211
})
211212
);
212213
const testCodeLensProvider = new GoRunTestCodeLensProvider();
214+
const mainCodeLensProvider = new GoMainCodeLensProvider();
213215
const referencesCodeLensProvider = new GoReferencesCodeLensProvider();
214216

215217
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, testCodeLensProvider));
218+
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, mainCodeLensProvider));
216219
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, referencesCodeLensProvider));
217220

218221
// debug
@@ -479,6 +482,7 @@ If you would like additional configuration for diagnostics from gopls, please se
479482

480483
if (updatedGoConfig['enableCodeLens']) {
481484
testCodeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['runtest']);
485+
mainCodeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['runmain']);
482486
referencesCodeLensProvider.setEnabled(updatedGoConfig['enableCodeLens']['references']);
483487
}
484488

src/goMainCodelens.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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

Comments
 (0)