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 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command and code action to extract into function and local variab…
…le (#2139)
- Loading branch information
1 parent
1f621cc
commit fa94793
Showing
4 changed files
with
153 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/*--------------------------------------------------------- | ||
* 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, getToolsEnvVars } from './util'; | ||
import { promptForMissingTool } from './goInstallTools'; | ||
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() { | ||
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 ${type === 'var' ? 'variable' : 'method'}` | ||
); | ||
return; | ||
} | ||
|
||
const newName = await vscode.window.showInputBox({ | ||
placeHolder: 'Please enter a name for the extracted variable.' | ||
}); | ||
|
||
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: getToolsEnvVars(), | ||
cwd: dirname(fileName) | ||
}, | ||
(err, stdout, stderr) => { | ||
if (err) { | ||
vscode.window.showErrorMessage(stderr || err.message); | ||
} | ||
} | ||
); | ||
}); | ||
} |
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
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,36 @@ | ||
/*--------------------------------------------------------- | ||
* 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'); | ||
|
||
export class GoRefactorProvider implements vscode.CodeActionProvider { | ||
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 | ||
); | ||
const extractVar = new vscode.CodeAction( | ||
'Extract to variable in local scope', | ||
vscode.CodeActionKind.RefactorExtract | ||
); | ||
extractFunction.command = { | ||
title: 'Extract to function in package scope', | ||
command: 'go.godoctor.extract' | ||
}; | ||
extractVar.command = { | ||
title: 'Extract to variable in local scope', | ||
command: 'go.godoctor.var' | ||
}; | ||
|
||
return [extractFunction, extractVar]; | ||
} | ||
} |