C# Rearrange code in preferred order

Answered

I am new to RIder and am trying to use the rearrange feature.  It does rearrange, but not in the code style we are using.  Our desired order is:

private members
public members (properties)
constructor
public methods
private methods

What we are seeing is the public methods and public members (properties) are mixing:

private member

public method

public member (property)

Another public methods

 

We can't seem to follow this: https://www.jetbrains.com/help/rider/File_and_Type_Layout.html to get the desired result.  Does anybody have a solution out there for this?

24 comments
Comment actions Permalink

Hi,

Could you please share the content of the "Preferences | Editor | Code Style | C#"  settings page (XML text with the layout settings)? 

 

Kind regards,

Sofia

0
Comment actions Permalink

Do you mean this file?

0
Comment actions Permalink

Yes,

I mean the content of this page (XML).

One can upload it via https://uploads.services.jetbrains.com/ or attach it to a new support request or youtrack ticket (https://youtrack.jetbrains.com/issues/RIDER).

0
Comment actions Permalink

I uploaded it:

Upload complete!
Uploading...
File uploaded: CSharpFile.xml
Upload complete!

 

2
Comment actions Permalink

Thank you, file received! I'll be back as soon as I investigate it.

0
Comment actions Permalink

Hello,

As far as I can see, CSharpFile.xml has the default settings. But to sort the members in the way you specified, you need to make additional changes for File Layout.

Something like this:

<TypePattern DisplayName="Default Pattern">
<Entry DisplayName="Private members">
<Entry.Match>
<And>
<Kind Is="Member" />
<Access Is="Private" />
</And>
</Entry.Match>
</Entry>
<Entry DisplayName="Public properties">
<Entry.Match>
<And>
<Kind Is="Property" />
<Access Is="Public" />
</And>
</Entry.Match>
</Entry>
<Entry DisplayName="Constructors">
<Entry.Match>
<Kind Is="Constructor" />
</Entry.Match>
<Entry.SortBy>
<Static />
</Entry.SortBy>
</Entry>
<Entry DisplayName="Public methods">
<Entry.Match>
<And>
<Kind Is="Method" />
<Access Is="Public" />
</And>
</Entry.Match>
</Entry>
<Entry DisplayName="Private methods" Priority="100">
<Entry.Match>
<And>
<Kind Is="Method" />
<Access Is="Private" />
</And>
</Entry.Match>
</Entry>

 

I attached the full file in another ticket that you created.

0
Comment actions Permalink

Hello,

Upon further testing, we found that public constants are not being listed above private members.  Is this possible?

For example, starting with:

 

public class Example
{
// public constants
public const int Exposed = 42;

// private members
private readonly IService _service;

// public members
public bool HasDoneStuff { get; set; }

// constructors (protected/private, public)
protected Example()
{
}

public Example(IService service)
{
_service = service;
}

// public methods
public void Execute()
{
}

// private methods
private void DoSomeWork()
{
}
}

 

When we format, it is moving the constants down like this:

public class Example
{
// private members
private readonly IService _service;

// public members
public bool HasDoneStuff { get; set; }

// constructors (protected/private, public)
protected Example()
{
}

public Example(IService service)
{
_service = service;
}

// public methods
public void Execute()
{
}

// private methods
private void DoSomeWork()
{
}

// public constants
public const int Exposed = 42;
}

 

Is this something that can be accomplished with your product?  Thank you.

0
Comment actions Permalink

bscharf please insert the following before "Private members" -

 

<Entry DisplayName="Public Constants">
<Entry.Match>
<And>
<Kind Is="Constant" />
<Access Is="Public" />
</And>
</Entry.Match>
</Entry>

Please let me know if it is more convenient for you to get the full setting file.

0
Comment actions Permalink

Thank you.  That works well.  We have one more question--can we order the protected constructors before the public constructors.  We tried this and it did not seem to work:

...

Entry DisplayName="Public properties">
<Entry.Match>
<And>
<Kind Is="Property" />
<Access Is="Public" />
</And>
</Entry.Match>
</Entry>
<Entry DisplayName="Protected Constructors">
<Entry.Match>
<And>
<Kind Is="Constructor" />
<Access Is="Protected" />
</And>
</Entry.Match>
<Entry.SortBy>
<Static />
</Entry.SortBy>
</Entry>
<Entry DisplayName="Public Constructors">
<Entry.Match>
<And>
<Kind Is="Constructor" />
<Access Is="Public" />
</And>
</Entry.Match>
<Entry.SortBy>
<Static />
</Entry.SortBy>
</Entry>

Here is an example class:
public class Example
{
// public methods
public void Execute()
{
}

// private methods
private void DoSomeWork()
{
}

// public constants
public const int Exposed = 42;


public Example(IService service)
{
_service = service;
}

// public members
public bool HasDoneStuff { get; set; }

// constructors (protected/private, public)
protected Example()
{
}

// private members
private readonly IService _service;

}

Thank you for your help and patience.
 
0
Comment actions Permalink

@bscharf Thanks for the update. Your example works for me. The only thing missing is the opening tag symbol "<" before 

Entry DisplayName="Public properties">

Do you have "<" in your settings? You can also share with us the full settings file using https://uploads.services.jetbrains.com or using any cloud services like Dropbox, GoogleDisk, etc.

0
Comment actions Permalink

Hi! Rearrange of order in File layout seems broken. Properties are under constructor

<Entry DisplayName="Fields">
<Entry.Match>
<And>
<Kind Is="Field" />
<Not>
<Static />
</Not>
</And>
</Entry.Match>

<Entry.SortBy>
<Readonly />
<Name />
</Entry.SortBy>
</Entry>

<Entry DisplayName="Public properties">
<Entry.Match>
<And>
<Kind Is="Property" />
<Access Is="Public" />

</And>
</Entry.Match>
</Entry>

<Entry DisplayName="Constructors">
<Entry.Match>
<Kind Is="Constructor" />
</Entry.Match>

<Entry.SortBy>
<Static/>
</Entry.SortBy>
</Entry>

<Entry DisplayName="Interface Implementations" Priority="100">
<Entry.Match>
<And>
<Kind Is="Member" />
<ImplementsInterface />
</And>
</Entry.Match>

<Entry.SortBy>
<ImplementsInterface Immediate="true" />
</Entry.SortBy>
</Entry>

<Entry DisplayName="All other members" />

<Entry DisplayName="Nested Types">
<Entry.Match>
<Kind Is="Type" />
</Entry.Match>
</Entry>


Always do next

private readonly BombModel _model;
private readonly IBombView _view;

public BombPresenter(IBombView view, BombModel model)
{
_view = view;
_model = model;
}


public BombModel Model { get; }

Please help!

0
Comment actions Permalink

Denis Bocherikov, thanks for the question. The template looks correct. Unfortunately, I could not reproduce the problem on my side, perhaps the problem is related to some other part of the template (a missing character, an extra tag, or something else). Please collect all Rider `.DotSettings` files:

- *Personal layer*: ```.DotSettings.user``` file near your sln (if any)
- *Team-shared layer*: ```.DotSettings``` file near your sln (if any)
- *Global settings layer*: Use "Help -> Diagnostic tools -> Special Files and Folders -> R# Global Settings" to locate ```GlobalSettingsStorage.DotSettings``` file

Please share these files with us using https://uploads.services.jetbrains.com. Thanks in advance!

0
Comment actions Permalink

Hi, Maria! Thanks for your response.

Upload ID is 2021_10_27_XvDTw4LDoY5RqyH7

Also my platform is Mac

0
Comment actions Permalink

Denis Bocherikov, thanks for the setting file. But I still can't reproduce the problem, even using Mac. Does the problem happen for a newly created solution or it's solution specific problem? What Rider version are you using?

0
Comment actions Permalink

Just tried another solution and there is the same problem.

JetBrains Rider 2021.2.1
Build #RD-212.5080.71, built on August 26, 2021

0
Comment actions Permalink

Denis Bocherikov, how do you run code reordering - Reformat and Cleanup Code | Built-in: Full Cleanup?

0
Comment actions Permalink

Denis Bocherikov, what type of project are you using? Is it Unity?

0
Comment actions Permalink

Denis Bocherikov, it looks like you have not specified the rules for Unity. Please configure them in the "Unity" tab in "File | Settings | Editor | Code Style | C#". Please let me know about the results. Thank you!

0
Comment actions Permalink

Thank you very match! Fixed it

0
Comment actions Permalink

Sorry I was wrong. Still not working.

<Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns"
xmlns:unity="urn:schemas-jetbrains-com:member-reordering-patterns-unity">

<!-- Pattern to match classes used by Unity that contain serialised fields and event
function methods. Based on the standard "Default Pattern", this will also order
event functions before normal methods, and does not reorder serialised fields,
as this order is reflected in the Unity editor's Inspector -->
<TypePattern DisplayName="Unity classes" Priority="100">
<TypePattern.Match>
<unity:SerializableClass />
</TypePattern.Match>

<Entry DisplayName="Public Delegates" Priority="100">
<Entry.Match>
<And>
<Access Is="Public" />
<Kind Is="Delegate" />
</And>
</Entry.Match>

<Entry.SortBy>
<Name />
</Entry.SortBy>
</Entry>

<Entry DisplayName="Public Enums" Priority="100">
<Entry.Match>
<And>
<Access Is="Public" />
<Kind Is="Enum" />
</And>
</Entry.Match>

<Entry.SortBy>
<Name />
</Entry.SortBy>
</Entry>

<Entry DisplayName="Static Fields and Constants">
<Entry.Match>
<Or>
<Kind Is="Constant" />
<And>
<Kind Is="Field" />
<Static />
</And>
</Or>
</Entry.Match>

<Entry.SortBy>
<Kind>
<Kind.Order>
<DeclarationKind>Constant</DeclarationKind>
<DeclarationKind>Field</DeclarationKind>
</Kind.Order>
</Kind>
</Entry.SortBy>
</Entry>

<Entry DisplayName="Serialized Fields">
<Entry.Match>
<unity:SerializedField />
</Entry.Match>

<!-- No sorting -->
</Entry>

<Entry DisplayName="Non-serialised Fields">
<Entry.Match>
<And>
<Kind Is="Field" />
<Not>
<unity:SerializedField />
</Not>
</And>
</Entry.Match>

<Entry.SortBy>
<Readonly />
<Name />
</Entry.SortBy>
</Entry>

<Entry DisplayName="Properties, Indexers">
<Entry.Match>
<Or>
<Kind Is="Property" />
<Kind Is="Indexer" />
</Or>
</Entry.Match>
</Entry>

<Entry DisplayName="Constructors">
<Entry.Match>
<Kind Is="Constructor" />
</Entry.Match>

<Entry.SortBy>
<Static/>
</Entry.SortBy>
</Entry>

<Entry DisplayName="Construct method">
<Entry.Match>
<And>
<Kind Is="Method" />
<Name Is="Construct" />
</And>
</Entry.Match>
</Entry>

<Entry DisplayName="Event Functions">
<Entry.Match>
<unity:EventFunction />
</Entry.Match>

<Entry.SortBy>
<!-- Prioritises common event functions. For alphabetical, replace with <Name /> -->
<unity:EventFunctionName />
</Entry.SortBy>
</Entry>

<Entry DisplayName="Interface Implementations" Priority="100">
<Entry.Match>
<And>
<Kind Is="Member" />
<ImplementsInterface />
</And>
</Entry.Match>

<Entry.SortBy>
<ImplementsInterface Immediate="true" />
</Entry.SortBy>
</Entry>

<Entry DisplayName="All other members" />

<Entry DisplayName="Nested Types">
<Entry.Match>
<Kind Is="Type" />
</Entry.Match>
</Entry>
</TypePattern>
</Patterns>

As a result I see this

[SerializeField] private int _id;

public CurvySpline Spline;
private ITarget _target;

public TargetLauncher()
{
_id = 1;
}

[Inject]
private void Construct(ITarget target)
{
_target = target;
}

public int Id => _id;


public void Launch()
{
_target.Fly(Spline);
}
0
Comment actions Permalink

Sorry for spam. I fixed with change 

<Entry DisplayName="Interface Implementations" Priority="100">
<Entry.Match>
<And>
<Kind Is="Member" />
<ImplementsInterface />
<Not>
<Kind Is="Property" />
</Not>
</And>
</Entry.Match>

<Entry.SortBy>
<ImplementsInterface Immediate="true" />
</Entry.SortBy>
</Entry>

Just add Not kind is property

0

Please sign in to leave a comment.