Skip to content

Commit 9d7846f

Browse files
committed
XML formatter implementation
1 parent 0177390 commit 9d7846f

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

src/CodeSnip/MainWindow.xaml.cs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,24 @@ private async void FormatCSharpier_Click(object sender, RoutedEventArgs e)
269269
{
270270

271271
string? code = mainViewModel.SelectedSnippet?.Category?.Language?.Code;
272-
if (code is not null and "cs")
272+
if (code is not null)
273273
{
274-
string originalCode = textEditor.Text;
275-
var (isSuccess, formatted, error) = await FormattingService.TryFormatCodeWithCSharpierAsync(originalCode);
274+
var originalCode = textEditor.Text;
275+
276+
(bool isSuccess, string? formatted, string? error) = code switch
277+
{
278+
"cs" => await FormattingService.TryFormatCodeWithCSharpierAsync(originalCode),
279+
"xml" => await FormattingService.TryFormatXmlWithCSharpierAsync(originalCode),
280+
_ => (false, null, $"Formatting for '{code}' is not supported with CSharpier")
281+
};
282+
276283
if (isSuccess)
277284
{
278-
textEditor.Document.Text = formatted;
285+
textEditor.Document.Text = formatted!;
279286
}
280287
else
281288
{
282-
MessageBox.Show($"Formatting failed: {error}");
289+
MessageBox.Show($"Formatting failed:\n {error}");
283290
}
284291
}
285292
}
@@ -357,7 +364,7 @@ private async void FormatAll_Click(object sender, RoutedEventArgs e)
357364
}
358365
else
359366
{
360-
MessageBox.Show(errorDfmt);
367+
MessageBox.Show($"Formatting (dfmt) failed:\n {errorDfmt}");
361368
}
362369
break;
363370

@@ -369,7 +376,7 @@ private async void FormatAll_Click(object sender, RoutedEventArgs e)
369376
}
370377
else
371378
{
372-
MessageBox.Show($"Formatting (CSharpier) failed: {errorCs}");
379+
MessageBox.Show($"Formatting (CSharpier) failed:\n{errorCs}");
373380
}
374381
break;
375382

@@ -381,7 +388,7 @@ private async void FormatAll_Click(object sender, RoutedEventArgs e)
381388
}
382389
else
383390
{
384-
MessageBox.Show(errorBlack);
391+
MessageBox.Show($"Formatting (Black) failed:\n{errorBlack}");
385392
}
386393
break;
387394

@@ -393,8 +400,21 @@ private async void FormatAll_Click(object sender, RoutedEventArgs e)
393400
}
394401
else
395402
{
396-
MessageBox.Show(errorRust);
403+
MessageBox.Show($"Formatting (rustfmt) failed:\n{ errorRust}");
404+
}
405+
break;
406+
407+
case "xml":
408+
var (successXml, formattedXml, errorXml) = await FormattingService.TryFormatXmlWithCSharpierAsync(originalCode);
409+
if (successXml)
410+
{
411+
textEditor.Document.Text = formattedXml;
412+
}
413+
else
414+
{
415+
MessageBox.Show($"Formatting (CSharpier XML) failed:\n{errorXml}");
397416
}
417+
398418
break;
399419

400420
default:
@@ -416,7 +436,7 @@ private async void FormatAll_Click(object sender, RoutedEventArgs e)
416436
}
417437
else
418438
{
419-
MessageBox.Show(errorClang);
439+
MessageBox.Show($"Formatting (clang-format) failed:\n{errorClang}");
420440
}
421441
}
422442
break;

src/CodeSnip/Services/FormattingService.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using CSharpier.Core.CSharp;
2+
using CSharpier.Core.Xml;
23
using System.Diagnostics;
34
using System.IO;
45

@@ -9,6 +10,8 @@ public static class FormattingService
910
private static bool? _isPythonInstalled;
1011
private static bool? _isBlackInstalled;
1112

13+
/// <summary>
14+
/// Formats C# code using CSharpier's C# formatter.
1215
public static async Task<(bool isSuccess, string? formattedCode, string? errorMessage)> TryFormatCodeWithCSharpierAsync(string code)
1316
{
1417
try
@@ -22,6 +25,20 @@ public static class FormattingService
2225
}
2326
}
2427

28+
/// <summary>
29+
/// Formats XML code using CSharpier's XML formatter.
30+
public static async Task<(bool isSuccess, string? formattedCode, string? errorMessage)> TryFormatXmlWithCSharpierAsync(string code)
31+
{
32+
try
33+
{
34+
var result = await Task.Run(() => XmlFormatter.Format(code));
35+
return (true, result.Code, null);
36+
}
37+
catch (Exception ex)
38+
{
39+
return (false, null, ex.Message);
40+
}
41+
}
2542

2643
/// <summary>
2744
/// Formats the code using clang-format.

0 commit comments

Comments
 (0)