Skip to content

Commit

Permalink
Merge branch 'main' into editor-config-overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
belav authored Sep 15, 2024
2 parents 4892ead + e90022e commit 3a62777
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,53 @@ public class ClassName
var unformatted = true;
var unformatted = true;
}

void ObjectInitialize()
{
return new SomeClass
{
// csharpier-ignore-start
SomeProperty = someValue
// csharpier-ignore-end
};

return new SomeClass
{
SomeProperty1 = 1,

// csharpier-ignore-start
SomeProperty2 = 2,
// csharpier-ignore-end
SomeProperty3 = 3,
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,
SomeProperty2 = someValue
// csharpier-ignore-end
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,

SomeProperty2 = someValue
// csharpier-ignore-end
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,


SomeProperty2 = someValue
// csharpier-ignore-end
};
}
}

public class ClassName2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,52 @@ public class ClassName
var unformatted = true;
var unformatted = true;
}

void ObjectInitialize()
{
return new SomeClass
{
// csharpier-ignore-start
SomeProperty = someValue
// csharpier-ignore-end
};

return new SomeClass
{
SomeProperty1 = 1,
// csharpier-ignore-start
SomeProperty2 = 2,
// csharpier-ignore-end
SomeProperty3 = 3,
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,
SomeProperty2 = someValue
// csharpier-ignore-end
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,

SomeProperty2 = someValue
// csharpier-ignore-end
};

return new Lines
{
// csharpier-ignore-start
SomeProperty = someValue,


SomeProperty2 = someValue
// csharpier-ignore-end
};
}
}

public class ClassName2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
public class Foo
{
/**
* comment
*/
public class Bar
{
/**
* comment
*/
var x = 0;
}

public void SomeFunction()
{
/*
The following line is an example with an indent:
This line is indented by one tab.
*/
/*
The following line is an example with an indent:
This line is indented by 4 spaces but will be converted to 1 tab
*/
/*
The following line is an example with an indent:
This line is indented by 3 spaces but will be left as 3 spaces
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,20 @@ public class Foo
*/
var x = 0;
}

public void SomeFunction()
{
/*
The following line is an example with an indent:
This line is indented by one tab.
*/
/*
The following line is an example with an indent:
This line is indented by 4 spaces but will be converted to 1 tab
*/
/*
The following line is an example with an indent:
This line is indented by 3 spaces but will be left as 3 spaces
*/
}
}
6 changes: 6 additions & 0 deletions Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ int CalculateIndentLength(string line) =>
this.Output.Append(indent.Value);
spacesToAppend -= indentLength;
}

while (spacesToAppend > 0 && spacesToAppend >= this.PrinterOptions.IndentSize)
{
this.Output.Append('\t');
spacesToAppend -= this.PrinterOptions.IndentSize;
}
}
if (spacesToAppend > 0)
{
Expand Down
2 changes: 1 addition & 1 deletion Src/CSharpier/SyntaxPrinter/MembersWithForcedLines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static List<Doc> Print<T>(
{
result.Add(Doc.HardLine);
}
;

var unFormattedCode = new StringBuilder();
var printUnformatted = false;
var lastMemberForcedBlankLine = false;
Expand Down
37 changes: 37 additions & 0 deletions Src/CSharpier/SyntaxPrinter/SeparatedSyntaxList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace CSharpier.SyntaxPrinter;

using System.Text;

internal static class SeparatedSyntaxList
{
public static Doc Print<T>(
Expand Down Expand Up @@ -40,8 +42,38 @@ private static Doc Print<T>(
where T : SyntaxNode
{
var docs = new List<Doc>();
var unFormattedCode = new StringBuilder();
var printUnformatted = false;
for (var x = startingIndex; x < list.Count; x++)
{
var member = list[x];

if (Token.HasLeadingCommentMatching(member, CSharpierIgnore.IgnoreEndRegex))
{
docs.Add(unFormattedCode.ToString().Trim());
unFormattedCode.Clear();
printUnformatted = false;
}
else if (Token.HasLeadingCommentMatching(member, CSharpierIgnore.IgnoreStartRegex))
{
if (!printUnformatted && x > 0)
{
docs.Add(Doc.HardLine);
}
printUnformatted = true;
}

if (printUnformatted)
{
unFormattedCode.Append(CSharpierIgnore.PrintWithoutFormatting(member, context));
if (x < list.SeparatorCount)
{
unFormattedCode.AppendLine(list.GetSeparator(x).Text);
}

continue;
}

docs.Add(printFunc(list[x], context));

// if the syntax tree doesn't have a trailing comma but we want want, then add it
Expand Down Expand Up @@ -86,6 +118,11 @@ private static Doc Print<T>(
}
}

if (unFormattedCode.Length > 0)
{
docs.Add(unFormattedCode.ToString().Trim());
}

return docs.Count == 0 ? Doc.Null : Doc.Concat(docs);
}
}
8 changes: 7 additions & 1 deletion docs/Ignore.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class ClassName

```

Use a ranged ignore to exclude multiple lines from formatting. A range is valid around statements and members.
Use a ranged ignore to exclude multiple lines from formatting. A range is not valid in all contexts. Currently it works with statements, members and object initializer expressions.
```csharp
// csharpier-ignore-start
public class Unformatted1 { }
Expand Down Expand Up @@ -104,6 +104,12 @@ public class ClassName
var formatted = true;
}

return new SomeClass
{
// csharpier-ignore-start
SomeProperty = true
// csharpier-ignore-end
}
}
```
Expand Down

0 comments on commit 3a62777

Please sign in to comment.