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
Changes from 9 commits
529c219
6dd48ec
df2d85f
2441c82
c8dbf9b
22961df
0a2626e
f1b14e3
33b340d
c50d846
8a1328c
e67f41f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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.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); | ||
if (organizedDocument == document) | ||
{ | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe the message the compiler gives here is |
||
return sourceText; | ||
} | ||
} | ||
} | ||
} | ||
|
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 didn't think equality for documents was implemented. Did we add that? I would normally compare the document ids.
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.
Well, I just want to know if the document was changed, so I thought a ref equals would be fine. Could use
HasTextChanged
instead tho.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,
HasTextChanged
seems the most efficient implementation since it looks at the text versionThere 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.
And it is internal. =) I could run GetTextChangesAsync and check to see if it is empty.
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.
Actually, I've verified the ref check is doing exactly what I want it to do. The call to Organize is returning the same document instance when there are no changes.
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.
ah, that's why I always recommend customers compare
DocumentId
s, I forgot.DocumentId
implements equitable so you can do(organizedDocument.Id == document.Id)
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.
Good to know. Feel free to ignore this then. My only nit is that I think this behavior is an implementation detail and not part of the public API contract.