Disable Visual Studio code formatting in Razor - visual-studio

This has been asked before: Why doesn't Visual Studio code formatting work properly for Razor markup?
But that question is a couple years old. And Razor formatting is still completely unacceptable. I have given up on fixing it.
Can I disable ALL Visual Studio formatting for CSHTML files? If not, can I disable all Visual Studio formatting entirely? Any 3rd party hacks I can use? There's got to be something!
I've upgraded to 2013 and the formatting is just as bad.
[EDIT]: 2015 is god-awful as well. I REALLY wish they would fix this.
[EDIT]: 2017 is god-awful as well. I REALLY wish they would fix this.
[EDIT]: 2019 is god-awful as well. I REALLY wish they would fix this. That said, Microsoft released a new formatting engine for .Net Core only.
[EDIT]: 2022 is... Well you know. If they ignore this 12 more years I may retire before seeing it fixed!

Under Tools -> Options -> Text Editor -> HTML -> Advanced
there is a key value item Paste which says "Format on paste" and has a boolean value next to it. Changing this to false has disabled formatting on paste for me in razor syntax.
I am using VS Professional 2013, Version 12.0.30110.00 Update 1

You cannot. This is built in "feature" of VS since the first .net version (visual studio 2002/2003).
There are tons of connect report about the formatting bugs, but Microsoft ignores them completly, or pushing the fix to the "next version". You can see an example Microsoft answer here.
You can post your own feedback to Microsoft Team here

The places to look:
Tools -> Options -> Web Essentials -> HTML -> Auto-format HTML on Enter
(Requires installation of Web Essentials extension)
Set to False
Tools -> Options -> Text Editor -> HTML -> Advanced -> Format on Paste
Set to False
With these settings I have no trouble in Visual Studio 2013.
I used to have to press Ctrl-Z far too often, and if you forgot untangling the mess once your code was written and tested was a huge pain. I feel Microsoft could do alot to help users configure this as well as make it more accessible.

What did the trick for me was pressing CTRL+Z after pasting, then the formatting is removed.

In my case R# ended up being the culprit, can be disabled here:
ReSharper > Options > Code Editing > Razor > Editor & Formatting
https://www.jetbrains.com/help/resharper/2016.1/Reference__Options__Languages__Razor__Editor.html

Unfortunately the only way to prevent autoformatting on paste is to comment out the location where you're going to paste some new code and after the pasting operation to remove the commenting instructions.
Search & Replace seems not to trigger auto-formatting. But pasting reformats the entire code block.
This is terrible beyond words. It makes Visual Studio IDE virtually unusable for Razor editing. One has to use an external editor for Razor files.
What's even worse, even the latest Visual Studio 2013 Update 1 crashes while editing Razor views all the time.

The fun part is, you can disable C# formatting and HTML formatting but not Razor formatting.
I've unchecked "Tools > Options > Text Editor > C# > Formatting > Automatically format on paste" and the same for HTML.
Which results in the following fun experience in cshtml files:
Paste HTML code: no format
Paste C# code: no format
Paste Razor code: format everything wrong
And since there is no option to change the Razor options, you have to live with this I guess.
My "solution": paste and ctrl-z to undo the auto format

I do not have a .Net Core project nor Blazor. So unfortunately I cannot test this. But a new formatting engine has been released by Microsoft for these languages.
https://devblogs.microsoft.com/aspnet/new-experimental-razor-editor-for-visual-studio/
Requires the latest edition of Visual Studio 2019.

I found these settings in Resharper 2019, which have improved my experience:

The below does not directly answer the posed question, but offers a potential fix that may be helpful.
In my situation, the auto formatting was only preforming exceptionally poorly for some files. This turned out to be related to the type of line feed used.
To resolve:
I enabled advanced save options
When saving the problematic files, I selected Windows (CR LF) as the line feed type
Supporting docs
After doing this, autoformat appeared to behave better

Related

Disabling Intellisense with VS 2022 editing non-source files

I'm editing some .INI files with detailed comments in VS 2022 (v. 17.3.6), and it keeps bugging me with auto completion, word guessing, and ending quotes where unneeded...
Is there a simple way to stop it from helping me?
As I go back and forth to C# files is helpful, but not with plain text!
Tools -> Options -> IntelliCode
Change the settings from there.

VS Intellisense: removing some autocomplete words

I like that the intellisense in VS corrects me when I make mistakes, but sometimes I don't make any mistakes and it corrects me still. I'm coding for Unity and I cannot write "var" without it being changed to "SerializePrivateVariables" as soon as I press space.
Is there a way to remove some of the autocomplete triggers in the intellisense? I couldn't find how to do so.
You cannot disable specific words from autocompletion in C# in Visual Studio 2015 as far as I know.
In your case, however, you can enable keywords in completion lists. Then the intellisense should understand that var is a proper C# and not replace it.
You can do this by going to TOOLS > Options > Text Editor > C# > Intellisense and checking the "Place keywords in completion lists" option.
There is a bit more options in VS2017 and VS2013 (and older), but none of them have the exact feature you have in mind.

Visual Studio replace tab with 4 spaces?

Is there a way to set TAB button to work as 4 spaces in Visual Studio 2010 instead of going to Edit->Advanced->Untabify Selected Lines?
You can edit this behavior in:
Tools->Options->Text Editor->All Languages->Tabs
Change Tab to use "Insert Spaces" instead of "Keep Tabs".
Note you can also specify this per language if you wish to have different behavior in a specific language.
First set in the following path Tools->Options->Text Editor->All Languages->Tabs
if still didn't work modify as mentioned below
Go to Edit->Advanced->Set Indentation ->Spaces
For VS2010 and above (VS2010 needs a plugin).
If you have checked/set the options of the tab size in Visual Studio but it still won't work. Then check if you have a .editorconfig file in your project! This will override the Visual Studio settings. Edit the tab-size in that file.
This can happen if you install an Angular application in your project with the Angular-Cli.
See MSDN blog
None of these answer were working for me on my macbook pro. So what i had to do was go to:
Preferences -> Source Code -> Code Formatting -> C# source code.
From here I could change my style and spacing tabs etc. This is the only project i have where the lead developer has different formatting than i do. It was a pain in the butt that my IDE would format my code different than theirs.
If you don't see the formatting option, you can do Tools->Import and Export settings to import the missing one.
For Visual Studio 2019 users:
By the comment under accepted answer, link:
Well... This is "almost" still the same in VS 2019... if you already done that and seems not to work, go to: Tools > Options, and then Text Editor > Advanced > Uncheck "Use adaptive formatting" as seen here

Is there an add-on to get better code formatting?

I've used the Banner style indention scheme for years (http://en.wikipedia.org/wiki/Indent_style), but the last two releases of Microsoft Visual Studio no longer handle this correctly and all day long I'm having to manually reformat code which is a huge pain.
Does anyone know if there are add-on tools that support Banner style indention inside Visual Studio?
Or perhaps you've found a way to trick VS into handling Banner style correctly?
Sure. Resharper can handle this.
Resharper has a 'Code Cleanup' feature that can reformat your entire code base according to your indentation preferences.

Visual Studio not displaying compile time errors in editor

For example, when I write:
string x = "turtle";
x.Go();
There is no red squiggly line detecting the absence of the Go() method on String.
Only when I compile does the error get detected.
I've just upgraded to Windows 7, I have Visual Studio 2008.
In my old environment the errors were detected before the actual compile.
Is there a setting that I am missing?
EDIT: "Tools -> Options -> Text Editor -> C# -> Underline errors in the editor" is checked.
I dont have the "Live Semantic" option. Maybe I need to go to SP1?
You need to turn on the underline errors in the editor and show live semantic errors options in Visual Studio.
These options can be found here:
Tools > Options > Text Editor > C# > Advanced > Editor Help
Edit: You will need to install SP1 for this functionality to work.
Select Tool -> Options, then Text Editor. Under the language you are using (ie C#), go to the Advanced and make sure the Underline errors in the editor and Show live semantic errors are checked
Stop the project.
Open Folder Project.
Delete .vs folder (he is a hidden folder)
Then restart Visual Studio
EDIT:
This approach has been around since the 2012 version of Visual Studio. This folder consists of keeping all breakpoint information and other settings saved. It is not known why, the configurations arrive at a time when the errors of compilations no longer appear. Deleting the .vs folder will "reset" your breakpoints forcing you to do them again if you need to.
For visual studio 2015 and higher:
Go to: Tools > Options > Text Editor > C# > Advanced > Editor Help
Then select: Enable Full solution analysis
I had the same issue and had SP1 installed and had Underline errors in the editor and Show live semantic errors checked in VS2008's options.
My solution was to download Microsoft Visual Studio 2008 Service Pack 1 (Installer) and re-install the package. It wasn't classified as a 'repair' or a 're-install' despite the fact it was already installed, but it worked.
Restarting VS solved my problem once.
my solution; I know it won't help like 80% of the viewers, but for the sake of who it will:
i have had a lot of noise in the IOS part of the solution, a VS bug that showed a lot of errors that weren't supposed to appear, so I just deleted the IOS part because I didn't really needed it as I didn't even had a Mac server to test it on... Something happened after that and the squiggly line returned! Seriously, VS team, fix your bugs...
JavaScript Type Checking
Sometimes type checking your JavaScript code can help you spot mistakes you might have not caught otherwise. You can run the TypeScript type checker against your existing JavaScript code by simply adding a // #ts-check comment to the top of your file.
// #ts-nocheck
let easy = true;
easy = 42;
Tip: You can also enable the checks workspace or application wide by adding "javascript.implicitProjectConfig.checkJs": true to your workspace or user settings and explicitly ignoring files or lines using // #ts-nocheck and // #ts-ignore. Check out the docs on JavaScript in VS Code to learn more.
In my case the problem was that I created a file with .s extension instead of .cs an then changed the extension to .cs once it was created.
I deleted it and created again correctly and now VS is underlining the errors in this file.

Resources