-
Notifications
You must be signed in to change notification settings - Fork 646
Goimpl implementation #939
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -117,6 +118,10 @@ export function activate(ctx: vscode.ExtensionContext): void { | |
removeTags(args); | ||
})); | ||
|
||
ctx.subscriptions.push(vscode.commands.registerCommand('go.impl.cursor', () => { | ||
implCursor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to add this command in Am curious, how did you test this without the command showing up in the command pallet? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I registered the command to a key command in keybindings.json There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! thought so There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @fatih