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 23 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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.

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>
68 changes: 68 additions & 0 deletions Scripts/UpdateCSharpierRepos.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
$repositories = @()
$repositories += "https://github.com/dotnet/aspnetcore.git"
$repositories += "https://github.com/aspnet/AspNetWebStack.git"
$repositories += "https://github.com/AutoMapper/AutoMapper.git"
$repositories += "https://github.com/castleproject/Core.git"
$repositories += "https://github.com/dotnet/command-line-api.git"
$repositories += "https://github.com/dotnet/format.git"
$repositories += "https://github.com/dotnet/efcore.git"
$repositories += "https://github.com/moq/moq4.git"
$repositories += "https://github.com/JamesNK/Newtonsoft.Json.git"
$repositories += "https://github.com/dotnet/roslyn.git"
$repositories += "https://github.com/dotnet/runtime.git"
$repositories += "https://github.com/mono/mono.git"
$repositories += "https://github.com/increase-POS/Res-Server.git"

$tempLocation = "c:\temp\UpdateRepos"

if (-not (Test-Path $tempLocation)) {
New-Item $tempLocation -Force -ItemType Directory
}

Set-Location $tempLocation

$ErrorActionPreference = "Continue"

foreach ($repository in $repositories) {
$repoFolder = $tempLocation + "/" + (Split-Path $repositories -Leaf).Replace(".git", "")
if (Test-Path $repoFolder) {
Set-Location $repoFolder
& git pull origin
Set-Location $tempLocation
}
else {
& git clone $repository
}
}

$destination = "C:\projects\csharpier-repos\"
Set-Location $destination
& git checkout main

Get-ChildItem $tempLocation | Copy-Item -Destination $destination -Recurse -Force

$extensions = (".cs", ".csproj", ".props", ".targets")

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

if (-not ($extensions.Contains($item.Extension)) -and $item.Name -ne ".csharpierignore") {
Remove-Item $item.FullName
}
}

$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 .
& git commit -m "Updating repos"
& git push origin
3 changes: 1 addition & 2 deletions Src/CSharpier.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using System.Threading;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.CodeAnalysis;

namespace CSharpier.Benchmarks;

using Microsoft.CodeAnalysis;

[MemoryDiagnoser]
public class Benchmarks
{
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
37 changes: 18 additions & 19 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 @@ -177,9 +176,9 @@ CancellationToken cancellationToken
cancellationToken
);

var originalDirectoryOrFile = commandLineOptions
.OriginalDirectoryOrFilePaths[x]
.Replace("\\", "/");
var originalDirectoryOrFile = commandLineOptions.OriginalDirectoryOrFilePaths[
x
].Replace("\\", "/");

var formattingCache = await FormattingCacheFactory.InitializeAsync(
commandLineOptions,
Expand Down Expand Up @@ -255,12 +254,8 @@ await FormatPhysicalFile(
return 1;
}

var tasks = fileSystem
.Directory.EnumerateFiles(
directoryOrFilePath,
"*.*",
SearchOption.AllDirectories
)
var tasks = fileSystem.Directory
.EnumerateFiles(directoryOrFilePath, "*.*", SearchOption.AllDirectories)
.Select(o =>
{
var normalizedPath = o.Replace("\\", "/");
Expand Down Expand Up @@ -380,18 +375,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 @@ -434,8 +424,17 @@ CancellationToken cancellationToken
return;
}

if (!commandLineOptions.Fast)
// TODO xml implement this stuff - maybe new PR?
// https://github.com/belav/csharpier/pull/858#issuecomment-1487385384
// TODO xml review this https://github.com/belav/csharpier-repos/pull/67
// TODO xml what about allowing lines between elements?
if (!commandLineOptions.Fast && fileToFormatInfo.Path.EndsWithIgnoreCase(".cs"))
{
// TODO xml the thing above should do this if we are using the csharp formatter
var sourceCodeKind = Path.GetExtension(fileToFormatInfo.Path).EqualsIgnoreCase(".csx")
? SourceCodeKind.Script
: SourceCodeKind.Regular;

var syntaxNodeComparer = new SyntaxNodeComparer(
fileToFormatInfo.FileContents,
codeFormattingResult.Code,
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 { Formatter = 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