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

Goimpl implementation #939

Merged
merged 4 commits into from
Jun 4, 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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@
"title": "Go: Generate unit tests for function",
"description": "Generates unit tests for the selected function in the current file"
},
{
"command": "go.impl.cursor",
"title": "Go: Generate interface stub",
"description": "Generates and inserts stub methods for implementing the provided interface at the cursor."
},
{
"command": "go.import.add",
"title": "Go: Add Import",
Expand Down Expand Up @@ -730,4 +735,4 @@
]
}
}
}
}
69 changes: 69 additions & 0 deletions src/goImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*---------------------------------------------------------
* 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 cp = require('child_process');
import { getBinPath } from './util';
import { promptForMissingTool } from './goInstallTools';

interface GoImplInput {
receiver: string;
interface: string;
}

// Supports only passing interface, see TODO in implCursor to finish
const inputRegex = /^(\w+\ \*?\w+\ )?([\w.]+)$/;

export function implCursor() {
let cursor = vscode.window.activeTextEditor.selection;
return vscode.window.showInputBox({
placeHolder: 'f *File io.Closer',
prompt: 'What interface do you want to implement?'
}).then(implInput => {
if (typeof implInput === 'undefined') {
return;
}
const matches = implInput.match(inputRegex);
if (!matches) {
vscode.window.showInformationMessage(`Not parsable input: ${implInput}`);
return;
}

// TODO: automatically detect type name at cursor
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, if you use the cursor to determine the type, then where would you propose to place the stub. End of the file?

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 was thinking of right after the type. So it would need some detection to find the first empty line after the type.

Copy link
Contributor

Choose a reason for hiding this comment

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

@fatih Where does vim-go paste the stub while taking the identifier under the cursor as receiver? Am referring to https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt#L665

Copy link

Choose a reason for hiding this comment

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

@ramya-rao-a currently it just places the generated stub under the cursor. It's not in place. There is an open PR that aims to fix it: josharian/impl#16

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @fatih

// if matches[1] is undefined then detect receiver type
// take first character and use as receiver name

let input: GoImplInput = {
receiver: matches[1],
interface: matches[2]
};

runGoImpl(input, cursor.start);
});
}

function runGoImpl(input: GoImplInput, insertPos: vscode.Position) {
let goimpl = getBinPath('impl');
let editor = vscode.window.activeTextEditor;
let p = cp.execFile(goimpl, [input.receiver, input.interface], (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('impl');
return;
}

if (err) {
vscode.window.showInformationMessage(`Cannot stub inteface: ${stderr}`);
return;
}

let output = stdout;
vscode.window.activeTextEditor.edit(editBuilder => {
editBuilder.insert(insertPos, stdout);
});
});
p.stdin.end();
}
3 changes: 2 additions & 1 deletion src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function getTools(goVersion: SemVersion): { [key: string]: string } {
'go-symbols': 'github.com/acroca/go-symbols',
'guru': 'golang.org/x/tools/cmd/guru',
'gorename': 'golang.org/x/tools/cmd/gorename',
'gomodifytags': 'github.com/fatih/gomodifytags'
'gomodifytags': 'github.com/fatih/gomodifytags',
'impl': 'github.com/josharian/impl'
};
if (goLiveErrorsEnabled()) {
tools['gotype-live'] = 'github.com/tylerb/gotype-live';
Expand Down
5 changes: 5 additions & 0 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { clearCacheForTools } from './goPath';
import { addTags, removeTags } from './goModifytags';
import { parseLiveFile } from './goLiveErrors';
import { GoCodeLensProvider } from './goCodelens';
import { implCursor } from './goImpl';

export let errorDiagnosticCollection: vscode.DiagnosticCollection;
let warningDiagnosticCollection: vscode.DiagnosticCollection;
Expand Down Expand Up @@ -117,6 +118,10 @@ export function activate(ctx: vscode.ExtensionContext): void {
removeTags(args);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.impl.cursor', () => {
implCursor();
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to add this command in package.json as well, so that the command shows up in the command pallet.

Am curious, how did you test this without the command showing up in the command pallet?

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 registered the command to a key command in keybindings.json

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah! thought so

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added this to the package.json in commit b640cc3

}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.cursor', (args) => {
let goConfig = vscode.workspace.getConfiguration('go');
testAtCursor(goConfig, args);
Expand Down