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

Commit

Permalink
Goimpl implementation (#939)
Browse files Browse the repository at this point in the history
* Add goimpl to tools list.

* Support github.com/josharian/impl. Fixes #259.

Does some basic input validation to ensure the required information is
passed.

The regex included makes the receiver name optional, the intention there
is that if the receiver name isn't included, that the type the cursor is
in should be used as the receiver for the interface.

However, I think that the first step of just having the ability to use
goimpl was a important first step.

* Missing semicolon lint fix.

* Add `go.impl.cursor` to package.json
  • Loading branch information
itsjamie authored and ramya-rao-a committed Jun 4, 2017
1 parent 2f398ae commit 4167655
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
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 @@ -790,4 +795,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
// 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 @@ -32,6 +32,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 @@ -116,6 +117,10 @@ export function activate(ctx: vscode.ExtensionContext): void {
removeTags(args);
}));

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

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

0 comments on commit 4167655

Please sign in to comment.