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

Commit

Permalink
Combine Open test file and Open implemention commands (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a authored Jan 23, 2017
1 parent 2a93232 commit fd09d3f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 42 deletions.
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,9 @@
"description": "install/update the required go packages"
},
{
"command": "go.open.test.file",
"title": "Go: Open Test File",
"description": "Opens test file (if any) corresponding to the Go file in the current active editor"
},
{
"command": "go.open.test.implementation",
"title": "Go: Open Implementation For Test File",
"description": "Opens the Go file with implementation for the test file in the current active editor"
"command": "go.toggle.test.file",
"title": "Go: Toggle Test File",
"description": "Toggles between file in current active editor and the corresponding test file."
}
],
"debuggers": [
Expand Down
41 changes: 13 additions & 28 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,26 @@ function checkActiveEditor(): vscode.TextEditor {
}

/**
* Opens test file (if any) corresponding to the Go file in the current active editor
* Toggles between file in current active editor and the corresponding test file.
*/
export function openTestFile(): void {
export function toggleTestFile(): void {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('Cannot open test file. No editor selected.');
vscode.window.showInformationMessage('Cannot toggle test file. No editor selected.');
return;
}
let filePath = editor.document.fileName;
if (!filePath.endsWith('.go')) {
vscode.window.showInformationMessage('Cannot open test file. File in the editor is not a Go file.');
let currentFilePath = editor.document.fileName;
if (!currentFilePath.endsWith('.go')) {
vscode.window.showInformationMessage('Cannot toggle test file. File in the editor is not a Go file.');
return;
}
let testFilePath = filePath.substr(0, filePath.lastIndexOf('.go')) + '_test.go';

vscode.commands.executeCommand('vscode.open', vscode.Uri.file(testFilePath));
}

/**
* Opens the Go file with implementation for the test file in the current active editor
*/
export function openImplementationForTestFile(): void {
let editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showInformationMessage('Cannot open file. No editor selected.');
return;
let targetFilePath = '';
if (currentFilePath.endsWith('_test.go')) {
targetFilePath = currentFilePath.substr(0, currentFilePath.lastIndexOf('_test.go')) + '.go';
} else {
targetFilePath = currentFilePath.substr(0, currentFilePath.lastIndexOf('.go')) + '_test.go';
}
let filePath = editor.document.fileName;
if (!filePath.endsWith('_test.go')) {
vscode.window.showInformationMessage('Cannot open file. File in the editor is not a Go test file.');
return;
}
let testFilePath = filePath.substr(0, filePath.lastIndexOf('_test.go')) + '.go';

vscode.commands.executeCommand('vscode.open', vscode.Uri.file(testFilePath));
vscode.commands.executeCommand('vscode.open', vscode.Uri.file(targetFilePath));
}

export function generateTestCurrentPackage(): Thenable<boolean> {
Expand Down Expand Up @@ -165,7 +150,7 @@ function generateTests(conf: Config): Thenable<boolean> {

vscode.window.showInformationMessage(message);
if (testsGenerated) {
openTestFile();
toggleTestFile();
}

return resolve(true);
Expand Down
8 changes: 2 additions & 6 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,8 @@ export function activate(ctx: vscode.ExtensionContext): void {
goGenerateTests.generateTestCurrentFunction();
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.open.test.file', () => {
goGenerateTests.openTestFile();
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.open.test.implementation', () => {
goGenerateTests.openImplementationForTestFile();
ctx.subscriptions.push(vscode.commands.registerCommand('go.toggle.test.file', () => {
goGenerateTests.toggleTestFile();
}));

vscode.languages.setLanguageConfiguration(GO_MODE.language, {
Expand Down

0 comments on commit fd09d3f

Please sign in to comment.