繁体   English   中英

“将表达式结果分配给新的局部变量”的 VSCode 键盘快捷键?

[英]VSCode keyboard shortcut for 'Assign expression result to new local variable'?

在 Eclipse 中,有一个非常方便的快捷方式,默认情况下映射到CTRL + 2 + L ,它在选择表达式时起作用。 它所做的是创建一个新的局部变量来保存表达式的结果。 例如...

this.doSomeCalculation();

如果鼠标光标位于上面的行上, CTRL + 2 + L将把该行变成...

double someCalculation = this.doSomeCalculation()

我发现自己在编写 Java 代码时经常使用这个快捷方式。 是否有类似的东西可用于在Visual Studio Code 中编辑 Typescript?

您可以将键绑定分配给重构,例如提取常量。

这是一个将ctrl shift e绑定到提取常量重构的键绑定:

{
  "key": "ctrl+shift+e",
  "command": "editor.action.refactor",
  "args": {
    "kind": "refactor.extract.constant",
    "apply": "first"
  }
}

此键绑定适用于 JavaScript 和 TypeScript(以及具有提取常量重构的任何其他语言)

PS 这是 JS/TS 的一个细微变化,它允许单个键绑定同时用于提取类型和提取常量:

{
  "key": "ctrl+shift+e",
  "command": "editor.action.refactor",
  "args": {
    "kind": "refactor.extract",
    "preferred": true,
    "apply": "first"
  }
}

我设法通过在keybindings.json的一些试验和错误来解决这个问题。 对你来说,我认为映射看起来像:

[
  {
    "key": "ctrl+2 ctrl+l",
    "command": "editor.action.codeAction",
    "args": {
      "kind": "refactor.assign.variable"
    },
    "when": "editorHasCodeActionsProvider && editorTextFocus && !editorReadonly"
  }
]

我个人使用ctrl + alt + space

[
  {
    "key": "ctrl+alt+space",
    "command": "editor.action.codeAction",
    "args": {
      "kind": "refactor.assign.variable"
    },
    "when": "editorHasCodeActionsProvider && editorTextFocus && !editorReadonly"
  }
]

在此链接上的 vscode 上几乎类似

https://code.visualstudio.com/docs/java/java-editing

它展示了如何将一部分代码提取到局部变量中。 它与日食有点不同。 在 vscode 上,它需要“选择”该语句,然后按 ctrl + shift + R 然后弹出一个窗口,您需要在其中选择提取到局部变量。

您可以将键盘快捷键配置为 Ctr + 2 l。

真的不是一回事,但...

您可以选择一个表达式,然后:

在 Mac 中:

选项+ Cmd + V

在 Windows 中:

Ctrl + Alt + V

暂无
暂无

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

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