Skip to content

Commit

Permalink
Use Avalonia.Edit to present code snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jul 26, 2023
1 parent 0c56244 commit ecb5a13
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/WireMockInspector/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public MainWindowViewModel()
{
LastHit = hitCalculator.GetFirstPerfectHitDateAfter(m.Guid, estimatedScenarioStateDate ?? DateTime.MinValue),
Description = $"[{m.WhenStateIs}] -> [{m.SetStateTo}]",
MappingDefinition =AsMarkdownCode("json", JsonConvert.SerializeObject(m, Formatting.Indented)).AsMarkdownSyntax(),
MappingDefinition = AsMarkdownCode("json", JsonConvert.SerializeObject(m, Formatting.Indented)),
TriggeredBy = new RequestLogViewModel()
{
MapToLogEntries(hitCalculator.GetPerfectHitCountAfter(m.Guid, estimatedScenarioStateDate))
Expand Down Expand Up @@ -269,7 +269,7 @@ public MainWindowViewModel()
ExpectedPaths = GetExpectedPathsDescription(model),
Description = model.Title != model.Description? model.Description: null,
UpdatedOn = model.UpdatedAt,
Content = AsMarkdownCode("json", JsonConvert.SerializeObject(model, Formatting.Indented)).AsMarkdownSyntax(),
Content = AsMarkdownCode("json", JsonConvert.SerializeObject(model, Formatting.Indented)),
PartialHitCount = partialHitCount,
PerfectHitCount = perfectHitCount,
PerfectMatches = new RequestLogViewModel()
Expand Down Expand Up @@ -435,7 +435,7 @@ await _settingsManager.UpdateSettings(settings =>
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(code =>
{
SelectedMapping.Code = AsMarkdownCode("cs", code).AsMarkdownSyntax();
SelectedMapping.Code = AsMarkdownCode("cs", code);
});

this.WhenAnyValue(x => x.SelectedRequest)
Expand Down Expand Up @@ -917,7 +917,7 @@ public record ScenarioEdgeNode(string Id, bool Current, bool Hit):ScenarioNode(I
public record ScenarioTransition(string Id, ScenarioNode From, ScenarioNode To, bool Hit)
{
public string Description { get; set; }
public string MappingDefinition { get; set; }
public Markdown MappingDefinition { get; set; }
public RequestLogViewModel TriggeredBy { get; set; }
public DateTime? LastHit { get; set; }
};
Expand Down Expand Up @@ -991,19 +991,19 @@ public class MappingViewModel: ViewModelBase
public DateTime? UpdatedOn { get; set; }
public string? Title { get; set; }
public string? Description { get; set; }
public string Content { get; set; }
public Markdown Content { get; set; }

public int PerfectHitCount { get; set; }
public int PartialHitCount { get; set; }
public MappingHitType HitType { get; set; }

public string Code
public Markdown Code
{
get => _code;
set => this.RaiseAndSetIfChanged(ref _code, value);
}

private string _code;
private Markdown _code;

public Scenario Scenario { get; set; }
public RequestLogViewModel PerfectMatches { get; set; }
Expand Down
76 changes: 76 additions & 0 deletions src/WireMockInspector/Views/CodeBlockViewer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using Avalonia;
using Avalonia.Media;
using Avalonia.Styling;
using AvaloniaEdit;
using AvaloniaEdit.Document;
using AvaloniaEdit.TextMate;
using TextMateSharp.Grammars;

namespace WireMockInspector.Views;

public class CodeBlockViewer:TextEditor, IStyleable
{
Type IStyleable.StyleKey => typeof(TextEditor);
public CodeBlockViewer()
{
this.Loaded += (sender, args) =>
{
};


this.Initialized += (sender, args) =>
{
//First of all you need to have a reference for your TextEditor for it to be used inside AvaloniaEdit.TextMate project.
var _textEditor = this;
_textEditor.Background = new SolidColorBrush(Color.FromRgb(30, 30, 30));
_textEditor.TextArea.TextView.Margin = new Thickness(10);
_textEditor.ShowLineNumbers = true;
_textEditor.IsReadOnly = true;
_textEditor.FontFamily = "Cascadia Code,Consolas,Menlo,Monospace";
};
this.PropertyChanged+= (sender, args) =>
{
if (args.Property.Name == nameof(Code))
{
SetMarkdown(args.NewValue as ViewModels.Markdown);
}
};
}

private void SetMarkdown(ViewModels.Markdown md)
{
if (md is not null)
{
this.Document = new TextDocument(md.rawValue);
if (_currentLang != md.lang)
{
_currentLang = md.lang;
CodeBlockViewer _textEditor = this;

var _registryOptions = new RegistryOptions(ThemeName.DarkPlus);
var _textMateInstallation = _textEditor.InstallTextMate(_registryOptions);
_textMateInstallation.SetGrammar(_registryOptions.GetScopeByLanguageId(_registryOptions.GetLanguageByExtension("."+md.lang).Id));
}
}
else
{
this.Document = new TextDocument("");
}

}

private string? _currentLang;
public ViewModels.Markdown Code
{
get { return GetValue(CodeProperty); }
set
{
SetValue(CodeProperty, value);
SetMarkdown(value);
}
}

public static readonly StyledProperty<ViewModels.Markdown> CodeProperty = AvaloniaProperty.Register<CodeBlockViewer, ViewModels.Markdown>(nameof(Code));
}
4 changes: 0 additions & 4 deletions src/WireMockInspector/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@
BorderBrush="#3baced">
<Panel>
<Panel.Styles>
<Style Selector="avaloniaEdit|TextEditor">
<Setter Property="ShowLineNumbers" Value="True"></Setter>
<Setter Property="Padding" Value="5,5,5,20"></Setter>
</Style>
</Panel.Styles>
<ExperimentalAcrylicBorder IsHitTestVisible="False">
<ExperimentalAcrylicBorder.Material>
Expand Down
6 changes: 3 additions & 3 deletions src/WireMockInspector/Views/MappingPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:WireMockInspector.ViewModels"
xmlns:views="clr-namespace:WireMockInspector.Views"
xmlns:avalonia="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
xmlns:md="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:DataType="viewModels:MainWindowViewModel"
x:Class="WireMockInspector.Views.MappingPage">
Expand Down Expand Up @@ -106,7 +106,7 @@
<views:ContentWithSidebar.Sidebar>
<TabControl Classes="minor">
<TabItem Header="Definition">
<avalonia:MarkdownScrollViewer Markdown="{Binding SelectedMapping.Content }"></avalonia:MarkdownScrollViewer>
<views:CodeBlockViewer Code="{Binding SelectedMapping.Content}" />
</TabItem>
<TabItem Header="Matches">
<ScrollViewer>
Expand Down Expand Up @@ -146,7 +146,7 @@
</Grid>
</TabItem>
<TabItem Header="Code">
<avalonia:MarkdownScrollViewer Markdown="{Binding SelectedMapping.Code }"></avalonia:MarkdownScrollViewer>
<views:CodeBlockViewer Code="{Binding SelectedMapping.Code}" />
</TabItem>
</TabControl>
</views:ContentWithSidebar.Sidebar>
Expand Down
5 changes: 3 additions & 2 deletions src/WireMockInspector/Views/RequestDetails.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:md="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
xmlns:generic="clr-namespace:System.Collections.Generic;assembly=System.Collections"
xmlns:viewModels="clr-namespace:WireMockInspector.ViewModels"
xmlns:views="clr-namespace:WireMockInspector.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:DataType="viewModels:MatchDetailsList"
x:Class="WireMockInspector.Views.RequestDetails">
Expand Down Expand Up @@ -76,7 +77,7 @@
<SelectableTextBlock Margin="5,0,0,0" TextWrapping="Wrap" VerticalAlignment="Center" Text="{Binding Value}" FontWeight="Bold"></SelectableTextBlock>
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:MarkdownActualValue}">
<md:MarkdownScrollViewer Markdown="{Binding Value}"></md:MarkdownScrollViewer>
<views:CodeBlockViewer Code="{Binding Value}" />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModels:KeyValueListActualValue}">
<DataGrid ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedActualValueGridItem}" CanUserReorderColumns="False" CanUserResizeColumns="True" AutoGenerateColumns="False" AreRowGroupHeadersFrozen="True" GridLinesVisibility="All" ClipboardCopyMode="ExcludeHeader" SelectionMode="Single">
Expand Down Expand Up @@ -114,7 +115,7 @@
</SplitButton>
</DockPanel>
</Border>
<md:MarkdownScrollViewer Markdown="{Binding Expectations}" IsVisible="{Binding !NoExpectations}"></md:MarkdownScrollViewer>
<views:CodeBlockViewer Code="{Binding Expectations}" IsVisible="{Binding !NoExpectations}"/>
<TextBlock FontSize="10" Margin="5,5,0,0" IsVisible="{Binding NoExpectations}">(No Expectations)</TextBlock>
</StackPanel>
</Expander>
Expand Down
4 changes: 2 additions & 2 deletions src/WireMockInspector/Views/RequestListFilters.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:WireMockInspector.ViewModels"
xmlns:avalonia="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
xmlns:md="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:DataType="viewModels:MainWindowViewModel"
x:Class="WireMockInspector.Views.RequestListFilters">
Expand All @@ -27,7 +27,7 @@
</Button.Styles>
<Button.Flyout>
<Flyout >
<avalonia:MarkdownScrollViewer Width="650" Height="700" Margin="10" Source="avares://WireMockInspector/Assets/request_search.md" />
<md:MarkdownScrollViewer Width="650" Height="700" Margin="10" Source="avares://WireMockInspector/Assets/request_search.md" />
</Flyout>
</Button.Flyout>
<PathIcon Data="{StaticResource question_circle_regular}">
Expand Down
7 changes: 4 additions & 3 deletions src/WireMockInspector/Views/RequestLogs.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:WireMockInspector.ViewModels"
xmlns:avalonia="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
xmlns:md="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
xmlns:views="clr-namespace:WireMockInspector.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="WireMockInspector.Views.RequestLogs"
x:DataType="viewModels:RequestLogViewModel">
Expand All @@ -25,10 +26,10 @@
</Expander.Header>
<TabControl Classes="minor">
<TabItem Header="Request">
<avalonia:MarkdownScrollViewer Markdown="{Binding RequestDefinition}"></avalonia:MarkdownScrollViewer>
<views:CodeBlockViewer Code="{Binding RequestDefinition}" />
</TabItem>
<TabItem Header="Response">
<avalonia:MarkdownScrollViewer Markdown="{Binding ResponseDefinition}"></avalonia:MarkdownScrollViewer>
<views:CodeBlockViewer Code="{Binding ResponseDefinition}" />
</TabItem>
</TabControl>
</Expander>
Expand Down
6 changes: 3 additions & 3 deletions src/WireMockInspector/Views/RequestPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:WireMockInspector.Views"
xmlns:viewModels="clr-namespace:WireMockInspector.ViewModels"
xmlns:avalonia="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
xmlns:md="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
xmlns:avaloniaEdit="https://github.com/avaloniaui/avaloniaedit"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:DataType="viewModels:MainWindowViewModel"
x:Class="WireMockInspector.Views.RequestPage">
Expand Down Expand Up @@ -81,8 +82,7 @@

</StackPanel>
</SplitView.Pane>
<avalonia:MarkdownScrollViewer Grid.Column="1" Markdown="{Binding OutputCode}"></avalonia:MarkdownScrollViewer>

<views:CodeBlockViewer Code="{Binding OutputCode}" />
</SplitView>
</StackPanel>
</ScrollViewer>
Expand Down
4 changes: 2 additions & 2 deletions src/WireMockInspector/Views/ScenarioPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:WireMockInspector.Views"
xmlns:viewModels="clr-namespace:WireMockInspector.ViewModels"
xmlns:avalonia="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
xmlns:md="clr-namespace:Markdown.Avalonia;assembly=Markdown.Avalonia"
x:DataType="viewModels:MainWindowViewModel"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="WireMockInspector.Views.ScenarioPage">
Expand Down Expand Up @@ -110,7 +110,7 @@
</Expander.Header>
<TabControl Classes="minor">
<TabItem Header="Definition">
<avalonia:MarkdownScrollViewer Markdown="{Binding MappingDefinition}"></avalonia:MarkdownScrollViewer>
<views:CodeBlockViewer Code="{Binding MappingDefinition}" />
</TabItem>
<TabItem Header="Triggered by">
<views:RequestLogs DataContext="{Binding TriggeredBy}"></views:RequestLogs>
Expand Down
3 changes: 3 additions & 0 deletions src/WireMockInspector/WireMockInspector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,23 @@
<PackageReference Include="AutomaticGraphLayout" Version="1.1.12" />
<PackageReference Include="AutomaticGraphLayout.Drawing" Version="1.1.12" />
<PackageReference Include="Avalonia" Version="11.0.0" />
<PackageReference Include="Avalonia.AvaloniaEdit" Version="11.0.0" />
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.0.0" />
<PackageReference Include="Avalonia.Desktop" Version="11.0.0" />
<PackageReference Include="Avalonia.Svg" Version="11.0.0" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.0.0" />
<PackageReference Include="Avalonia.ReactiveUI" Version="11.0.0" />
<PackageReference Include="AvaloniaEdit.TextMate" Version="11.0.0" />
<PackageReference Include="Lucene.Net" Version="4.8.0-beta00016" />
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00016" />
<PackageReference Include="Lucene.Net.QueryParser" Version="4.8.0-beta00016" />
<PackageReference Include="Markdown.Avalonia" Version="11.0.0" />
<PackageReference Include="Markdown.Avalonia.SyntaxHigh" Version="11.0.0" />
<PackageReference Include="MessageBox.Avalonia" Version="3.0.0" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="TextMateSharp.Grammars" Version="1.0.55" />
<PackageReference Include="WireMock.Net.RestClient" Version="1.5.23" />
<!--<PackageReference Include="XamlNameReferenceGenerator" Version="1.6.1" />-->
</ItemGroup>
Expand Down

0 comments on commit ecb5a13

Please sign in to comment.