CodeStyle for C# FileLayout - SerializeField for field or backing field of auto-Property

What is the rule for matching serialized field or auto-property?

public bool SomePublicField;

[SerializeField]
private bool SomePrivateSerializedField;

[field: SerializeField]
private bool SomePrivateSerializedProperty { get; set; }

[field: SerializeField]
public bool SomePublicSerializedProperty { get; set; }

 

I want to match those four in one group in FileLayout rules.

This is my starting point and it doesn't work as expected:

```

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

<Entry DisplayName="Serialized Fields and Properties">
    <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">
    <Entry.Match>
        <Or>
            <Kind Is="Property" />
            <Not>
                <unity:SerializedField />
            </Not>
        </Or>
    </Entry.Match>
</Entry>

```

Is it supported? What's the closest supported version of the rules?

 

Similar problem here: https://rider-support.jetbrains.com/hc/en-us/community/posts/4409933268754-Attribute-field-SerializeField-for-properties-is-unseen-while-rearranging-

 

Alexandra Guk - I'm pinging you because you replied to that previous topic. Could you also help?

7 comments
Comment actions Permalink

Hi Trooperice

Please  try to use this layout: 

<Entry DisplayName="Serialized Fields and Properties">
<Entry.Match>
<Or>
<unity:SerializedField />
<unity:SerializedField />
<Or>
<Kind Is="Property"/>
<Kind Is="Autoproperty"/>
</Or>
</Or>
</Entry.Match>
<Entry.SortBy>
<Kind>
<Kind.Order>
<DeclarationKind>Field</DeclarationKind>
<DeclarationKind>Autoproperty</DeclarationKind>
</Kind.Order>
</Kind>
<Name />
</Entry.SortBy>
</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>

If it does not fit your expectations, please add a sample screenshot of how you want to see your code. We will be glad to help you with adjusting the layout. 

Should you have any questions, let us know. Have a great day! 

0
Comment actions Permalink

Unfortunately, it doesn't work as expected :/
For now, that's my layout settings:

<Entry DisplayName="Serialized Fields and Properties">
    <Entry.Match>
        <Or>
            <unity:SerializedField />
            <And>
                <unity:SerializedField />
                <Or>
                    <Kind Is="Property"/>
                    <Kind Is="Autoproperty"/>
                </Or>
            </And>
        </Or>
    </Entry.Match>
</Entry>

<Entry DisplayName="Non-serialised Properties">
    <Entry.Match>
        <And>
            <Not>
                <unity:SerializedField />
            </Not>
            <Or>
                <Kind Is="Property"/>
                <Kind Is="Autoproperty"/>
            </Or>
        </And>
    </Entry.Match>

    <Entry.SortBy>
        <Access />
    </Entry.SortBy>
</Entry>

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

    <Entry.SortBy>
        <Access />
    </Entry.SortBy>
</Entry>

This is the layout I want to achieve (in the comments I described what matching-groups should contain):

0
Comment actions Permalink

Hi Trooperice

Could you please also add a screenshot of the Code Cleanup results with your file layout - how do you see it now? 

Empty lines are not regulated by file layout. For this, use File | Settings | Editor | Code Style | C# => Blank Lines

Thanks! 

0
Comment actions Permalink

This is what it looks like after cleanup (lines 28-30) should be between first and second comment:

One more example:
This is a valid layout

This is after cleanup:
Public auto-properties (lines 14-16) have been moved to the bottom for some reason (they should be left untouched). Private serialized auto-properties (lines 33-35) as in the previous case have been moved to the bottom. They should be between the first and second comments.

As you can see, something is still wrong. :/

For blank lines, I found that I had 1 blank line set for auto-properties. I changed it to 0 before making these screenshots.

0
Comment actions Permalink

Hi Trooperice

Thank you for the details. Is the layout provided above full? Do you have anything else in your Unity layout tab? Please, attach the full layout "as is". 

Thanks! 

0
Comment actions Permalink

This is my current Unity File Layout

<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 and Properties">
<Entry.Match>
<Or>
<unity:SerializedField />
<And>
<unity:SerializedField />
<Or>
<Kind Is="Property"/>
<Kind Is="Autoproperty"/>
</Or>
</And>
</Or>
</Entry.Match>
</Entry>

<Entry DisplayName="Non-serialised Properties">
<Entry.Match>
<And>
<Not>
<unity:SerializedField />
</Not>
<Or>
<Kind Is="Property"/>
<Kind Is="Autoproperty"/>
</Or>
</And>
</Entry.Match>

<Entry.SortBy>
<Access />
</Entry.SortBy>
</Entry>

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

<Entry.SortBy>
<Access />
</Entry.SortBy>
</Entry>

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

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

<Entry DisplayName="Indexers">
<Entry.Match>
<Or>
<Kind Is="Indexer" />
</Or>
</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>
0
Comment actions Permalink

Bump

Alexandra Guk Do you know what is wrong with my layout?

0

Please sign in to leave a comment.