Is there a Visual Studio 2010 extension to sort methods?
Asked Answered
I

5

7

StyleCop mandates a particular sort order for the contents of C# files. For example, fields should be declared before methods and public declarations should come before private ones.

It would be useful to have a Visual Studio extension to move these things into that order automatically.

Does a good extension exist for this (I've found CodeSorter, but the feedback suggests it's very buggy)? Or are there any extensions or shortcuts that make it less painful to reorder this content manually?

Infective answered 3/12, 2011 at 20:53 Comment(2)
Sounds like a make-work silly rule if you ask me.Nevels
I believe the idea is to present information in decreasing order of importance so that someone reading it (i.e. intending to use it) can get to grips with it more quickly.Infective
B
8

You can try with Regionerate :

Regionerate is an automated tool which helps you to preserve your code's layout over time. Regionerate lets you define regions in your code and determine the way members (fields, methods, properties etc.) should be placed inside them. Regionerate is a zero-friction tool - when setting up, you can choose a Code Layout (the way you want your code to look) or just use the default Code Layout. From that moment on, Regionerate will make sure your code follows that Code Layout.

Bac answered 3/12, 2011 at 21:2 Comment(0)
A
6

ReSharper can do this. It has a "Type Members Layout" feature that allows you to use either a default set of rules, or to define your own.

Artificial answered 3/12, 2011 at 22:1 Comment(1)
After some searching I found this blog post that gives a set of ReSharper rules for this very scenario.Infective
O
5

CodeMaid is a free and open source Visual Studio extension that will reorganize code to follow StyleCop conventions. You can find it here: http://visualstudiogallery.msdn.microsoft.com/76293c4d-8c16-4f4a-aee6-21f83a571496

Disclaimer: This is a totally shameless plug, I wrote it. ;)

Octet answered 15/6, 2012 at 23:54 Comment(0)
C
1

I use ReSharper and their Type Member Layout. See my post for more details.

Cloven answered 22/12, 2011 at 4:1 Comment(0)
C
0

If Code Sorting is your only concern, you can try the Visual Studio Extension CodeSorter

CodeSorter is highly customizable extension that allows its users to sort C# code itby various conditions such as names, types (method, class, struct, ...), visibility modifiers (private, public, ...), constness modifiers (const, static, readonly) and virtuality modifiers (virtual, override, new, ...).

It is possible to assign multiple search criteria to have the same priority, for example place protected and private members first, and then publics and internals.

Names are compared in a way that leading "m_" and "_" is stripped. This allows fields and properties (eg. _foo and Foo) to be right next to each other.

One-lined declarations are placed without delimiting blank line, while multi-lined declarations are separated by a blank line. The exception is that one-lined field followed by a multi-lined property without comment of the same name will be placed without the delimiting blank line.

Crepitate answered 26/4, 2015 at 6:32 Comment(2)
I mentioned CodeSorter in the question.Infective
@MatthewStrawbridge my mistake. I've been using it for long, and haven't faced any bugs so far.Crepitate

© 2022 - 2024 — McMap. All rights reserved.