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

--folder option doesn't allow for whitespace fixes. #878

Closed
Tylertron1998 opened this issue Nov 26, 2020 · 6 comments
Closed

--folder option doesn't allow for whitespace fixes. #878

Tylertron1998 opened this issue Nov 26, 2020 · 6 comments
Labels
Resolved-Duplicate This issue or pull request already exists

Comments

@Tylertron1998
Copy link

I've just stumbled across this tool and am wondering about using it for some basic chat bot formatting in discord - however, it doesn't allow for --fix-whitespace when using the --folder option. Is this on purpose? Or is there any way around this?

@Tylertron1998
Copy link
Author

Tylertron1998 commented Nov 26, 2020

scratch that - looks like that option has been removed in later builds anyway. However I am still running into an issue when trimming whitespace:

given the input of:

using System;

for (var i = 0; i < 10010100; i++)
{



    Console.WriteLine("HELLO WORLD");



}

I would expect the output of:

using System;

for (var i = 0; i < 10010100; i++)
{
    Console.WriteLine("HELLO WORLD");
}

however it doesn't seem to quite work.

edit: Even when this code is put inside a class & method, it doesn't trim down the excess whitespace.

@JoeRobich
Copy link
Member

@Tylertron1998 Glad things are working a little better for you. The Roslyn formatter does not have a rule for removing blank lines. There is a StyleCop Analyzer package which you can add to your project which will provide an analyzer and codefix for consecutive blank lines (See this comment #67 (comment)). The nightly 5.x builds are able to run 3rd party analyzers and fixers (See https://github.com/dotnet/format/tree/master/docs#3rd-party-analysis). See how to install the 5.x build here - https://github.com/dotnet/format#development-builds-v5x

Closing this issue as a duplicate of #67

@JoeRobich JoeRobich added the Resolved-Duplicate This issue or pull request already exists label Nov 30, 2020
@Tylertron1998
Copy link
Author

Running 3rd party analysis requires the use of a MSBuild solution or project file as the workspace. 3rd party analyzers are discovered from the specified in the workspace project files.

Hm. This is rather unfortunate as this means I'd need a workspace, right? That's a bit of a problem, as my exact usecase for this is for formatting small snippets, that are sent in the C# discord - so the snippets aren't always 'valid' C# in the sense that someone might just send a method, or the body of a method - for example. What I am currently doing is something like this:

  • Take in the code i.e.:
for(var i = 0; i < 10; i++)

{
Console.WriteLine( "Hello" + i ); }
  • writing the code to a file (test.cs)
  • having the formatter 'correct' the file
  • reading its contents, and sending it back.

Now, from what I gather, the formatter won't actually work for this in context of a project/workspace, as the code would have to be a 100% valid C# file - is this correct?

@JoeRobich
Copy link
Member

@Tylertron1998 I put together a proof of concept for your example. The C# compiler is very good about providing semantic information for code that may not be fully compilable. In addition, the new support for Top Level Statements probably helps here as well (see https://docs.microsoft.com/en-us/dotnet/csharp/tutorials/exploration/top-level-statements).

While looking into this exact set of analyzers and fixers, I've fixed a dotnet format bug that was keeping StyleCop fixes from being applied (#884).

toplevel.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>

.editorconfig

root = true

[*.cs]
indent_style = space
indent_size = 4
insert_final_newline = false

# Disable all StyleCop analyzers then we can enable them one at a time.
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.DocumentationRules.severity = none
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.LayoutRules.severity = none
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.MaintainabilityRules.severity = none
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.NamingRules.severity = none
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.OrderingRules.severity = none
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.ReadabilityRules.severity = none
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.SpacingRules.severity = none
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.SpecialRules.severity = none

# Two or more consecutive blank lines: Remove down to one blank line. SA1507
dotnet_diagnostic.SA1507.severity = error

# Blank line immediately before or after a { line: remove it. SA1505, SA1509
dotnet_diagnostic.SA1505.severity = error
dotnet_diagnostic.SA1509.severity = error

# Blank line immediately before a } line: remove it. SA1508
dotnet_diagnostic.SA1508.severity = error

Program.cs

for(var i = 0; i < 10; i++)

{
Console.WriteLine( "Hello" + i ); }

Once the bug fix mentioned above is merged and a new nightly build is available. Running dotnet-format should produce the following output for this project.

~/Source/toplevel> dotnet format --fix-whitespace --fix-analyzers -v diag
  The dotnet runtime version is '5.0.0'.
  The dotnet CLI version is '5.0.100'.
  Using MSBuild.exe located in '/usr/local/share/dotnet/sdk/5.0.100/'.
  Formatting code files in workspace '/Users/joeyrobichaud/Source/toplevel/toplevel.csproj'.
  Loading workspace.
  Complete in 3521ms.
  Determining formattable files.
  Complete in 359ms.
  Running formatters.
  Program.cs(1,1): Fix whitespace formatting.
  Program.cs(5,2): Fix final newline.
  Running Analyzer Reference analysis.
  Determining diagnostics...
  Running 4 analyzers on toplevel.
  Program.cs(4,5): The name 'Console' does not exist in the current context (CS0103)
  Program.cs(3,1): Opening braces should not be preceded by blank line. (SA1509)
  Complete in 1758ms.
  Fixing diagnostics...
  Unable to fix CS0103. No associated code fix found.
  Running 1 analyzers on toplevel.
  Complete in 374ms.
  Analysis complete in 2133ms.
  Complete in 2943ms.
  Formatted code file '/Users/joeyrobichaud/Source/toplevel/Program.cs'.
  Formatted 1 of 3 files.
  Format complete in 6877ms.

The resulting Program.cs file will be formatted like so:

for (var i = 0; i < 10; i++)
{
    Console.WriteLine("Hello" + i);
}

@JoeRobich
Copy link
Member

Version 5.0.160201 is available and has the StyleCop FixAll fix.

@Tylertron1998
Copy link
Author

@JoeRobich this is awesome! Thank you so much - this looks perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolved-Duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants