Skip to content

Commit ca0ef6d

Browse files
feat: added support for ignoring using directives
1 parent bc8987b commit ca0ef6d

15 files changed

Lines changed: 246 additions & 6 deletions

Src/CSharpier.Core/CSharp/SyntaxPrinter/UsingDirectives.cs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ or SyntaxKind.UndefDirectiveTrivia
6868
}
6969
}
7070

71+
if (Token.HasLeadingCommentMatching(usings[0], CSharpierIgnore.IgnoreStartRegex))
72+
{
73+
initialComments.Clear();
74+
}
75+
7176
docs.Add(Token.PrintLeadingTrivia([.. initialComments], context));
7277
if (keepUsingsUntilEndIf)
7378
{
@@ -94,6 +99,7 @@ or SyntaxKind.UndefDirectiveTrivia
9499
}
95100

96101
var isFirst = true;
102+
var prevShouldFormat = true;
97103
var index = 0;
98104
var reorderedDirectives = false;
99105
foreach (var groupOfUsingData in GroupUsings(usingList, [.. triviaWithinIf], context))
@@ -102,10 +108,15 @@ or SyntaxKind.UndefDirectiveTrivia
102108
{
103109
if (!isFirst)
104110
{
105-
docs.Add(Doc.HardLine);
111+
if (usingData.ShouldFormat || prevShouldFormat)
112+
{
113+
docs.Add(Doc.HardLine);
114+
}
106115
}
107116

108-
if (usingData.LeadingTrivia != Doc.Null)
117+
prevShouldFormat = usingData.ShouldFormat;
118+
119+
if (usingData.LeadingTrivia != Doc.Null && usingData.ShouldFormat)
109120
{
110121
docs.Add(usingData.LeadingTrivia);
111122
}
@@ -117,7 +128,9 @@ or SyntaxKind.UndefDirectiveTrivia
117128
}
118129

119130
index++;
120-
docs.Add(UsingDirective.Print(usingData.Using, context, printExtraLines));
131+
docs.Add(usingData.ShouldFormat
132+
? UsingDirective.Print(usingData.Using, context, printExtraLines)
133+
: CSharpierIgnore.PrintWithoutFormatting(usingData.Using, context));
121134
}
122135

123136
isFirst = false;
@@ -159,10 +172,21 @@ PrintingContext context
159172
var directiveGroup = new List<UsingData>();
160173
var ifCount = 0;
161174
var isFirst = true;
162-
175+
var shouldFormat = true;
176+
163177
foreach (var usingDirective in usings)
164178
{
165179
var openIf = ifCount > 0;
180+
if (Token.HasLeadingCommentMatching(usingDirective, CSharpierIgnore.IgnoreEndRegex))
181+
{
182+
openIf = true;
183+
shouldFormat = true;
184+
}
185+
else if (Token.HasLeadingCommentMatching(usingDirective, CSharpierIgnore.IgnoreStartRegex))
186+
{
187+
shouldFormat = false;
188+
}
189+
166190
foreach (var directive in usingDirective.GetLeadingTrivia().Where(o => o.IsDirective))
167191
{
168192
if (directive.RawSyntaxKind() is SyntaxKind.IfDirectiveTrivia)
@@ -182,6 +206,7 @@ PrintingContext context
182206
{
183207
Using = usingDirective,
184208
LeadingTrivia = PrintLeadingTrivia(usingDirective),
209+
ShouldFormat = shouldFormat,
185210
}
186211
);
187212
}
@@ -190,17 +215,22 @@ PrintingContext context
190215
if (openIf)
191216
{
192217
directiveGroup.Add(
193-
new UsingData { LeadingTrivia = PrintLeadingTrivia(usingDirective) }
218+
new UsingData { LeadingTrivia = PrintLeadingTrivia(usingDirective), ShouldFormat = shouldFormat}
194219
);
195220
}
196221

197222
var usingData = new UsingData
198223
{
199224
Using = usingDirective,
200225
LeadingTrivia = !openIf ? PrintLeadingTrivia(usingDirective) : Doc.Null,
226+
ShouldFormat = shouldFormat,
201227
};
202228

203-
if (usingDirective.GlobalKeyword.RawSyntaxKind() != SyntaxKind.None)
229+
if (!shouldFormat)
230+
{
231+
directiveGroup.Add(usingData);
232+
}
233+
else if (usingDirective.GlobalKeyword.RawSyntaxKind() != SyntaxKind.None)
204234
{
205235
if (usingDirective.Alias is not null)
206236
{
@@ -274,6 +304,7 @@ private class UsingData
274304
{
275305
public Doc LeadingTrivia { get; init; } = Doc.Null;
276306
public UsingDirectiveSyntax? Using { get; init; }
307+
public required bool ShouldFormat { get; init; } = true;
277308
}
278309

279310
private static bool IsSystemName(NameSyntax value)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// csharpier-ignore-start
2+
3+
using B;
4+
5+
using A;
6+
7+
// csharpier-ignore-end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// csharpier-ignore-start
2+
3+
using B;
4+
5+
using A;
6+
7+
// csharpier-ignore-end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using C;
2+
using D;
3+
4+
// csharpier-ignore-start
5+
6+
using B;
7+
8+
using A;
9+
10+
// csharpier-ignore-end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using D;
2+
using C;
3+
4+
// csharpier-ignore-start
5+
6+
using B;
7+
8+
using A;
9+
10+
// csharpier-ignore-end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using C;
2+
using D;
3+
// csharpier-ignore-start
4+
5+
using B;
6+
7+
using A;
8+
9+
// csharpier-ignore-end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// csharpier-ignore-start
2+
3+
using B;
4+
5+
using A;
6+
7+
// csharpier-ignore-end
8+
using D;
9+
using C;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using C;
2+
using D;
3+
// csharpier-ignore-start
4+
5+
using B;
6+
7+
using A;
8+
9+
// csharpier-ignore-end
10+
11+
12+
13+
14+
15+
// csharpier-ignore-start
16+
17+
using F;
18+
19+
using E;
20+
21+
// csharpier-ignore-end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// csharpier-ignore-start
2+
3+
using B;
4+
5+
using A;
6+
7+
// csharpier-ignore-end
8+
using D;
9+
using C;
10+
11+
12+
13+
14+
// csharpier-ignore-start
15+
16+
using F;
17+
18+
using E;
19+
20+
// csharpier-ignore-end
21+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using C;
2+
using D;
3+
// csharpier-ignore-start
4+
#if BUILD_MSI_TASKS
5+
6+
7+
using System;
8+
using Microsoft.
9+
Build.Framework;
10+
11+
12+
#endif
13+
14+
// csharpier-ignore-end
15+
16+
namespace RepoTasks;
17+
18+
class ClassName { }

0 commit comments

Comments
 (0)