Resharper-unity: Unability to name Unity serialized field starting with underscore

Created on 26 Nov 2018  路  13Comments  路  Source: JetBrains/resharper-unity

default

Why tho?

default
Settings are not helping also

All 13 comments

The next build of Rider 2018.3 will contain extra settings here - prefix, suffix, style and a checkbox to disable inspections.

I have similar problem
It frustrated me

Sorry to hear that, but I'd ask you to please be civil.

This problem was fixed in Rider 2018.3, and it's possible to configure the naming convention for serialised fields to begin with a prefix set to underscore.

If you're still seeing an issue, please provide more details, including version and some repro code.

Sorry to hear that, but I'd ask you to please be civil.

This problem was fixed in Rider 2018.3, and it's possible to configure the naming convention for serialised fields to begin with a prefix set to underscore.

If you're still seeing an issue, please provide more details, including version and some repro code.

Where can I find this setting? C# > Naming and I dont see anything for "Serialized Fields"

It's in the Settings -> Languages and Frameworks -> Unity Engine pane

image

Sorry to hear that, but I'd ask you to please be civil.

This problem was fixed in Rider 2018.3, and it's possible to configure the naming convention for serialised fields to begin with a prefix set to underscore.

If you're still seeing an issue, please provide more details, including version and some repro code.

I'm sorry my opinion changed
Rider is Amazing!
I love it

Sorry to post in an old closed issue, but I'm having issues with fields that start with on, e.g., ButtonClickedEvent onPointerDown:
image
And I don't understand why I see these warnings.

@RunninglVlan You can press Alt+Enter and then go there

image

And add a prefix for the serialized fields
image

The thing is I don't want to have some prefixes/suffixes, I tried that, they just require all serialized fields to have them.
As you see I want, e.g., 2 fields:

[SerializeField] bool keepOnScreen;
[SerializeField] ButtonClickedEvent onPointerUp;

I'm prefixing 2nd field with on because it's an Event.

I expect you have the on prefix already assigned to events in the standard C# naming styles. Once a prefix is defined for a particular entity type, you will see a warning if you try to use it for another entity type. E.g. if you wanted to use m as a prefix for fields, to show that they are "members", then you would see a warning if you had a local variable called mName. To do otherwise would be confusing and defeat the purpose of having prefixes.

But that doesn't help in this situation where we have a field that is semantically an "event", even though it's not a C# event, but is a field, and more importantly, is a Unity serialised field.

I don't have a good solution right now. It might be possible to add another entity that can have its own rules (serialised field based on UnityEvent), but I'm not sure on ordering (how to make sure that the "serialised field" rule doesn't match these new events), and more importantly, I don't think I'd enable the rule by default, and we don't have the UI to create this rule - that is an outstanding feature request: RIDER-8339. It looks like this is finally scheduled for Rider 2021.1, so once that's implemented, I'll revisit this.

I'm sorry I don't have better news right now 馃槥

@citizenmatt, we have some rules related to events that were autogenerated when we first created .editorconfig:

dotnet_naming_rule.event_rule.severity=warning
dotnet_naming_rule.event_rule.style=on_upper_camel_case_style
dotnet_naming_rule.event_rule.symbols=event_symbols
dotnet_naming_symbols.event_symbols.applicable_accessibilities=*
dotnet_naming_symbols.event_symbols.applicable_kinds=event
dotnet_style_qualification_for_event=false:suggestion
resharper_csharp_naming_rule.event=on + AaBb, AaBb

But I can't say that I saw them in action anywhere, so I don't know how they work.
For example, we have the following event: public event Action EntitiesCreated = delegate { };
No warnings here.
I tried subscribing to it with local variables:

Action onEntitiesCreated = () => { };
Action entitiesCreated = () => { };
world.EntitiesCreated += onEntitiesCreated;
world.EntitiesCreated += entitiesCreated;

No warnings here either, Rider only suggests using local functions instead.

The resharper_csharp_naming_rule.event=on + AaBb, AaBb rule states that you can have both on + AaBb ("UpperCamelCase" with on as a prefix) style names and AaBb ("UpperCamelCase") style names, so Rider will accept both onEntitiesCreated as well as EntitiesCreated for standard .NET events. But because this rule is using on as a prefix, it means Rider won't allow on at the start of a name for any other entity.


But that makes me realise that there is of course a workaround.

Since we can have multiple naming policies for a given rule, we can add an extra policy to the serialised field rule to allow on + AaBb. The only problem is that Rider doesn't have a UI for adding or editing custom rules, only the specific static rules already available in the UI. This is why the UI for serialised fields is in the Unity preferences page - the Unity plugin automatically adds a custom rule for serialised fields. (RIDER-8339 is the ticket for adding support for custom rules.)

Fortunately, we can modify the settings manually. Add the following line to either .sln.DotSettings or .sln.DotSettings.user, depending on if you want it checked into source control or not.

<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/UserRules/=5f0fdb63_002Dc892_002D4d2c_002D9324_002D15c80b22a7ef/@EntryIndexedValue">&lt;Policy&gt;&lt;Descriptor Staticness="Instance" AccessRightKinds="Any" Description="Unity serialized field"&gt;&lt;ElementKinds&gt;&lt;Kind Name="UNITY_SERIALISED_FIELD" /&gt;&lt;/ElementKinds&gt;&lt;/Descriptor&gt;&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb"&gt;&lt;ExtraRule Prefix="on" Suffix="" Style="AaBb" /&gt;&lt;/Policy&gt;&lt;/Policy&gt;</s:String>

It's not a nice file format (yes, that's encoded JSON inside XML!), but this adds an ExtraRule with a prefix of on and "UpperCamelCase". So Rider will now happily match both onPointerUp and pointerUp for serialised fields.

Thanks, adding the rule solved the problem. Thanks for clarifying this too:

But because this rule is using on as a prefix, it means Rider won't allow on at the start of a name for any other entity.

With this knowledge I solved other warnings - we had Action parameters in methods with prefix on.

Was this page helpful?
0 / 5 - 0 ratings