Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
markheinis committed Jul 12, 2024
1 parent 89f88c7 commit 5c031ca
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/WireMockInspector/Views/CodeBlockViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,14 @@ private void SetMarkdown(ViewModels.MarkdownCode md)
}

this.TextArea.TextView.LineTransformers.Add(new DiffLineBackgroundRenderer(md.oldTextLines));

// Adding syntax highlighter for C#
if (md.lang == "csharp")
{
this.TextArea.TextView.LineTransformers.Add(new CSharpSyntaxHighlighter());
}
if (md.lang == "json")
{
this.TextArea.TextView.LineTransformers.Add(new JsonSyntaxHighlighter());
}
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public class CSharpSyntaxHighlighter : DocumentColorizingTransformer
};

// Colors
private static readonly SolidColorBrush KeywordBrush = new SolidColorBrush(Colors.Blue);
private static readonly SolidColorBrush MethodBrush = new SolidColorBrush(Colors.Yellow);
private static readonly SolidColorBrush StringLiteralBrush = new SolidColorBrush(Colors.LightBlue);
private static readonly SolidColorBrush ClassNameBrush = new SolidColorBrush(Colors.Teal);
private static readonly SolidColorBrush KeywordBrush = new SolidColorBrush(Color.Parse("#3988D6"));
private static readonly SolidColorBrush MethodBrush = new SolidColorBrush(Color.Parse("#ADD795"));
private static readonly SolidColorBrush StringLiteralBrush = new SolidColorBrush(Color.Parse("#D6936B"));
private static readonly SolidColorBrush ClassNameBrush = new SolidColorBrush(Color.Parse("#41C2B0"));

protected override void ColorizeLine(DocumentLine line)
{
Expand Down
75 changes: 75 additions & 0 deletions src/WireMockInspector/Views/Transformer/JsonSyntaxHighlighter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Text.RegularExpressions;
using Avalonia.Media;
using AvaloniaEdit.Document;
using AvaloniaEdit.Rendering;

namespace WireMockInspector.Views.Transformer
{
public class JsonSyntaxHighlighter : DocumentColorizingTransformer
{
// Regex patterns for different JSON components
private static readonly Regex PropertyNameRegex = new Regex(@"""[^""\\]*(?:\\.[^""\\]*)*""(?=\s*:)", RegexOptions.Compiled);
private static readonly Regex StringLiteralRegex = new Regex(@"""[^""\\]*(?:\\.[^""\\]*)*""(?=\s*[,}\]])(?!\s*:)", RegexOptions.Compiled);
private static readonly Regex NumberRegex = new Regex(@"\b\d+(\.\d+)?\b", RegexOptions.Compiled);
private static readonly Regex BooleanNullRegex = new Regex(@"\b(true|false|null)\b", RegexOptions.Compiled);
private static readonly Regex PunctuationRegex = new Regex(@"[\[\]{}:,]", RegexOptions.Compiled);

// Colors using the same palette as CSharpSyntaxHighlighter
private static readonly SolidColorBrush PropertyNameBrush = new SolidColorBrush(Color.Parse("#ADD795"));
private static readonly SolidColorBrush StringLiteralBrush = new SolidColorBrush(Color.Parse("#D6936B"));
private static readonly SolidColorBrush NumberBrush = new SolidColorBrush(Color.Parse("#ADD795"));
private static readonly SolidColorBrush BooleanNullBrush = new SolidColorBrush(Color.Parse("#41C2B0"));
private static readonly SolidColorBrush PunctuationBrush = new SolidColorBrush(Color.Parse("#3988D6"));

protected override void ColorizeLine(DocumentLine line)
{
var lineText = CurrentContext.Document.GetText(line);
int lineStartOffset = line.Offset;

// Highlight property names
foreach (Match match in PropertyNameRegex.Matches(lineText))
{
ChangeLinePart(
lineStartOffset + match.Index,
lineStartOffset + match.Index + match.Length,
element => element.TextRunProperties.SetForegroundBrush(PropertyNameBrush));
}

// Highlight string literals
foreach (Match match in StringLiteralRegex.Matches(lineText))
{
ChangeLinePart(
lineStartOffset + match.Index,
lineStartOffset + match.Index + match.Length,
element => element.TextRunProperties.SetForegroundBrush(StringLiteralBrush));
}

// Highlight numbers
foreach (Match match in NumberRegex.Matches(lineText))
{
ChangeLinePart(
lineStartOffset + match.Index,
lineStartOffset + match.Index + match.Length,
element => element.TextRunProperties.SetForegroundBrush(NumberBrush));
}

// Highlight boolean and null values
foreach (Match match in BooleanNullRegex.Matches(lineText))
{
ChangeLinePart(
lineStartOffset + match.Index,
lineStartOffset + match.Index + match.Length,
element => element.TextRunProperties.SetForegroundBrush(BooleanNullBrush));
}

// Highlight punctuation
foreach (Match match in PunctuationRegex.Matches(lineText))
{
ChangeLinePart(
lineStartOffset + match.Index,
lineStartOffset + match.Index + match.Length,
element => element.TextRunProperties.SetForegroundBrush(PunctuationBrush));
}
}
}
}
7 changes: 0 additions & 7 deletions src/WireMockInspector/WireMockInspector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,6 @@
<AvaloniaResource Include="Assets\**" />
</ItemGroup>

<ItemGroup>
<AvaloniaXaml Remove="Models\**" />
<Compile Remove="Models\**" />
<EmbeddedResource Remove="Models\**" />
<None Remove="Models\**" />
</ItemGroup>

<ItemGroup>
<None Remove="Assets\request_search.md" />
<None Remove="CodeGenerators\Json\default_template.liquid" />
Expand Down

0 comments on commit 5c031ca

Please sign in to comment.