forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goImpl.ts
73 lines (64 loc) · 2.05 KB
/
goImpl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
'use strict';
import cp = require('child_process');
import { dirname } from 'path';
import vscode = require('vscode');
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool } from './goInstallTools';
import { getBinPath } from './util';
// Supports only passing interface, see TODO in implCursor to finish
const inputRegex = /^(\w+\ \*?\w+\ )?([\w./]+)$/;
export function implCursor() {
const editor = vscode.window.activeTextEditor;
if (!editor) {
vscode.window.showErrorMessage('No active editor found.');
return;
}
const cursor = editor.selection;
return vscode.window
.showInputBox({
placeHolder: 'f *File io.Closer',
prompt: 'Enter receiver and interface 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
runGoImpl([matches[1], matches[2]], cursor.start, editor);
});
}
function runGoImpl(args: string[], insertPos: vscode.Position, editor: vscode.TextEditor) {
const goimpl = getBinPath('impl');
const p = cp.execFile(
goimpl,
args,
{ env: toolExecutionEnvironment(), cwd: dirname(editor.document.fileName) },
(err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('impl');
return;
}
if (err) {
vscode.window.showInformationMessage(`Cannot stub interface: ${stderr}`);
return;
}
editor.edit((editBuilder) => {
editBuilder.insert(insertPos, stdout);
});
}
);
if (p.pid) {
p.stdin.end();
}
}