XamlStyler icon indicating copy to clipboard operation
XamlStyler copied to clipboard

Option for configuring newline exemptions for attributes

Open michael-hawker opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe. I like to have attributes split out, but there are some pairs that I like to keep on the same line together like Grid.Row and Grid.Column or the X1, Y1 points of a Line on one line and X2, Y2 on another.

NewlineExceptionElements only allows to completely ignore ordering on a parent element, but sometimes I really want to be more granular than that.

Describe the solution you'd like A collection like NewlineExemptionElements, but NewlineExemptionAttributes which will ignore those attributes and leave them on the line with the previous attribute (according to the ordering rules).

For instance adding Grid.Column as an exempted attribute value, would do this:

<Button Grid.Row="1" Grid.Column="2"
        Style="{StaticResource MyCustomStyle}"/>

vs. the default behavior:

<Button Grid.Row="1"
        Grid.Column="2"
        Style="{StaticResource MyCustomStyle}"/>

Describe alternatives you've considered

Additional context Add any other context or screenshots about the feature request here.

michael-hawker avatar Sep 26 '22 21:09 michael-hawker

Interesting idea! Your example works out nicely, but I am curious what expected behavior might be under less ideal scenarios. For instance, in your example, let's say you've specified Grid.Column as an exempted attribute value, but this is the original input:

<Button
    x:Key="MyCustomButton"
    Grid.Column="2"
    Style="{StaticResource MyCustomStyle}"/>

grochocki avatar Sep 30 '22 06:09 grochocki

@grochocki yeah, I was thinking about that a bit more as well. There's definitely some complications. Easy for us humans to look at and know what we want, harder to quantify to the machines. 😋

I think the common piece here with a lot of these types of attributes and scenarios is that they're really tied to groups/sets of properties, e.g. you want your canvas attached properties or grid ones together.

Since AttributeOrderingRuleGroups kind of establishes these types of groups already, maybe it'd make sense to leverage that list as well?

So, the newline exemption would only apply if the attribute is paired with another one of its partner attributes in that collection?


For example, typically we have this:

    "AttributeOrderingRuleGroups": [
        "x:Class",
        "xmlns, xmlns:x",
        "xmlns:*",
        "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
        "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
        "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
        "Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
        "*:*, *",
        "PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
        "mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
        "Storyboard.*, From, To, Duration"
    ],

So if the NewlineExceptionElements was defined as:

    "NewlineExceptionElements": "Grid.Column, Canvas.Top, Height"

Then if one of these is encountered, say Grid.Column it looks in the AttributeOrderingRuleGroups and finds where Grid.Column is. Then only applies the exemption if one of those other properties before it is found (in this case Grid.Row or Grid.RowSpan).

So in your example:

<Button
    x:Key="MyCustomButton"
    Grid.Column="2"
    Style="{StaticResource MyCustomStyle}"/>

Since there's no Grid.Row it doesn't get the exemption. But if you were to add one, then it would apply:

<Button
    x:Key="MyCustomButton"
    Grid.Row="1" Grid.Column="2"
    Style="{StaticResource MyCustomStyle}"/>

Even if the developer put them in different spots (before formatting):

<Button
    x:Key="MyCustomButton"
    Grid.Column="2"
    Style="{StaticResource MyCustomStyle}"
    Grid.Row="1"/>

Since they'd get ordered first by the attribute ordering rules, then it should be able to flow the logic and be consistent, right?

michael-hawker avatar Sep 30 '22 20:09 michael-hawker

Leveraging AttributeOrderingRuleGroups could make things really clean. What if we lean in even further into those groupings and instead of indivudal attribute-based NewlineExemptionAttributes, created attribute group index-based NewlineExemptionAttributeGroups?

So, with this (abridged) config:

    "AttributeOrderingRuleGroups": [
		...
0        "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
1        "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
2        "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
		...
    ],
	"NewlineExemptionAttributeGroups": ["1"],

You would get:

<Button
    x:Key="MyCustomButton"
    Grid.Row="1" Grid.Column="2"
    Style="{StaticResource MyCustomStyle}"/>

If you wanted to, say, only exempt Grid.*, and not Canvas.* attributes, split grouping:

    "AttributeOrderingRuleGroups": [
		...
0        "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
1        "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan",
2        "Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
3        "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
		...
    ],
	"NewlineExemptionAttributeGroups": ["1"],

Or both, but on separate lines:

    "AttributeOrderingRuleGroups": [
		...
0        "x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
1        "Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan",
2        "Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
3        "Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
		...
    ],
	"NewlineExemptionAttributeGroups": ["1", "2"],

grochocki avatar Sep 30 '22 21:09 grochocki

Yeah, that could work too. Only issue with the index-based approach is that if someone changes the order of their attribute rule groups that can have unintended/adverse effects they don't expect, if they forgot they set the other property (or weren't aware of it with multiple contributors).

michael-hawker avatar Sep 30 '22 22:09 michael-hawker