简体   繁体   English

在Visual Studio代码中提取TypeScript方法

[英]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? 在编写TypeScript时是否有可能在Visual Studio Code中使用快捷方式提取方法?

function printOwing() {
  printBanner();

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

So that with one shortcut I could extract the printDetails() : 因此,通过一个快捷方式,我可以提取printDetails()

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

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

Similar to Eclipse → right click → RefactorExtract method… . 与Eclipse类似→右键单击→ 重构提取方法....

Extract method and extract function were added in Visual Studio Code 1.16: JavaScript and TypeScript refactorings Visual Studio Code 1.16中添加了Extract方法和提取函数: JavaScript和TypeScript重构

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. 要使用它们,请选择一个代码块,然后单击灯泡或使用editor.action.quickFix操作( cmd )查看重构列表。

You can also setup a keybinding for extract method : 您还可以为extract方法设置键绑定

{
  "key": "ctrl+shift+r ctrl+e",
  "command": "editor.action.codeAction",
  "args": {
    "kind": "refactor.extract.function"
  }
}

Visual Studio Code doesn't ship with a default shortcut to extract methods in a TypeScript file (although it does look like it is supported in C# files ). Visual Studio Code没有提供默认快捷方式来提取TypeScript文件中的方法(尽管它看起来像C#文件中支持 )。

You might be able to find an extension with this capability on the marketplace , but I was not able to. 您可能能够在市场上找到具有此功能的扩展,但我无法做到。

The "refactorix" and "JS Refactorings" extensions have an extract variable, but no extract method. “refactorix”和“JS Refactorings”扩展具有提取变量,但没有提取方法。 So close! 很近!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM