From f0f1532dba15a58b965e63f55f4a3c65596168ed Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Mon, 8 Jun 2020 13:27:01 -0700 Subject: [PATCH] Only run Imports formatter when is has configuration in the .editorconfig --- CHANGELOG.md | 4 ++++ eng/Versions.props | 2 +- src/Formatters/CharsetFormatter.cs | 7 +++---- src/Formatters/DocumentFormatter.cs | 4 ++-- src/Formatters/EndOfLineFormatter.cs | 7 +++---- src/Formatters/FinalNewlineFormatter.cs | 5 ++--- src/Formatters/ImportsFormatter.cs | 9 ++++++++- src/Formatters/WhitespaceFormatter.cs | 2 +- tests/Formatters/ImportsFormatterTests.cs | 20 ++++++++++++++++++++ 9 files changed, 44 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e4657e4a4..4428428caa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog All changes to the project will be documented in this file. +## [4.1.x] - Not yet released +- [Remove the 255 argument limit for OneOrMany options such as --include and --exclude(700)](https://www.github.com/dotnet/roslyn/pull/700) +- [Only run Imports formatter when is has configuration in the .editorconfig(701)](https://www.github.com/dotnet/roslyn/pull/701) + ## [4.0.130203] - 2020-06-01 [View Complete Diff of Changes](https://www.github.com/dotnet/format/compare/3f2b20c65d32a59ca6bbc68b788a31ed38576d8e...f772fc306ff4b70cabebbea76beba9cdfd7ecb80) ### Breaking Changes: diff --git a/eng/Versions.props b/eng/Versions.props index a13c25f0f9..26dc2a03b3 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -2,7 +2,7 @@ 4 - 0 + 1 diff --git a/src/Formatters/CharsetFormatter.cs b/src/Formatters/CharsetFormatter.cs index 3851da7fbf..07f13063ee 100644 --- a/src/Formatters/CharsetFormatter.cs +++ b/src/Formatters/CharsetFormatter.cs @@ -24,7 +24,7 @@ internal sealed class CharsetFormatter : DocumentFormatter Document document, SourceText sourceText, OptionSet optionSet, - AnalyzerConfigOptions? analyzerConfigOptions, + AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken) @@ -67,10 +67,9 @@ private static byte[] GetEncodedBytes(string text, Encoding encoding) return stream.ToArray(); } - private static bool TryGetCharset(AnalyzerConfigOptions? analyzerConfigOptions, [NotNullWhen(true)] out Encoding? encoding) + private static bool TryGetCharset(AnalyzerConfigOptions analyzerConfigOptions, [NotNullWhen(true)] out Encoding? encoding) { - if (analyzerConfigOptions is object && - analyzerConfigOptions.TryGetValue("charset", out var charsetOption)) + if (analyzerConfigOptions.TryGetValue("charset", out var charsetOption)) { encoding = GetCharset(charsetOption); return true; diff --git a/src/Formatters/DocumentFormatter.cs b/src/Formatters/DocumentFormatter.cs index ae32adbf42..412ca1a0f3 100644 --- a/src/Formatters/DocumentFormatter.cs +++ b/src/Formatters/DocumentFormatter.cs @@ -42,7 +42,7 @@ internal abstract class DocumentFormatter : ICodeFormatter Document document, SourceText sourceText, OptionSet optionSet, - AnalyzerConfigOptions? analyzerConfigOptions, + AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken); @@ -91,7 +91,7 @@ internal abstract class DocumentFormatter : ICodeFormatter private async Task<(SourceText originalText, SourceText? formattedText)> GetFormattedSourceTextAsync( Document document, OptionSet optionSet, - AnalyzerConfigOptions? analyzerConfigOptions, + AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken) diff --git a/src/Formatters/EndOfLineFormatter.cs b/src/Formatters/EndOfLineFormatter.cs index 0537e1c209..746fb56f25 100644 --- a/src/Formatters/EndOfLineFormatter.cs +++ b/src/Formatters/EndOfLineFormatter.cs @@ -19,7 +19,7 @@ internal sealed class EndOfLineFormatter : DocumentFormatter Document document, SourceText sourceText, OptionSet optionSet, - AnalyzerConfigOptions? analyzerConfigOptions, + AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken) @@ -58,10 +58,9 @@ internal sealed class EndOfLineFormatter : DocumentFormatter }); } - public static bool TryGetEndOfLine(AnalyzerConfigOptions? analyzerConfigOptions, [NotNullWhen(true)] out string? endOfLine) + public static bool TryGetEndOfLine(AnalyzerConfigOptions analyzerConfigOptions, [NotNullWhen(true)] out string? endOfLine) { - if (analyzerConfigOptions is object && - analyzerConfigOptions.TryGetValue("end_of_line", out var endOfLineOption)) + if (analyzerConfigOptions.TryGetValue("end_of_line", out var endOfLineOption)) { endOfLine = GetEndOfLine(endOfLineOption); return true; diff --git a/src/Formatters/FinalNewlineFormatter.cs b/src/Formatters/FinalNewlineFormatter.cs index d439f2b57c..ec1f5a7c22 100644 --- a/src/Formatters/FinalNewlineFormatter.cs +++ b/src/Formatters/FinalNewlineFormatter.cs @@ -19,13 +19,12 @@ internal sealed class FinalNewlineFormatter : DocumentFormatter Document document, SourceText sourceText, OptionSet optionSet, - AnalyzerConfigOptions? analyzerConfigOptions, + AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken) { - if (analyzerConfigOptions is null || - !analyzerConfigOptions.TryGetValue("insert_final_newline", out var insertFinalNewlineValue) || + if (!analyzerConfigOptions.TryGetValue("insert_final_newline", out var insertFinalNewlineValue) || !bool.TryParse(insertFinalNewlineValue, out var insertFinalNewline)) { return await document.GetTextAsync(cancellationToken); diff --git a/src/Formatters/ImportsFormatter.cs b/src/Formatters/ImportsFormatter.cs index 9558db947b..d06f49039d 100644 --- a/src/Formatters/ImportsFormatter.cs +++ b/src/Formatters/ImportsFormatter.cs @@ -24,13 +24,20 @@ internal sealed class ImportsFormatter : DocumentFormatter Document document, SourceText sourceText, OptionSet optionSet, - AnalyzerConfigOptions? analyzerConfigOptions, + AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken) { try { + // Only run formatter if the user has specifically configured one of the driving properties. + if (!analyzerConfigOptions.TryGetValue("dotnet_sort_system_directives_first", out _) && + !analyzerConfigOptions.TryGetValue("dotnet_separate_import_directive_groups", out _)) + { + return sourceText; + } + var organizedDocument = await Formatter.OrganizeImportsAsync(document, cancellationToken); var isSameVersion = await IsSameDocumentAndVersionAsync(document, organizedDocument, cancellationToken).ConfigureAwait(false); diff --git a/src/Formatters/WhitespaceFormatter.cs b/src/Formatters/WhitespaceFormatter.cs index ed7800a8f9..e71b84e562 100644 --- a/src/Formatters/WhitespaceFormatter.cs +++ b/src/Formatters/WhitespaceFormatter.cs @@ -21,7 +21,7 @@ internal sealed class WhitespaceFormatter : DocumentFormatter Document document, SourceText sourceText, OptionSet optionSet, - AnalyzerConfigOptions? analyzerConfigOptions, + AnalyzerConfigOptions analyzerConfigOptions, FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken) diff --git a/tests/Formatters/ImportsFormatterTests.cs b/tests/Formatters/ImportsFormatterTests.cs index 6e83f86615..f79ae04a81 100644 --- a/tests/Formatters/ImportsFormatterTests.cs +++ b/tests/Formatters/ImportsFormatterTests.cs @@ -137,5 +137,25 @@ class C await TestAsync(testCode, expectedCode, editorConfig); } + + [Fact] + public async Task WhenNeitherOptionIsConfigured_AndImportsNotSortedOrSeparated_NoChange() + { + var code = @" +using Microsoft.CodeAnalysis; +using System.Linq; +using System; + +class C +{ +}"; + + var editorConfig = new Dictionary() + { + ["end_of_line"] = EndOfLineFormatter.GetEndOfLineOption(Environment.NewLine) + }; + + await TestAsync(code, code, editorConfig); + } } }