Skip to content

Commit 3323dc5

Browse files
authored
Auto-format our XAML files and enforce in CI (microsoft#9589)
This adds [`XamlStyler.Console`] to our solution, and calls it when we format the code, to also format our .xaml files. * `XamlStyler.Console` is a dotnet tool so it needs to be restored with `dotnet tool restore` * I've added a set of rules to approximately follow [@cmaneu's XAML guidelines]. Those guidelines also recommend things based on the code-behind, which this tool can't figure out, but also _don't matter that much_. * There's an extra step to strip BOMs from the output, since Xaml Styler adds a BOM by default. Some had them before and others didn't. BOMs have been nothing but trouble though. [`XamlStyler.Console`]: https://github.com/Xavalon/XamlStyler [@cmaneu's XAML guidelines]: https://github.com/cmaneu/xaml-coding-guidelines
1 parent c19aa89 commit 3323dc5

38 files changed

+2863
-2262
lines changed

Diff for: .config/dotnet-tools.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"XamlStyler.Console": {
6+
"version": "3.2008.4",
7+
"commands": [
8+
"xstyler"
9+
]
10+
}
11+
}
12+
}

Diff for: .github/actions/spelling/excludes.txt

+1
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,4 @@ SUMS$
6464
^\.github/actions/spelling/
6565
^\.gitignore$
6666
^doc/reference/master-sequence-list.csv$
67+
^\XamlStyler.json$

Diff for: .github/actions/spelling/expect/expect.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2061,6 +2061,7 @@ runtests
20612061
runtimeclass
20622062
runuia
20632063
runut
2064+
runxamlformat
20642065
rvalue
20652066
RVERTICAL
20662067
rxvt
@@ -2852,6 +2853,7 @@ XResource
28522853
xsd
28532854
xsi
28542855
xsize
2856+
xstyler
28552857
XSubstantial
28562858
xtended
28572859
xterm

Diff for: .nuget/packages.config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="vswhere" version="2.6.7" />
4-
</packages>
4+
</packages>

Diff for: XamlStyler.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"AttributesTolerance": 1,
3+
"KeepFirstAttributeOnSameLine": true,
4+
"MaxAttributeCharactersPerLine": 0,
5+
"MaxAttributesPerLine": 1,
6+
"NewlineExemptionElements": "RadialGradientBrush, GradientStop, LinearGradientBrush, ScaleTransform, SkewTransform, RotateTransform, TranslateTransform, Trigger, Condition, Setter",
7+
"SeparateByGroups": false,
8+
"AttributeIndentation": 0,
9+
"AttributeIndentationStyle": 1,
10+
"RemoveDesignTimeReferences": false,
11+
"EnableAttributeReordering": true,
12+
"AttributeOrderingRuleGroups": [
13+
"x:Class",
14+
"xmlns, xmlns:x",
15+
"xmlns:*",
16+
"x:Key, Key, x:Name, Name, x:Uid, Uid, Title",
17+
"Grid.Row, Grid.RowSpan, Grid.Column, Grid.ColumnSpan, Canvas.Left, Canvas.Top, Canvas.Right, Canvas.Bottom",
18+
"Width, Height, MinWidth, MinHeight, MaxWidth, MaxHeight",
19+
"Margin, Padding, HorizontalAlignment, VerticalAlignment, HorizontalContentAlignment, VerticalContentAlignment, Panel.ZIndex",
20+
"*:*, *",
21+
"PageSource, PageIndex, Offset, Color, TargetName, Property, Value, StartPoint, EndPoint",
22+
"mc:Ignorable, d:IsDataSource, d:LayoutOverrides, d:IsStaticText",
23+
"Storyboard.*, From, To, Duration"
24+
],
25+
"FirstLineAttributes": "",
26+
"OrderAttributesByName": true,
27+
"PutEndingBracketOnNewLine": false,
28+
"RemoveEndingTagOfEmptyElement": true,
29+
"SpaceBeforeClosingSlash": true,
30+
"RootElementLineBreakRule": 0,
31+
"ReorderVSM": 2,
32+
"ReorderGridChildren": false,
33+
"ReorderCanvasChildren": false,
34+
"ReorderSetters": 0,
35+
"FormatMarkupExtension": true,
36+
"NoNewLineMarkupExtensions": "x:Bind, Binding",
37+
"ThicknessSeparator": 2,
38+
"ThicknessAttributes": "Margin, Padding, BorderThickness, ThumbnailClipMargin",
39+
"FormatOnSave": true,
40+
"CommentPadding": 2,
41+
}

Diff for: build/scripts/Invoke-FormattingCheck.ps1

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@
33
# Checks for code formatting errors. Will throw exception if any are found.
44
function Invoke-CheckBadCodeFormatting() {
55
Import-Module ./tools/OpenConsole.psm1
6-
Invoke-CodeFormat
6+
7+
# Don't run the XAML formatter in this step - even if it changes nothing,
8+
# it'll still touch all the .xaml files.
9+
Invoke-CodeFormat -IgnoreXaml
10+
711
# returns a non-zero exit code if there are any diffs in the tracked files in the repo
812
git diff-index --quiet HEAD --
913
if ($lastExitCode -eq 1) {
14+
15+
# Write the list of files that need updating to the log
16+
git diff-index --name-only HEAD
17+
1018
throw "code formatting bad, run Invoke-CodeFormat on branch"
1119
}
20+
21+
# Manually check the formatting of our .xaml files, without touching them.
22+
Verify-XamlFormat
23+
1224
}
1325

1426
Invoke-CheckBadCodeFormatting

Diff for: consolegit2gitfilters.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
".wrn",
3939
".rec",
4040
".err",
41+
"XamlStyler.json",
4142
".xlsx"
4243
]
4344
}

Diff for: samples/ConPTY/GUIConsole/GUIConsole.WPF/App.xaml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
<Application x:Class="GUIConsole.Wpf.App"
2-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
StartupUri="MainWindow.xaml">
5-
<Application.Resources>
6-
7-
</Application.Resources>
8-
</Application>
1+
<Application x:Class="GUIConsole.Wpf.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
StartupUri="MainWindow.xaml">
5+
<Application.Resources />
6+
</Application>
+100-84
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,100 @@
1-
<Window x:Class="GUIConsole.Wpf.MainWindow"
2-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6-
mc:Ignorable="d"
7-
8-
Title="MainWindow" Height="450" Width="800"
9-
Background="#C7000000"
10-
AllowsTransparency="True"
11-
WindowStyle="None"
12-
MouseDown="Window_MouseDown"
13-
BorderThickness="1"
14-
BorderBrush="LightSlateGray"
15-
16-
KeyDown="Window_KeyDown"
17-
Loaded="Window_Loaded">
18-
<Window.Resources>
19-
<Style x:Key="TitleBarButtonStyle" TargetType="{x:Type Button}">
20-
<Setter Property="Background" Value="Transparent"/>
21-
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
22-
<Setter Property="FontSize" Value="14"/>
23-
<Setter Property="Height" Value="32"/>
24-
<Setter Property="Width" Value="46"/>
25-
<Setter Property="Background" Value="Transparent"/>
26-
<Setter Property="Foreground" Value="White"/>
27-
<Setter Property="BorderThickness" Value="0"/>
28-
</Style>
29-
30-
<Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource TitleBarButtonStyle}">
31-
<Setter Property="Content" Value="&#xE10A;"/>
32-
<!--Remove the default Button template's Triggers, otherwise they'll override our trigger below.-->
33-
<Setter Property="Template">
34-
<Setter.Value>
35-
<ControlTemplate TargetType="{x:Type Button}">
36-
<Border Background="{TemplateBinding Background}">
37-
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
38-
</Border>
39-
</ControlTemplate>
40-
</Setter.Value>
41-
</Setter>
42-
<Style.Triggers>
43-
<Trigger Property="IsMouseOver" Value="True">
44-
<Setter Property="Button.Background" Value="Red"/>
45-
</Trigger>
46-
</Style.Triggers>
47-
</Style>
48-
</Window.Resources>
49-
50-
<Grid>
51-
<Grid.RowDefinitions>
52-
<RowDefinition Height="Auto"/>
53-
<RowDefinition Height="*"/>
54-
</Grid.RowDefinitions>
55-
56-
<Grid Grid.Row="0">
57-
<Grid.ColumnDefinitions>
58-
<ColumnDefinition Width="*"/>
59-
<ColumnDefinition Width="Auto"/>
60-
</Grid.ColumnDefinitions>
61-
<TextBlock x:Name="TitleBarTitle" Grid.Column="0" VerticalAlignment="Center" Padding="10 0" Foreground="White">
62-
GUIConsole
63-
</TextBlock>
64-
<StackPanel x:Name="TitleBarButtons" Grid.Column="1" Orientation="Horizontal" VerticalAlignment="Top">
65-
<Button x:Name="MinimizeButton"
66-
Click="MinimizeButton_Click"
67-
Content="&#xE108;"
68-
Style="{StaticResource TitleBarButtonStyle}"/>
69-
<Button x:Name="MaximizeRestoreButton"
70-
Click="MaximizeRestoreButton_Click"
71-
Content="&#xE922;"
72-
FontSize="12"
73-
Style="{StaticResource TitleBarButtonStyle}"/>
74-
<Button x:Name="CloseButton"
75-
Click="CloseButton_Click"
76-
FontSize="12"
77-
Style="{StaticResource CloseButtonStyle}"/>
78-
</StackPanel>
79-
</Grid>
80-
<ScrollViewer x:Name="TerminalHistoryViewer" Grid.Row="1" ScrollChanged="ScrollViewer_ScrollChanged">
81-
<TextBlock x:Name="TerminalHistoryBlock" FontFamily="Consolas" TextWrapping="Wrap" Foreground="White"/>
82-
</ScrollViewer>
83-
</Grid>
84-
</Window>
1+
<Window x:Class="GUIConsole.Wpf.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
Title="MainWindow"
7+
Width="800"
8+
Height="450"
9+
AllowsTransparency="True"
10+
Background="#C7000000"
11+
BorderBrush="LightSlateGray"
12+
BorderThickness="1"
13+
KeyDown="Window_KeyDown"
14+
Loaded="Window_Loaded"
15+
MouseDown="Window_MouseDown"
16+
WindowStyle="None"
17+
mc:Ignorable="d">
18+
<Window.Resources>
19+
<Style x:Key="TitleBarButtonStyle"
20+
TargetType="{x:Type Button}">
21+
<Setter Property="Background" Value="Transparent" />
22+
<Setter Property="FontFamily" Value="Segoe MDL2 Assets" />
23+
<Setter Property="FontSize" Value="14" />
24+
<Setter Property="Height" Value="32" />
25+
<Setter Property="Width" Value="46" />
26+
<Setter Property="Background" Value="Transparent" />
27+
<Setter Property="Foreground" Value="White" />
28+
<Setter Property="BorderThickness" Value="0" />
29+
</Style>
30+
31+
<Style x:Key="CloseButtonStyle"
32+
BasedOn="{StaticResource TitleBarButtonStyle}"
33+
TargetType="{x:Type Button}">
34+
<Setter Property="Content" Value="&#xE10A;" />
35+
<!-- Remove the default Button template's Triggers, otherwise they'll override our trigger below. -->
36+
<Setter Property="Template">
37+
<Setter.Value>
38+
<ControlTemplate TargetType="{x:Type Button}">
39+
<Border Background="{TemplateBinding Background}">
40+
<ContentPresenter HorizontalAlignment="Center"
41+
VerticalAlignment="Center" />
42+
</Border>
43+
</ControlTemplate>
44+
</Setter.Value>
45+
</Setter>
46+
<Style.Triggers>
47+
<Trigger Property="IsMouseOver" Value="True">
48+
<Setter Property="Button.Background" Value="Red" />
49+
</Trigger>
50+
</Style.Triggers>
51+
</Style>
52+
</Window.Resources>
53+
54+
<Grid>
55+
<Grid.RowDefinitions>
56+
<RowDefinition Height="Auto" />
57+
<RowDefinition Height="*" />
58+
</Grid.RowDefinitions>
59+
60+
<Grid Grid.Row="0">
61+
<Grid.ColumnDefinitions>
62+
<ColumnDefinition Width="*" />
63+
<ColumnDefinition Width="Auto" />
64+
</Grid.ColumnDefinitions>
65+
<TextBlock x:Name="TitleBarTitle"
66+
Grid.Column="0"
67+
Padding="10,0"
68+
VerticalAlignment="Center"
69+
Foreground="White">
70+
GUIConsole
71+
</TextBlock>
72+
<StackPanel x:Name="TitleBarButtons"
73+
Grid.Column="1"
74+
VerticalAlignment="Top"
75+
Orientation="Horizontal">
76+
<Button x:Name="MinimizeButton"
77+
Click="MinimizeButton_Click"
78+
Content="&#xE108;"
79+
Style="{StaticResource TitleBarButtonStyle}" />
80+
<Button x:Name="MaximizeRestoreButton"
81+
Click="MaximizeRestoreButton_Click"
82+
Content="&#xE922;"
83+
FontSize="12"
84+
Style="{StaticResource TitleBarButtonStyle}" />
85+
<Button x:Name="CloseButton"
86+
Click="CloseButton_Click"
87+
FontSize="12"
88+
Style="{StaticResource CloseButtonStyle}" />
89+
</StackPanel>
90+
</Grid>
91+
<ScrollViewer x:Name="TerminalHistoryViewer"
92+
Grid.Row="1"
93+
ScrollChanged="ScrollViewer_ScrollChanged">
94+
<TextBlock x:Name="TerminalHistoryBlock"
95+
FontFamily="Consolas"
96+
Foreground="White"
97+
TextWrapping="Wrap" />
98+
</ScrollViewer>
99+
</Grid>
100+
</Window>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
<Application
2-
x:Class="TestHostApp.App"
3-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:local="using:TestHostApp"
6-
RequestedTheme="Dark">
7-
8-
</Application>
1+
<Application x:Class="TestHostApp.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="using:TestHostApp"
5+
RequestedTheme="Dark" />

0 commit comments

Comments
 (0)