Skip to content

Commit baa3138

Browse files
authored
Some refactoring that was in the csproj branch but didn't really belng there (#1415)
1 parent bfedce4 commit baa3138

File tree

245 files changed

+607
-723
lines changed

Some content is hidden

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

245 files changed

+607
-723
lines changed

.editorconfig

+26
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,31 @@
22
indent_size = 2
33
indent_style = space
44

5+
csharp_using_directive_placement = outside_namespace
6+
csharp_style_inlined_variable_declaration = true
7+
csharp_style_pattern_matching_over_is_with_cast_check = true
8+
csharp_style_var_elsewhere = true
9+
csharp_style_var_for_built_in_types = true
10+
csharp_style_var_when_type_is_apparent = true
11+
512
[*.{cs,vb}]
613
dotnet_diagnostic.RS0041.severity = warning
14+
dotnet_diagnostic.IDE0007.severity = warning
15+
dotnet_diagnostic.IDE0018.severity = warning
16+
dotnet_diagnostic.IDE0019.severity = warning
17+
dotnet_diagnostic.IDE0020.severity = warning
18+
dotnet_diagnostic.IDE0029.severity = warning
19+
dotnet_diagnostic.IDE0030.severity = warning
20+
dotnet_diagnostic.IDE0031.severity = warning
21+
dotnet_diagnostic.IDE0032.severity = warning
22+
dotnet_diagnostic.IDE0036.severity = warning
23+
dotnet_diagnostic.IDE0038.severity = warning
24+
dotnet_diagnostic.IDE0065.severity = warning
25+
dotnet_diagnostic.IDE0075.severity = warning
26+
dotnet_diagnostic.IDE0078.severity = warning
27+
dotnet_diagnostic.IDE0150.severity = warning
28+
dotnet_diagnostic.IDE0290.severity = warning
29+
dotnet_diagnostic.IDE0300.severity = warning
30+
dotnet_diagnostic.IDE0301.severity = warning
31+
32+

CSharpier.sln

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
2424
Directory.Build.props = Directory.Build.props
2525
Directory.Packages.props = Directory.Packages.props
2626
Nuget\Build.props = Nuget\Build.props
27+
.editorconfig = .editorconfig
2728
EndProjectSection
2829
EndProject
2930
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpier.Benchmarks", "Src\CSharpier.Benchmarks\CSharpier.Benchmarks.csproj", "{A338903F-69AD-4950-B827-8EE75F98B620}"

Directory.Build.props

+2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@
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 -->
88
<WarningsAsErrors>CS8032</WarningsAsErrors>
99
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
10+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
11+
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
1012
</PropertyGroup>
1113
</Project>

Src/CSharpier.Benchmarks/Program.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
using System.Threading;
33
using BenchmarkDotNet.Attributes;
44
using BenchmarkDotNet.Running;
5+
using Microsoft.CodeAnalysis;
56

67
namespace CSharpier.Benchmarks;
78

8-
using Microsoft.CodeAnalysis;
9-
109
[MemoryDiagnoser]
1110
public class Benchmarks
1211
{

Src/CSharpier.Cli.Tests/CliTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
namespace CSharpier.Cli.Tests;
2-
31
using System.Collections.Generic;
42
using System.Linq;
53
using System.Text;
@@ -8,6 +6,8 @@ namespace CSharpier.Cli.Tests;
86
using FluentAssertions;
97
using NUnit.Framework;
108

9+
namespace CSharpier.Cli.Tests;
10+
1111
// these tests are kind of nice as c# because they run in the same place.
1212
// except the one test that has issues with console input redirection
1313
// they used to be powershell, but doing the multiple file thing didn't work

Src/CSharpier.Cli.Tests/EditorConfig/SectionTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
namespace CSharpier.Cli.Tests.EditorConfig;
2-
31
using CSharpier.Cli.EditorConfig;
42
using FluentAssertions;
53
using IniParser.Model;
64
using NUnit.Framework;
75

6+
namespace CSharpier.Cli.Tests.EditorConfig;
7+
88
[TestFixture]
99
public class SectionTests
1010
{

Src/CSharpier.Cli.Tests/ServerTests.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
namespace CSharpier.Cli.Tests;
2-
31
using System.Diagnostics;
42
using System.Net.Http.Json;
53
using System.Text;
@@ -9,6 +7,8 @@ namespace CSharpier.Cli.Tests;
97
using FluentAssertions;
108
using NUnit.Framework;
119

10+
namespace CSharpier.Cli.Tests;
11+
1212
[TestFixture]
1313
public class ServerTests
1414
{
@@ -79,7 +79,8 @@ public void RunTwo()
7979

8080
async Task NewFunction()
8181
{
82-
var command = Cli.Wrap("dotnet")
82+
var command = CliWrap
83+
.Cli.Wrap("dotnet")
8384
.WithArguments(path + " --server")
8485
.WithValidation(CommandResultValidation.None);
8586

Src/CSharpier.Cli/CommandLineFormatter.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
using System.Text;
44
using CSharpier.Cli.Options;
55
using CSharpier.Utilities;
6+
using Microsoft.CodeAnalysis;
67
using Microsoft.Extensions.Logging;
78

89
namespace CSharpier.Cli;
910

10-
using Microsoft.CodeAnalysis;
11-
1211
internal static class CommandLineFormatter
1312
{
1413
public static async Task<int> Format(

Src/CSharpier.Cli/CommandLineOptions.cs

+16-26
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System.CommandLine;
2+
using Microsoft.Extensions.Logging;
23

34
namespace CSharpier.Cli;
45

5-
using Microsoft.Extensions.Logging;
6-
76
internal class CommandLineOptions
87
{
9-
public string[] DirectoryOrFilePaths { get; init; } = Array.Empty<string>();
8+
public string[] DirectoryOrFilePaths { get; init; } = [];
109
public bool Check { get; init; }
1110
public bool Fast { get; init; }
1211
public bool SkipWrite { get; init; }
@@ -17,7 +16,7 @@ internal class CommandLineOptions
1716
public bool IncludeGenerated { get; init; }
1817
public string? StandardInFileContents { get; init; }
1918
public string? ConfigPath { get; init; }
20-
public string[] OriginalDirectoryOrFilePaths { get; init; } = Array.Empty<string>();
19+
public string[] OriginalDirectoryOrFilePaths { get; init; } = [];
2120

2221
internal delegate Task<int> Handler(
2322
string[] directoryOrFile,
@@ -47,57 +46,48 @@ public static RootCommand Create()
4746
Description =
4847
"One or more paths to a directory containing C# files to format or a C# file to format. It may be ommited when piping data via stdin.",
4948
}.LegalFilePathsOnly(),
50-
new Option(
51-
new[] { "--check" },
52-
"Check that files are formatted. Will not write any changes."
53-
),
49+
new Option(["--check"], "Check that files are formatted. Will not write any changes."),
5450
new Option<string>(
55-
new[] { "--loglevel" },
51+
["--loglevel"],
5652
() => LogLevel.Information.ToString(),
5753
"Specify the log level - Debug, Information (default), Warning, Error, None"
5854
),
5955
new Option(
60-
new[] { "--no-cache" },
56+
["--no-cache"],
6157
"Bypass the cache to determine if a file needs to be formatted."
6258
),
6359
new Option(
64-
new[] { "--no-msbuild-check" },
60+
["--no-msbuild-check"],
6561
"Bypass the check to determine if a csproj files references a different version of CSharpier.MsBuild."
6662
),
6763
new Option(
68-
new[] { "--include-generated" },
64+
["--include-generated"],
6965
"Include files generated by the SDK and files that begin with <autogenerated /> comments"
7066
),
7167
new Option(
72-
new[] { "--fast" },
68+
["--fast"],
7369
"Skip comparing syntax tree of formatted file to original file to validate changes."
7470
),
7571
new Option(
76-
new[] { "--skip-write" },
72+
["--skip-write"],
7773
"Skip writing changes. Generally used for testing to ensure csharpier doesn't throw any errors or cause syntax tree validation failures."
7874
),
75+
new Option(["--write-stdout"], "Write the results of formatting any files to stdout."),
7976
new Option(
80-
new[] { "--write-stdout" },
81-
"Write the results of formatting any files to stdout."
82-
),
83-
new Option(
84-
new[] { "--pipe-multiple-files" },
77+
["--pipe-multiple-files"],
8578
"Keep csharpier running so that multiples files can be piped to it via stdin."
8679
),
8780
new Option(
88-
new[] { "--server" },
81+
["--server"],
8982
"Run CSharpier as a server so that multiple files may be formatted."
9083
),
9184
new Option<int?>(
92-
new[] { "--server-port" },
85+
["--server-port"],
9386
"Specify the port that CSharpier should start on. Defaults to a random unused port."
9487
),
95-
new Option<string>(
96-
new[] { "--config-path" },
97-
"Path to the CSharpier configuration file"
98-
),
88+
new Option<string>(["--config-path"], "Path to the CSharpier configuration file"),
9989
new Option(
100-
new[] { "--compilation-errors-as-warnings" },
90+
["--compilation-errors-as-warnings"],
10191
"Treat compilation errors from files as warnings instead of errors."
10292
),
10393
};

Src/CSharpier.Cli/ConsoleLogger.cs

+8-17
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,10 @@
22

33
namespace CSharpier.Cli;
44

5-
internal class ConsoleLogger : ILogger
5+
internal class ConsoleLogger(IConsole console, LogLevel loggingLevel) : ILogger
66
{
77
private static readonly object ConsoleLock = new();
88

9-
private readonly IConsole console;
10-
private readonly LogLevel loggingLevel;
11-
12-
public ConsoleLogger(IConsole console, LogLevel loggingLevel)
13-
{
14-
this.console = console;
15-
this.loggingLevel = loggingLevel;
16-
}
17-
189
public virtual void Log<TState>(
1910
LogLevel logLevel,
2011
EventId eventId,
@@ -23,7 +14,7 @@ public virtual void Log<TState>(
2314
Func<TState, Exception, string> formatter
2415
)
2516
{
26-
if (logLevel < this.loggingLevel)
17+
if (logLevel < loggingLevel)
2718
{
2819
return;
2920
}
@@ -32,23 +23,23 @@ void Write(string value)
3223
{
3324
if (logLevel >= LogLevel.Error)
3425
{
35-
this.console.WriteError(value);
26+
console.WriteError(value);
3627
}
3728
else
3829
{
39-
this.console.Write(value);
30+
console.Write(value);
4031
}
4132
}
4233

4334
void WriteLine(string? value = null)
4435
{
4536
if (logLevel >= LogLevel.Error)
4637
{
47-
this.console.WriteErrorLine(value);
38+
console.WriteErrorLine(value);
4839
}
4940
else
5041
{
51-
this.console.WriteLine(value);
42+
console.WriteLine(value);
5243
}
5344
}
5445

@@ -63,9 +54,9 @@ void WriteLine(string? value = null)
6354

6455
if (logLevel >= LogLevel.Warning)
6556
{
66-
this.console.ForegroundColor = GetColorLevel(logLevel);
57+
console.ForegroundColor = GetColorLevel(logLevel);
6758
Write($"{logLevel} ");
68-
this.console.ResetColor();
59+
console.ResetColor();
6960
}
7061

7162
var stringReader = new StringReader(message);

0 commit comments

Comments
 (0)