Organize Members Settings
- 7 minutes to read
You can access the Organize Members settings on the Editor | All Languages | Organize Members options page.
The Organize Members options page contains the following settings:
- Empty Line Count Between Groups
- Insert an Empty Line Above and Below All Organized Members
- Skip Initialized Fields
- Use Member Section
- Rule Priority
- Empty Line Count Between Members
- Grouping Rules
- Sorting Rules
The sections below describe each option and explain how to use it.
Empty Line Count Between Groups
Use the “Empty line count between groups” setting to specify how many empty lines CodeRush should place between different code sections. The empty line count default value is 1.
For example, the code below has 2 empty lines between groups. To change the empty line count to 1:
1) Set “Empty line count between groups” to 1.
2) Enable the “Remove extra line breaks” option.
This option removes extra blank lines between members (beyond what you have specified in the “Empty line count between groups” setting).
3) Run Organize Members.
Insert an Empty Line Above and Below All Organized Members
Enable the corresponding option to insert an empty line above and below all organized members.
The following screencast shows this feature in action.
Skip Initialized Fields
This option excludes initialized fields from sorting to preserve field initialization order.
In some cases, field initialization order can be important (for example, one field depends on another field). If you change this order in code, it can lead to an exception when a class is created. To avoid this, the “Skip Initialized fields” option is initially enabled. CodeRush does not move initialized fields into the ‘Fields’ group.
Rule Priority
Each rule may match zero or more members from the type members list (for example, the “Public methods” rule matches all public methods). Members are only allowed to match one rule since Organize Members can move a member to one place in code.
To illustrate this approach, consider the following code:
using NUnit.Framework;
namespace NunitTestProject {
public class Calculation {
public int Sum(int a, int b) {
return a + b;
}
[Test]
public void Sum10Plus15() {
Assert.AreEqual(10 + 15, Sum(10, 15));
}
[Test]
public void Sum10Plus5() {
Assert.AreEqual(10 + 5, Sum(10, 5));
}
}
}
To wrap tests marked with the [Test] attribute into a single region and place those tests at the end of the file, do the following:
1) Open the Organize Members options page.
2) Create a rule (for example, Test methods).
3) Click the Region button to put test methods inside a region.
4) Specify the grouping criteria in the “Group by” section.
The screenshot below shows the resulting settings:
5) Run Organize Members on the sample code above.
Organize Members does not change the sample code above. This happened because the “Public methods” rule has already matched all public methods before the “Test methods” rule is applied.
To organize members, use the Rule priority option. This option allows you to prioritize which rules CodeRush applies first to members. You can set this option to High priority, Normal priority or Low priority.
CodeRush runs rules with the high rule priority first, followed by medium-priority rules, and ends with low priority rules. Rule priority impacts the order in which the rule is checked and has zero impact on the member order. The member order is determined by the order in which the rules appear in the list.
To organize the code above, do the following:
1) Set the “Region Test methods” Rule priority to High priority.
2) Run Organize Members.
Empty Line Count Between Members
This setting specifies empty line count CodeRush must insert above and below members. The screencast below shows how to specify “Empty line count between members” for fields.
Grouping Rules
The Group by section contains the filter editor. It allows you to configure what kind of members CodeRush groups together by the rule.
Merging Groups
The filter editor can simplify conditions. For example, if the “Public enums” rule contains the following condition:
CodeRush merges the redundant “Or” group with the parent “And” group when you leave the “Public enums” rule and return back to it.
Grouping Criteria
CodeRush can add members into groups by the following criteria:
Node kind - allows you to choose the kind of member for which CodeRush should create a group.
Parent kind - allows you to choose a parent type for which CodeRush should create a group.
Visibility - allows you to specify members’ visibility criteria (private, protected, protected internal, internal, or public).
Access modifier - allows you to specify an access modifier criteria to group members (abstract, read-only, override, sealed, static, virtual, or extern).
Has attribute - use this setting to specify member attributes.
Parent has attribute - specify this setting if CodeRush should take attributes into account for parent types.
Is explicit interface implementor - choose this setting to allow CodeRush to create a group for explicit interface members.
Is interface implementor - choose this setting to allow CodeRush to create a group for explicit and implicit interface members. Example: How to: Group Interface Implementations and Wrap them into Regions
Is event handler - specify this setting if you need CodeRush to create a group for event handlers.
- Name - specify this setting to group members (property/field type, method return type, etc.) by member name. Example: How to: Group Members by Member Name
Member type name - specify this setting to group members by member type name.
Example: How to: Group Members by Member Type NameNote
You can use Contains, Starts with, and Equals string operations and Regex expressions for Name and Member type name criteria to group members.
Is backing field - choose this setting to allow CodeRush to create a group for properties with backing fields members.
Example: How to: Keep Backing Fields with their Corresponding Properties
Property visibility - allows you to specify visibility criteria (private, protected, protected internal, internal, or public) for properties with backing fields.
Example: How to: Group Properties with Backing Fields by Visibility
Add, Or, Not And, and Not Or conditions allow you to create grouping criteria.
For information on how to configure grouping, refer to the following example: How to: Create a Rule
Sorting Rules
You can configure the sort order of members within a group for each member organization rule in the Sorting rules section.
CodeRush can sort group items by:
- Name
- Kind
- Access modifier (abstract, read-only, override, sealed, static, virtual, extern)
- Visibility
- Parameter count
- Explicit interface name
- Interface name
- Event handler (IsEventHandler mode)
- Member type name
- Property name
- Property visibility
You can also specify the sort order: ascending or descending.
For example, you can sort fields by visibility, member type name, and name in the ascending order.
The following screencast shows how Organize Members sorts fields according to this criteria:
Wrap Distinct Groups in Regions
The Wrap distinct groups in regions option allows CodeRush to wrap sorted groups in dynamic regions with built-in variables.
CodeRush supports the following built-in variables in the region name:
- {Kind} - method, property, field, etc.
- {Kinds} - methods, properties, fields, etc.
- {Visibility} - private, public, protected, etc.
- {ExplicitInterfaceName} - the interface name the member explicitly implements.
See the How to Create Regions for Explicit Interface Implementations example. - {InterfaceName} - the interface name the member implements explicitly or implicitly. See the How to Group Interface Implementations and Wrap them into Regions example.
- {Abstract}, {Readonly}, {Override}, {Sealed}, {Static}, {Virtual}, {Extern} - use the built-in variables for members with the corresponding modifier.
Example
To create regions for properties with different visibility:
1) Add the Properties rule to the rule set. See the following topic for more information: Organize Members.
2) Change “Sort by” from Name to Visibility and enable the Wrap distinct groups in regions option.
3) Type “{Visibility} properties” in the region name text box.
4) Click Apply to save the changes and click OK to close the Organize Members options page.
5) Run Organize Members.