Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit ae6678a

Browse files
committed
Disable table formatting by default
This changes the behavior of the formatter to not define DOTNET_FORMATTER by default. It can be opted into by using the /tables option. The reason for this change is to avoid a bug in Roslyn around changing and resetting the parser options. When hit Roslyn will end up changing all files in the solution to have a different encoding / line ending. I haven't yet tracked down this bug but short term making this opt-in behavior so users aren't adversely affected by it.
1 parent 36f8784 commit ae6678a

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/CodeFormatter/Program.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ private static int Main(string[] args)
2424
{
2525
if (args.Length < 1)
2626
{
27-
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/c:<config1,config2> [/copyright:file]");
27+
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/tables] [/c:<config1,config2> [/copyright:file]");
2828
Console.WriteLine(" <rule types> - Rule types to use in addition to the default ones.");
2929
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
3030
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
3131
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter");
3232
Console.WriteLine(" should run under.");
3333
Console.WriteLine(" <copyright> - Specifies file containing copyright header.");
34+
Console.WriteLine(" <tables> - Let tables opt out of formatting by defining DOTNET_FORMATTER");
3435
return -1;
3536
}
3637

@@ -45,6 +46,7 @@ private static int Main(string[] args)
4546
var ruleTypeBuilder = ImmutableArray.CreateBuilder<string>();
4647
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
4748
var copyrightHeader = FormattingConstants.DefaultCopyrightHeader;
49+
var allowTables = false;
4850
var comparer = StringComparer.OrdinalIgnoreCase;
4951

5052
for (int i = 1; i < args.Length; i++)
@@ -80,6 +82,10 @@ private static int Main(string[] args)
8082
{
8183
copyrightHeader = ImmutableArray<string>.Empty;
8284
}
85+
else if (comparer.Equals(arg, "/tables"))
86+
{
87+
allowTables = true;
88+
}
8389
else
8490
{
8591
ruleTypeBuilder.Add(arg);
@@ -99,6 +105,7 @@ private static int Main(string[] args)
99105
fileNamesBuilder.ToImmutableArray(),
100106
configBuilder.ToImmutableArray(),
101107
copyrightHeader,
108+
allowTables,
102109
ct).Wait(ct);
103110
Console.WriteLine("Completed formatting.");
104111
return 0;
@@ -124,13 +131,15 @@ private static async Task RunAsync(
124131
ImmutableArray<string> fileNames,
125132
ImmutableArray<string[]> preprocessorConfigurations,
126133
ImmutableArray<string> copyrightHeader,
134+
bool allowTables,
127135
CancellationToken cancellationToken)
128136
{
129137
var workspace = MSBuildWorkspace.Create();
130138
var engine = FormattingEngine.Create(ruleTypes);
131139
engine.PreprocessorConfigurations = preprocessorConfigurations;
132140
engine.FileNames = fileNames;
133141
engine.CopyrightHeader = copyrightHeader;
142+
engine.AllowTables = allowTables;
134143

135144
string extension = Path.GetExtension(projectOrSolutionPath);
136145
if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ internal sealed class FormattingEngineImplementation : IFormattingEngine
3333
private readonly IEnumerable<ILocalSemanticFormattingRule> _localSemanticRules;
3434
private readonly IEnumerable<IGlobalSemanticFormattingRule> _globalSemanticRules;
3535
private readonly Stopwatch _watch = new Stopwatch();
36+
private bool _allowTables;
3637

3738
public ImmutableArray<string> CopyrightHeader
3839
{
@@ -58,6 +59,12 @@ public IFormatLogger FormatLogger
5859
set { _options.FormatLogger = value; }
5960
}
6061

62+
public bool AllowTables
63+
{
64+
get { return _allowTables; }
65+
set { _allowTables = value; }
66+
}
67+
6168
[ImportingConstructor]
6269
internal FormattingEngineImplementation(
6370
Options options,
@@ -142,11 +149,21 @@ private Solution RemoveTablePreprocessorSymbol(Solution newSolution, Solution ol
142149
internal async Task<Solution> FormatCoreAsync(Solution originalSolution, IReadOnlyList<DocumentId> documentIds, CancellationToken cancellationToken)
143150
{
144151
var solution = originalSolution;
145-
solution = AddTablePreprocessorSymbol(originalSolution);
152+
153+
if (_allowTables)
154+
{
155+
solution = AddTablePreprocessorSymbol(originalSolution);
156+
}
157+
146158
solution = await RunSyntaxPass(solution, documentIds, cancellationToken);
147159
solution = await RunLocalSemanticPass(solution, documentIds, cancellationToken);
148160
solution = await RunGlobalSemanticPass(solution, documentIds, cancellationToken);
149-
solution = RemoveTablePreprocessorSymbol(solution, originalSolution);
161+
162+
if (_allowTables)
163+
{
164+
solution = RemoveTablePreprocessorSymbol(solution, originalSolution);
165+
}
166+
150167
return solution;
151168
}
152169

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public interface IFormattingEngine
1515
ImmutableArray<string> CopyrightHeader { get; set; }
1616
ImmutableArray<string[]> PreprocessorConfigurations { get; set; }
1717
ImmutableArray<string> FileNames { get; set; }
18+
bool AllowTables { get; set; }
1819
Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);
1920
Task FormatProjectAsync(Project porject, CancellationToken cancellationToken);
2021
}

0 commit comments

Comments
 (0)