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?
Please sign in to leave a comment.
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
Here is a guide about how to upload files to JetBrains: https://rider-support.jetbrains.com/hc/en-us/articles/208199755-Uploading-Large-Files-for-JetBrains-Support-Team
Do you mean this file?
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).
I uploaded it:
Thank you, file received! I'll be back as soon as I investigate it.
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.
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.
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.
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:
...
@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.
Hi! Rearrange of order in File layout seems broken. Properties are under constructor
Always do next
Please help!
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!
Hi, Maria! Thanks for your response.
Upload ID is 2021_10_27_XvDTw4LDoY5RqyH7
Also my platform is Mac
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?
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
Denis Bocherikov, how do you run code reordering - Reformat and Cleanup Code | Built-in: Full Cleanup?
Yes
Denis Bocherikov, what type of project are you using? Is it Unity?
Yes, it's Unity
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!
Thank you very match! Fixed it
Sorry I was wrong. Still not working.
As a result I see this
Sorry for spam. I fixed with change
Just add Not kind is property
@Maria Pleskunina Why not attaching the template here simply? So not every rider user has to ask the same question when facing the same problem!
Hello Assem Hussein,
there's no universal template. It all depends on how you'd like to rearrange the type members in your code and it varies according to different code styles.