Repository navigation
Expand file tree
/
Copy pathCSharpierIgnore.cs
More file actions
157 lines (140 loc) · 5.25 KB
/
Copy pathCSharpierIgnore.cs
File metadata and controls
157 lines (140 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using CSharpier.Core.DocTypes;
using CSharpier.Core.Utilities;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace CSharpier.Core.CSharp.SyntaxPrinter;
internal static partial class CSharpierIgnore
{
#if NET8_0_OR_GREATER
[GeneratedRegex("^// csharpier-ignore($| -)")]
private static partial Regex IgnoreRegexGenerator();
[GeneratedRegex("^// csharpier-ignore-start($| -)")]
private static partial Regex IgnoreStartRegexGenerator();
[GeneratedRegex("^// csharpier-ignore-end($| -)")]
private static partial Regex IgnoreEndRegexGenerator();
[GeneratedRegex(@"[\t\v\f ]*(\r\n?|\n)")]
private static partial Regex WhiteSpaceLineEndingsGenerator();
private static readonly Regex IgnoreRegex = IgnoreRegexGenerator();
public static readonly Regex IgnoreStartRegex = IgnoreStartRegexGenerator();
public static readonly Regex IgnoreEndRegex = IgnoreEndRegexGenerator();
public static readonly Regex WhiteSpaceLineEndingsRegex = WhiteSpaceLineEndingsGenerator();
#else
private static readonly Regex IgnoreRegex = new(
"^// csharpier-ignore($| -)",
RegexOptions.Compiled
);
public static readonly Regex IgnoreStartRegex = new(
"^// csharpier-ignore-start($| -)",
RegexOptions.Compiled
);
public static readonly Regex IgnoreEndRegex = new(
"^// csharpier-ignore-end($| -)",
RegexOptions.Compiled
);
public static readonly Regex WhiteSpaceLineEndingsRegex = new(
@"[\t\v\f ]*(\r\n?|\n)",
RegexOptions.Compiled
);
#endif
public static bool HasIgnoreComment(SyntaxNode syntaxNode)
{
return Token.HasLeadingCommentMatching(syntaxNode, IgnoreRegex);
}
public static bool HasIgnoreComment(SyntaxToken syntaxToken)
{
return Token.HasLeadingCommentMatching(syntaxToken, IgnoreRegex);
}
public static bool IsNodeIgnored(SyntaxNode syntaxNode)
{
// this get handled in BaseMethodDeclaration and/or AttributeList
if (
syntaxNode is BaseMethodDeclarationSyntax baseMethodDeclarationSyntax
&& baseMethodDeclarationSyntax.AttributeLists.Any()
)
{
return false;
}
return syntaxNode.Parent?.Kind()
is SyntaxKind.ClassDeclaration
or SyntaxKind.StructDeclaration
or SyntaxKind.InterfaceDeclaration
or SyntaxKind.RecordDeclaration
or SyntaxKind.RecordStructDeclaration
or SyntaxKind.EnumDeclaration
or SyntaxKind.Block
or SyntaxKind.CompilationUnit
or SyntaxKind.NamespaceDeclaration
or SyntaxKind.SwitchSection
&& HasIgnoreComment(syntaxNode);
}
[SkipLocalsInit]
public static List<Doc> PrintNodesRespectingRangeIgnore<T>(
SyntaxList<T> list,
CSharpPrintingContext context
)
where T : SyntaxNode
{
var statements = new List<Doc>();
var unFormattedCode = new StringBuilder();
var printUnformatted = false;
SyntaxNode? firstUnformattedNode = null;
foreach (var node in list)
{
if (Token.HasLeadingCommentMatching(node, IgnoreEndRegex))
{
statements.Add(
firstUnformattedNode is StatementSyntax statementSyntax
? Doc.Concat(
ExtraNewLines.Print(statementSyntax),
unFormattedCode.ToString().Trim()
)
: unFormattedCode.ToString().Trim()
);
unFormattedCode.Clear();
firstUnformattedNode = null;
printUnformatted = false;
}
else if (Token.HasLeadingCommentMatching(node, IgnoreStartRegex))
{
firstUnformattedNode ??= node;
printUnformatted = true;
}
if (printUnformatted)
{
unFormattedCode.Append(PrintWithoutFormatting(node, context));
}
else
{
statements.Add(Node.Print(node, context));
}
}
if (unFormattedCode.Length > 0)
{
statements.Add(
firstUnformattedNode is StatementSyntax statementSyntax
? Doc.Concat(
ExtraNewLines.Print(statementSyntax),
unFormattedCode.ToString().Trim()
)
: unFormattedCode.ToString().Trim()
);
}
return statements;
}
public static string PrintWithoutFormatting(
SyntaxNode syntaxNode,
CSharpPrintingContext context
)
{
return PrintWithoutFormatting(syntaxNode.GetText().ToString(), context);
}
public static string PrintWithoutFormatting(string code, CSharpPrintingContext context)
{
// trim trailing whitespace + replace only existing line endings
return WhiteSpaceLineEndingsRegex.Replace(code, context.LineEnding);
}
}