forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoDoctor.ts
101 lines (89 loc) · 2.74 KB
/
goDoctor.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-disable @typescript-eslint/no-unused-vars */
/*---------------------------------------------------------
* 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, isAbsolute } from 'path';
import { toolExecutionEnvironment } from './goEnv';
import { promptForMissingTool } from './goInstallTools';
import { getBinPath } from './util';
import vscode = require('vscode');
/**
* Extracts function out of current selection and replaces the current selection with a call to the extracted function.
*/
export function extractFunction() {
return () => 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() {
return () => extract('var');
}
type typeOfExtraction = 'var' | 'extract';
async function extract(type: typeOfExtraction): Promise<void> {
const 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 ${type === 'var' ? 'variable' : 'method'}`
);
return;
}
const newName = await vscode.window.showInputBox({
placeHolder: `Please enter a name for the extracted ${type === 'var' ? 'variable' : 'method'}.`
});
if (!newName) {
return;
}
runGoDoctor(newName, activeEditor.selection, activeEditor.document.fileName, type);
}
/**
* @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<void> {
const godoctor = getBinPath('godoctor');
return new Promise((resolve, reject) => {
if (!isAbsolute(godoctor)) {
promptForMissingTool('godoctor');
return resolve();
}
cp.execFile(
godoctor,
[
'-w',
'-pos',
`${selection.start.line + 1},${selection.start.character + 1}:${selection.end.line + 1},${
selection.end.character
}`,
'-file',
fileName,
type,
newName
],
{
env: toolExecutionEnvironment(),
cwd: dirname(fileName)
},
(err, stdout, stderr) => {
if (err) {
vscode.window.showErrorMessage(stderr || err.message);
}
}
);
});
}