Skip to content

A Swiss Army knife for developers.

made Json Formatter working

Etienne Baudoux75fc85395f40
diff --git a/src/dev/impl/DevTools.Common/UI/Controls/FormattedTextBlock/FormattedTextBlock.xaml.cs b/src/dev/impl/DevTools.Common/UI/Controls/FormattedTextBlock/FormattedTextBlock.xaml.cs
index f90bec16b..612c6699f 100644
--- a/src/dev/impl/DevTools.Common/UI/Controls/FormattedTextBlock/FormattedTextBlock.xaml.cs
+++ b/src/dev/impl/DevTools.Common/UI/Controls/FormattedTextBlock/FormattedTextBlock.xaml.cs
@@ -78,8 +78,21 @@ namespace DevTools.Common.UI.Controls.FormattedTextBlock
if (!string.IsNullOrEmpty(text))
{
- var formatter = new RichTextBlockFormatter(ActualTheme);
- formatter.FormatRichTextBlock(text, language, RichTextBlock);
+ if (language is not null)
+ {
+ var formatter = new RichTextBlockFormatter(ActualTheme);
+ formatter.FormatRichTextBlock(text, language, RichTextBlock);
+ }
+ else
+ {
+ var p = new Paragraph();
+ var r = new Run()
+ {
+ Text = text
+ };
+ p.Inlines.Add(r);
+ RichTextBlock.Blocks.Add(p);
+ }
}
ApplySettings();
diff --git a/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolPage.xaml b/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolPage.xaml
index f1ae10ccd..0271c2544 100644
--- a/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolPage.xaml
+++ b/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolPage.xaml
@@ -48,12 +48,13 @@
<texteditor:TextEditor
Grid.Column="0"
Header="Input"
+ SettingsProvider="{x:Bind ViewModel.SettingsProvider}"
Text="{x:Bind ViewModel.InputValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<toolkit:GridSplitter
Grid.Column="1"
Width="16"
- Margin="0,27,0,0"
+ Margin="0,42,0,0"
ResizeBehavior="BasedOnAlignment"
ResizeDirection="Auto">
<toolkit:GridSplitter.Element>
@@ -64,8 +65,10 @@
</toolkit:GridSplitter>
<formattedtextblock:FormattedTextBlock
+ x:Name="OutputTextBlock"
Grid.Column="2"
- Header="Output"/>
+ Header="Output"
+ SettingsProvider="{x:Bind ViewModel.SettingsProvider}"/>
</Grid>
</Grid>
</Page>
diff --git a/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolPage.xaml.cs b/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolPage.xaml.cs
index 2ee50d955..577dc65a8 100644
--- a/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolPage.xaml.cs
+++ b/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolPage.xaml.cs
@@ -38,6 +38,8 @@ namespace DevTools.Providers.Impl.Tools.JsonFormatter
ViewModel = (JsonFormatterToolViewModel)parameters.Parameter!;
DataContext = ViewModel;
+ ViewModel.OutputTextBlock = OutputTextBlock;
+
base.OnNavigatedTo(e);
}
}
diff --git a/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolViewModel.cs b/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolViewModel.cs
index 631708083..088efc6b8 100644
--- a/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolViewModel.cs
+++ b/src/dev/impl/DevTools.Providers.Impl/Tools/JsonFormatter/JsonFormatterToolViewModel.cs
@@ -1,12 +1,19 @@
#nullable enable
+using ColorCode;
using DevTools.Common;
+using DevTools.Common.UI.Controls.FormattedTextBlock;
+using DevTools.Core;
using DevTools.Core.Settings;
using DevTools.Core.Threading;
using Microsoft.Toolkit.Mvvm.ComponentModel;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Composition;
+using System.IO;
+using System.Text;
using System.Threading.Tasks;
namespace DevTools.Providers.Impl.Tools.JsonFormatter
@@ -14,14 +21,17 @@ namespace DevTools.Providers.Impl.Tools.JsonFormatter
[Export(typeof(JsonFormatterToolViewModel))]
public sealed class JsonFormatterToolViewModel : ObservableRecipient, IToolViewModel
{
- private const string DefaultIndentation = "TwoSpaces";
+ private const string TwoSpaceIndentation = "TwoSpaces";
+ private const string FourSpaceIndentation = "FourSpaces";
+ private const string OneTabIndentation = "OneTab";
+ private const string NoIndentation = "Minified";
private readonly IThread _thread;
- private readonly Queue<string> _formattingQueue = new ();
+ private readonly Queue<string> _formattingQueue = new();
private bool _formattingInProgress;
private string? _inputValue;
- private string _indentation = DefaultIndentation;
+ private string _indentation = TwoSpaceIndentation;
public Type View { get; } = typeof(JsonFormatterToolPage);
@@ -55,6 +65,8 @@ namespace DevTools.Providers.Impl.Tools.JsonFormatter
internal ISettingsProvider SettingsProvider { get; }
+ internal IFormattedTextBlock? OutputTextBlock { private get; set; }
+
[ImportingConstructor]
public JsonFormatterToolViewModel(IThread thread, ISettingsProvider settingsProvider)
{
@@ -75,31 +87,92 @@ namespace DevTools.Providers.Impl.Tools.JsonFormatter
return;
}
+ Assumes.NotNull(OutputTextBlock, nameof(OutputTextBlock));
+
_formattingInProgress = true;
await TaskScheduler.Default;
while (_formattingQueue.TryDequeue(out string text))
{
- //Task<string>? conversionResult = null;
- //if (string.Equals(ConversionMode, DefaultConversion, StringComparison.Ordinal))
- //{
- // conversionResult = EncodeBase64DataAsync(text);
- //}
- //else
- //{
- // conversionResult = DecodeBase64DataAsync(text);
- //}
-
- //await Task.WhenAll(conversionResult).ConfigureAwait(false);
-
- //_thread.RunOnUIThreadAsync(ThreadPriority.Low, () =>
- //{
- // OutputValue = conversionResult.Result;
- //}).ForgetSafely();
+ var success = FormatJson(text, out string result);
+
+ _thread.RunOnUIThreadAsync(ThreadPriority.Low, () =>
+ {
+ if (success)
+ {
+ OutputTextBlock!.SetText(result, Languages.JavaScript);
+ }
+ else
+ {
+ OutputTextBlock!.SetText(result);
+ }
+ }).ForgetSafely();
}
_formattingInProgress = false;
}
+
+ private bool FormatJson(string input, out string output)
+ {
+ try
+ {
+ JToken? jtoken = JToken.Parse(input);
+ if (jtoken is not null)
+ {
+ var stringBuilder = new StringBuilder();
+ using (var stringWriter = new StringWriter(stringBuilder))
+ using (var jsonTextWriter = new JsonTextWriter(stringWriter))
+ {
+ switch (Indentation)
+ {
+ case TwoSpaceIndentation:
+ jsonTextWriter.Formatting = Formatting.Indented;
+ jsonTextWriter.IndentChar = ' ';
+ jsonTextWriter.Indentation = 2;
+ break;
+
+ case FourSpaceIndentation:
+ jsonTextWriter.Formatting = Formatting.Indented;
+ jsonTextWriter.IndentChar = ' ';
+ jsonTextWriter.Indentation = 4;
+ break;
+
+ case OneTabIndentation:
+ jsonTextWriter.Formatting = Formatting.Indented;
+ jsonTextWriter.IndentChar = '\t';
+ jsonTextWriter.Indentation = 1;
+ break;
+
+ case NoIndentation:
+ jsonTextWriter.Formatting = Formatting.None;
+ break;
+
+ default:
+ throw new NotSupportedException();
+ }
+
+ jtoken.WriteTo(jsonTextWriter);
+ }
+
+ output = stringBuilder.ToString();
+ return true;
+ }
+
+ output = string.Empty;
+ return false;
+ }
+ catch (JsonReaderException ex)
+ {
+ output = ex.Message;
+ return false;
+ }
+ catch (Exception ex) //some other exception
+ {
+ // TODO: Log this.
+ output = ex.Message;
+ return false;
+ }
+ }
}
}
diff --git a/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolPage.xaml b/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolPage.xaml
index a66c4abe7..3a0f34126 100644
--- a/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolPage.xaml
+++ b/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolPage.xaml
@@ -39,7 +39,7 @@
<toolkit:GridSplitter
Grid.Column="1"
Width="16"
- Margin="0,27,0,0"
+ Margin="0,42,0,0"
ResizeBehavior="BasedOnAlignment"
ResizeDirection="Auto">
<toolkit:GridSplitter.Element>
diff --git a/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolPage.xaml.cs b/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolPage.xaml.cs
index f0240bcda..dc32870cd 100644
--- a/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolPage.xaml.cs
+++ b/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolPage.xaml.cs
@@ -38,7 +38,7 @@ namespace DevTools.Providers.Impl.Tools.TextDiff
ViewModel = (TextDiffToolViewModel)parameters.Parameter!;
DataContext = ViewModel;
- ViewModel.FormattedTextBlock = OuputTextEditor;
+ ViewModel.OutputTextBlock = OuputTextEditor;
base.OnNavigatedTo(e);
}
diff --git a/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolViewModel.cs b/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolViewModel.cs
index d411329a5..4eeea6761 100644
--- a/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolViewModel.cs
+++ b/src/dev/impl/DevTools.Providers.Impl/Tools/TextDiff/TextDiffToolViewModel.cs
@@ -36,7 +36,7 @@ namespace DevTools.Providers.Impl.Tools.TextDiff
{
SetProperty(ref _oldText, value, broadcast: true);
QueueComparison();
- FormattedTextBlock?.SetInlineTextDiff(value, NewText, lineDiff: false);
+ OutputTextBlock?.SetInlineTextDiff(value, NewText, lineDiff: false);
}
}
@@ -47,11 +47,11 @@ namespace DevTools.Providers.Impl.Tools.TextDiff
{
SetProperty(ref _newText, value, broadcast: true);
QueueComparison();
- FormattedTextBlock?.SetInlineTextDiff(OldText, value, lineDiff: false);
+ OutputTextBlock?.SetInlineTextDiff(OldText, value, lineDiff: false);
}
}
- internal IFormattedTextBlock? FormattedTextBlock { private get; set; }
+ internal IFormattedTextBlock? OutputTextBlock { private get; set; }
[ImportingConstructor]
public TextDiffToolViewModel(ISettingsProvider settingsProvider, IThread thread)
@@ -75,7 +75,7 @@ namespace DevTools.Providers.Impl.Tools.TextDiff
_comparisonInProgress = true;
- Assumes.NotNull(FormattedTextBlock, nameof(FormattedTextBlock));
+ Assumes.NotNull(OutputTextBlock, nameof(OutputTextBlock));
await TaskScheduler.Default;