Skip to content

Commit fa778fe

Browse files
committed
feat: add running main func code lens
1 parent 8984ac8 commit fa778fe

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed

docs/settings.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,14 @@ Feature level setting to enable/disable code lens for references and run/debug t
210210
| --- | --- |
211211
| `references` | If true, enables the references code lens. Uses guru. Recalculates when there is change to the document followed by scrolling. Unnecessary when using the language server; use the call graph feature instead. <br/> Default: `false` |
212212
| `runtest` | If true, enables code lens for running and debugging tests <br/> Default: `true` |
213+
| `runmain` | If true, enables code lens for running main func <br/> Default: `true` |
213214

214215
Default:
215216
```
216217
{
217218
"references" : false,
218219
"runtest" : true,
220+
"runmain" : true,
219221
}
220222
```
221223
### `go.formatFlags`
@@ -492,7 +494,7 @@ Allowed Options: `package`, `workspace`, `off`
492494
Default: `"package"`
493495
### `gopls`
494496

495-
Customize `gopls` behavior by specifying the gopls' settings in this section. For example,
497+
Customize `gopls` behavior by specifying the gopls' settings in this section. For example,
496498
```
497499
"gopls" : {
498500
"build.directoryFilters": ["-node_modules"]

package.json

+12-1
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@
481481
"command": "go.global.resetState",
482482
"title": "Go: Reset Global State",
483483
"description": "Reset keys in global state to undefined."
484+
},
485+
{
486+
"command": "go.runMain",
487+
"title": "Go: Run main() func on file",
488+
"description": "Run main() func on file"
484489
}
485490
],
486491
"breakpoints": [
@@ -1579,12 +1584,18 @@
15791584
"type": "boolean",
15801585
"default": true,
15811586
"description": "If true, enables code lens for running and debugging tests"
1587+
},
1588+
"runmain": {
1589+
"type": "boolean",
1590+
"default": true,
1591+
"description": "If true, enables code lens for running main func"
15821592
}
15831593
},
15841594
"additionalProperties": false,
15851595
"default": {
15861596
"references": false,
1587-
"runtest": true
1597+
"runtest": true,
1598+
"runmain": true
15881599
},
15891600
"description": "Feature level setting to enable/disable code lens for references and run/debug tests",
15901601
"scope": "resource"

src/goMain.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +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';
64+
import { GoMainCodeLensProvider, runMainFunc } from './goMainCodelens';
6565
import { disposeGoStatusBar, expandGoStatusBar, outputChannel, updateGoStatusBar } from './goStatus';
6666
import {
6767
debugPrevious,
@@ -566,6 +566,12 @@ If you would like additional configuration for diagnostics from gopls, please se
566566
})
567567
);
568568

569+
ctx.subscriptions.push(
570+
vscode.commands.registerCommand('go.runMain', (args) => {
571+
runMainFunc()
572+
})
573+
)
574+
569575
ctx.subscriptions.push(
570576
vscode.commands.registerCommand('go.show.commands', () => {
571577
const extCommands = getExtensionCommands();

src/goMainCodelens.ts

+52-12
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
'use strict';
99

1010
import vscode = require('vscode');
11+
import cp = require('child_process');
12+
1113
import { CancellationToken, CodeLens, TextDocument } from 'vscode';
1214
import { getGoConfig } from './config';
1315
import { GoBaseCodeLensProvider } from './goBaseCodelens';
1416
import { GoDocumentSymbolProvider } from './goOutline';
15-
import { getBenchmarkFunctions, getTestFunctions } from './testUtils';
16-
17-
const mainFuncRegx = /^main$/u;
17+
import { getBinPath } from './util';
18+
import { envPath, getCurrentGoRoot } from './utils/pathUtils';
19+
import { reject } from 'lodash';
1820

1921
export class GoMainCodeLensProvider extends GoBaseCodeLensProvider {
20-
private readonly mainRegex = /^main.+/;
22+
private readonly mainRegex = /^main$/;
2123

2224
public async provideCodeLenses(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
2325
if (!this.enabled) {
@@ -52,7 +54,7 @@ export class GoMainCodeLensProvider extends GoBaseCodeLensProvider {
5254
}
5355
const children = symbol.children;
5456

55-
return children.find(sym => sym.kind === vscode.SymbolKind.Function && mainFuncRegx.test(sym.name));
57+
return children.find(sym => sym.kind === vscode.SymbolKind.Function && this.mainRegex.test(sym.name));
5658
}
5759

5860
private async getCodeLensForMainFunc(document: TextDocument, token: CancellationToken): Promise<CodeLens[]> {
@@ -64,13 +66,8 @@ export class GoMainCodeLensProvider extends GoBaseCodeLensProvider {
6466

6567
return [
6668
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',
69+
title: 'run main',
70+
command: 'go.runMain',
7471
arguments: [{ functionName: mainFunc.name }]
7572
})
7673
];
@@ -79,3 +76,46 @@ export class GoMainCodeLensProvider extends GoBaseCodeLensProvider {
7976
return await mainPromise();
8077
}
8178
}
79+
80+
const mainFuncOutputChannel = vscode.window.createOutputChannel('Go Main');
81+
82+
export async function runMainFunc() {
83+
let outputChannel = mainFuncOutputChannel
84+
const goRuntimePath = getBinPath('go');
85+
if (!goRuntimePath) {
86+
vscode.window.showErrorMessage(
87+
`Failed to run "go run ." as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot}) or PATH(${envPath})`
88+
);
89+
return Promise.resolve(false);
90+
}
91+
92+
const editor = vscode.window.activeTextEditor;
93+
const documentUri = editor ? editor.document.uri : null;
94+
const args = ['run', documentUri.path];
95+
96+
outputChannel.clear()
97+
outputChannel.show(true)
98+
outputChannel.appendLine(["Running main func: ", goRuntimePath, ...args].join(' '))
99+
100+
cp.execFile(
101+
goRuntimePath,
102+
args,
103+
{ },
104+
(err, stdout, stderr) => {
105+
try {
106+
if (err) {
107+
outputChannel.appendLine(err.message);
108+
return;
109+
}
110+
if (stdout) {
111+
outputChannel.append(stdout);
112+
}
113+
if (stderr) {
114+
outputChannel.append(stderr);
115+
}
116+
} catch (e) {
117+
reject(e);
118+
}
119+
}
120+
)
121+
}

0 commit comments

Comments
 (0)