Vscode: Add support for code extract into variable and method

Created on 2 Nov 2016  路  12Comments  路  Source: microsoft/vscode

This feature exist in Jet brains products and I find it very useful to refactoring your code.
Basically you :

  1. select a piece of code
  2. click on some short key (let's say ctrl+alt+v || ctrl+alt+m)
  3. choose a name for the variable or method (if method choose also the scope)
  4. replace the selected or all occurrences of the code with the variable (or method call)

Example:

var arr = [1,2,3,4,5,6,7];
for( var i = 0; i< arr.length; i++){
 var a = arr[i];
 var b = arr[i] *2;
 console.log(a,b);
}

in this case if we select arr[i]; , click ctrl+alt+v , type in 'value', we will get

var arr = [1,2,3,4,5,6,7];
for( var i = 0; i< arr.length; i++){
 var value = arr[i];
 var a = value ;
 var b = value  *2;
 console.log(a,b);
}
feature-request javascript on-testplan

Most helpful comment

Not sure why this has to be closed. This more or less is the functionality you expect from the vscode, as its a code editor.

Extensions would become clumsy & not maintained properly. For this kind of functionality, this part of the app should be developed & maintained by the product owner than the community

All 12 comments

There's an extension for JS refactoring: https://marketplace.visualstudio.com/items?itemName=cmstead.jsrefactor

I don't use it myself so I can't tell how well it works, but it lists "Extract Variable" functionality in their README, so you might give it a try.

Close this first as you can archive this through extensions.

Not sure why this has to be closed. This more or less is the functionality you expect from the vscode, as its a code editor.

Extensions would become clumsy & not maintained properly. For this kind of functionality, this part of the app should be developed & maintained by the product owner than the community

This was added for c# two years ago, couldn't that have been done with an extension? https://code.visualstudio.com/updates/v0_7_0#_c35

I have a working poc of an extension that supports "extract to method" for javascript which supports types (typescript/flow) in the created function/method.
I plan to publish it in two weeks or so.

@rebornix Could you reconsider your position?

I know you love extensions but I think every day developers using VSCode starts to have way too much extensions and why should we use an extension for such a basic feature?

Or maybe offer official language extensions that would at least group various features that could be enabled/disabled from a clean ui interface.

+1 This would be fantastic

As far as i see - we already have method/function extraction - https://code.visualstudio.com/updates/v1_16#_javascript-and-typescript-refactorings

Should we close this and create new (or find existing one) feature request?

Maybe rename this feature request to just extract variable?

Extract to constant added with ts 2.6

extract constant

You can try it out in the current insiders builds

I wonder why it is not implemented in the c# plugin yet... I agree that it should be inside a plugin, not in the editor.

Great to see this in the new release! Is there any way to bind a keyboard shortcut to extract as a constant?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

borekb picture borekb  路  3Comments

biij5698 picture biij5698  路  3Comments

VitorLuizC picture VitorLuizC  路  3Comments

villiv picture villiv  路  3Comments

lukehoban picture lukehoban  路  3Comments