Reputation: 7254
In Eclipse, there's this really handy shortcut, mapped to CTRL + 2 + L by default, which works when an expression is selected. 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...
double someCalculation = this.doSomeCalculation()
I find myself using this shortcut a lot when coding Java. Is there something similar available for editing Typescript in Visual Studio Code?
Upvotes: 30
Views: 12181
Reputation: 7780
I managed to get this working through a bit of trial and error in 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
:
[
{
"key": "ctrl+alt+space",
"command": "editor.action.codeAction",
"args": {
"kind": "refactor.assign.variable"
},
"when": "editorHasCodeActionsProvider && editorTextFocus && !editorReadonly"
}
]
Upvotes: 5
Reputation: 1444
You can select an expression and then:
In Mac:
Opt + Cmd + V
In Windows:
Ctrl + Alt + V
Upvotes: 1
Reputation: 65223
You can assign keybinding to refactorings such as extract constant.
Here's a keybinding that binds ctrlshifte to the extract constant refactoring:
{
"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)
P.S. Here is a slight variation for JS/TS that lets a single keybinding work for both extract type and extract constant:
{
"key": "ctrl+shift+e",
"command": "editor.action.refactor",
"args": {
"kind": "refactor.extract",
"preferred": true,
"apply": "first"
}
}
Upvotes: 6
Reputation: 192
Almost similar thing on vscode on this link
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.
You could configure Keyboard Shortcut to Ctr + 2 l.
Really it is not same thing but...
Upvotes: 6