Skip to main content

How to: Group Interface Implementations and Wrap them into Regions

  • 3 minutes to read

Organize Members can group explicit and implicit interface implementations and wrap them into regions according to the interface implementation rule. This example shows how to create, configure, and apply the interface implementations rule.

Add an Interface Implementations Rule to a Rule Set

  1. Open the Editor | All Languages | Organize Members options page to configure Organize Members.

    organize-members-rules-page

  2. Click Add Rule to add a rule for interface implementations.

    add-rule

    CodeRush adds the new rule to the end of the rule list.

  3. Change the suggested rule name to “Interface implementations” and press Enter to save the change.

    Type Rule Name

  4. Change the “Interface implementations” rule’s position, as shown in the screencast below, so that Interface implementations follow indexers in a type. To change the rule’s position, drag the “Interface implementations” rule in the rule list and drop it when you reach the desired position.

    OrganizeMembersPage

Note

Organize Members applies rules according to the specified order in the rule set and the Rule priority setting.

Customize the Rule

Change the Rule Priority

Set the Rule Priority setting to High priority to allow Organize Members to wrap interface implementations into regions. For information about this setting, see the following topic: Rule Priority

specify-rule-priority

Note

The Rule priority setting does not affect the rule order specified in the rule set.

Configure Grouping

This section describes how to specify a condition which is used to group interface implementations.

  1. Select the “Interface implementations” rule in the rule list.

  2. In the “Group by” section, click the “+” button to add a new condition to the “And” group.

    click-plus-button-to-add-condition

    CodeRush adds the “Node kind”.

    added-node-kind

  3. Click the first “Node kind” and choose Is interface implementor from the list.

    choose-is-interface-implementor

    The And group looks as follows:

    add-group

When you run Organize Members CodeRush analyzes all type’s members and checks the Is interface implementor condition to create an interface implementations group.

Configure Sorting

This section describes how to specify a condition which is used to sort interface implementation groups.

  1. In the “Sort by” section, set the Sort by to InterfaceName. This allows Organize Members to sort interface implementation groups by interface name.

    sort-interface-name

  2. Enable the Wrap distinct groups in regions option to activate wrapping of implicit and explicit interface implementations in regions.

    activate-region-wrapping

  3. Type “{InterfaceName} implementations” in the region name text box.

    type-interface-name

  4. Click Apply to save changes or click OK to save changes and close the Organize Members options page.

Run Organize Members

This section shows how to apply the interface implementation rule.

In the following code, place the caret to any place in the class body.

using System.Drawing;

namespace ConsoleApp {

    interface IDimensions {
        float GetLength();
        float GetWidth();
    }

    interface IColor {
        Color GetBackgroundColor();
        Color GetForegroundColor();
    }

    public class Box : IDimensions, IColor {
        Color boxColor;
        float lengthInches;
        float widthInches;

        Box(float length, float width, Color color) {
            lengthInches = length;
            widthInches = width;
            boxColor = color;
        }

        float GetLength() {
            return lengthInches;
        }
        float IDimensions.GetLength() {
            return lengthInches;
        }
        float GetWidth() {
            return widthInches;
        }

        float IDimensions.GetWidth() {
            return widthInches;
        }

        public Color GetForegroundColor() {
            return boxColor;
        }
        public Color GetBackgroundColor() {
            return boxColor;
        }
    }
}

Press Ctrl+. or Ctrl+~ to invoke the Code Actions Menu. Select Organize Members and press Enter.

organize-implementations-result

CodeRush groups explicit and implicit interface implementations according to the specified rule and wraps these implementations into regions.

See Also