Format Line not actually formatting. - c#

I have Resharper 9 installed and StyleCop as I'm trialling then to see if my work should get the tools installed for all developers (So I'm very new to using both tools).
StyleCop has a number of rules that affect line formatting, such as: SA1116. Resharper then picks up when these rules are violated and it offers the option to have it automatically fixed (as you can see below).
The only problem is when I actually hit enter nothing happens. Well nothing happens 80% of the time for this rule violation as well as for a bunch of the other formatting issues such as a space between a cast and a variable (e.g. (double) myDouble;)
It seems so erratic, does anybody know how to improve this or is it simply credit to StyleCop not completely integrating with Resharper? (In which case should I stop looking at recharper/stylecop combo as a "press button to fix" tool and more like "here's a warning, your welcome. Now if there's a button, press it you're lucky")
Cheers,

It might be worth raising this as an issue with the StyleCop project on CodePlex. However, the CodePlex project seems to be a bit abandoned - the current ReSharper 9 plugin is provided by a community member. There's a GitHub repo (although it doesn't have source) that you could use to try and file an issue.

Related

Current VS tools to view C# code differently per developer

Are there any tools for VS that allow developers to view code according to their style desires?
For instance, some developers like K&R style brackets while others like ANSI style.
The backing code file can be in whatever format it wants, but the viewed source needs to be in the developer style.
It can be done. You just have to set the indentation options in the Visual Studio options. Then when a developer loads a file, he can just select "Reformat File" from the Edit menu (I think it's in one of the advanced sub-menus) or go to the bottom of the file, remove the ending brace, and re-add it. The file will automatically be reformatted.
That said, this is a really bad idea. If you're using source control, then every time a developer checks out a file that was previously checked in by somebody who used a different formatting convention, that file is going to show as modified. So you'll have the potential for a lot of spurious checkins, and even if you somehow avoid the spurious checkins you have the problem that a developer might change one line of code, but a diff is going to show LOTS of changes because of all the formatting changes.
That's not the only problem. Let's say that a tester reports an error and a stack trace that says an exception occurred on line 263 of some module. You have to be sure to get the latest code from the source repository and keep it in the format that it was last compiled in. Otherwise, line 263 where the error was probably won't be line 263 in your editor.
Tell your developers to decide on ONE indentation style. Anything else is going to cause you no end of grief.
By the way, I gave this some serious thought a few years back and even wrote about it in Source Control and Formatting Standards. I thought it was a good idea provided I could build the tools to do the automatic conversion to canonical format. At the time, I didn't take into account the "where is line 263" problem. That one's a killer, and the tools to do the automatic conversion are no picnic, either. All in all, I decided that it's much easier and more reliable to just settle on a single style. It takes only a few days to get accustomed to an unfamiliar style.

If I turn off ReSharper 5's IntelliSense, what am I missing?

During my trial of ReSharper 5, I noticed its version of IntelliSense falls behind Visual Studio 2010's in three ways that were key to me:
ReSharper doesn't support IntelliSense in the "QuickWatch..." debugger utility.
ReSharper's IntelliSense seems to break down for me in .aspx files between the <%= %> tags.
I couldn't find a way to get a listing of properties within an object initializer block. (VS does this if you hit the space key.)
Thankfully ReSharper lets you use Visual Studio IntelliSense alongside its other great features. Am I missing out on anything great by not using ReSharper's IntelliSense?
Well you're losing quite a bit. Here are some quick facts about ReSharper code completion: http://www.jetbrains.com/resharper/webhelp/Coding_Assistance__Code_Completion.html
Smart Completion (Ctrl+Shift+Space in IntelliJ IDEA keymap) is especially useful because in common scenarios it gives you a narrow selection of symbols that you most likely want to complete, and in some cases acts as a shortcut to code generation features
By the way, Smart Completion is the kind of completion that you should use with object initializers: www.jetbrains.com/resharper/webhelp/Coding_Assistance__Code_Completion__Smart.html#object_initializers
As for completion within the <%= %> pair, this should work fine. Please let us know what exactly went wrong by submitting an issue to youtrack.jetbrains.net/issues/RSRP Thanks!
P.S. I work at JetBrains
A five minute comparison I'd say that ReSharper's gives you a bit of help in the typing.
So if you have a method that's A(int a, int b) and you hit ctrl+space when you select A it will add (), will place you in the middle of the brackets and will show you the information about the method. Other than that you're probably not missing much.
For me that is quite helpful, but if you're having trouble doing ASP.NET and those are known bugs / limitations its always best to pick the best tool for the job.
Personally I turn it off. It is annoying and slows me down. Here is a prime example:
No you're not missing much apart from a test runner that supports NUnit. I've had the same problem and also (rather worryingly), hideous performance. I tend to use the productivity power tools extension and the native refactor stuff instead. Info here:
http://visualstudiogallery.msdn.microsoft.com/en-us/d0d33361-18e2-46c0-8ff2-4adea1e34fef
R# also does stuff which I plainly do not want to do and my code ends up littered with commented resharper hints.
I find the CompleteCodeSmart functionality something I use frequently and save me alot of typing.

When I use WinForms (C#) designer in VS2010, it still generates code that StyleCop complains about

Some problems that I recall (there may be more):
Includes regions
Does not use this. prefix for member variables and methods
Includes comments like the one below ( having // by itself catches the eye of StyleCop)
//
// fileNameTextBox
//
If I make a change to the text, and then open the designer again, and screws up my previously perfected fruits of hard labor. How did / would you solve this problem?
I heard but did not personally experience a similar problem with WPF. How did / would you fix that?
Thanks.
There are several ways to make StyleCop ignore generated code:
StyleCop: How To Ignore Generated Code
As I recall it, ignoring generated code is the default setting (at least, it ignores my VS 2008 generated code with the standard settings).
I don't use VS 2010, so I don't know if they changed the designer generated files somehow so that StyleCop doesn't recognize them as generated anymore.
Maybe the link helps you to figure that out in your code.
StyleCop can be instructed to ignore generated source files. I don't know why designer-generated files have to adhere to any arbitrary coding standard – the only thing that should ever read or write them is an automated tool, not the developer.
Code styles are mainly there to help developers. Code generators certainly couldn't care less.

Is there an automatic code formatter for C#? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
In my work I deal mostly with C# code nowadays, with a sprinkle of java from time to time. What I absolutely love about Eclipse (and I know people using it daily love it even more) is a sophisticated code formatter, able to mould code into any coding standard one might imagine. Is there such a tool for C#? Visual Studio code formatting (Crtl+K, Crtl+D) is subpar and StyleCop only checks the source without fixing it.
My dream tool would run from console (for easy inclusion in automated builds or pre-commit hooks and for execution on Linux + Mono), have text-file based configuration easy to store in a project repository and a graphical rule editor with preview - just like the Eclipse Code Formatter does.
For Visual Studio, take a look at ReSharper. It's an awesome tool and a definite must-have. Versions after 4.0 have the code formatting and clean-up feature that you are looking for. There's also plugin integration with StyleCop, including formatting settings file.
You'll probably want Agent Smith plugin as well, for spell-checking the identifiers and comments. ReSharper supports per-solution formatting setting files, which can be checked into version control system and shared by the whole team. The keyboard shortcut for code cleanup is Ctrl + E, C.
In 'vanilla' Visual Studio, the current file can be automatically formatted with Ctrl + K, Ctrl + D, and Ctrl + K, Ctrl + F formats the selected text.
As for a runs-everywhere command line tool to be used with commit hooks, try NArrange. It's free, can process whole directories at once and runs on Mono as well as on Microsoft .NET.
Some people also use the Artistic Style command line tool, although it requires Perl and works better with C/C++ code than with C#.
The .NET Foundation just released their code formatting tool on GitHub
https://github.com/dotnet/codeformatter
It uses the Roslyn compiler services to parse project documents and convert them to their expected formatting conventions. They're applying this to some of the very old CLR code in order to make all the projects consistent.
Further to #Chris Karcher's answer - you can also automatically format the whole document by hitting Ctrl+K, Ctrl+D.
These formatting features work on a variety of file formats - it works wonders on ugly HTML.
Another option: NArrange;
free
console based (so good for commit hooks etc, but can still be used as an "External Tool" in VS)
flexible config file
For me, Ctrl + Shift + F maps to Find in Files. When I need to format code, I highlight it and hit Ctrl + K, Ctrl + F.
I understand this doesn't really address automated formatting. I just wanted to clarify for those who may not know this feature even exists in Visual Studio.
I've heard only good things about ReSharper. It's on my to-learn list.
http://www.sourceformat.com/
This tool is around (~30$). I tried it and it works nice (with multiple languages).
I like this tool the best because it doesn't check code file for correctness. I can post code snippets from the Internet and it will translate them correctly no matter if they are in missing parts of the code. Other tools I try complain in that cases. The tool can also be integrated easily into editors as it allows command line driving.
Other tools:
http://www.polystyle.com/index.jsp
http://astyle.sourceforge.net/ (open source)
Not directly, but I use the Agent Smith plugin for R# to do this. Unfortunately, R# isn't free.
Also take a look at Microsoft StyleCop
See this previous question:
Is there any tool for reformatting C# code?
Searching for [c#] astyle shows up some more previous questions too.
I haven't tried this (found it through Google). It might work. http://www.semdesigns.com/Products/Formatters/CSharpFormatter.html. It's fairly cheap at USD50, but a trial is not available.
Maybe you could be interested in this free Addin for Visual Studio 2010/2012.
Here is an open source code formatting tool which has amazing features
CodeMaid
If you want to do it online, have a freecodeformat.

How to solve the Single stepping problem with VS2008 SP1

Debugging in visual studio seems to have been broken with sp1.
Single stepping randomly does not work and just starts to run. Sometimes breakpoints are ignored. It is unpredictable and unusable. It will generally hit the first break point but after that it is totally unpredictable.
Any idea what needs to be done to correct this behavior ?
Make sure you are debuging using the debug configuration, not the release one. Also make sure optimizations are disabled in debug configuration.
Optimizations must be off when you debug else it can lead to very erratic behaviours like these.
For C# projects, which I am assuming the question is about looking at the tags, the optimization option would be located in the "Build" tab of "Project > Properties..." Last option of "General" it's called "Optimize Code".
There is a fix which for some reason isn't included in the update process:
http://code.msdn.microsoft.com/KB957912/Release/ProjectReleases.aspx?ReleaseId=1796
It worked for me although some people say they still have the same problem.
We are using c# as a language.
The problem has been identified by microsoft.
quote from forums:
We have identified the root cause of this issue and are currently working on a solution. We apologize for the inconvenience that this is causing you. We will let you know as soon as we have a solution. In the mean time, if we discover any workrounds, we will post them here.

Categories

Resources