Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract a TypeScript method in Visual Studio Code

Is there a possibility to extract a method with a shortcut in Visual Studio Code when writing TypeScript?

function printOwing() {
  printBanner();

  // Print details
  console.log("name:  " + name);
  console.log("amount " + amount);
}

So that with one shortcut I could extract the printDetails():

function printOwing() {
  printBanner();
  printDetails();
}

function printDetails() {
  console.log("name:  " + name);
  console.log("amount " + amount);
}

Similar to Eclipse → right click → RefactorExtract method….

like image 567
sandrozbinden Avatar asked Apr 11 '17 17:04

sandrozbinden


1 Answers

Extract method and extract function were added in Visual Studio Code 1.16: JavaScript and TypeScript refactorings

To use them, select a block of code and then either click on the lightbulb or use the editor.action.quickFix action (cmd.) to view the list of refactorings.

You can also setup a keybinding for extract method:

{
  "key": "ctrl+shift+r ctrl+e",
  "command": "editor.action.codeAction",
  "args": {
    "kind": "refactor.extract.function"
  }
}
like image 101
Matt Bierner Avatar answered Sep 18 '22 10:09

Matt Bierner