Skip to content

Releases: belav/csharpier

0.24.1

26 Apr 16:23
6ab7e3e
Compare
Choose a tag to compare

What's Changed

0.24.0 Regression csharpier-ignore causes blank lines between statements to be removed. #879

// input & expected output

// csharpier-ignore
public string Example
{
  get
     {
       if (_example is not null)
         return _example;

       var number = Random.Shared.Next();

       return _example = number.ToString();
     }
}

// 0.24.0

// csharpier-ignore
public string Example
{
  get
     {
       if (_example is not null)
         return _example;
       var number = Random.Shared.Next();
       return _example = number.ToString();
     }
}

Thanks go to @Pentadome for reporting the regression bug.

0.24.0

26 Apr 03:34
a066013
Compare
Choose a tag to compare

What's Changed

Formatting named list patterns loses code and causes compilation error #876

// input & expected output
return list switch
{
    [var elem] => elem * elem,
    [] => 0,
    [..] elems => elems.Sum(e => e + e),
};

// 0.23.0
return list switch
{
    [var elem] => elem * elem,
    [] => 0,
    [..] => elems.Sum(e => e + e),
};

Thanks go to @Dragemil for reporting the bug

CSharpier.MSBuild does not support usernames or project paths with spaces #872

CSharpier.MSBuild would throw an exception when building a project if the username had a space, or if the project path had a space.

Thanks go to @ooo2003003v2 for reporting the bug.

#pragma with long line introduces extra line break #865

// input & expected output
if (
    e is
#pragma warning disable CS0618
    BadHttpRequestException
#pragma warning restore CS0618
    {
        Message: "______________________________________________________________________________________________________________"
    }
) { }

// 0.23.0
if (
    e is
#pragma warning disable CS0618
    BadHttpRequestException
#pragma warning restore CS0618

    {
        Message: "______________________________________________________________________________________________________________"
    }
) { }

Thanks go to @Denton-L for reporting the bug

Better support for ignore on method attributes #848

// input
public class AttributesAndMethods
{
    // csharpier-ignore - only the first attribute
    [Attribute          ]
    [Attribute          ]
    public void MethodThatShouldFormat()     { }

    [Attribute]
    // csharpier-ignore - only the second attribute
    [Attribute         ]
    public void MethodThatShouldFormat()     { }

    [Attribute  ]
    [Attribute  ]
    // csharpier-ignore - just the method
    public void MethodThatShouldNotFormat(           ) { }
}

// 0.23.0
public class AttributesAndMethods
{
    // csharpier-ignore - only the first attribute
    [Attribute          ]
    [Attribute          ]
    public void MethodThatShouldFormat()     { }

    [Attribute]
    // csharpier-ignore - only the second attribute
    [Attribute]
    public void MethodThatShouldFormat() { }

    [Attribute]
    [Attribute]
    // csharpier-ignore - just the method
    public void MethodThatShouldNotFormat() { }
}

// 0.24.0
public class AttributesAndMethods
{
    // csharpier-ignore - only the first attribute
    [Attribute          ]
    [Attribute]
    public void MethodThatShouldFormat() { }

    [Attribute]
    // csharpier-ignore - only the second attribute
    [Attribute]
    public void MethodThatShouldFormat() { }

    [Attribute]
    [Attribute]
    // csharpier-ignore - just the method
    public void MethodThatShouldNotFormat() { }
}

Thanks go to @Billuc for reporting the bug

Ranged ignore applies some formatting when multiple statements are on a line #846

// input & expected output
void MethodName()
{
    // csharpier-ignore-start
    var packet = new List<byte>();
    packet.Add(0x0f); packet.Add(0x00);
    packet.Add(0x00); packet.Add(0x00);
    // csharpier-ignore-end
}

// 0.23.0
void MethodName()
{
    // csharpier-ignore-start
    var packet = new List<byte>();
    packet.Add(0x0f);
packet.Add(0x00);
    packet.Add(0x00);
packet.Add(0x00);
    // csharpier-ignore-end
}

Thanks go to @Billuc for reporting the bug

Support scoped variables (better handling of unrecognized syntax nodes) #839

Scoped variables are a language proposal. CSharpier has some support for printing unrecognized syntax nodes but the validation logic didn't account for them and would throw an exception

scoped Span<byte> span;

Thanks go to @Dragemil for reporting the bug

Unrecognized syntax nodes lose comments #869

CSharpier now supports printing commends on unrecognized nodes.

// comment on unrecognized node
scoped Span<byte> span;

Full Changelog: 0.23.0...0.24.0

0.23.0

06 Mar 22:54
a8c1c6a
Compare
Choose a tag to compare

0.23.0

Breaking Changes

Make compile errors public when using CSharpier.Core #799

Previously CodeFormatter.Format(unformattedCode) and its overloads returned only the formatted code. It now returns a result object.

public class CodeFormatterResult
{
    public string Code { get; }
    public IEnumerable<Diagnostic> CompilationErrors { get; }
}

This is a breaking change. There were also a number of types that should not have been public that were made internal.

Thanks go to @verdverm for the suggestion

What's Changed

Allow comment-description suffix on csharpier-ignore comments #835

It is now possible to include a suffix on csharpier-ignore comments. The description must be seperated from the comment by at least one - character.

// csharpier-ignore - class copied as-is from another project
public class Unformatted     { 
        private string     unformatted;
}

// csharpier-ignore-start -- class copied as-is from another project
public class Unformatted1     { }
public class Unformatted2     { }
// csharpier-ignore-end

Thanks go to @Strepto for the suggestion

Fix formatting for open generics #832

// 0.22.1
typeof(AnExceptionallyLongAndElaborateClassNameToMakeAnExampleRegardingOpenGenerics<
    ,
>).MakeGenericType(typeof(string), typeof(int));

// 0.23.0
typeof(AnExceptionallyLongAndElaborateClassNameToMakeAnExampleRegardingOpenGenerics<,>).MakeGenericType(
    typeof(string),
    typeof(int)
);

Thanks go to @jonstodle for reporting the issue

#region should be indented based on context #812

Previously the preceding whitespace was left as is on #region and #endregion which resulted undesired formatting.

// 0.22.1
public class ClassName
{
            #region Ugly methods
    public int LongUglyMethod()
    {    
        return 42;
    }
            #endregion
}

// 0.23.0
public class ClassName
{
    #region Ugly methods
    public int LongUglyMethod()
    {    
        return 42;
    }
    #endregion
}

Thanks go to @jods4 for reporting the issue

Return statement followed by linq query syntax not indenting correctly #811

// 0.22.1
return from i in Enumerable.Range(0, 10)
let i2 = i * i
where i2 < 100
select new { Square = i2, Root = i };

// 0.23.0
return from i in Enumerable.Range(0, 10)
    let i2 = i * i
    where i2 < 100
    select new { Square = i2, Root = i };

Thanks go to @jods4 for reporting the issue

Array and dictionary initializers should break in some cases to improve readability #809

// 0.22.1
var dictionaryInitializer = new Dictionary<int, string> { { 1, "" }, { 2, "a" }, { 3, "b" } };
int[,,] cube = { { { 111, 112 }, { 121, 122 } }, { { 211, 212 }, { 221, 222 } } };
int[][] jagged = { { 111 }, { 121, 122 } };

// 0.23.0
var dictionaryInitializer = new Dictionary<int, string>
{
    { 1, "" },
    { 2, "a" },
    { 3, "b" }
};
int[,,] cube =
{
    {
        { 111, 112 },
        { 121, 122 }
    },
    {
        { 211, 212 },
        { 221, 222 }
    }
};
int[][] jagged =
{
    { 111 },
    { 121, 122 }
};

List initializer inside object initializer breaks poorly #802

// 0.22.1
var someObject = new SomeObject { SomeArray = new SomeOtherObject[]
    {
        new SomeOtherObject { SomeProperty = 1 },
        new SomeOtherObject()
    }.CallMethod().CallMethod() };

// 0.23.0
var someObject = new SomeObject
{
    SomeArray = new SomeOtherObject[]
    {
        new SomeOtherObject { SomeProperty = 1 },
        new SomeOtherObject()
    }
        .CallMethod()
        .CallMethod()
};

Thanks go to @shocklateboy92 for reporting the issue

Allow passing --config-path to cli #777

It is now possible to pass --config-path to the cli for cases where it is not in the root or you want to bypass the auto location and speed up formatting requests.

dotnet csharpier . --config-path "./config/.csharpierrc"

Thanks go to @bdovaz for the suggestion

Allow blank lines in query syntax #754

It is now possible to add blank lines in query syntax expressions which can aid in readability

var result = await (
    from post in dbContext.Posts
    join blog in dbContext.Blogs on post.BlogId equals blog.Id
    
    let count = dbContext.Posts.Count(p => p.Name == post.Name)
    
    where post.Id == 1
    select new 
    {
         Post = post,
         Blog = blog,
         SamePostNameCount = count
    }
)
    .AsNoTracking()
    .FirstAsync();

Thanks go to @TwentyFourMinutes for the suggestion

#if causes line after it to break when it contains an if #666

// 0.22.1
class ClassName
{
    public void MethodName()
    {
#if !IF_STATEMENT_HERE_SHOULD_NOT_BREAK_INVOCATION_AFTER_ENDIF
        if (true)
        {
            return;
        }
#endif
        SomeObject
            .CallMethod()
            .CallOtherMethod(shouldNotBreak);
    }
}

// 0.23.0
class ClassName
{
    public void MethodName()
    {
#if !IF_STATEMENT_HERE_SHOULD_NOT_BREAK_INVOCATION_AFTER_ENDIF
        if (true)
        {
            return;
        }
#endif
        SomeObject.CallMethod().CallOtherMethod(shouldNotBreak);
    }
}

Full Changelog: 0.22.0...0.23.0

VSCode 1.3.6

24 Jan 00:12
fed7936
Compare
Choose a tag to compare

1.3.6

What's Changed

Fix bug where 2nd instance of VSCode was not able to format code #784

Thanks to @BenasB for reporting the issue

0.22.1

18 Jan 00:12
Compare
Choose a tag to compare

0.22.1

What's Changed

Fix for CSharpier.MsBuild so it selects a compatible framework if the project does not target net6 or net7 #797

This fix auto selects net7.0 for projects that do not target net6.0 or net7.0. This means the CSharpier_FrameworkVersion property is only required if a project is targeting < net6.0 and net7.0 is not installed.

Thanks go to @samtrion for submitting the fix.

Full Changelog: 0.21.1...0.22.1

0.22.0

15 Jan 17:31
054652e
Compare
Choose a tag to compare

0.22.0

Breaking Changes

Support only UTF8 and UTF8-BOM files #787

Previously UTF.Unknown was used to try to determine file encodings.
This was problematic because if a file was too small it would not properly detect the encoding.

public enum MeetingLocation
{
  Café,
  Restaurant
}

This file saved as UTF8 would be detected as SBCSCodePageEncoding and result in CSharpier trying to parse the following file

public enum MeetingLocation
{
  Café,
  Restaurant
}

CSharpier now only supports UTF8 & UTF8-BOM files. This is consistent with the IDE plugins, which stream files to CSharpier as UTF8.

Thanks go to @Meligy for reporting the problem.

CSharpier.MSBuild support for .NET 7 #773

CSharpier.MSBuild now multi-targets net6.0 and net7.0. As a side effect of multi-targeting, the CSharpier_FrameworkVersion property is now required for projects that do not target net6.0 or net7.0. See https://csharpier.com/docs/MsBuild#target-frameworks

Thanks go to @OneCyrus for reporting it

What's Changed

Fix for CSharpier.MsBuild "Specified condition "$(CSharpier_Check)" evaluates to "" instead of a boolean" #788

When projects referencing CSharpier.MsBuild were reloaded, they would get the error "Specified condition "$(CSharpier_Check)" evaluates to "" instead of a boolean" and fail to load.

Thanks go to @samtrion for submitting the fix.

List Pattern support for subpattern within a slice #779

CSharpier did not have proper support for the new c# 11 slice pattern. When a slice contained a pattern, that pattern would be lost.

// input
var someValue = someString is [var firstCharacter, .. var rest];

// 0.21.0
var someValue = someString is [var firstCharacter, ..];

// 0.22.0
var someValue = someString is [var firstCharacter, .. var rest];

Thanks go to @domn1995 for reporting it

Fix for comments within expressions in interpolated strings #774

When an interpolated string contained a comment within an expression, CSharpier was inserting a line break that resulted in invalid code.

// input
var trailingComment = $"{someValue /* Comment shouldn't cause new line */}";

// 0.21.0
var trailingComment = $"{someValue /* Comment shouldn't cause new line */
    }";

// 0.22.0
var trailingComment = $"{someValue /* Comment shouldn't cause new line */}";

Thanks go to @IT-CASADO for reporting it

Always put generic type constraints onto a new line #527

// 0.21.0
public class SimpleGeneric<T> where T : new() { }

// 0.22.0 
public class SimpleGeneric<T>
    where T : new() { }

Always put constructor initializers on their own line #526

// 0.21.0
public Initializers() : this(true) { }

public Initializers(string value) : base(value) { }

// 0.22.0
public Initializers()
    : this(true) { }

public Initializers(string value)
    : base(value) { }

Full Changelog: 0.21.0...0.22.0

0.21.0

29 Nov 00:47
Compare
Choose a tag to compare

0.21.0

What's Changed

Support file scoped types #748

CSharpier now supports a file scoped type

file class FileScopedClass
{
    // implementation
}

Csharpier removes empty lines in ignored blocks of code #742

In some instances csharpier was removing empty lines in csharpier-ignore blocks of code

// input
public class KeepLines1
{
    // csharpier-ignore-start
    private string    first;

    private string    second;
    // csharpier-ignore-end
}

// 0.20.0
public class KeepLines1
{
    // csharpier-ignore-start
    private string    first;private string    second;
    // csharpier-ignore-end
}

Thanks go to @MonstraG for reporting it

Await + LINQ query syntax indents incorrectly #740

// 0.20.0
var result = await from thing in Things
from otherThing in OtherThings
from finalThing in SomethingAsync(thing, otherThing)
select finalThing;

// 0.21.0
var result = await
    from thing in Things
    from otherThing in OtherThings
    from finalThing in SomethingAsync(thing, otherThing)
    select finalThing;

Thanks go to @domn1995 for reporting it.

Break anonymous object creation when there are more than two properties #753

Object initializers break when they have more than two properties. For example

var x = new Thing
{
    Post = post,
    Blog = blog,
    SamePostNameCount = count
};

Anonymous object initializers were not included in this logic prior to 0.21.0

// 0.20.0
var result =
    from post in Posts
    select new { Post = post, Blog = blog, SamePostNameCount = count };

// 0.21.0
var result =
    from post in Posts
    select new
    {
        Post = post,
        Blog = blog,
        SamePostNameCount = count
    };

Thanks go to @TwentyFourMinutes for reporting it.

Support net7 #756

The CSharpier dotnet tool now works with net6 or net7.

Fix for ignoring subfolders in node_modules #762

CSharpier was not properly ignoring .cs files when they were in a subfolder of node_modules

Thanks go to @snebjorn for reporting the bug.

Full Changelog: 0.20.0...0.21.0

0.20.0

03 Oct 16:50
4aabbf0
Compare
Choose a tag to compare

What's Changed

Improve Tuple formatting #735

Tuples would break poorly in some cases

// 0.19.2

public async Task<(ILookup<string, int> someLookup, ILookup<int, string> reverseLookup, ILookup<
        string,
        ClassName
    > thirdLookup)> CreateLookups()
{
    return (null, null);
}

public void TuplesAsInput(
    (int myInt, string myString, ClassName myClassNameInstance, Dictionary<
        int,
        string
    > wordList) inputArgs
)
{
    // do something
}

// 0.20.0
public async Task<(
    ILookup<string, int> someLookup,
    ILookup<int, string> reverseLookup,
    ILookup<string, ClassName> thirdLookup
)> CreateLookups()
{
  return (null, null);
}

public void TuplesAsInput(
    (
        int myInt,
        string myString,
        ClassName myClassNameInstance,
        Dictionary<int, string> wordList
    ) inputArgs
 )
 {
   // do something
 }

Thanks go to @BenjaBobs for reporting the bug.

Full Changelog: 0.19.2...0.20.0

0.19.0

15 Aug 15:42
56a7ff7
Compare
Choose a tag to compare

What's Changed

Adding a cache to speed up formatting. #692

CSharpier now caches information about files that it has formatted to speed up subsequent runs.
By default the following are used as cache keys and a file is only formatted if one of them has changed.

  • CSharpier Version
  • CSharpier Options
  • Content of the file

The cache is stored at [LocalApplicationData]/CSharpier/.formattingCache.

Ignore node_modules #699

CSharpier now ignores any files within a node_modules folder.

Thanks go to @RichiCoder1 for the suggestion and @SubjectAlpha for the implementation.

Extra space before curly brace in array initializer #693

// 0.18.0
public class ClassName
{
    public int[] SomeArray { get; set; } =  { 1, 2, 3 };
}
// 0.19.0
public class MyClass
{
    public int[] SomeArray { get; set; } = { 1, 2, 3 };
}

Thanks go to @TiraelSedai for reporting the bug.

New Contributors

Full Changelog: 0.18.0...0.19.0

0.18.0

13 Jun 18:42
Compare
Choose a tag to compare

What's Changed

Initial C# 11 support #686

CSharpier can format the following c# 11 features

  • Raw string literals
  • Generic attributes
  • Static abstract members in interfaces
  • Newlines in string interpolation expressions CSharpier will leave existing new lines within expressions and not add new ones
  • List Patterns
  • UTF8 string literals
  • Unsigned right shift operator
  • Checked operator
  • Generic math

Use relative file path in CommandLineFormatter #680

CSharpier now outputs relative or absolute file paths so that they are clickable in terminals.

dotnet csharpier .

# csharpier 0.17.0
Error Invalid.cs - Failed to compile so was not formatted.

# csharpier 0.18.0
Error ./Invalid.cs - Failed to compile so was not formatted.

dotnet csharpier c:/src

# csharpier 0.17.0
Error Invalid.cs - Failed to compile so was not formatted.

# csharpier 0.18.0
Error c:/src/Invalid.cs - Failed to compile so was not formatted.

Thanks go to @dlech

Invalid code for comments inside expressions in verbatim interpolated strings #679

// input
var someValue =
    $@"
    {
        // comment
        "hi"
    }
    ";
// 0.17.0
var someValue =
    $@"
    {
        // comment "hi"}
    ";
// 0.18.0
var someValue =
    $@"
    {
        // comment
        "hi"
    }
    ";

Thanks go to @ivan-razorenov

CSharpier ranged ignore #678

CSharpier now has the ability to ignore a range of statements or members. See Ignore for more details

// csharpier-ignore-start
var unformatted =        true;
var unformatted =        true;
// csharpier-ignore-end

Thanks go to @pingzing

New Contributors

Full Changelog: 0.17.0...0.18.0