Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions THIRD-PARTY-NOTICES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Third-Party Notices

This project incorporates material from the projects listed below.

---

## SqlFormatter

- **Source:** https://github.com/madskristensen/SqlFormatter
- **License:** MIT (Apache-2.0 per repository; individual files carry MIT terms)
- **Copyright:** Copyright (c) Mads Kristensen

The `SqlFormattingService` in this project was inspired by and partially derived
from the SqlFormatter extension for Visual Studio by Mads Kristensen. It uses
`Microsoft.SqlServer.TransactSql.ScriptDom` for T-SQL parsing and formatting.

### MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
10 changes: 10 additions & 0 deletions src/PlanViewer.App/Controls/QuerySessionControl.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@
Height="28" Padding="10,0" FontSize="12" IsEnabled="False"
Theme="{StaticResource AppButton}"
ToolTip.Tip="Execute the repro script and capture actual plan with runtime stats"/>
<TextBlock Text="|" VerticalAlignment="Center"
Foreground="{DynamicResource ForegroundBrush}" Margin="4,0"/>
<Button x:Name="FormatButton" Content="&#x1F4DD; Format" Click="Format_Click"
Height="28" Padding="10,0" FontSize="12"
Theme="{StaticResource AppButton}"
ToolTip.Tip="Format the SQL query"/>
<Button x:Name="FormatOptionsButton" Content="&#x2699; Format Options" Click="FormatOptions_Click"
Height="28" Padding="10,0" FontSize="12"
Theme="{StaticResource AppButton}"
ToolTip.Tip="Configure SQL formatting options"/>
</StackPanel>
<TextBlock x:Name="StatusText" DockPanel.Dock="Right"
HorizontalAlignment="Right" VerticalAlignment="Center"
Expand Down
82 changes: 82 additions & 0 deletions src/PlanViewer.App/Controls/QuerySessionControl.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2005,4 +2005,86 @@ private Window GetParentWindow()
var parent = this.VisualRoot;
return parent as Window ?? throw new InvalidOperationException("No parent window");
}

private async void Format_Click(object? sender, RoutedEventArgs e)
{
var sql = QueryEditor.Text;
if (string.IsNullOrWhiteSpace(sql))
return;

FormatButton.IsEnabled = false;
SetStatus("Formatting...");

try
{
var settings = SqlFormatSettingsService.Load(out var loadError);
if (loadError != null)
SetStatus("Warning: using default format settings (load failed)");

var (formatted, errors) = await Task.Run(() => SqlFormattingService.Format(sql, settings));

if (errors != null && errors.Count > 0)
{
var errorMessages = string.Join("\n", errors.Select(err => $"Line {err.Line}: {err.Message}"));
var dialog = new Window
{
Title = "SQL Format Error",
Width = 500,
Height = 250,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Icon = GetParentWindow().Icon,
Background = (IBrush)this.FindResource("BackgroundBrush")!,
Foreground = (IBrush)this.FindResource("ForegroundBrush")!,
Content = new StackPanel
{
Margin = new Avalonia.Thickness(20),
Children =
{
new TextBlock
{
Text = $"Could not format: {errors.Count} parse error(s)",
FontWeight = Avalonia.Media.FontWeight.Bold,
FontSize = 14,
Margin = new Avalonia.Thickness(0, 0, 0, 10)
},
new TextBlock
{
Text = errorMessages,
TextWrapping = TextWrapping.Wrap,
FontSize = 12
}
}
}
};
dialog.ShowDialog(GetParentWindow());
SetStatus($"Format failed: {errors.Count} error(s)");
return;
}

var caretOffset = QueryEditor.CaretOffset;

QueryEditor.Document.BeginUpdate();
try
{
QueryEditor.Document.Replace(0, QueryEditor.Document.TextLength, formatted);
}
finally
{
QueryEditor.Document.EndUpdate();
}

QueryEditor.CaretOffset = Math.Min(caretOffset, QueryEditor.Document.TextLength);
SetStatus("Formatted");
}
finally
{
FormatButton.IsEnabled = true;
}
}

private void FormatOptions_Click(object? sender, RoutedEventArgs e)
{
var dialog = new Dialogs.FormatOptionsWindow();
dialog.ShowDialog(GetParentWindow());
}
}
121 changes: 121 additions & 0 deletions src/PlanViewer.App/Dialogs/FormatOptionsWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dialogs="using:PlanViewer.App.Dialogs"
x:Class="PlanViewer.App.Dialogs.FormatOptionsWindow"
Title="SQL Format Options"
Width="650" Height="580"
MinWidth="520" MinHeight="400"
Background="{DynamicResource BackgroundBrush}"
Foreground="{DynamicResource ForegroundBrush}"
WindowStartupLocation="CenterOwner">
Comment thread
rferraton marked this conversation as resolved.

<Window.Styles>
<Style Selector="ToggleSwitch">
<Setter Property="OnContent" Value=""/>
<Setter Property="OffContent" Value=""/>
<Setter Property="MinWidth" Value="0"/>
<Setter Property="MinHeight" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="RenderTransformOrigin" Value="0%,50%"/>
<Setter Property="RenderTransform" Value="scale(0.75)"/>
</Style>
<Style Selector="ToggleSwitch:checked /template/ Border#SwitchKnobBounds">
<Setter Property="Background" Value="{DynamicResource AccentBrush}"/>
<Setter Property="BorderBrush" Value="{DynamicResource AccentBrush}"/>
</Style>
<Style Selector="ToggleSwitch:checked /template/ Border#OuterBorder">
<Setter Property="BorderBrush" Value="{DynamicResource AccentBrush}"/>
</Style>
</Window.Styles>

<Grid RowDefinitions="*,Auto" Margin="12">
<DataGrid x:Name="OptionsGrid" Grid.Row="0"
x:DataType="dialogs:FormatOptionRow"
AutoGenerateColumns="False"
CanUserReorderColumns="False"
CanUserSortColumns="False"
HeadersVisibility="Column"
GridLinesVisibility="Horizontal"
IsReadOnly="False"
Background="{DynamicResource BackgroundBrush}"
Foreground="{DynamicResource ForegroundBrush}"
Margin="0,0,0,12">
<DataGrid.Columns>
<DataGridTextColumn Header="Parameter" Binding="{Binding Name}" IsReadOnly="True" Width="2*"/>
<DataGridTemplateColumn Header="Current Value" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="dialogs:FormatOptionRow">
<Panel>
<ToggleSwitch IsChecked="{Binding BoolValue}"
IsVisible="{Binding IsBool}"
VerticalAlignment="Center"
Margin="4,0"/>
<ComboBox ItemsSource="{Binding ChoiceOptions}"
SelectedItem="{Binding CurrentValue}"
IsVisible="{Binding IsChoice}"
VerticalAlignment="Center"
MinHeight="0" Height="26" FontSize="12"
Margin="4,0"/>
<TextBlock Text="{Binding CurrentValue}"
IsVisible="{Binding IsText}"
VerticalAlignment="Center"
Margin="8,0"/>
</Panel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:DataType="dialogs:FormatOptionRow">
<Panel>
<ToggleSwitch IsChecked="{Binding BoolValue}"
IsVisible="{Binding IsBool}"
VerticalAlignment="Center"
Margin="4,0"/>
<ComboBox ItemsSource="{Binding ChoiceOptions}"
SelectedItem="{Binding CurrentValue}"
IsVisible="{Binding IsChoice}"
VerticalAlignment="Center"
MinHeight="0" Height="26" FontSize="12"
Margin="4,0"/>
<TextBox Text="{Binding CurrentValue}"
IsVisible="{Binding IsText}"
VerticalAlignment="Center"
Margin="4,0"/>
</Panel>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Default Value" IsReadOnly="True" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="dialogs:FormatOptionRow">
<Panel>
<ToggleSwitch IsChecked="{Binding DefaultBoolValue}"
IsVisible="{Binding IsBool}"
IsEnabled="False"
VerticalAlignment="Center"
Margin="4,0"/>
<TextBlock Text="{Binding DefaultValue}"
IsVisible="{Binding !IsBool}"
VerticalAlignment="Center"
Margin="8,0"/>
<!-- Choice defaults shown as text -->
</Panel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right" Spacing="8">
<Button x:Name="RevertButton" Content="Revert to Default" Click="Revert_Click"
Height="32" Padding="16,0" FontSize="12"
Theme="{StaticResource AppButton}"/>
<Button x:Name="SaveButton" Content="Save" Click="Save_Click"
Height="32" Padding="16,0" FontSize="12"
Theme="{StaticResource AppButton}"/>
<Button x:Name="CloseButton" Content="Close" Click="Close_Click"
Height="32" Padding="16,0" FontSize="12"
Theme="{StaticResource AppButton}"/>
</StackPanel>
</Grid>
</Window>
Loading
Loading