This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
2f398ae
commit 4167655
Showing
4 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters