Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new XAML option page #10

Draft
wants to merge 23 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
path = fantomas-stable
url = https://github.com/fsprojects/fantomas.git
branch = master

[submodule "fsharp-highlighting"]
path = config/highlighting
url = https://github.com/deviousasti/fsharp-highlighting.git
6 changes: 6 additions & 0 deletions config/editor/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
9 changes: 9 additions & 0 deletions config/editor/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="editor.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FantomasVs.Editor"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions config/editor/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace editor
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
29 changes: 29 additions & 0 deletions config/editor/Editor.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<RootNamespace>FantomasVs.Editor</RootNamespace>
<TargetFramework>net48</TargetFramework>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<AssemblyTitle>editor</AssemblyTitle>
<UseWPF>true</UseWPF>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Properties\**" />
<EmbeddedResource Remove="Properties\**" />
<None Remove="Properties\**" />
<Page Remove="Properties\**" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\FantomasLatest\FantomasLatest.fsproj">
<Aliases>FantomasLatest</Aliases>
</ProjectReference>
<ProjectReference Include="..\..\src\FantomasStable\FantomasStable.fsproj">
<Aliases>FantomasStable</Aliases>
</ProjectReference>
<ProjectReference Include="..\highlighting\src\FSharp.Highlighting.fsproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="5.0.1" />
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions config/editor/Fakes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Microsoft.VisualStudio.Shell
{
public class DialogPage
{
}
}

namespace System.Runtime.CompilerServices
{
internal static class IsExternalInit { }
}
166 changes: 166 additions & 0 deletions config/editor/FantomasOptionsEditor.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<UserControl
x:Class="FantomasVs.Editor.FantomasOptionsEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:highlighting="clr-namespace:FSharp.Highlighting;assembly=FSharp.Highlighting"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:editor="clr-namespace:FantomasVs.Editor"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d"
>
<UserControl.Resources>
<editor:EditorTemplateSelector x:Key="editorSelector" />
<Style x:Key="EnableToggle" TargetType="{x:Type ToggleButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ToggleButton}">
<Border BorderThickness="{TemplateBinding BorderThickness}"
BorderBrush="{TemplateBinding BorderBrush}"
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
CornerRadius="3">
<TextBlock Text="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
FontFamily="Consolas" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Content" Value="Disabled" />
<Setter Property="Background" Value="#FFEEEEEE" />
<Setter Property="BorderBrush" Value="#FFAAAAAA" />
<Setter Property="Padding" Value="4" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content" Value="Enabled" />
<Setter Property="Background" Value="#FF7FDAD5" />
</Trigger>
</Style.Triggers>
</Style>

<Style TargetType="{x:Type editor:SpinControl}">
<Style.Resources>
<Style x:Key="UpDown" TargetType="{x:Type RepeatButton}">
<Setter Property="Width" Value="20" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="FontSize" Value="14" />
</Style>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="editor:SpinControl">
<StackPanel Orientation="Horizontal">
<RepeatButton Name="PART_DownButton" Style="{StaticResource UpDown}">-</RepeatButton>
<TextBox Name="PART_Text" Text="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type editor:SpinControl}},
Path=Value, UpdateSourceTrigger=PropertyChanged}" FontFamily="Consolas" Padding="4" Width="30" BorderThickness="0,1" />
<RepeatButton Name="PART_UpButton" Style="{StaticResource UpDown}">+</RepeatButton>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<DataTemplate x:Key="BooleanTemplate">
<ToggleButton Style="{StaticResource EnableToggle}" IsChecked="{Binding Value}" Width="70" />
</DataTemplate>

<DataTemplate x:Key="Int32Template">
<editor:SpinControl Value="{Binding Value, Mode=TwoWay}" />
</DataTemplate>

<DataTemplate x:Key="EnumTemplate">
<ComboBox SelectedValue="{Binding Value}"
ItemsSource="{Binding Values}"
DisplayMemberPath="Label"
SelectedValuePath="Value"
MinWidth="70"
Padding="4"/>
</DataTemplate>
</UserControl.Resources>
<Grid>

<Grid.RowDefinitions>
<RowDefinition Height="34" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<!--<Grid Background="#FFF1F1F1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

-->
<!--<TextBlock
VerticalAlignment="Center"
Margin="4"
Text="Displays settings as defined in the .editorconfig or if no settings can be found the user profile's formatting options."
TextWrapping="Wrap" />

<Button Margin="4" Grid.Column="1">
<TextBlock>Generate <Span FontStyle="Italic">.editorconfig</Span></TextBlock>
</Button>-->
<!--
<Label VerticalAlignment="Center">Search</Label>
<TextBox Text="{Binding SearchText}" Margin="4" Grid.Column="1" />
</Grid>-->

<Grid>
<TextBox x:Name="SearchTermTextBox" Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}" Margin="4" VerticalContentAlignment="Center"/>
<TextBlock IsHitTestVisible="False" Text="Search Here" VerticalAlignment="Center" Margin="10,0" Opacity="0.5">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Text, ElementName=SearchTermTextBox}" Value="">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</Grid>


<ListView x:Name="PropertyView" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Header="Property" Width="Auto" DisplayMemberBinding="{Binding DisplayName}" />
<GridViewColumn Header="Value" Width="Auto" CellTemplateSelector="{StaticResource editorSelector}" />
</GridView>
</ListView.View>
<ListView.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander IsExpanded="True" Header="{Binding Name}">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ListView.GroupStyle>
</ListView>

<GridSplitter Grid.Row="1" Height="3" Background="#FF2B46C1" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" ShowsPreview="True" />

<Border Grid.Row="2">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<highlighting:Highlighter x:Name="SourcePreview" SourceText="let x = 10" Padding="8" Background="White" />
</ScrollViewer>
</Border>


</Grid>
</UserControl>
85 changes: 85 additions & 0 deletions config/editor/FantomasOptionsEditor.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
extern alias FantomasLatest;
extern alias FantomasStable;

using FSharp.Compiler.SourceCodeServices;
using System;
using System.Threading;
using System.Windows.Controls;


using StableCodeFormatter = FantomasStable::Fantomas.CodeFormatter;
using LatestCodeFormatter = FantomasLatest::Fantomas.CodeFormatter;

namespace FantomasVs.Editor
{
/// <summary>
/// Interaction logic for FantomasOptionsPage2.xaml
/// </summary>
public partial class FantomasOptionsEditor : UserControl
{
public FantomasOptionsPage FantomasOptions { get; }

public EditorPage<FantomasOptionsPage> Editor { get; }

public FSharpChecker FSharpCheckerInstance { get; }

public string SampleText { get; set; } =
@"
let rec distribute e = function
| [] -> [[e]]
| x::xs as list ->
[e::list] @ [for xs in distribute e xs -> x::xs]
";

#region Checker

private readonly Lazy<FSharpChecker> _checker = new(() =>
FSharpChecker.Create(null, null, null, null, null, null, null, null, null)
);

protected FSharpChecker CheckerInstance => _checker.Value;

#endregion

public FantomasOptionsEditor()
{
InitializeComponent();
FantomasOptions = new FantomasOptionsPage();
Editor = new EditorPage<FantomasOptionsPage>(FantomasOptions);
Editor.PropertyEdited += OnPropertyEdited;
PropertyView.ItemsSource = Editor.View;
DataContext = Editor;
OnPropertyEdited(nameof(FantomasOptionsPage.CommitChanges));
}

CancellationTokenSource tokenSource;

private void OnPropertyEdited(string name)
{
var config = FantomasOptions.ToOptions(null);
var defaults = FSharpParsingOptions.Default;
var opts = new FSharpParsingOptions(
sourceFiles: new string[] { "sample.fsx" },
conditionalCompilationDefines: defaults.ConditionalCompilationDefines,
errorSeverityOptions: defaults.ErrorSeverityOptions,
isInteractive: defaults.IsInteractive,
lightSyntax: defaults.LightSyntax,
compilingFsLib: defaults.CompilingFsLib,
isExe: true // let's have this on for now
);


//var fsasync = LatestCodeFormatter.FormatDocumentAsync("sample.fsx", Fantomas.SourceOrigin.SourceOrigin.NewSourceString(SampleText), config, opts, FSharpCheckerInstance);
var fsasync = StableCodeFormatter.FormatDocumentAsync("sample.fsx", Fantomas.SourceOrigin.SourceOrigin.NewSourceString(SampleText), config, opts, FSharpCheckerInstance);

tokenSource?.Cancel();
tokenSource?.Dispose();

tokenSource = new();

var token = tokenSource.Token;
var task = Microsoft.FSharp.Control.FSharpAsync.StartAsTask(fsasync, null, token);
task.ContinueWith(t => Dispatcher.Invoke(() => SourcePreview.SourceText = t.Result), token);
}
}
}
Loading