简体   繁体   English

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

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

In Eclipse, there's this really handy shortcut, mapped to CTRL + 2 + L by default, which works when an expression is selected.在 Eclipse 中,有一个非常方便的快捷方式,默认情况下映射到CTRL + 2 + L ,它在选择表达式时起作用。 What it does is to create a new local variable to hold the result of the expression.它所做的是创建一个新的局部变量来保存表达式的结果。 For example...例如...

this.doSomeCalculation();

If the mouse cursor is positioned over the line above, CTRL + 2 + L will turn the line into...如果鼠标光标位于上面的行上, CTRL + 2 + L将把该行变成...

double someCalculation = this.doSomeCalculation()

I find myself using this shortcut a lot when coding Java.我发现自己在编写 Java 代码时经常使用这个快捷方式。 Is there something similar available for editing Typescript in Visual Studio Code ?是否有类似的东西可用于在Visual Studio Code 中编辑 Typescript?

You can assign keybinding to refactorings such as extract constant.您可以将键绑定分配给重构,例如提取常量。

Here's a keybinding that binds ctrl shift e to the extract constant refactoring:这是一个将ctrl shift e绑定到提取常量重构的键绑定:

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

This keybinding will work in JavaScript and TypeScript (and in any other languages that have an extract constant refactoring)此键绑定适用于 JavaScript 和 TypeScript(以及具有提取常量重构的任何其他语言)

PS Here is a slight variation for JS/TS that lets a single keybinding work for both extract type and extract constant: PS 这是 JS/TS 的一个细微变化,它允许单个键绑定同时用于提取类型和提取常量:

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

I managed to get this working through a bit of trial and error in keybindings.json .我设法通过在keybindings.json的一些试验和错误来解决这个问题。 For you I think the mapping will look something like:对你来说,我认为映射看起来像:

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

I'm personally using ctrl + alt + space :我个人使用ctrl + alt + space

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

Almost similar thing on vscode on this link在此链接上的 vscode 上几乎类似

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

It shows how to extract a part of code to local variable.它展示了如何将一部分代码提取到局部变量中。 It's a little diferent from eclipse.它与日食有点不同。 On vscode it needs to "select" the statement and then press ctrl + shift + R then pops a window where you need to select to extract to local variable.在 vscode 上,它需要“选择”该语句,然后按 ctrl + shift + R 然后弹出一个窗口,您需要在其中选择提取到局部变量。

You could configure Keyboard Shortcut to Ctr + 2 l.您可以将键盘快捷键配置为 Ctr + 2 l。

Really it is not same thing but...真的不是一回事,但...

You can select an expression and then:您可以选择一个表达式,然后:

In Mac:在 Mac 中:

Opt + Cmd + V选项+ Cmd + V

In Windows:在 Windows 中:

Ctrl + Alt + V Ctrl + Alt + V

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

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