Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Mar 2, 2019
1 parent bbb7047 commit 2b987d3
Showing 1 changed file with 42 additions and 73 deletions.
115 changes: 42 additions & 73 deletions src/goDoctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,53 @@ import vscode = require('vscode');
import cp = require('child_process');
import { getBinPath, getToolsEnvVars } from './util';
import { promptForMissingTool } from './goInstallTools';
import { dirname } from 'path';
import { resolve } from 'dns';
import { dirname, isAbsolute } from 'path';

/**
* Extracts function out of current selection and replaces the current selection with a call to the extracted function.
*/
export function extractFunction() {
let activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (activeEditor.selections.length !== 1) {
vscode.window.showInformationMessage(
'You need to have a single selection for extracting method'
);
return;
}
let showInputBoxPromise = vscode.window.showInputBox({
placeHolder: 'Please enter a name for the extracted function.'
});
showInputBoxPromise.then((functionName: string) => {
if (typeof functionName === 'string') {
runGoDoctor(
functionName,
activeEditor.selection,
activeEditor.document.fileName,
'extract'
).then(errorMessage => {
if (errorMessage) {
vscode.window.showErrorMessage(errorMessage);
}
});
}
});
extract('extract');
}

/**
* Extracts expression out of current selection into a var in the local scope and
* replaces the current selection with the new var.
*/
export function extractVariable() {
extract('var');
}

type typeOfExtraction = 'var' | 'extract';

async function extract(type: typeOfExtraction): Promise<void> {
let activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
vscode.window.showInformationMessage('No editor is active.');
return;
}
if (activeEditor.selections.length !== 1) {
vscode.window.showInformationMessage(
'You need to have a single selection for extracting variable'
`You need to have a single selection for extracting ${type === 'var' ? 'variable' : 'method'}`
);
return;
}
let showInputBoxPromise = vscode.window.showInputBox({
placeHolder: 'Plese enter a name for the extracted variable.'
});
showInputBoxPromise.then((varName: string) => {
if (typeof varName === 'string') {
runGoDoctor(
varName,
activeEditor.selection,
activeEditor.document.fileName,
'var'
).then(errorMessage => {
if (errorMessage) {
vscode.window.showErrorMessage(errorMessage);
}
});
}

const newName = await vscode.window.showInputBox({
placeHolder: 'Please enter a name for the extracted variable.'
});
}

type typeOfExtraction = 'var' | 'extract';
if (!newName) {
return;
}

runGoDoctor(
newName,
activeEditor.selection,
activeEditor.document.fileName,
type
);
}

/**
* @param newName name for the extracted method
Expand All @@ -94,41 +68,36 @@ function runGoDoctor(
selection: vscode.Selection,
fileName: string,
type: typeOfExtraction
): Thenable<string> {
let godoctor = getBinPath('godoctor');
): Thenable<void> {
const godoctor = getBinPath('godoctor');

return new Promise((resolve, reject) => {
console.log(selection);
let args = [
'-w',
'-pos',
`${selection.start.line + 1},${selection.start.character +
1}:${selection.end.line + 1},${selection.end.character}`,
'-file',
fileName,
type,
newName
];
console.log(args);
let p = cp.execFile(
if (!isAbsolute(godoctor)) {
promptForMissingTool('godoctor');
return resolve();
}

cp.execFile(
godoctor,
args,
[
'-w',
'-pos',
`${selection.start.line + 1},${selection.start.character +
1}:${selection.end.line + 1},${selection.end.character}`,
'-file',
fileName,
type,
newName
],
{
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);
vscode.window.showErrorMessage(stderr || err.message);
}
}
);
if (p.pid) {
p.stdin.end();
}
});
}

0 comments on commit 2b987d3

Please sign in to comment.