New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Imports Formatter #693
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
529c219
Add using directive formatting
wojciechcibor 6dd48ec
Merge branch 'master' into feature/sort_usings
JoeRobich df2d85f
Add ImportFormatter's fix message to resources
JoeRobich 2441c82
Port ImportFormatter changes from features/analyzers
JoeRobich c8dbf9b
Update FormatterTests to add AnalyzerConfig to test workspace
JoeRobich 22961df
Port ImportsFormatter tests from features/analyzers
JoeRobich 0a2626e
Run format against format.sln
JoeRobich f1b14e3
Add catch for imports organizer
JoeRobich 33b340d
Add issue link to ImportsFormatter workaround
JoeRobich c50d846
Log warning when unable to organize imports
JoeRobich 8a1328c
Use Path.Combine when building paths
JoeRobich e67f41f
Run format against format.sln
JoeRobich File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.Formatting; | ||
using Microsoft.CodeAnalysis.Options; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Microsoft.CodeAnalysis.Tools.Formatters | ||
{ | ||
/// <summary> | ||
/// ImportsFormatter that uses the <see cref="Formatter"/> to format document import directives. | ||
/// </summary> | ||
internal sealed class ImportsFormatter : DocumentFormatter | ||
{ | ||
protected override string FormatWarningDescription => Resources.Fix_imports_ordering; | ||
private readonly DocumentFormatter _endOfLineFormatter = new EndOfLineFormatter(); | ||
|
||
internal override async Task<SourceText> FormatFileAsync( | ||
Document document, | ||
SourceText sourceText, | ||
OptionSet optionSet, | ||
AnalyzerConfigOptions? analyzerConfigOptions, | ||
FormatOptions formatOptions, | ||
ILogger logger, | ||
CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
var organizedDocument = await Formatter.OrganizeImportsAsync(document, cancellationToken); | ||
|
||
var isSameVersion = await IsSameDocumentAndVersionAsync(document, organizedDocument, cancellationToken).ConfigureAwait(false); | ||
if (isSameVersion) | ||
{ | ||
return sourceText; | ||
} | ||
|
||
// Because the Formatter does not abide the `end_of_line` option we have to fix up the ends of the organized lines. | ||
// See https://github.com/dotnet/roslyn/issues/44136 | ||
var organizedSourceText = await organizedDocument.GetTextAsync(cancellationToken); | ||
return await _endOfLineFormatter.FormatFileAsync(organizedDocument, organizedSourceText, optionSet, analyzerConfigOptions, formatOptions, logger, cancellationToken); | ||
} | ||
catch (InsufficientExecutionStackException) | ||
{ | ||
// This case is normally not hit when running against a handwritten code file. | ||
// https://github.com/dotnet/roslyn/issues/44710#issuecomment-636253053 | ||
logger.LogWarning(Resources.Unable_to_organize_imports_for_0_The_document_is_too_complex, Path.GetFileName(document.FilePath)); | ||
return sourceText; | ||
} | ||
} | ||
|
||
private static async Task<bool> IsSameDocumentAndVersionAsync(Document a, Document b, CancellationToken cancellationToken) | ||
{ | ||
if (a == b) | ||
{ | ||
return true; | ||
} | ||
|
||
if (a.Id != b.Id) | ||
{ | ||
return false; | ||
} | ||
|
||
var aVersion = await a.GetTextVersionAsync(cancellationToken); | ||
var bVersion = await b.GetTextVersionAsync(cancellationToken); | ||
|
||
return aVersion == bVersion; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably tell the user that the file was too large to format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, although is it a size issue or an expression complexity issue. Perhaps can just be vague and say "Unable to organize file
{filename}
"There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the message the compiler gives here is
expression is too complex