belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.41k stars 98 forks source link

Do not format field attributes #1299

Closed Smooth-E closed 4 months ago

Smooth-E commented 4 months ago

I want CSharpier to keep the attributes formatted as they are. Somewhere on the same line, somewhere - on different lines. Basically, I want constructions like this to be considered okay:

[Header("Event information")]
[SerializeField] private Building _pointOfInterest;
[SerializeField] private float _duration;

I use the following in my .editorconfig:

[*.cs]
place_attribute_on_same_line = false:warning
csharp_place_field_attribute_on_same_line = false:warning

Input:

public class Building : MonoBehaviour
{
    [SerializeField] private string _identifier;
    [SerializeField] private string _name;
    [SerializeField] private Sprite _cardImage;

    // ...

Output:

public class Building : MonoBehaviour
{
    [SerializeField]
    private string _identifier;

    [SerializeField]
    private string _name;

    [SerializeField]
    private Sprite _cardImage;

    // ...

Expected behavior:

Should not change, basically.

public class Building : MonoBehaviour
{
    [SerializeField] private string _identifier;
    [SerializeField] private string _name;
    [SerializeField] private Sprite _cardImage;

    // ...

Notes

Issue template says "option requests are out of scope". Does this count as an option request?

I leave the possibility that this may be an issue on my side. I use the following command in CI to lint-check my Unity project:

dotnet csharpier --check --loglevel Debug .

The .editorconfig file is located at the root of the repository.

belav commented 4 months ago

The only editorconfig options CSharpier takes into consideration are

indent_style = space
indent_size = 4
max_line_length = 100

If you want to keep the formatting of your attributes you'd need to use some form of ignoring code

Smooth-E commented 4 months ago

I would need to add this a lot of times then. Unity components have a lot of private-but-serialized fields and all of them use this one-line formatting. I found CSharpier to be the only formatter that does not require solution files and can work with just code, which allows to format before building in CI.

belav commented 4 months ago

It's been a while since I've used Unity, but I imagine it won't care if you just let csharpier format the files. Attributes on their own line seems to be the standard in c#.

Smooth-E commented 4 months ago

I know but it is way more convenient when attributes are located on the same line. I think this should be possible to change this behavior of the formatter, especially conidering that it understands attributes and their position. Just a simple flag like preserve_attribute_formatting would be a good addition.

Example: Attributes above fields. This is how CSharpier formats the code.

public class EventCard : MonoBehaviour
{
    [SerializeField]
    private Image _image;

    [SerializeField]
    private TMP_Text _title;

    [SerializeField]
    private TMP_Text _description;

    [SerializeField]
    private TMP_Text _rewards;

    [SerializeField]
    private TMP_Text _requiredMoney;

    [SerializeField]
    private TMP_Text _duration;

    [SerializeField]
    private Button _button;

    private EventData _eventData;

    public void UpdateUI()
    {
        // Reflect new data in the UI
    }
}

Example: Attributes inline with fields. This is how JB Rider formats the code.

public class EventCard : MonoBehaviour
{
    [SerializeField] private Image _image;
    [SerializeField] private TMP_Text _title;
    [SerializeField] private TMP_Text _description;
    [SerializeField] private TMP_Text _rewards;
    [SerializeField] private TMP_Text _requiredMoney;
    [SerializeField] private TMP_Text _duration;
    [SerializeField] private Button _button;

    private EventData _eventData;

    public void UpdateUI()
    {
        // Reflect new data in the UI
    }
}
belav commented 4 months ago

Option requests are out of scope for csharpier.