Visual Code Editor refactoring task before and after paste - refactoring

Let's say I have the following example code in Visual Studio Code:
expect(Miyagi.ApplyTraining).toHaveBeenCalledWith(danielSan, 'Sand Floor');
expect(Miyagi.ApplyTraining).toHaveBeenCalledWith(danielSan, 'Wash Car');
expect(Miyagi.ApplyTraining).toHaveBeenCalledWith(danielSan, 'Wax Car');
expect(Miyagi.ApplyTraining).toHaveBeenCalledWith(danielSan, 'Paint Fence');
I want to refactor it quickly like this:
it('',()=>{
expect(Miyagi.ApplyTraining).toHaveBeenCalledWith(danielSan, 'Sand Floor');
});
it('',()=>{
expect(Miyagi.ApplyTraining).toHaveBeenCalledWith(danielSan, 'Wash Car');
});
it('',()=>{
expect(Miyagi.ApplyTraining).toHaveBeenCalledWith(danielSan, 'Wax Car');
});
it('',()=>{
expect(Miyagi.ApplyTraining).toHaveBeenCalledWith(danielSan, 'Paint Fence');
});
Is there a way of highlighting a block and specifying a "before" and "after" each-line paste option?
Obviously I can write my own refactor tool outside of this editor but I thought I'd ask the community first!

Okay so you actually can do this pretty quickly with shortcuts...!!
e.g. Highlight "expect" on the first line and then press [CTRL+D] repeatedly until all "expects" are highlighted and then press [home] (you should have a cursor in front of each line flashing).
Now type the "it('',()=>{" and press [return].
Press [END] and then [return]. Finally type the last part "});"
Here's a few other tips:
I commonly use these helpful shortcuts too (saves you having to highlight the entire line):
[Alt]+[Up or Down] moves a line up/down.
[Shift]+[Alt]+[Up] copies the line your on to the line above.
[Ctrl]+[Shift]+[k] removes the line your on.

Related

Change variable name in multiple lines in visual studio

So, I have some code for a Rect variable mageSection:
And I'd like to copy the same code for a different variable warriorSection. To achieve this:
So I'd like to know if there is a visual studio shortcut for allow you to change variable name in multiple line in visual studio without refactoring the whole variable name.i.e. I don't have to manually change those names from mageSection to warriorSectionfor these 4 lines.
It is a question about shortcut in visual studio rather than writing functions, as I'd like to know the shortcut for this. Many thanks!
Copy and paste the section of code.
Now select the pasted code and hit Ctrl-H to bring up the Quick Replace dialog.
In the top box, type "mageSection".
In the bottom box, type "warriorSection".
Hit Enter and it will find the first occurrence and replace it.
Hit Enter three more times...done.
If I'm understanding your question correctly you should be able to hold down alt while clicking into multiple lines and change them all at once. Then hit ESC to exit multiline editing.

Get outside parentheses in Visual Studio 2013

All right, this is stupid, but I have no clue how people deal with this and I'm hoping I'm missing something...
When I write something like this:
if (n == 0)
...The closing parenthesis gets added as soon as I type the opening one. The only way I know of to get out of them now is to reach over and tap the End key or something, which kind of ... Damages my shui, you know? And yet apparently they thought this feature was a cool idea, so...
What do you normally do to tell the IDE that you're done with this bit and you're ready to move on to the next? Same question applies to automatic quotes and stuff in the XAML editor, I guess.
Just keep typing - if you type your own ) when the cursor is just before the automatic one, it should not create another ), but just move the cursor over it.
(Disclaimer: I'm not certain this works on a vanilla Visual Studio - I have a bunch of extensions installed)
You can press Ctrl + Shift + Enter to open a new line below the current one without moving the cursor to the end of the line.
To add to this, Tab is another option and I find it easier:
In your example, type the logic, press Tab to skip passed the end bracket.
If you are inside auto quotes, type your text, then press Tab twice.
Like the other answer, it only applies when first being typed, so if you go back to edit something, you are left using the End and Arrow keys.
In Visual Studio 2015 with Productivity Power Tools installed (probably works earlier as well), when being inside an auto-complete block (quotes or braces):
end goes to line end ( typically slow to type/find)
"/) typing same key as auto-complete already inserted will simply replace the auto-inserted character
tab jumps over end of auto-complete (e. g. braces or quotes)
shift+enter adds ; at end of line, goes to new line (often what you want)
ctrl+shift+enter goes to new line
There is a short key in VS - "Edit.LineEnd" - pressing "End" you get to the end of the current line. I've re-assigned it (for me the best variant was "Alt-Enter") and use it to get outside of parentheses and quotes.
Hit the enter key when you are done typing.

Visual Studio 2010 Code snippet

i have managed to make a code snippet and its all good, the question is i dont want to use ctrl+k ctrl +S then click on the snippet , instead i want to use a combination of keys to just use the snippet right away , or at least pressing ctrl+k ctrl + S then another combination ?
is that possible ?
thanks in advance...
If you say you're pressing Ctrl+K, Ctrl+S is because the snippet you added was a Surround With snippet, isn't it?
I suggest you installing ReSharper for this purpose. You can easily add all kind of snippets (called templates in ReSharper), and of course, Surround-With snippets.
After adding a template, you can give it the number you want.
Then all you have to do is press: Ctrl+Alt+J and you will see the following:
And all you have to do is press the number of the snippet you want.
For example, if you want to surround some code with an if, you press: Ctrl+Alt+J, 1.
Note:
In this moment, I don't really remember if Ctrl+Alt+J is the keyboard shortcut by default, because I have changed many of them, but you can check it in ReSharper > Edit > Surround With Template...
Easier than typing a key-combination is just typing the name+[Tab]+[Tab]. Visual Studio is pretty good at remembering the last ones used to make it even easier.
"f+[tab]+[tab]" creates a for loop snippet.
In your code snippet file, you can set up a shortcut element. Your code snippets are located under(depending on language):
"\Documents\Visual Studio 2010\Code
Snippets\Visual C#\My Code Snippets"
Under the CodeSnippet\Header\Shortcut section in the code snippet file, type a unique short relevant series of characters.
<CodeSnippet>
<Header>
<Shortcut>spi</Shortcut>
</Header>
Typing these characters followed by tab in the code window will insert your code snippet.

Visual Studio Editor: Remove Structured IF/End If

Is there and easy way to remove the IF/End If structured pairing.
Being able to do this in one keystroke would be nice (I also use Refactor! Pro)
Right now this is what I do:
Delete the IF line
Watch Visual Studio reformat the code to line up correctly taking into account that the IF is Missing.
Navigate to the End If
Delete the End If line
i.e. In the following example I want to change the code from
IF Value = True Then
DoSomething()
DoSomething2()
End IF
To
DoSomething()
DoSomething2()
While this is not a literal refactoring in the sense specified by Martin Fowler's book Refactoring, This is how I use resharper to achieve this goal:
Move/click on like with if statement
Press control + delete to delete the line
Press Alt + enter, and the option remove braces will be the first one specified.
Press enter
Done. Not quite simple, but the keystrokes are short, and not too complicated, and I don't have to spend/waste time with dumb arrow keys or the mouse to accomplish this type of code change.
Resharper supports VB.net code as of 4.0, I believe.

Can you set Visual Studio's "smart indent" to not remove tabs in blank lines?

When Visual Studio (2005) has Options -> Text Editor -> C/C++ -> Tabs -> Indenting set to Smart it will automatically indent code blocks and line up squiggly brackets, {}, as expected. However, if you hit enter inside a code block, move the cursor to another line, and then move it back, the inserted tabs are gone and the cursor is positioned all the way to the left. Is there a way to set Visual Studio to keep these tabs?
As far as I know, the only way to do that is to enter something (anything) on that line, then delete it. Or hit space and you'll never see it there until you return to that line.
Once VS determines that you've edited a line of text, it won't automatically modify it for you (at least, not in that way that you've described).
This is an annoyance to myself as well. Anytime the code is reformatted the blank lines are de-tabbed.
You might look at this: http://visualstudiogallery.msdn.microsoft.com/ac4d4d6b-b017-4a42-8f72-55f0ffe850d7 it's not exactly a solution but a step in the right direction

Resources