Block auto-format ignore command in visual studio - visual-studio-2012

I love using the Auto Formatting tool in Visual Studio CTRL K + CTRL D However, there are times when VS makes the formatting just a tad off from what I want if, for instance, I'm making a detailed kind of pseudo-code that relies on specific comments and indentation. Is there a way I can still use the CTRL K + CTRL D command and set visual studio to ignore a certain block of code or set of line numbers? Also, if there's an answer in VS 2013 but not in 2012, please post because I may be upgrading soon. Thanks in advance.

I got hit by this problem today with my pseudo-code comments too and thought I'd share the way I solved it.
While there is no way to prevent Visual Studio from auto-formatting parts of code, there is a way to prevent it from auto-formatting pseudo-code comments.
Instead of using something this like this
// if condition
// do this
// else
// do something else
use the tripple-slashes /// instead
/// if condition
/// do this
/// else
/// do something else
As a bonus you get automatic insertion of /// in new lines and auto-indenting that keeps previous row's indentation level.
This seems not to depend on editor indentation options.
It also seems not to mess up XML documentation
(Yes, I know this is an old question. No, I do not want to take Cameron's right for accepted answer.)

Related

Resharper setting to analyze code only when I press Enter at the end of the line?

While I'm editing a line of code, I'd like to have access to Code Completion, etc.
But most of the time while editing, before having pressed Enter, syntax color coding, squigglies, and variable type hints appear and move things.
I would like to finish editing the line and analyze the line only when I press Enter.
I've checked the documentation, and I can turn it off entirely under Code Inspection > Settings. But there's no setting there to wait until I'm done.
I have found that you can swap Tab and Enter, but that's for Code Completion only.
I have the latest stable Visual Studio 2022 and the latest stable Resharper version.
Any solution that doesn't change a character or spacing on the line I'm editing is eligible. So it can analyze the line as long as it doesn't do this. But I'd prefer to defer analysis until I press the Enter key at the end of the line.
No, that's not possible. As you already mentioned, you can disable certain things, but you cannot defer its execution. This is a basic paradigm across Visual Studio and ReSharper (Rider as well) to give you feedback while editing, not after. Maybe using VS Code with the C# extension is an option for you.

How to Prevent ReSharper from Wrapping One-Line Statements?

What is the best way to prevent ReSharper from wrapping certain lines of code. For example, an if statement with one condition and a simple body should not be wrapped. ReSharper currently forces this line...
if (item == null) { return null; }
... to break like this:
if (item == null)
{
return null;
}
I don't want to blindly disable this feature necessarily... just make it a bit more intelligent.
Update for newer versions of ReSharper
(my version is 2021.1)
Since a picture is worth than a thousand words, you probably want to configure it like so. In this way it takes care of constructs such as using too:
Check the ones that start with Keep existing arrangement [...] if you want to instruct ReSharper to leave, say, single-line ifs alone if they are already written that way.
Took me longer to figure out than I wanted it to, hopefully it will help someone else.
You can change this in the R# Settings:
Open the menu Resharper -> Options
Go to Code Editing -> C# -> Formatting Style -> Line Breaks and Wrapping
Change to option Break line in single embedded statement to Put on a single line or Do not change
Edit:
As mentioned by #Alexander it would be more suitable to use the option Break Line in a block with a single statement which I think is available since R# 8
In latest Resharper it is under General >> "Wrap log lines"

How to configure rules in JSLint extension for Visual Studio 2012

I tried to use JSLint.VS2012 extension to check the JavaScript code. it is useful for it found a lot syntax issues in my code. However, is it possible to configure the rules in JSLint to enable/disable certain rules? Currently, I'd like to switch off 2 rules:
1) The code format checking. Because I use Visual Studio 2012 as the editor, it has a default code formatter, the formated code is OK for me. But, seems JSLint requires a different code format style, so I prefer VS2012 auto format feature, and need switch off the JSLint rule. Here is a snapshot shows JSLint complaining the 'bad' formatted code.
2) The second I want to be disabled rule is "Unexpected 'else' after 'return':
I don't this rule is good, but guarded return could improve the readability of if-else switches, below is a snapshot:
This has been silent long enough I'll at least answer for VS 2008 and hope it's similar for the VS 2012 plugin.
Question 1: Go to your tools menu. Select "JSLint Options". Look for "Tolerate sloppy whitespace", and check it. Hopefully the 2012 version has a similar checkbox.
Question 2: The second is a little nastier. I don't believe there's a setting to turn it off, but even the VS plugin just uses the JSLint javascript file under the hood (or at least the 2008 version does via Noesis). So if you want, you can open up jslint.js and edit it.
If you edit this part, the error is ignored:
if (peek(0).id === '}' && peek(1).id === 'else') {
// commented out >>> this.warn('unexpected_else');
}
Voila. The JSLint code really is good, clean stuff that's easy to customize. But I am required to say that you really should take JSLint's advice, and in this case, I'd tend to agree. But inherited code? Comment the line out and keep moving along.

Tab behavior Resharper Intellisense Options

A product like Resharper has many settings, and I can't seem to find the right settings to get the exact behavior I desire from it.
The behavior I am trying to change is this:
Suppose the endresult I want is
but the line currently is
As you expect I start typing:
and after a tab I get this
but I wanted it to become
Does anyone know how I can change the Resharper settings in such a manner that my desired behavior is realized?
Thank you.
EDIT: Note that tab does perform the desired behavior in the default VS Intellisense.
In this particular case, here's what you should do:
Go to ReSharper > Options > Environment > IntelliSense > IntelliSense Behavior, and set Automatically insert parentheses after completion to Opening only.
Now, with the caret right before SomeMethod(), type in Math.Ab, and complete Abs with Enter, as correctly suggested by Rob H. You'll end up with Math.Abs(SomeMethod();
Finally, press Ctrl+Shift+Enter to invoke the Complete Statement, which in this case will insert the required parenthesis at the end of the statement, and put the caret on the next line.
Keep in mind though that this might not be the optimal solution provided that you probably have plenty of other usages of code completion that could conflict with this kind of settings change.
If that kind of similar code modification is required in multiple places, you should possibly create a search and replace pattern instead.
Hitting Enter instead of Tab will insert without overwriting, although you then end up with
Math.Abs()SomeMethod();

How to Make a Nice Block of Commenting in Visual Studio

I just switched back to c++ after leaving it for awhile and I can't seem to remember how to make nice function/class comment boxes in VS. What I'm looking for is something like this:
/**
* Convenience struct: coord
* -------------------------
* Simple C++ struct (which is like a class, except there are
* no methods and everything is public) that bundles two related
* pieces of data together.
*/
struct coord {
int row;
int col;
};
I don't, however, want to have to format out the nice starring and alignment every time I want to write a new comment. I feel like I used to be able to just type '/**' and then hit return and it would automatically make a comment section for me where every time I hit return a new star would appear aligned with the others. Is there a way to activate this in VS 2010?
Visual Assist is the tool you require. If you can move away from MSVC IDE, can try Eclipse also.
You should be able to record a nice macro to do this, then bind it to an unused button/shortcut key combo and your set, else its pretty trivial to code a simple VS plugin to do this with some extra embelishments
Try GhostDoc for a starting point. It is fairly configurable and gives you a documentaion template with one hotkey sequence.
[Edit]
I mistook this for a C# question. As Billy pointed out, GhostDoc is not a good solution for C++ in VS.
Try this SO question about "GhostDoc for C++?" for some other ideas.

Resources