Turn off "smart semicolon" in Visual Studio C# editor - visual-studio

In Visual Studio 2019, it likes to try to guess where it should put the semicolon when you hit it. Often it will jump to a completely different line or insert newlines. Every time it does anything besides append it as the next character, this is extremely disruptive, and I have to go back and fix what it broke. This is akin to the disruptive "Automatic brace completion" which always puts braces where I don't want them, but can be turned off. I can't find anywhere to turn off the semicolon behavior. Is there any way to turn this feature off?
Most of the time when the semicolon misbehaves, it's because I hit it by mistake, but rather than hitting backspace, I now have a bigger mess to clean up. And I've never had a situation where it did something extra that I wanted it to do.
Some examples, with * being the cursor location:
// between a ) and ;
Foo()*;
// inserts an extra space
Foo();* ;
// before an existing semicolon:
return null*;
// moves the old semicolon to the next line:
return null;*
;
// pressing semicolon in the middle of a multi-line function call:
string path = Path.Combine(
"C:\\",
"b"*
"filename.txt");
// moves the cursor to the end of the block and inserts a "; " before ";":
string path = Path.Combine(
"C:\\",
"b"
"filename.txt");* ;

Visual Studio 16.10 adds a new configuration option to control this:
Text Editor -> C# -> Intellisense -> Automatically complete statements on semicolon
With that checked, I get the problematic behavior described in the question. When I uncheck that option, it works as it did before.

Related

Visual Studio 2015 - brackets not automatically formatting when pressing shift + enter

Normally, to create a new bracket block I would type:
"{" then enter
This produces:
{
}
however, (I have seen this on two different machines so far, it might be the default setting) if like me, you like to keep shift held down, or accidentally keep it down and instead press shift + enter, it creates a new line underneath.
{ }
//current cursor position
Leaving the un-formatted brackets behind!
This is really annoying as I nearly always do this, and have to fiddle around for a little second to get the brackets back to where I want them. What can I do to change the behavior of shift + enter?
Go to Options > Environment > Keyboard
Search for Edit.SmartBreakLine and remove the assigned shortcut (Shift + Enter)
Search for Edit.BreakLine, click on "Press shortcut keys:" textbox, press Shift + Enter and click Assign
If it doesn't work try restarting Visual Studio.
Update: This issue is fixed in Visual Studio 2015 Update 1 - SmartBreakLine works as expected.
We stumbled on this thread and it felt like we could make a nice improvement here to Shift+Enter (SmartBreakLine).
So, in cases where a block was opened { } and if shift was held down intentionally or unintentionally, we now do this:
{
|
}
instead of
{ }
|
this way people who are used to using shift+enter to complete the line (in C#, it adds a semicolon to the end of line if needed, formats the line, adds a new line after current line) do not have to lose those functionalities by remapping the shortcut to BreakLine.
See: https://github.com/dotnet/roslyn/pull/5790
this should make it in the next update of Vs2015 and we hope you like it.
It works well in my VS 2015. Try resetting all the settings.
Tools > Import and Export Settings > Reset all settings > Next > Visual C#
You can reset for all other languages similar like this.
Hope this helps!

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.

How to quickly move to the end of generated braces/parenthesis/etc in Visual Studio with Resharper?

My google fu is failing me. Visual Studio and Resharper helpfully provide closing brackets and the like. For example, typing "var foo = new Foo(" immediately inserts a ). Great! So I go to insert a bunch of other stuff. I know I can just type in the closing characters and it'll gloss over fine, but when I do something like this...
var foo = new Foo(new Bar()
{
Something = 48,
SomethingElse = new Fred(new Bob())
};
Right after typing Bob the rest is generated. I want to be able to cleanly move to the end of that generated code and move to the next line without touching the arrow keys. A simpler example:
if (something)
{
Something();
}
That last curly brace is made for me, but I want to go past it without touching the arrow keys. Is there any shortcut that I'm missing?
Just keep typing. Type the closing ")" (ReSharper will move past it), then the semicolon (ReSharper will enter it).

Does anyone know a visual studio keyboard short cut to swap around two sides of a statement?

Just wondering if anyone knows the keyboard shortcut to swap around two sides of a statement. For example:
I want to swap
firstNameTextbox.Text = myData.FirstName;
to
myData.FirstName = firstNameTextbox.Text;
Does anyone know the shortcut, if there is one? Obviously I would type them out, but there is a lot of statements I need to swap, and I think a shortcut like that would be useful!
Feel free to throw in any shortcuts you think are cool!
My contribution would be CTRL + E, D - this will format your code to Visual Studio standards! Pretty well known I'm guessing but I use it all the time! :)
UPDATE
Just to let everyone know, using a bit of snooping of the article that was posted, I managed to construct a regular expression, so here it is:
Find:
{.+\.Text = myData\..+};
And replace with:
\2 = \1;
Hopefully people can apply this to their own expressions they want to swap!
I think the following thread is a good place to begin with
Invert assignment direction in Visual Studio
Here's how I would go about doing that without a specific keyboard shortcut:
First, select the text you want to modify and replace
" = " with " = "
(the key here is to add a lot of spaces).
If you hold down Alt and use the mouse, you can select a "block" of code. Use this to select only the text on the right side of the equation (it's helpful to add extra white space here in your selection)
Use the same Alt + Left-Click combination to select the beginning of the left side (just select a blank area). You should be able to paste text into here.
If you added extra white space to the text you just added, just should be able to easily insert an = using the Alt + Click technique. Use the same trick to remove the equal sign that's dangling on the right side of your code block.
While this might not do exactly what you're looking for, I've found these tricks quite useful.
If you're using ReSharper, you can do this by pressing CtrlAltShift + ← or →
The feature is in Resharper. Select the code segment and click the content wizard, which is a pencil icon in the left corner reading View Actions List, then choose Reverse Assignment.
It is done.
swap-word is a VSCode extension which sounds like it would do what you want.
Quickly swap places two words or selections...
But I'm not sure if it is compatible with VS.
Since I was not happy with the answers where I need to enter complicated strings into the Visual Studio search/replace dialog, I wrote myself a little AutoHotkey script, that performs the swaps with only the need to press a keyboard shortcut. And this, no matter if you are in VS or in another IDE.
This hotkey (start it once simply from a textfile as script or compiled to exe) runs whenever Win+Ctrl-S is pressed
#^s Up::
clipboard := "" ; Empty the clipboard
Sendinput {Ctrl down}c{ctrl up}
Clipwait
Loop, Parse, clipboard, `n, `r ; iterates over seperates lines
{
array := StrSplit(RegExReplace(A_LoopField,";",""),"=") ; remove semicolon and split by '='
SendInput, % Trim(array[2]) . " = " . Trim(array[1]) . ";{Enter}"
}
return
Many more details are possible, e.g. also supporting code where lines end with a comma
...and I can put many more hotkeys and hotstrings into the same script, e.g. for my most mistyped words:
::esle::else ; this 1 line rewrites all my 'else' typos
I recommend using the find-replace option in Visual Studio. IMHO the REGEX string is not that complicated, and moreover, you don't need to understand the expression in order to use it.
The following regex string works for most programming languages:
([\w\.]+)\s*=\s*([\w\.]+)
For Visual Studio's you want to use $ argument in the replace text.
$2 = $1
Make sure to enable regex.
To do this in one shot, you can select a section of the document, and click the replace-all option.
Before:
comboBoxAddOriginalSrcTextToComment.SelectedIndex = Settings.Default.comboBoxAddOriginalSrcTextToComment;
comboBoxDefaultLanguageSet.SelectedIndex = Settings.Default.comboBoxDefaultLanguageSet;
comboBoxItemsPerTransaltionRequest.SelectedIndex = Settings.Default.comboBoxItemsPerTransaltionRequest;
comboBoxLogFileVerbosityLevel.SelectedIndex = Settings.Default.comboBoxLogFileVerbosityLevel;
comboBoxScreenVerbosityLevel.SelectedIndex = Settings.Default.comboBoxScreenVerbosityLevel;
After:
Settings.Default.comboBoxAddOriginalSrcTextToComment = comboBoxAddOriginalSrcTextToComment.SelectedIndex;
Settings.Default.comboBoxDefaultLanguageSet = comboBoxDefaultLanguageSet.SelectedIndex;
Settings.Default.comboBoxItemsPerTransaltionRequest = comboBoxItemsPerTransaltionRequest.SelectedIndex;
Settings.Default.comboBoxLogFileVerbosityLevel = comboBoxLogFileVerbosityLevel.SelectedIndex;
Settings.Default.comboBoxScreenVerbosityLevel = comboBoxScreenVerbosityLevel.SelectedIndex;
IMHO: It's better for a developer to learn to use the IDE (Integrated Development Environment), then to create new tools to do the same thing the IDE can do.

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