-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89f88c7
commit 5c031ca
Showing
4 changed files
with
83 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/WireMockInspector/Views/Transformer/JsonSyntaxHighlighter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters