Skip to content

Commit a1af199

Browse files
perf: only run PreprocessorSymbols when #if found
1 parent dd366b4 commit a1af199

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

Src/CSharpier.Core/CSharp/CSharpFormatter.cs

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static Task<CodeFormatterResult> FormatAsync(
4444
syntaxTree,
4545
(options ?? new()).ToPrinterOptions(),
4646
SourceCodeKind.Regular,
47+
true,
4748
cancellationToken
4849
);
4950
}
@@ -67,11 +68,13 @@ CancellationToken cancellationToken
6768
)
6869
{
6970
var initialSymbolSet = Array.Empty<string>();
71+
var hasPreprocessorSymbols = code.Contains("#if");
7072

7173
return FormatAsync(
7274
ParseText(code, initialSymbolSet, sourceCodeKind, cancellationToken),
7375
printerOptions,
7476
sourceCodeKind,
77+
hasPreprocessorSymbols,
7578
cancellationToken
7679
);
7780
}
@@ -99,6 +102,7 @@ internal static async Task<CodeFormatterResult> FormatAsync(
99102
SyntaxTree syntaxTree,
100103
PrinterOptions printerOptions,
101104
SourceCodeKind sourceCodeKind,
105+
bool hasPreprocessorSymbols,
102106
CancellationToken cancellationToken
103107
)
104108
{
@@ -165,36 +169,48 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult)
165169
.ReorderedUsingsWithDisabledText;
166170
var movedTrailingTrivia = printingContext.State.MovedTrailingTrivia;
167171

168-
foreach (var symbolSet in PreprocessorSymbols.GetSets(syntaxTree))
172+
if (hasPreprocessorSymbols)
169173
{
170-
syntaxTree = ParseText(formattedCode, symbolSet, sourceCodeKind, cancellationToken);
171-
172-
if (TryGetCompilationFailure(out result))
174+
foreach (var symbolSet in PreprocessorSymbols.GetSets(syntaxTree))
173175
{
174-
return result;
175-
}
176+
syntaxTree = ParseText(
177+
formattedCode,
178+
symbolSet,
179+
sourceCodeKind,
180+
cancellationToken
181+
);
176182

177-
var formattingContext2 = new PrintingContext
178-
{
179-
Options = new PrintingContext.PrintingContextOptions
183+
if (TryGetCompilationFailure(out result))
180184
{
181-
LineEnding = lineEnding,
182-
IndentSize = printerOptions.IndentSize,
183-
UseTabs = printerOptions.UseTabs,
184-
},
185-
};
186-
document = Node.Print(
187-
await syntaxTree.GetRootAsync(cancellationToken),
188-
formattingContext2
189-
);
190-
formattedCode = DocPrinter.DocPrinter.Print(document, printerOptions, lineEnding);
191-
reorderedModifiers =
192-
reorderedModifiers || formattingContext2.State.ReorderedModifiers;
193-
reorderedUsingsWithDisabledText =
194-
reorderedUsingsWithDisabledText
195-
|| formattingContext2.State.ReorderedUsingsWithDisabledText;
196-
movedTrailingTrivia =
197-
movedTrailingTrivia || formattingContext2.State.MovedTrailingTrivia;
185+
return result;
186+
}
187+
188+
var formattingContext2 = new PrintingContext
189+
{
190+
Options = new PrintingContext.PrintingContextOptions
191+
{
192+
LineEnding = lineEnding,
193+
IndentSize = printerOptions.IndentSize,
194+
UseTabs = printerOptions.UseTabs,
195+
},
196+
};
197+
document = Node.Print(
198+
await syntaxTree.GetRootAsync(cancellationToken),
199+
formattingContext2
200+
);
201+
formattedCode = DocPrinter.DocPrinter.Print(
202+
document,
203+
printerOptions,
204+
lineEnding
205+
);
206+
reorderedModifiers =
207+
reorderedModifiers || formattingContext2.State.ReorderedModifiers;
208+
reorderedUsingsWithDisabledText =
209+
reorderedUsingsWithDisabledText
210+
|| formattingContext2.State.ReorderedUsingsWithDisabledText;
211+
movedTrailingTrivia =
212+
movedTrailingTrivia || formattingContext2.State.MovedTrailingTrivia;
213+
}
198214
}
199215

200216
return new CodeFormatterResult

0 commit comments

Comments
 (0)