Skip to content

Commit 6939ec2

Browse files
authored
Adding XML formatting (#858)
closes #819
1 parent 9a9a98f commit 6939ec2

File tree

85 files changed

+1853
-489
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+1853
-489
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ build
4747
publish
4848

4949
*.doctree.txt
50-
*.actual.cst
5150
*.actual.test
5251
*.vsix
5352
/.husky/pre-commit

.idea/.idea.CSharpier/.idea/prettier.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ host.AddSection(
11941194
tags: Tags.SamplesUsed,
11951195
tasks:
11961196
[
1197-
// TODO: Add any used third party instruments below as you discover them.
1197+
// TO-DO: Add any used third party instruments below as you discover them.
11981198
]
11991199
);
12001200

@@ -1204,7 +1204,7 @@ host.AddSection(
12041204
tags: Tags.SamplesUsed,
12051205
tasks:
12061206
[
1207-
// TODO: Add any used third party instruments below as you discover them.
1207+
// TO-DO: Add any used third party instruments below as you discover them.
12081208
]
12091209
);
12101210
```

CSharpier.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">False</s:Boolean>
23
<s:Boolean x:Key="/Default/UserDictionary/Words/=Concated/@EntryIndexedValue">True</s:Boolean>
34
<s:Boolean x:Key="/Default/UserDictionary/Words/=csharpier/@EntryIndexedValue">True</s:Boolean>
45
<s:Boolean x:Key="/Default/UserDictionary/Words/=csharpierignore/@EntryIndexedValue">True</s:Boolean>

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<!-- this is the warning generators get when they fail to generate a source file this makes sure the error for it failing is obvious -->
8-
<WarningsAsErrors>CS8032</WarningsAsErrors>
8+
<WarningsAsErrors>CS8032;CS8033;</WarningsAsErrors>
99
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1010
<EnableNETAnalyzers>true</EnableNETAnalyzers>
1111
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>

Shell/UpdateCSharpierRepos.psm1

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,20 @@ function CSH-UpdateCSharpierRepos()
4646
Set-Location $destination
4747
& git checkout main
4848

49-
Get-ChildItem $tempLocation | Copy-Item -Destination $destination -Filter *.cs -Recurse -Force
49+
$extensions = (".csproj", ".props", ".targets", ".xml", ".config", ".cs")
50+
51+
foreach ($extension in $extensions)
52+
{
53+
Get-ChildItem $tempLocation | Copy-Item -Destination $destination -Filter "*$extension" -Recurse -Force
54+
}
5055

56+
return
57+
5158
$items = Get-ChildItem -Recurse C:\projects\csharpier-repos -File
5259
$count = 0
5360
foreach ($item in $items)
5461
{
55-
if ($item.Name -eq ".git")
56-
{
57-
Remove-Item -Force -Recurse $item.FullName
58-
}
59-
elseif ($item.Extension -ne ".cs")
60-
{
61-
if ($item.Name -ne ".csharpierignore")
62-
{
63-
Remove-Item $item.FullName
64-
}
65-
}
66-
else
62+
if ($item.Extension -eq ".cs")
6763
{
6864
# we don't really need all of these files, let's just cut out every other one
6965
if ($count % 2 -eq 0)
@@ -74,15 +70,6 @@ function CSH-UpdateCSharpierRepos()
7470
}
7571
}
7672

77-
$items = Get-ChildItem C:\projects\csharpier-repos -Directory -Recurse
78-
foreach ($item in $items)
79-
{
80-
if ($item.Name -eq ".git")
81-
{
82-
Remove-Item -Force -Recurse $item.FullName
83-
}
84-
}
85-
8673
Set-Location $destination
8774

8875
& git add .

Src/CSharpier.Benchmarks/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ public class Benchmarks
1212
[Benchmark]
1313
public void Default_CodeFormatter()
1414
{
15-
CSharpFormatter.FormatAsync(this.largeCode, new PrinterOptions()).GetAwaiter().GetResult();
15+
CSharpFormatter
16+
.FormatAsync(this.largeCode, new PrinterOptions(Formatter.CSharp))
17+
.GetAwaiter()
18+
.GetResult();
1619
}
1720

1821
[Benchmark]

Src/CSharpier.Cli.Tests/CliTests.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
31
using System.Text;
42
using CliWrap;
53
using CliWrap.Buffered;
@@ -95,8 +93,7 @@ public async Task Should_Respect_Ignore_File_With_Subdirectory_When_DirectorOrFi
9593
await this.WriteFileAsync(filePath, unformattedContent);
9694
await this.WriteFileAsync(".csharpierignore", filePath);
9795

98-
var result = await new CsharpierProcess().WithArguments("format .").ExecuteAsync();
99-
result.Output.Should().StartWith("Formatted 0 files in ");
96+
await new CsharpierProcess().WithArguments("format .").ExecuteAsync();
10097
var fileContents = await this.ReadAllTextAsync(filePath);
10198

10299
fileContents
@@ -395,7 +392,7 @@ await this.WriteFileAsync(
395392

396393
result.ErrorOutput.Should().BeEmpty();
397394
result.ExitCode.Should().Be(0);
398-
result.Output.Should().StartWith("Formatted 0 files in ");
395+
result.Output.Should().StartWith("Formatted 1 files in ");
399396
}
400397

401398
[Test]

Src/CSharpier.Cli.Tests/ServerTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Diagnostics;
22
using System.Net.Http.Json;
3-
using System.Text;
43
using CliWrap;
54
using CliWrap.EventStream;
65
using CSharpier.Cli.Server;

Src/CSharpier.Cli/CommandLineFormatter.cs

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,13 @@ CancellationToken cancellationToken
379379

380380
cancellationToken.ThrowIfCancellationRequested();
381381

382-
CodeFormatterResult codeFormattingResult;
383-
384-
var sourceCodeKind = Path.GetExtension(fileToFormatInfo.Path).EqualsIgnoreCase(".csx")
385-
? SourceCodeKind.Script
386-
: SourceCodeKind.Regular;
382+
CodeFormatterResult? codeFormattingResult;
387383

388384
try
389385
{
390-
codeFormattingResult = await CSharpFormatter.FormatAsync(
386+
codeFormattingResult = await CodeFormatter.FormatAsync(
391387
fileToFormatInfo.FileContents,
392388
printerOptions,
393-
sourceCodeKind,
394389
cancellationToken
395390
);
396391
}
@@ -427,6 +422,12 @@ CancellationToken cancellationToken
427422
return;
428423
}
429424

425+
if (!codeFormattingResult.WarningMessage.IsBlank())
426+
{
427+
fileIssueLogger.WriteWarning(codeFormattingResult.WarningMessage);
428+
return;
429+
}
430+
430431
if (!codeFormattingResult.FailureMessage.IsBlank())
431432
{
432433
fileIssueLogger.WriteError(codeFormattingResult.FailureMessage);
@@ -435,35 +436,45 @@ CancellationToken cancellationToken
435436

436437
if (!commandLineOptions.Fast)
437438
{
438-
var syntaxNodeComparer = new SyntaxNodeComparer(
439-
fileToFormatInfo.FileContents,
440-
codeFormattingResult.Code,
441-
codeFormattingResult.ReorderedModifiers,
442-
codeFormattingResult.ReorderedUsingsWithDisabledText,
443-
codeFormattingResult.MovedTrailingTrivia,
444-
sourceCodeKind,
445-
cancellationToken
446-
);
447-
448-
try
439+
if (printerOptions.Formatter is Formatter.CSharp or Formatter.CSharpScript)
449440
{
450-
var failure = await syntaxNodeComparer.CompareSourceAsync(cancellationToken);
451-
if (!string.IsNullOrEmpty(failure))
441+
var sourceCodeKind =
442+
printerOptions.Formatter is Formatter.CSharpScript
443+
? SourceCodeKind.Script
444+
: SourceCodeKind.Regular;
445+
446+
var syntaxNodeComparer = new SyntaxNodeComparer(
447+
fileToFormatInfo.FileContents,
448+
codeFormattingResult.Code,
449+
codeFormattingResult.ReorderedModifiers,
450+
codeFormattingResult.ReorderedUsingsWithDisabledText,
451+
codeFormattingResult.MovedTrailingTrivia,
452+
sourceCodeKind,
453+
cancellationToken
454+
);
455+
456+
try
457+
{
458+
var failure = await syntaxNodeComparer.CompareSourceAsync(cancellationToken);
459+
if (!string.IsNullOrEmpty(failure))
460+
{
461+
Interlocked.Increment(
462+
ref commandLineFormatterResult.FailedSyntaxTreeValidation
463+
);
464+
fileIssueLogger.WriteError($"Failed syntax tree validation.\n{failure}");
465+
}
466+
}
467+
catch (Exception ex)
452468
{
453469
Interlocked.Increment(
454-
ref commandLineFormatterResult.FailedSyntaxTreeValidation
470+
ref commandLineFormatterResult.ExceptionsValidatingSource
455471
);
456-
fileIssueLogger.WriteError($"Failed syntax tree validation.\n{failure}");
457-
}
458-
}
459-
catch (Exception ex)
460-
{
461-
Interlocked.Increment(ref commandLineFormatterResult.ExceptionsValidatingSource);
462472

463-
fileIssueLogger.WriteError(
464-
"Failed with exception during syntax tree validation.",
465-
ex
466-
);
473+
fileIssueLogger.WriteError(
474+
"Failed with exception during syntax tree validation.",
475+
ex
476+
);
477+
}
467478
}
468479
}
469480

0 commit comments

Comments
 (0)