Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding csproj formatting #858

Draft
wants to merge 43 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
fec0970
Working on moving around tests
belav Mar 20, 2023
ee13114
Making some changes so that adding more file types is easier to test
belav Mar 20, 2023
85e1731
Refactoring towards multiple formatters
belav Mar 20, 2023
c617fe4
Updating ignore
belav Mar 20, 2023
b1fdc0f
Some cleanup
belav Mar 20, 2023
756b51f
Working on getting the generators happy with multiple directories
belav Mar 20, 2023
57395fd
Getting basics working
belav Mar 20, 2023
e72b76d
Adding csproj to playground. Switching to a sane state management pat…
belav Mar 27, 2023
5887f44
Trying to format with the CLI
belav Mar 27, 2023
30f05a4
fixing warnings
belav Mar 27, 2023
f5e8be4
Some notes and fixes
belav Mar 28, 2023
4a4c2a2
Adding some tests. Fixing <?xml being removed
belav Apr 17, 2023
e51834d
Getting the basics of printing Xml with Doc going
belav Apr 17, 2023
e61f38d
Merge branch 'main' into csproj-formatting
belav Apr 17, 2023
a8d1c64
Fixing thing
belav Apr 17, 2023
ebd2316
Getting attributes working better
belav Apr 17, 2023
f95ad65
some little cleanup
belav Apr 17, 2023
a913e77
Merge branch 'main' into csproj-formatting
belav Sep 15, 2024
14fa782
get playground partly working
belav Sep 15, 2024
21b7185
cleanup usings
belav Sep 15, 2024
a8ffa77
Getting things working with the changes that were in main
belav Sep 16, 2024
9367648
starting to pull in all the logic from how prettier prints html
belav Sep 17, 2024
25ce144
add note for looking into the xml plugin to see if it is easier
belav Sep 17, 2024
eaf396d
getting child elements partly working
belav Sep 22, 2024
d6ab600
kinda getting somewhere.... maybe
belav Sep 23, 2024
2f37675
now this is actually getting somewhat close
belav Sep 23, 2024
9eb07e0
actually getting close now
belav Sep 23, 2024
a99fd33
getting initial xml formatting done. settling on 2 spaces for default…
belav Sep 27, 2024
3fd3bb8
Fixing up some edge cases
belav Oct 5, 2024
3bb0661
adding some more notes about edge cases
belav Oct 5, 2024
4f4f476
Merge branch 'main' into csproj-formatting
belav Oct 20, 2024
463f89d
Merge branch 'main' into csproj-formatting
belav Oct 20, 2024
3b7f27e
fixing more edge cases
belav Oct 20, 2024
fb448ab
Merge branch 'main' into csproj-formatting
belav Nov 11, 2024
662b362
resolving merge issues
belav Nov 11, 2024
5bf1b55
some cleanup
belav Nov 11, 2024
6264407
Merge branch 'main' into csproj-formatting
belav Nov 11, 2024
968cef9
Fixing more issues
belav Nov 11, 2024
36673f1
Merge branch 'main' into csproj-formatting
belav Nov 17, 2024
681b2fe
finding a road block
belav Nov 17, 2024
902729a
Merge branch 'main' into csproj-formatting
belav Nov 18, 2024
fbcefb8
getting things mostly converted to using XDocument instead
belav Nov 18, 2024
ac66f01
getting things kinda sorta working
belav Nov 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ indent_style = space

[*.{cs,vb}]
dotnet_diagnostic.RS0041.severity = warning
dotnet_diagnostic.IDE0065.severity = warning
csharp_using_directive_placement = outside_namespace
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ build
publish

*.doctree.txt
*.actual.cst
*.actual.test
*.vsix
/.husky/pre-commit
Expand Down
7 changes: 7 additions & 0 deletions .idea/.idea.CSharpier/.idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ host.AddSection(
tags: Tags.SamplesUsed,
tasks:
[
// TODO: Add any used third party instruments below as you discover them.
// TO-DO: Add any used third party instruments below as you discover them.
]
);

Expand All @@ -1107,7 +1107,7 @@ host.AddSection(
tags: Tags.SamplesUsed,
tasks:
[
// TODO: Add any used third party instruments below as you discover them.
// TO-DO: Add any used third party instruments below as you discover them.
]
);
```
Expand Down
1 change: 1 addition & 0 deletions CSharpier.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<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">
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Concated/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=csharpier/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=csharpierignore/@EntryIndexedValue">True</s:Boolean>
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<!-- this is the warning generators get when they fail to generate a source file this makes sure the error for it failing is obvious -->
<WarningsAsErrors>CS8032</WarningsAsErrors>
<WarningsAsErrors>CS8032;CS8033;</WarningsAsErrors>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
31 changes: 9 additions & 22 deletions Shell/UpdateCSharpierRepos.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,20 @@ function CSH-UpdateCSharpierRepos()
Set-Location $destination
& git checkout main

Get-ChildItem $tempLocation | Copy-Item -Destination $destination -Filter *.cs -Recurse -Force
$extensions = (".csproj", ".props", ".targets", ".xml", ".config", ".cs")

foreach ($extension in $extensions)
{
Get-ChildItem $tempLocation | Copy-Item -Destination $destination -Filter "*$extension" -Recurse -Force
}

return

$items = Get-ChildItem -Recurse C:\projects\csharpier-repos -File
$count = 0
foreach ($item in $items)
{
if ($item.Name -eq ".git")
{
Remove-Item -Force -Recurse $item.FullName
}
elseif ($item.Extension -ne ".cs")
{
if ($item.Name -ne ".csharpierignore")
{
Remove-Item $item.FullName
}
}
else
if ($item.Extension -eq ".cs")
{
# we don't really need all of these files, let's just cut out every other one
if ($count % 2 -eq 0)
Expand All @@ -74,15 +70,6 @@ function CSH-UpdateCSharpierRepos()
}
}

$items = Get-ChildItem C:\projects\csharpier-repos -Directory -Recurse
foreach ($item in $items)
{
if ($item.Name -eq ".git")
{
Remove-Item -Force -Recurse $item.FullName
}
}

Set-Location $destination

& git add .
Expand Down
8 changes: 5 additions & 3 deletions Src/CSharpier.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
using System.Threading;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.CodeAnalysis;

namespace CSharpier.Benchmarks;

using Microsoft.CodeAnalysis;

[MemoryDiagnoser]
public class Benchmarks
{
[Benchmark]
public void Default_CodeFormatter()
{
CSharpFormatter.FormatAsync(this.largeCode, new PrinterOptions()).GetAwaiter().GetResult();
CSharpFormatter
.FormatAsync(this.largeCode, new PrinterOptions(Formatter.CSharp))
.GetAwaiter()
.GetResult();
}

[Benchmark]
Expand Down
6 changes: 2 additions & 4 deletions Src/CSharpier.Cli.Tests/CliTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
namespace CSharpier.Cli.Tests;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using CliWrap;
using CliWrap.Buffered;
using FluentAssertions;
using NUnit.Framework;

namespace CSharpier.Cli.Tests;

// these tests are kind of nice as c# because they run in the same place.
// except the one test that has issues with console input redirection
// they used to be powershell, but doing the multiple file thing didn't work
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Cli.Tests/EditorConfig/SectionTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace CSharpier.Cli.Tests.EditorConfig;

using CSharpier.Cli.EditorConfig;
using FluentAssertions;
using IniParser.Model;
using NUnit.Framework;

namespace CSharpier.Cli.Tests.EditorConfig;

[TestFixture]
public class SectionTests
{
Expand Down
8 changes: 4 additions & 4 deletions Src/CSharpier.Cli.Tests/ServerTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace CSharpier.Cli.Tests;

using System.Diagnostics;
using System.Net.Http.Json;
using System.Text;
using CliWrap;
using CliWrap.EventStream;
using CSharpier.Cli.Server;
using FluentAssertions;
using NUnit.Framework;

namespace CSharpier.Cli.Tests;

[TestFixture]
public class ServerTests
{
Expand Down Expand Up @@ -79,7 +78,8 @@ public void RunTwo()

async Task NewFunction()
{
var command = Cli.Wrap("dotnet")
var command = CliWrap
.Cli.Wrap("dotnet")
.WithArguments(path + " --server")
.WithValidation(CommandResultValidation.None);

Expand Down
68 changes: 36 additions & 32 deletions Src/CSharpier.Cli/CommandLineFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
using System.Text;
using CSharpier.Cli.Options;
using CSharpier.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;

namespace CSharpier.Cli;

using Microsoft.CodeAnalysis;

internal static class CommandLineFormatter
{
public static async Task<int> Format(
Expand Down Expand Up @@ -380,18 +379,13 @@ CancellationToken cancellationToken

cancellationToken.ThrowIfCancellationRequested();

CodeFormatterResult codeFormattingResult;

var sourceCodeKind = Path.GetExtension(fileToFormatInfo.Path).EqualsIgnoreCase(".csx")
? SourceCodeKind.Script
: SourceCodeKind.Regular;
CodeFormatterResult? codeFormattingResult;

try
{
codeFormattingResult = await CSharpFormatter.FormatAsync(
codeFormattingResult = await CodeFormatter.FormatAsync(
fileToFormatInfo.FileContents,
printerOptions,
sourceCodeKind,
cancellationToken
);
}
Expand Down Expand Up @@ -436,34 +430,44 @@ CancellationToken cancellationToken

if (!commandLineOptions.Fast)
{
var syntaxNodeComparer = new SyntaxNodeComparer(
fileToFormatInfo.FileContents,
codeFormattingResult.Code,
codeFormattingResult.ReorderedModifiers,
codeFormattingResult.ReorderedUsingsWithDisabledText,
sourceCodeKind,
cancellationToken
);

try
if (printerOptions.Formatter is Formatter.CSharp or Formatter.CSharpScript)
{
var failure = await syntaxNodeComparer.CompareSourceAsync(cancellationToken);
if (!string.IsNullOrEmpty(failure))
var sourceCodeKind =
printerOptions.Formatter is Formatter.CSharpScript
? SourceCodeKind.Script
: SourceCodeKind.Regular;

var syntaxNodeComparer = new SyntaxNodeComparer(
fileToFormatInfo.FileContents,
codeFormattingResult.Code,
codeFormattingResult.ReorderedModifiers,
codeFormattingResult.ReorderedUsingsWithDisabledText,
sourceCodeKind,
cancellationToken
);

try
{
var failure = await syntaxNodeComparer.CompareSourceAsync(cancellationToken);
if (!string.IsNullOrEmpty(failure))
{
Interlocked.Increment(
ref commandLineFormatterResult.FailedSyntaxTreeValidation
);
fileIssueLogger.WriteError($"Failed syntax tree validation.\n{failure}");
}
}
catch (Exception ex)
{
Interlocked.Increment(
ref commandLineFormatterResult.FailedSyntaxTreeValidation
ref commandLineFormatterResult.ExceptionsValidatingSource
);
fileIssueLogger.WriteError($"Failed syntax tree validation.\n{failure}");
}
}
catch (Exception ex)
{
Interlocked.Increment(ref commandLineFormatterResult.ExceptionsValidatingSource);

fileIssueLogger.WriteError(
"Failed with exception during syntax tree validation.",
ex
);
fileIssueLogger.WriteError(
"Failed with exception during syntax tree validation.",
ex
);
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions Src/CSharpier.Cli/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.CommandLine;
using Microsoft.Extensions.Logging;

namespace CSharpier.Cli;

using Microsoft.Extensions.Logging;

internal class CommandLineOptions
{
public string[] DirectoryOrFilePaths { get; init; } = Array.Empty<string>();
Expand Down
10 changes: 7 additions & 3 deletions Src/CSharpier.Cli/EditorConfig/EditorConfigSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ internal class EditorConfigSections

var formatter =
resolvedConfiguration.Formatter
?? (filePath.EndsWith(".cs") || filePath.EndsWith(".csx") ? "csharp" : null);
?? (
filePath.EndsWith(".cs") ? "CSharp"
: filePath.EndsWith(".csx") ? "CSharpScript"
: null
);

if (formatter == null)
if (!Enum.TryParse<Formatter>(formatter, ignoreCase: true, out var parsedFormatter))
{
return null;
}

var printerOptions = new PrinterOptions { Formatter = formatter };
var printerOptions = new PrinterOptions(parsedFormatter);

if (resolvedConfiguration.MaxLineLength is { } maxLineLength)
{
Expand Down
7 changes: 4 additions & 3 deletions Src/CSharpier.Cli/EditorConfig/GlobMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
* From https://github.com/SLaks/Minimatch
*/

#nullable disable

namespace CSharpier.Cli.EditorConfig;

using System;
using System.Collections.Generic;
Expand All @@ -16,6 +13,10 @@ namespace CSharpier.Cli.EditorConfig;
using System.Text;
using System.Text.RegularExpressions;

#nullable disable

namespace CSharpier.Cli.EditorConfig;

// ReSharper disable UnusedAutoPropertyAccessor.Global

///<summary>Contains options that control how Minimatch matches strings.</summary>
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Cli/EditorConfig/Section.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace CSharpier.Cli.EditorConfig;

using IniParser.Model;

namespace CSharpier.Cli.EditorConfig;

internal class Section
{
private readonly GlobMatcher matcher;
Expand Down
3 changes: 1 addition & 2 deletions Src/CSharpier.Cli/FormattingCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
using System.IO.Hashing;
using System.Text;
using System.Text.Json;
using CSharpier.Cli.Options;
using CSharpier.Utilities;

namespace CSharpier.Cli;

using CSharpier.Cli.Options;

internal interface IFormattingCache
{
Task ResolveAsync(CancellationToken cancellationToken);
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Cli/Options/CaseInsensitiveEnumConverter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace CSharpier.Cli.Options;

using System.Text.Json;
using System.Text.Json.Serialization;

namespace CSharpier.Cli.Options;

internal class CaseInsensitiveEnumConverter<TEnum> : JsonConverter<TEnum>
where TEnum : struct
{
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier.Cli/Options/ConfigFileParser.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace CSharpier.Cli.Options;

using System.IO.Abstractions;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace CSharpier.Cli.Options;

internal static class ConfigFileParser
{
private static readonly string[] validExtensions = { ".csharpierrc", ".json", ".yml", ".yaml" };
Expand Down
Loading
Loading