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

Commit

Permalink
Make requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aswinmprabhu committed Feb 16, 2019
1 parent 72d5422 commit 373c228
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 78 deletions.
146 changes: 72 additions & 74 deletions src/goDoctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,27 @@ export function extractFunction() {
return;
}
if (activeEditor.selections.length !== 1) {
vscode.window.showInformationMessage('You need to have a single selection for extracting method');
vscode.window.showInformationMessage(
'You need to have a single selection for extracting method'
);
return;
}
let showInputBoxPromise = vscode.window.showInputBox({ placeHolder: 'Plese enter a name for the extracted function.' });
let showInputBoxPromise = vscode.window.showInputBox({
placeHolder: 'Please enter a name for the extracted function.'
});
showInputBoxPromise.then((functionName: string) => {
runGoDoctorExtract(functionName, activeEditor.selection, activeEditor).then(errorMessage => {
if (errorMessage) {
vscode.window.showErrorMessage(errorMessage);
}
});
if (typeof functionName === 'string') {
runGoDoctor(
functionName,
activeEditor.selection,
activeEditor.document.fileName,
'extract'
).then(errorMessage => {
if (errorMessage) {
vscode.window.showErrorMessage(errorMessage);
}
});
}
});
}

Expand All @@ -46,91 +57,78 @@ export function extractVariable() {
return;
}
if (activeEditor.selections.length !== 1) {
vscode.window.showInformationMessage('You need to have a single selection for extracting variable');
vscode.window.showInformationMessage(
'You need to have a single selection for extracting variable'
);
return;
}
let showInputBoxPromise = vscode.window.showInputBox({ placeHolder: 'Plese enter a name for the extracted variable.' });
showInputBoxPromise.then((varName: string) => {
runGoDoctorVar(varName, activeEditor.selection, activeEditor).then(errorMessage => {
if (errorMessage) {
vscode.window.showErrorMessage(errorMessage);
}
});
let showInputBoxPromise = vscode.window.showInputBox({
placeHolder: 'Plese enter a name for the extracted variable.'
});
}

/**
* @param functionName name for the extracted method
* @param selection the editor selection from which method is to be extracted
* @param activeEditor the editor that will be used to apply the changes from godoctor
* @returns errorMessage in case the method fails, null otherwise
*/
export function runGoDoctorExtract(functionName: string, selection: vscode.Selection, activeEditor: vscode.TextEditor): Thenable<string> {
let godoctor = getBinPath('godoctor');

return new Promise((resolve, reject) => {
if (typeof functionName === 'undefined') {
return resolve('Function Name is undefined');
}
let args = [
'-w',
'-pos',
`${selection.start.line + 1},${selection.start.character + 1}:${selection.end.line + 1},${selection.end.character + 1}`,
'-file',
activeEditor.document.fileName,
'extract',
functionName,
];
let p = cp.execFile(godoctor, args, { env: getToolsEnvVars(), cwd: dirname(activeEditor.document.fileName) }, (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('godoctor');
return resolve('Could not find godoctor');
}
if (err) {
return resolve(`Could not extract function : \n\n${stderr}`);
}
});
if (p.pid) {
p.stdin.end();
showInputBoxPromise.then((varName: string) => {
if (typeof varName === 'string') {
runGoDoctor(
varName,
activeEditor.selection,
activeEditor.document.fileName,
'var'
).then(errorMessage => {
if (errorMessage) {
vscode.window.showErrorMessage(errorMessage);
}
});
}

});
}

type typeOfExtraction = 'var' | 'extract';

/**
* @param varName name for the extracted method
* @param selection the editor selection from which method is to be extracted
* @param activeEditor the editor that will be used to apply the changes from godoctor
* @returns errorMessage in case the method fails, null otherwise
*/
export function runGoDoctorVar(varName: string, selection: vscode.Selection, activeEditor: vscode.TextEditor): Thenable<string> {
* @param newName name for the extracted method
* @param selection the editor selection from which method is to be extracted
* @param activeEditor the editor that will be used to apply the changes from godoctor
* @returns errorMessage in case the method fails, null otherwise
*/
function runGoDoctor(
newName: string,
selection: vscode.Selection,
fileName: string,
type: typeOfExtraction
): Thenable<string> {
let godoctor = getBinPath('godoctor');

return new Promise((resolve, reject) => {
if (typeof varName === 'undefined') {
return resolve('Function Name is undefined');
}
console.log(selection);
let args = [
'-w',
'-pos',
`${selection.start.line + 1},${selection.start.character + 1}:${selection.end.line + 1},${selection.end.character + 1}`,
`${selection.start.line + 1},${selection.start.character +
1}:${selection.end.line + 1},${selection.end.character}`,
'-file',
activeEditor.document.fileName,
'var',
varName,
fileName,
type,
newName
];
let p = cp.execFile(godoctor, args, { env: getToolsEnvVars(), cwd: dirname(activeEditor.document.fileName) }, (err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('godoctor');
return resolve('Could not find godoctor');
console.log(args);
let p = cp.execFile(
godoctor,
args,
{
env: getToolsEnvVars(),
cwd: dirname(fileName)
},
(err, stdout, stderr) => {
if (err && (<any>err).code === 'ENOENT') {
promptForMissingTool('godoctor');
return resolve('Could not find godoctor');
}
if (err) {
return resolve(stderr);
}
}
if (err) {
return resolve(`Could not extract variable : \n\n${stderr}`);
}
});
);
if (p.pid) {
p.stdin.end();
}

});
}
}
13 changes: 9 additions & 4 deletions src/goRefactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import vscode = require('vscode');

export class GoRefactorProvider implements vscode.CodeActionProvider {
public provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeAction[]> {
public provideCodeActions(
document: vscode.TextDocument,
range: vscode.Range,
context: vscode.CodeActionContext,
token: vscode.CancellationToken
): vscode.ProviderResult<vscode.CodeAction[]> {
const extractFunction = new vscode.CodeAction(
'Extract to function in package scope',
vscode.CodeActionKind.RefactorExtract
Expand All @@ -19,13 +24,13 @@ export class GoRefactorProvider implements vscode.CodeActionProvider {
);
extractFunction.command = {
title: 'Extract to function in package scope',
command: 'go.godoctor.extract',
command: 'go.godoctor.extract'
};
extractVar.command = {
title: 'Extract to variable in local scope',
command: 'go.godoctor.var',
command: 'go.godoctor.var'
};

return [extractFunction, extractVar];
}
}
}

0 comments on commit 373c228

Please sign in to comment.