How to selectively suppress code formatting in Visual Studio - visual-studio-2010

I work with large C# files. I use the Edit > Format Document feature of VS2010 (Keyboard shortcut=> Ctrl+k, Ctrl+d) to format the layout of the code. Sometimes however, I would like to align code in a specific way; for readability.Question: Is it possible to isolate a section of code which I want the VS formatter to ignore?

How about using
Ctrl+k, Ctrl+f
Which is Format Selection

Related

Visual Studio -> wrap every parameter

Using Visual Studio 2019, I found a really useful refactoring option in 'Quick actions and refactoring':
(might come from PowerTools, whatever)
I just wondered how:
I can make this wrapping settings a default formatting settings
Or apply this formatting on my whole solution at once (without resharper)
For the later, a solution with Visual Studio Code would be perfect as well !
Thanks for your help
You can try using Rewrap extension which formats code, comments, and other text to a given line length (80 by default).
The main Rewrap command is: Rewrap Code / Comment, by default bound to
Alt+Q. Put the text cursor inside a comment line, block or plain text
paragraph and invoke the command to wrap. You can also select just a few lines, or multiple comments in one selection.
There is currently an Open request for this in the VS Code Issue tracker on GitHub

How to automatically format XAML code in Visual Studio?

Here:
https://social.msdn.microsoft.com/Forums/de-DE/b77c7529-298f-4b9a-874a-f94f699986ac/automatically-formatting-xaml-code?forum=vswpfdesigner
... it is written that one can use "Ctrl+K+D" ... but that didn't work.
I also tried "shift + alt + F", which was suggested here:
How do you format code in Visual Studio Code (VSCode)
... it didn't work either.
So my question is: how can you automatically format XAML code in Visual Studio?
Probably your formatting options are wrong, go to: Tools -> Options -> Text Editor -> XAML -> Formatting -> Spacing and on Attribute Spacing section click "Position each attribute on a separete line" and then try "Ctrl+K+D" again.
I would check out XAML Styler, which is a Visual Studio extension to help format your XAML source code (full disclosure, I am one of the owners on the project).
Edit: Forgot to mention that Visual Studio does not have a very rich set of XAML formatting capabilities on its own.
Shortcut key for XAML formatting in Visual Studio for Mac machines: Ctrl+I
In case you guys still don't know how...
It's called 'Format Document', you can find it inside menu:
Edit => Advanced => Format Document
There is also shortcut for it:
Ctrl+E then Ctrl+D
This works for most document, including XAML.
EDIT: When this post was made, I was using VS 2017
I use Xaml Formatter. Works pretty good for me. You will just have to assign code formatting with Xaml Formatter to a Ctrl+K Ctrl+D shortcut in Xaml files, since it is not done by default (it's creating Format Xaml option in Tools menu).
Also Inline Color Picker is a very simple but extremely useful extension if you are developing a lot of Xaml code.
Try using "XML Tools" for VS Code, the shortcut is Alt + Shift + F
For anyone who is searching for kind of combining "Insert a single space between attributes" and "Position each attribute on a seperate line" I found this solution:
Tools -> Options -> Text Editor -> XAML -> Formatting -> General -> Tag Wrapping
and tick "Wrap tags that exceed specified length".
Self-explanatory that one can adjust the length in the following input field.

Formatting JavaScript code embedded in a cshtml file

Is it possible to format the JavaScript code embedded in a .cshtml file using the Visual Studio editor?
I tried the Ctrl+K, Ctrl+D keyboard shortcut, and also the "Format Selection" option in the context menu. However, neither of these seem to work on JavaScript code.
I just searched and found the PrettyJs plugin. I do understand that there might be other plugins that do the same thing, but this works perfectly well for me.
With this plugin, you can format your JavaScript code either as a document or by selection. Both can be done using the shortcut keys Ctrl+B, Ctrl+F and Ctrl+B, Ctrl+G, respectively.

How to triple-slash selected text in Visual Studio 2010

In Visual Studio 2010 we can Comment selected text via Ctrl+E,C - This prefixes selected lines with the double-slash.
Is there a way to select text and have VS2010 prefix the selected lines with a triple-slash?
For the curious; I am adding XML comments to my c# code. Specifically, I am adding example code within the <example><code></code></example> section of my comments. I'm am building the sample code in a throw-away console app (just to make sure I'm creating code that works). When satisfied with the sample code, I copy/paste it into my app that I'm adding XML Comments to. When I paste the sample code, I have to manually add all the triple-slashes. I'm getting kinda bored doing this, so I'm hoping there is a way I can have VS2010 do this for me.
There is no built-in command to do that, AFAIK.
However, you can do it using VS's built-in advanced text-editing features.
Hold down Alt, select a zero-width column of text at the beginning of the lines, then type ///. This will type into all of the lines simultaneously.
You can select that with the keyboard by pressing Home (as applicable), then repeatedly pressing Alt+Shift+↓.

Visual Studio 2010 Beta 2: Can I print in color?

I have to turn in a hard copy of some code with an assignment. Is there any way in Visual Studio 2010 to print C# source code with syntax highlighting?
PS: The assignment is solving a math problem, so the choice of language isn't important and the teacher doesn't need to compile and run the program. She just wants to see our approach and results.
There is an extension now :) Visual Studio 2010 Color Printing Extension
Works well! :)
The best way I've found to accomplish this is to copy from Visual Studio and paste into something like MS Word or OpenOffice Writer.
This gives you full source code, with syntax highlighting. You can then print from Word (including adding your intro documentation before the code, etc).
Just to let everyone know, unfortunately printing in color was cut from Visual Studio 2010 because of resource constraints. Since we've rewritten the editor from scratch in WPF, we didn't have time to reimplement everything so we had to sacrifice this feature. We will try to implement this in the next version of Visual Studio. For now, copy to clipboard and paste into other app such as Microsoft Word is the recommended solution for printing code with color.
If you go to Tools -> Options -> Environment -> Fonts and Colors you can change settings to print with syntax highlighting (change 'Show settings for' dropdown to 'Printer'). But you will need to change all the individual settings to match your IDE (I don't know of a way to make it automatic)
Edit: you can use that "Use..." button next to the dropdown to copy settings from the Text Editor
Simplest of all copy code to clipboard and paste into MS-Word is the way I do and it works...
Have a look at VS.NETcodePrint 2010 availabe from www.starprinttools.com. You will be able to print and export the color coded output to PDF.
Joginder Nahil
Due the fact MSVS does not support it anymore I think the best way is really to copy the code and paste it into WinWord.
The advantages are listed below. You can
set the font/size exactly how you want it.
set the format of line numbers.
have your own header/footer.
remove #region from printing.
add a watermark to the output.
For me - I print once in 2 months a source code - it is a very comfortable way which I never could achieve with any 3rd party extension.

Resources