Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

RunTest with CodeLens #937

Merged
merged 7 commits into from
Apr 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { GoReferenceProvider } from './goReferences';
import { GoDocumentFormattingEditProvider, Formatter } from './goFormat';
import { GoRenameProvider } from './goRename';
import { GoDocumentSymbolProvider } from './goOutline';
import { GoRunTestCodeLensProvider } from './goRunTestCodelens';
import { GoSignatureHelpProvider } from './goSignature';
import { GoWorkspaceSymbolProvider } from './goSymbol';
import { GoCodeActionProvider } from './goCodeAction';
Expand Down Expand Up @@ -83,6 +84,7 @@ export function activate(ctx: vscode.ExtensionContext): void {
ctx.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(GO_MODE, new GoDocumentFormattingEditProvider()));
ctx.subscriptions.push(vscode.languages.registerRenameProvider(GO_MODE, new GoRenameProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeActionsProvider(GO_MODE, new GoCodeActionProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, new GoRunTestCodeLensProvider()));
ctx.subscriptions.push(vscode.languages.registerCodeLensProvider(GO_MODE, new GoCodeLensProvider()));

errorDiagnosticCollection = vscode.languages.createDiagnosticCollection('go-error');
Expand Down
60 changes: 60 additions & 0 deletions src/goRunTestCodelens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------*/

'use strict';

import vscode = require('vscode');
import { CodeLensProvider, TextDocument, CancellationToken, CodeLens, Command } from 'vscode';
import { getTestFunctions } from './goTest';
import { GoDocumentSymbolProvider } from './goOutline';

export class GoRunTestCodeLensProvider implements CodeLensProvider {
public provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
if (!document.fileName.endsWith('_test.go')) {
return;
}

return Promise.all([
this.getCodeLensForPackage(document),
this.getCodeLensForFunctions(document)
]).then(res => {
return res[0].concat(res[1]);
});
}

private getCodeLensForPackage(document: TextDocument): Thenable<CodeLens[]> {
let documentSymbolProvider = new GoDocumentSymbolProvider();
return documentSymbolProvider.provideDocumentSymbols(document, null)
.then(symbols => symbols.find(sym => sym.kind === vscode.SymbolKind.Package && !!sym.name))
.then(pkg => {
if (pkg) {
const range = pkg.location.range;
return [
new CodeLens(range, {
title: 'run package tests',
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 cents suggestion: s/run/Run. The same for run file tests, run test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about Run tests in package, Run tests in file and Run test ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer lowercase + shorter text to not get much attention. It should be as unobtrusive as possible so devs can focus on code an not on these actions. It's like this on C# codelens

Next enhancement could be debug test 😄

Copy link
Contributor

@ramya-rao-a ramya-rao-a Apr 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as unobtrusive as possible

well, when you put it that way it makes sense.

Next enhancement could be debug test

There is already an ask for it #879 :)

command: 'go.test.package'
}),
new CodeLens(range, {
title: 'run file tests',
command: 'go.test.file'
})
];
}
});
}

private getCodeLensForFunctions(document: TextDocument): Thenable<CodeLens[]> {
return getTestFunctions(document).then(testFunctions => {
return testFunctions.map(func => {
let command: Command = {
title: 'run test',
command: 'go.test.cursor',
arguments: [ { functionName: func.name} ]
};
return new CodeLens(func.location.range, command);
});
});
}
}
32 changes: 20 additions & 12 deletions src/goTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,32 @@ export function testAtCursor(goConfig: vscode.WorkspaceConfiguration, args: any)
return;
}
getTestFunctions(editor.document).then(testFunctions => {
let testFunction: vscode.SymbolInformation;
// Find any test function containing the cursor.
for (let func of testFunctions) {
let selection = editor.selection;
if (selection && func.location.range.contains(selection.start)) {
testFunction = func;
break;
}
};
if (!testFunction) {
let testFunctionName: string;

// We use functionName if it was provided as argument
// Otherwise find any test function containing the cursor.
if (args.functionName) {
testFunctionName = args.functionName;
} else {
for (let func of testFunctions) {
let selection = editor.selection;
if (selection && func.location.range.contains(selection.start)) {
testFunctionName = func.name;
break;
}
};
}

if (!testFunctionName) {
vscode.window.showInformationMessage('No test function found at cursor.');
return;
}

return goTest({
goConfig: goConfig,
dir: path.dirname(editor.document.fileName),
flags: getTestFlags(goConfig, args),
functions: [testFunction.name]
functions: [ testFunctionName ]
});
}).then(null, err => {
console.error(err);
Expand Down Expand Up @@ -237,7 +245,7 @@ export function goTest(testconfig: TestConfig): Thenable<boolean> {
* @param the URI of a Go source file.
* @return test function symbols for the source file.
*/
function getTestFunctions(doc: vscode.TextDocument): Thenable<vscode.SymbolInformation[]> {
export function getTestFunctions(doc: vscode.TextDocument): Thenable<vscode.SymbolInformation[]> {
let documentSymbolProvider = new GoDocumentSymbolProvider();
return documentSymbolProvider
.provideDocumentSymbols(doc, null)
Expand Down