Skip to content

Commit

Permalink
Merge pull request dotnet#701 from JoeRobich/run-when-configured
Browse files Browse the repository at this point in the history
Only run Imports formatter when is has configuration in the .editorconfig
  • Loading branch information
JoeRobich committed Jun 8, 2020
2 parents 97a9540 + 4a32920 commit 083a1ea
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 16 deletions.
4 changes: 4 additions & 0 deletions 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:
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<MajorVersion>4</MajorVersion>
<MinorVersion>0</MinorVersion>
<MinorVersion>1</MinorVersion>
<!-- Build release-only package. -->
<PreReleaseVersionLabel />
</PropertyGroup>
Expand Down
7 changes: 3 additions & 4 deletions src/Formatters/CharsetFormatter.cs
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Formatters/DocumentFormatter.cs
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 3 additions & 4 deletions src/Formatters/EndOfLineFormatter.cs
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/Formatters/FinalNewlineFormatter.cs
Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/Formatters/ImportsFormatter.cs
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Formatters/WhitespaceFormatter.cs
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions tests/Formatters/ImportsFormatterTests.cs
Expand Up @@ -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<string, string>()
{
["end_of_line"] = EndOfLineFormatter.GetEndOfLineOption(Environment.NewLine)
};

await TestAsync(code, code, editorConfig);
}
}
}

0 comments on commit 083a1ea

Please sign in to comment.