Skip to content

Commit

Permalink
Merge branch 'main' into extensions-for-formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
belav authored May 31, 2024
2 parents 1c6a2c0 + a4dd17d commit de6d436
Show file tree
Hide file tree
Showing 16 changed files with 126 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class ClassName
// comment stays on this line
$"two";

public string TrailingComment = someCondition
? $"empty" // trailing comment
: someString;

public string TwoValues = $"{SomeValue}{SomeOtherValue}";

public string ForceFlatInString =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,19 @@ class ClassName
)
=> originalField.Type.Equals(updatedField.Type)
};

return someValue switch
{
// comment
Some.One => 1,
Some.Two => 2,
};

return someValue switch
{
Some.One => 1,
// comment
Some.Two => 2,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version
var customPath = this.customPathInstaller.GetPathForVersion(version);

this.logger.Debug("Adding new version " + version + " process for " + directory);

var installedVersion = new Version(version);
var installedVersion = GetInstalledVersion(version);
var pipeFilesVersion = new Version("0.12.0");
var serverVersion = new Version("0.28.0");
ICSharpierProcess cSharpierProcess;
Expand Down Expand Up @@ -278,6 +277,27 @@ private ICSharpierProcess SetupCSharpierProcess(string directory, string version
return NullCSharpierProcess.Instance;
}

private Version GetInstalledVersion(string version)
{
if (Version.TryParse(version, out var installedVersion) && installedVersion != null)
{
return installedVersion;
}

// handle semver versions
// regex from https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
var regex = Regex.Match(
version,
@"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"
);
if (regex.Success && regex.Groups.Count > 3)
{
return new Version($"{regex.Groups[1]}.{regex.Groups[2]}.{regex.Groups[3]}");
}

throw new ArgumentException($"Unable to parse version '{version}'", nameof(version));
}

private void DisplayFailureMessage()
{
var actionButton = new InfoBarActionButton
Expand Down
8 changes: 7 additions & 1 deletion Src/CSharpier/SyntaxPrinter/FormattingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ internal class FormattingContext
// context.State.PrintingDepth
public int PrintingDepth { get; set; }
public bool NextTriviaNeedsLine { get; set; }
public bool ShouldSkipNextLeadingTrivia { get; set; }
public bool SkipNextLeadingTrivia { get; set; }

// we need to keep track if we reordered modifiers because when modifiers are moved inside
// of an #if, then we can't compare the before and after disabled text in the source file
public bool ReorderedModifiers { get; set; }

// we also need to keep track if we move around usings with disabledText
public bool ReorderedUsingsWithDisabledText { get; set; }

public FormattingContext WithSkipNextLeadingTrivia()
{
this.SkipNextLeadingTrivia = true;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
if (modifiers is not { Count: > 0 })
{
docs.Add(Token.PrintLeadingTrivia(returnType.GetLeadingTrivia(), context));
context.ShouldSkipNextLeadingTrivia = true;
context.SkipNextLeadingTrivia = true;
}

declarationGroup.Add(Node.Print(returnType, context), " ");
context.ShouldSkipNextLeadingTrivia = false;
context.SkipNextLeadingTrivia = false;
}

if (explicitInterfaceSpecifier != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public static Doc Print(InterpolatedStringExpressionSyntax node, FormattingConte
};

docs.AddRange(node.Contents.Select(o => Node.Print(o, context)));
docs.Add(Token.Print(node.StringEndToken, context));
docs.Add(Token.PrintWithoutTrailingTrivia(node.StringEndToken, context));

return Doc.Concat(
// pull out the leading trivia so it doesn't get forced flat
// pull out the trivia so it doesn't get forced flat
Token.PrintLeadingTrivia(node.StringStartToken, context),
Doc.ForceFlat(docs)
Doc.ForceFlat(docs),
Token.PrintTrailingTrivia(node.StringEndToken)
);
}

Expand Down
12 changes: 2 additions & 10 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/RecursivePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ FormattingContext context
node.Parent is SwitchExpressionArmSyntax or CasePatternSwitchLabelSyntax
? Doc.Null
: Doc.SoftLine,
Token.PrintLeadingTrivia(node.PositionalPatternClause.OpenParenToken, context),
Doc.Group(
Token.PrintWithoutLeadingTrivia(
node.PositionalPatternClause.OpenParenToken,
context
),
Token.Print(node.PositionalPatternClause.OpenParenToken, context),
Doc.Indent(
Doc.SoftLine,
SeparatedSyntaxList.Print(
Expand Down Expand Up @@ -73,18 +69,14 @@ node.Parent is SwitchExpressionArmSyntax or CasePatternSwitchLabelSyntax
else
{
result.Add(
Token.PrintLeadingTrivia(node.PropertyPatternClause.OpenBraceToken, context),
Doc.Group(
node.Type != null
&& !node.PropertyPatternClause.OpenBraceToken.LeadingTrivia.Any(o =>
o.IsDirective || o.IsComment()
)
? Doc.Line
: Doc.Null,
Token.PrintWithoutLeadingTrivia(
node.PropertyPatternClause.OpenBraceToken,
context
),
Token.Print(node.PropertyPatternClause.OpenBraceToken, context),
Doc.Indent(
node.PropertyPatternClause.Subpatterns.Any() ? Doc.Line : Doc.Null,
SeparatedSyntaxList.Print(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public static Doc Print(SwitchExpressionSyntax node, FormattingContext context)
(o, _) =>
Doc.Concat(
ExtraNewLines.Print(o),
Token.PrintLeadingTrivia(
o.Pattern.GetLeadingTrivia(),
context.WithSkipNextLeadingTrivia()
),
Doc.Group(
Node.Print(o.Pattern, context),
o.WhenClause != null ? Node.Print(o.WhenClause, context) : Doc.Null,
Expand Down
24 changes: 17 additions & 7 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public static Doc PrintWithoutLeadingTrivia(SyntaxToken syntaxToken, FormattingC
return PrintSyntaxToken(syntaxToken, context, skipLeadingTrivia: true);
}

public static Doc PrintWithoutTrailingTrivia(SyntaxToken syntaxToken, FormattingContext context)
{
return PrintSyntaxToken(syntaxToken, context, skipTrailingTrivia: true);
}

public static Doc Print(SyntaxToken syntaxToken, FormattingContext context)
{
return PrintSyntaxToken(syntaxToken, context);
Expand All @@ -30,7 +35,8 @@ private static Doc PrintSyntaxToken(
SyntaxToken syntaxToken,
FormattingContext context,
Doc? suffixDoc = null,
bool skipLeadingTrivia = false
bool skipLeadingTrivia = false,
bool skipTrailingTrivia = false
)
{
if (syntaxToken.RawSyntaxKind() == SyntaxKind.None)
Expand All @@ -39,7 +45,7 @@ private static Doc PrintSyntaxToken(
}

var docs = new List<Doc>();
if (!skipLeadingTrivia && !context.ShouldSkipNextLeadingTrivia)
if (!skipLeadingTrivia && !context.SkipNextLeadingTrivia)
{
var leadingTrivia = PrintLeadingTrivia(syntaxToken, context);
if (leadingTrivia != Doc.Null)
Expand All @@ -48,7 +54,7 @@ private static Doc PrintSyntaxToken(
}
}

context.ShouldSkipNextLeadingTrivia = false;
context.SkipNextLeadingTrivia = false;

if (
(
Expand Down Expand Up @@ -113,10 +119,14 @@ or SyntaxKind.InterpolatedRawStringEndToken
{
docs.Add(syntaxToken.Text);
}
var trailingTrivia = PrintTrailingTrivia(syntaxToken);
if (trailingTrivia != Doc.Null)

if (!skipTrailingTrivia)
{
docs.Add(trailingTrivia);
var trailingTrivia = PrintTrailingTrivia(syntaxToken);
if (trailingTrivia != Doc.Null)
{
docs.Add(trailingTrivia);
}
}

if (suffixDoc != null)
Expand Down Expand Up @@ -344,7 +354,7 @@ private static bool IsMultiLineComment(SyntaxKind kind) =>
private static bool IsRegion(SyntaxKind kind) =>
kind is SyntaxKind.RegionDirectiveTrivia or SyntaxKind.EndRegionDirectiveTrivia;

private static Doc PrintTrailingTrivia(SyntaxToken node)
public static Doc PrintTrailingTrivia(SyntaxToken node)
{
return PrintTrailingTrivia(node.TrailingTrivia);
}
Expand Down
5 changes: 5 additions & 0 deletions Src/SyntaxFinder/ResultWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public static class ResultWriter
{
public static void WriteMatching(int total, int matching)
{
if (total == 0)
{
return;
}

if (matching > total)
{
Console.WriteLine("Matching was > than Total, so you did something wrong.");
Expand Down
12 changes: 3 additions & 9 deletions Src/SyntaxFinder/SyntaxFinderWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ namespace SyntaxFinder;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;

public abstract class SyntaxFinderWalker : CSharpSyntaxWalker
public abstract class SyntaxFinderWalker(string file) : CSharpSyntaxWalker
{
private bool wroteFile;
private readonly int maxCodeWrites = 250;
private static int codeWrites = 0;
private readonly string file;

protected SyntaxFinderWalker(string file)
{
this.file = file;
}
private static int codeWrites;

protected void WriteCode(SyntaxNode syntaxNode)
{
Expand All @@ -34,7 +28,7 @@ protected void WriteFilePath(bool onlyIfWritingCode = false)

if (!this.wroteFile)
{
Console.WriteLine(this.file);
Console.WriteLine(file);
this.wroteFile = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
namespace SyntaxFinder;
namespace SyntaxFinder.Walkers;

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

public class ExampleWalker : CSharpSyntaxWalker
public class ExampleWalker(string file) : SyntaxFinderWalker(file)
{
public static readonly ConcurrentDictionary<string, List<string>> MembersInType = new();
public static int Total;
public static int Matching;
private readonly string file;
private bool wroteFile;
private readonly int maxCodeWrites = 250;
private int codeWrites = 0;

public ExampleWalker(string file)
{
this.file = file;
}

public override void VisitCompilationUnit(CompilationUnitSyntax node)
{
Expand Down Expand Up @@ -70,24 +61,6 @@ node.Parent is TypeDeclarationSyntax typeDeclarationSyntax
base.VisitMethodDeclaration(node);
}

private void WriteCode(SyntaxNode syntaxNode)
{
if (this.codeWrites < this.maxCodeWrites)
{
Interlocked.Increment(ref this.codeWrites);
Console.WriteLine(syntaxNode.SyntaxTree.GetText().ToString(syntaxNode.Span));
}
}

private void WriteFilePath()
{
if (!this.wroteFile)
{
Console.WriteLine(this.file);
this.wroteFile = true;
}
}

private static bool IsMultiline(SyntaxNode syntaxNode)
{
var lineSpan = syntaxNode.SyntaxTree.GetLineSpan(syntaxNode.Span);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace SyntaxFinder;
namespace SyntaxFinder.Walkers;

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis.CSharp.Syntax;

public class ModifiersWalker : SyntaxFinderWalker
Expand Down
37 changes: 37 additions & 0 deletions Src/SyntaxFinder/Walkers/NestedConditionalExpressionsWalker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace SyntaxFinder.Walkers;

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

public class NestedConditionalExpressionsWalker(string file) : SyntaxFinderWalker(file)
{
public static readonly ConcurrentDictionary<string, List<string>> MembersInType = new();
public static int Total;
public static int Matching;

public override void VisitConditionalExpression(ConditionalExpressionSyntax node)
{
if (node.WhenTrue is ConditionalExpressionSyntax)
{
this.WriteFilePath();
// this.WriteCode(node.Parent!);
Interlocked.Increment(ref Total);
}
}

public static void WriteResult()
{
foreach (var entry in MembersInType)
{
Console.WriteLine(entry.Key);
foreach (var member in entry.Value.OrderBy(o => o))
{
Console.WriteLine(" " + member);
}
}

ResultWriter.WriteMatching(Total, Matching);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace SyntaxFinder;
namespace SyntaxFinder.Walkers;

using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace SyntaxFinder;
namespace SyntaxFinder.Walkers;

using System.Collections.Concurrent;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

Expand Down

0 comments on commit de6d436

Please sign in to comment.