Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #4 from Projektanker/development
Browse files Browse the repository at this point in the history
Version 1.2.0
  • Loading branch information
just-seba authored Jan 31, 2021
2 parents 0f05d9a + 19dac06 commit e6ab31d
Show file tree
Hide file tree
Showing 30 changed files with 354 additions and 645 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 1.2.0 (January 31, 2021)
- Bug fixes:
- Removes buggy member padding fix feature. It is replaced by a simpler approach due to lack of time:
- Removes trailing whitespace.
- Removes consecutive blank lines. between the members. Adds a blank line between members if there is none.


## 1.1.0 (December 25, 2020)

- Bug fixes:
Expand Down
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This tool is heavily inspired by [CodeMaid](https://www.codemaid.net). As it is
Using this tool will cleanup your `C#` file by
1. reorganizing the layout of the members in the C# file to follow Microsoft's StyleCop conventions
2. sorting it's using directives
3. fixing the line paddings between the members in the C# file
3. fixing tailing whitespace and consecutive blank lines
4. (only in [VS code extension](https://marketplace.visualstudio.com/items?itemName=projektanker.code-butler)) executing `Format Document` command.

as described below.
Expand Down Expand Up @@ -61,20 +61,10 @@ using MyAlias = Example.Bar;
using static System.Math;
```

### Fix line padding between the members in a C# file
Removes consecutive blank lines between the members. Adds a blank line between members if there is none.
### Fix tailing whitespace and consecutive blank lines
- Removes trailing whitespace.
- Removes consecutive blank lines. between the members. Adds a blank line between members if there is none.

Fields are treated specially. Blank lines between fields are removed. Fields with leading comments or attributes are surrounded by a blank line:
```csharp
private int _capacity;
private int _count;

// This is a message
[JsonProperty("Message")]
private string _message;

private string _prefix;
```
## Prerequisites
- [.NET 5 runtime](https://dotnet.microsoft.com/download/dotnet/5.0)

Expand Down
65 changes: 62 additions & 3 deletions src/CodeButler/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
root = true

[*.cs]
# CA1031: Do not catch general exception types
dotnet_diagnostic.CA1031.severity = suggestion

# CA1062: Null check in externally visible methods
dotnet_diagnostic.CA1062.severity = none

# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = suggestion

# CA1305: ToString() with IFormatProvider
dotnet_diagnostic.CA1305.severity = silent

# CA1716: Identifiers should not match keywords
dotnet_diagnostic.CA1716.severity = suggestion

# CA2007: ConfigureAwait
dotnet_diagnostic.CA2007.severity = silent

# IDE0010: Populate switch statement
dotnet_diagnostic.IDE0010.severity = suggestion

# IDE0072: Populate switch expression
dotnet_diagnostic.IDE0072.severity = suggestion

# SA0001: XML comment analysis is disabled due to project configuration
dotnet_diagnostic.SA0001.severity = none

Expand All @@ -14,14 +38,32 @@ dotnet_diagnostic.SA1123.severity = suggestion
# SA1124: Do not use regions
dotnet_diagnostic.SA1124.severity = suggestion

# SA1127 Generic type constraints should be on their own line
dotnet_diagnostic.SA1127.severity = none

# SA1200: Using directives should be placed correctly
dotnet_diagnostic.SA1200.severity = none

# SA1201: Elements must appear in the correct order
dotnet_diagnostic.SA1201.severity = none

# SA1309: Field names must not begin with underscore
dotnet_diagnostic.SA1309.severity = none

# SA1413: Use trailing comma in multi-line initializers
dotnet_diagnostic.SA1413.severity = none

# SA1516: Elements should be separated by blank line
dotnet_diagnostic.SA1516.severity = none

# SA1600: Elements should be documented
dotnet_diagnostic.SA1600.severity = suggestion
dotnet_diagnostic.SA1600.severity = none

# SA1601: Partial elements should be documented
dotnet_diagnostic.SA1601.severity = none

# SA1602: Enumeration items should be documented
dotnet_diagnostic.SA1602.severity = none

# SA1633: File should have header
dotnet_diagnostic.SA1633.severity = none
Expand All @@ -40,12 +82,29 @@ dotnet_diagnostic.SX1309S.severity = warning
dotnet_naming_symbols.private_field.applicable_kinds = field
dotnet_naming_symbols.private_field.applicable_accessibilities = private

# .NET naming style: underscore_camel_case
# private_constant
dotnet_naming_symbols.private_constant.applicable_kinds = field
dotnet_naming_symbols.private_constant.applicable_accessibilities = private
dotnet_naming_symbols.private_constant.required_modifiers = const


# .NET naming style:
#underscore_camel_case
dotnet_naming_style.underscore_camel_case.required_prefix = _
dotnet_naming_style.underscore_camel_case.capitalization = camel_case

#.NET naming rule: private_fields_underscore_camelcase
#upper_case
dotnet_naming_style.upper_case.capitalization = all_upper
dotnet_naming_style.upper_case.word_separator = _

#.NET naming rule:
#private_fields_underscore_camelcase
dotnet_naming_rule.private_fields_underscore_camelcase.symbols = private_field
dotnet_naming_rule.private_fields_underscore_camelcase.style = underscore_camel_case
dotnet_naming_rule.private_fields_underscore_camelcase.severity = suggestion

#private_constants_upper_case
dotnet_naming_rule.private_constants_upper_case.symbols = private_field
dotnet_naming_rule.private_constants_upper_case.style = underscore_camel_case
dotnet_naming_rule.private_constants_upper_case.severity = suggestion

8 changes: 8 additions & 0 deletions src/CodeButler/CodeButler.Console/Mode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CodeButler
{
internal enum Mode
{
Console,
File,
}
}
18 changes: 18 additions & 0 deletions src/CodeButler/CodeButler.Console/Padding/PaddingCleaner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Linq;
using System.Text.RegularExpressions;

namespace CodeButler.Padding
{
public class PaddingCleaner
{
private static readonly Regex _dirtyNewLine = new Regex(@"[\t ]+(\r\n|\n)", RegexOptions.Multiline);
private static readonly Regex _multiEmptyLine = new Regex(@"^(\r\n|\n)+", RegexOptions.Multiline);

public string Clean(string input)
{
string output = _dirtyNewLine.Replace(input, match => match.Groups.Values.Last().Value);
output = _multiEmptyLine.Replace(output, match => match.Groups.Values.Last().Value);
return output;
}
}
}
41 changes: 19 additions & 22 deletions src/CodeButler/CodeButler.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
using System;
using System.IO;
using System.Threading.Tasks;
using CodeButler.Padding;
using CodeButler.Syntax;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Formatting;

namespace CodeButler
{
internal enum Mode
{
Console,
File,
}

internal class Program
public class Program
{
public static async Task<int> Main(string[] args)
{
Expand All @@ -35,16 +29,27 @@ public static async Task<int> Main(string[] args)
}

string input = await GetInput(mode, args).ConfigureAwait(false);

SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(input);
CompilationUnitSyntax root = syntaxTree.GetCompilationUnitRoot();

CompilationUnitSyntax root = Parse(input);
var organizedRoot = Reorganize(root);

await SetOuput(organizedRoot, mode, args).ConfigureAwait(false);
await SetOutput(organizedRoot, mode, args).ConfigureAwait(false);
return 0;
}

public static CompilationUnitSyntax Parse(string input)
{
var paddingCleaner = new PaddingCleaner();
string cleanInput = paddingCleaner.Clean(input);
SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(cleanInput);
CompilationUnitSyntax root = syntaxTree.GetCompilationUnitRoot();
return root;
}

public static CompilationUnitSyntax Reorganize(CompilationUnitSyntax compilationUnit)
{
return compilationUnit.Reorganize();
}

private static async Task<string> GetInput(Mode mode, string[] args)
{
switch (mode)
Expand All @@ -63,15 +68,7 @@ private static async Task<string> GetInput(Mode mode, string[] args)
}
}

private static CompilationUnitSyntax Reorganize(CompilationUnitSyntax compilationUnit)
{
return compilationUnit
.WithReorganizedUsings()
.WithReorganizeMembers()
.WithCorrectPadding();
}

private static async Task SetOuput(CompilationUnitSyntax compilationUnit, Mode mode, string[] args)
private static async Task SetOutput(CompilationUnitSyntax compilationUnit, Mode mode, string[] args)
{
switch (mode)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace CodeButler.Reorganizing
{
Expand Down Expand Up @@ -82,21 +81,18 @@ public int CompareByMemberType(MemberOrderInfo? other)
return other is null ? -1 : MemberType - other.MemberType;
}

/// <inheritdoc/>
public int CompareTo(MemberOrderInfo? other)
{
return _compareMethods
.Select(compareMethod => compareMethod(other))
.FirstOrDefault(result => result != 0);
}

/// <inheritdoc/>
public override bool Equals(object? obj)
{
return Equals(obj as MemberOrderInfo);
}

/// <inheritdoc/>
public bool Equals(MemberOrderInfo? other)
{
return other != null &&
Expand All @@ -106,7 +102,6 @@ public bool Equals(MemberOrderInfo? other)
AdditionalModifier == other.AdditionalModifier;
}

/// <inheritdoc/>
public override int GetHashCode()
{
return HashCode.Combine(MemberType, Identifier, AccessModifier, AdditionalModifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,28 +108,24 @@ public int CompareByName(UsingOrderInfo? other)
}
}

/// <inheritdoc/>
public int CompareTo(UsingOrderInfo? other)
{
return _compareMethods
.Select(compareMethod => compareMethod(other))
.FirstOrDefault(result => result != 0);
}

/// <inheritdoc/>
public override bool Equals(object? obj)
{
return Equals(obj as UsingOrderInfo);
}

/// <inheritdoc/>
public bool Equals(UsingOrderInfo? other)
{
return other != null &&
Name == other.Name;
}

/// <inheritdoc/>
public override int GetHashCode()
{
return HashCode.Combine(Name);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using CodeButler.Reorganizing;
using Microsoft.CodeAnalysis.CSharp;
Expand Down

This file was deleted.

Loading

0 comments on commit e6ab31d

Please sign in to comment.