Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Mar 14, 2022
1 parent 3d0c2c9 commit 40c6af1
Show file tree
Hide file tree
Showing 21 changed files with 74 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public sealed override Task RegisterCodeFixesAsync(CodeFixContext context)

protected abstract string GetTitle();

#if CODE_STYLE
private async Task<Document> RemoveUnnecessaryImportsAsync(
#else
private static async Task<Document> RemoveUnnecessaryImportsAsync(
#endif
Document document,
CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -197,8 +198,9 @@ private static Document Format(ICommentSelectionService service, ITextSnapshot s
return null;
}

var formattingOptions = SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
var textSpans = changes.SelectAsArray(change => change.Span.ToTextSpan());
return service.FormatAsync(document, textSpans, cancellationToken).WaitAndGetResult(cancellationToken);
return service.FormatAsync(document, textSpans, formattingOptions, cancellationToken).WaitAndGetResult(cancellationToken);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,18 @@ internal sealed class CSharpSyntaxWrappingOptions : SyntaxWrappingOptions
public readonly bool NewLinesForBracesInObjectCollectionArrayInitializers;

public CSharpSyntaxWrappingOptions(
bool useTabs,
int tabSize,
string newLine,
SyntaxFormattingOptions formattingOptions,
int wrappingColumn,
OperatorPlacementWhenWrappingPreference operatorPlacement,
bool newLinesForBracesInObjectCollectionArrayInitializers)
: base(useTabs, tabSize, newLine, wrappingColumn, operatorPlacement)
: base(formattingOptions, wrappingColumn, operatorPlacement)
{
NewLinesForBracesInObjectCollectionArrayInitializers = newLinesForBracesInObjectCollectionArrayInitializers;
}

public static CSharpSyntaxWrappingOptions Create(AnalyzerConfigOptions options, CodeActionOptions ideOptions)
=> new(
useTabs: options.GetOption(FormattingOptions2.UseTabs),
tabSize: options.GetOption(FormattingOptions2.TabSize),
newLine: options.GetOption(FormattingOptions2.NewLine),
CSharpSyntaxFormattingOptions.Create(options),
operatorPlacement: options.GetOption(CodeStyleOptions2.OperatorPlacementWhenWrapping),
wrappingColumn: ideOptions.WrappingColumn,
newLinesForBracesInObjectCollectionArrayInitializers: options.GetOption(CSharpFormattingOptions2.NewLinesForBracesInObjectCollectionArrayInitializers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,9 +417,11 @@ private static async Task<ImmutableArray<ReferencedSymbol>> FindChangeSignatureR
{
var updatedDoc = currentSolution.GetRequiredDocument(docId).WithSyntaxRoot(updatedRoots[docId]);
var addImportOptions = await AddImportPlacementOptions.FromDocumentAsync(updatedDoc, cancellationToken).ConfigureAwait(false);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(updatedDoc, cancellationToken).ConfigureAwait(false);

var docWithImports = await ImportAdder.AddImportsFromSymbolAnnotationAsync(updatedDoc, addImportOptions, cancellationToken).ConfigureAwait(false);
var reducedDoc = await Simplifier.ReduceAsync(docWithImports, Simplifier.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
var formattedDoc = await Formatter.FormatAsync(reducedDoc, SyntaxAnnotation.ElasticAnnotation, cancellationToken: cancellationToken).ConfigureAwait(false);
var formattedDoc = await Formatter.FormatAsync(reducedDoc, SyntaxAnnotation.ElasticAnnotation, formattingOptions, cancellationToken).ConfigureAwait(false);

currentSolution = currentSolution.WithDocumentSyntaxRoot(docId, (await formattedDoc.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false))!);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ private static async Task<Document> FixReferencingDocumentAsync(
await FixReferencesAsync(document, changeNamespaceService, addImportService, refLocations, newNamespaceParts, cancellationToken)
.ConfigureAwait(false);

var optionSet = await documentWithRefFixed.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var addImportsOptions = await AddImportPlacementOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

var documentWithAdditionalImports = await AddImportsInContainersAsync(
Expand All @@ -663,9 +663,10 @@ await FixReferencesAsync(document, changeNamespaceService, addImportService, ref
cancellationToken).ConfigureAwait(false);

// Need to invoke formatter explicitly since we are doing the diff merge ourselves.
var formattedDocument = await Formatter.FormatAsync(documentWithAdditionalImports, Formatter.Annotation, optionSet, cancellationToken)
var formattedDocument = await Formatter.FormatAsync(documentWithAdditionalImports, Formatter.Annotation, formattingOptions, cancellationToken)
.ConfigureAwait(false);

var optionSet = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
return await Simplifier.ReduceAsync(formattedDocument, optionSet, cancellationToken).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ internal abstract class AbstractCommentSelectionService : ICommentSelectionServi
public abstract string SingleLineCommentString { get; }
public abstract bool SupportsBlockComment { get; }

public Task<Document> FormatAsync(Document document, ImmutableArray<TextSpan> changes, CancellationToken cancellationToken)
public Task<Document> FormatAsync(Document document, ImmutableArray<TextSpan> changes, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
var root = document.GetRequiredSyntaxRootSynchronously(cancellationToken);
var formattingSpans = changes.Select(s => CommonFormattingHelpers.GetFormattingSpan(root, s));

return Formatter.FormatAsync(document, formattingSpans, cancellationToken: cancellationToken);
return Formatter.FormatAsync(document, formattingSpans, formattingOptions, rules: null, cancellationToken);
}

public Task<CommentSelectionInfo> GetInfoAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Text;

Expand All @@ -14,6 +15,6 @@ internal interface ICommentSelectionService : ILanguageService
{
Task<CommentSelectionInfo> GetInfoAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken);

Task<Document> FormatAsync(Document document, ImmutableArray<TextSpan> changes, CancellationToken cancellationToken);
Task<Document> FormatAsync(Document document, ImmutableArray<TextSpan> changes, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ private async Task<Document> DetermineNewDocumentAsync(Document document, Comple
return document;
}

var insertionRoot = await GetTreeWithAddedSyntaxNodeRemovedAsync(memberContainingDocument, cancellationToken).ConfigureAwait(false);
var insertionText = await GenerateInsertionTextAsync(memberContainingDocument, cancellationToken).ConfigureAwait(false);
var memberContainingDocumentFormattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var insertionRoot = await GetTreeWithAddedSyntaxNodeRemovedAsync(memberContainingDocument, memberContainingDocumentFormattingOptions, cancellationToken).ConfigureAwait(false);
var insertionText = await GenerateInsertionTextAsync(memberContainingDocument, memberContainingDocumentFormattingOptions, cancellationToken).ConfigureAwait(false);

var destinationSpan = ComputeDestinationSpan(insertionRoot);

Expand All @@ -98,7 +99,8 @@ private async Task<Document> DetermineNewDocumentAsync(Document document, Comple
var declaration = GetSyntax(newRoot.FindToken(destinationSpan.End));

document = document.WithSyntaxRoot(newRoot.ReplaceNode(declaration, declaration.WithAdditionalAnnotations(_annotation)));
return await Formatter.FormatAsync(document, _annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
return await Formatter.FormatAsync(document, _annotation, formattingOptions, cancellationToken).ConfigureAwait(false);
}

private async Task<Document?> GenerateMemberAndUsingsAsync(
Expand Down Expand Up @@ -177,17 +179,17 @@ private TextSpan ComputeDestinationSpan(SyntaxNode insertionRoot)
}

private async Task<string> GenerateInsertionTextAsync(
Document memberContainingDocument, CancellationToken cancellationToken)
Document memberContainingDocument, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
memberContainingDocument = await Simplifier.ReduceAsync(memberContainingDocument, Simplifier.Annotation, optionSet: null, cancellationToken).ConfigureAwait(false);
memberContainingDocument = await Formatter.FormatAsync(memberContainingDocument, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
memberContainingDocument = await Formatter.FormatAsync(memberContainingDocument, Formatter.Annotation, formattingOptions, cancellationToken).ConfigureAwait(false);

var root = await memberContainingDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
return root.GetAnnotatedNodes(_annotation).Single().ToString().Trim();
}

private async Task<SyntaxNode> GetTreeWithAddedSyntaxNodeRemovedAsync(
Document document, CancellationToken cancellationToken)
Document document, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
// Added imports are annotated for simplification too. Therefore, we simplify the document
// before removing added member node to preserve those imports in the document.
Expand All @@ -201,7 +203,7 @@ private async Task<SyntaxNode> GetTreeWithAddedSyntaxNodeRemovedAsync(

var dismemberedDocument = document.WithSyntaxRoot(root);

dismemberedDocument = await Formatter.FormatAsync(dismemberedDocument, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
dismemberedDocument = await Formatter.FormatAsync(dismemberedDocument, Formatter.Annotation, formattingOptions, cancellationToken).ConfigureAwait(false);
return await dismemberedDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,17 @@ public override async Task<CompletionChange> GetChangeAsync(
// Add required using/imports directive.
var addImportService = document.GetRequiredLanguageService<IAddImportsService>();
var generator = document.GetRequiredLanguageService<SyntaxGenerator>();
var importsPlacement = await AddImportPlacementOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

var addImportsOptions = await AddImportPlacementOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

var importNode = CreateImport(document, containingNamespace);

var compilation = await document.Project.GetRequiredCompilationAsync(cancellationToken).ConfigureAwait(false);
var rootWithImport = addImportService.AddImport(compilation, root, addImportContextNode!, importNode, generator, importsPlacement, cancellationToken);
var rootWithImport = addImportService.AddImport(compilation, root, addImportContextNode!, importNode, generator, addImportsOptions, cancellationToken);
var documentWithImport = document.WithSyntaxRoot(rootWithImport);
// This only formats the annotated import we just added, not the entire document.
var formattedDocumentWithImport = await Formatter.FormatAsync(documentWithImport, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
var formattedDocumentWithImport = await Formatter.FormatAsync(documentWithImport, Formatter.Annotation, formattingOptions, cancellationToken).ConfigureAwait(false);

using var _ = ArrayBuilder<TextChange>.GetInstance(out var builder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,16 @@ private async Task<Solution> EncapsulateFieldAsync(
var markFieldPrivate = field.DeclaredAccessibility != Accessibility.Private;
var rewrittenFieldDeclaration = await RewriteFieldNameAndAccessibilityAsync(finalFieldName, markFieldPrivate, document, declarationAnnotation, cancellationToken).ConfigureAwait(false);

document = await Formatter.FormatAsync(document.WithSyntaxRoot(rewrittenFieldDeclaration), Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
document = await Formatter.FormatAsync(document.WithSyntaxRoot(rewrittenFieldDeclaration), Formatter.Annotation, formattingOptions, cancellationToken).ConfigureAwait(false);

solution = document.Project.Solution;
foreach (var linkedDocumentId in document.GetLinkedDocumentIds())
{
var linkedDocument = solution.GetDocument(linkedDocumentId);
var linkedDocumentFormattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(linkedDocument, cancellationToken).ConfigureAwait(false);
var updatedLinkedRoot = await RewriteFieldNameAndAccessibilityAsync(finalFieldName, markFieldPrivate, linkedDocument, declarationAnnotation, cancellationToken).ConfigureAwait(false);
var updatedLinkedDocument = await Formatter.FormatAsync(linkedDocument.WithSyntaxRoot(updatedLinkedRoot), Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
var updatedLinkedDocument = await Formatter.FormatAsync(linkedDocument.WithSyntaxRoot(updatedLinkedRoot), Formatter.Annotation, linkedDocumentFormattingOptions, cancellationToken).ConfigureAwait(false);
solution = updatedLinkedDocument.Project.Solution;
}

Expand All @@ -230,7 +232,7 @@ private async Task<Solution> EncapsulateFieldAsync(
document);

var solutionWithProperty = await AddPropertyAsync(
document, document.Project.Solution, field, generatedProperty, cancellationToken).ConfigureAwait(false);
document, document.Project.Solution, field, generatedProperty, formattingOptions, cancellationToken).ConfigureAwait(false);

return solutionWithProperty;
}
Expand Down Expand Up @@ -316,7 +318,7 @@ private ISet<Location> GetConstructorLocations(INamedTypeSymbol containingType)

internal abstract IEnumerable<SyntaxNode> GetConstructorNodes(INamedTypeSymbol containingType);

protected static async Task<Solution> AddPropertyAsync(Document document, Solution destinationSolution, IFieldSymbol field, IPropertySymbol property, CancellationToken cancellationToken)
protected static async Task<Solution> AddPropertyAsync(Document document, Solution destinationSolution, IFieldSymbol field, IPropertySymbol property, SyntaxFormattingOptions formattingOptions, CancellationToken cancellationToken)
{
var codeGenerationService = document.GetLanguageService<ICodeGenerationService>();

Expand All @@ -329,7 +331,7 @@ protected static async Task<Solution> AddPropertyAsync(Document document, Soluti
var updatedDocument = await codeGenerationService.AddPropertyAsync(
destinationSolution, destination, property, context, cancellationToken).ConfigureAwait(false);

updatedDocument = await Formatter.FormatAsync(updatedDocument, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
updatedDocument = await Formatter.FormatAsync(updatedDocument, Formatter.Annotation, formattingOptions, cancellationToken).ConfigureAwait(false);
updatedDocument = await Simplifier.ReduceAsync(updatedDocument, cancellationToken: cancellationToken).ConfigureAwait(false);

return updatedDocument.Project.Solution;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,14 @@ private static async Task<Solution> GetFormattedSolutionAsync(Solution unformatt
foreach (var documentId in documentIds)
{
var document = formattedSolution.GetDocument(documentId);

var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);

var formattedDocument = await Formatter.FormatAsync(
document,
Formatter.Annotation,
cancellationToken: cancellationToken).ConfigureAwait(false);
formattingOptions,
cancellationToken).ConfigureAwait(false);

var simplifiedDocument = await Simplifier.ReduceAsync(
formattedDocument,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private static async Task<Document> FixOneAsync(CodeFixContext context, Diagnost

protected override async Task FixAllAsync(Document document, ImmutableArray<Diagnostic> diagnostics, SyntaxEditor editor, CancellationToken cancellationToken)
{
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
var options = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var updatedDocument = await Formatter.FormatAsync(document, options, cancellationToken).ConfigureAwait(false);
editor.ReplaceNode(editor.OriginalRoot, await updatedDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ internal IntroduceVariableAllOccurrenceCodeAction(

protected override async Task<Document> PostProcessChangesAsync(Document document, CancellationToken cancellationToken)
{
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
document = await Simplifier.ReduceAsync(document, Simplifier.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
document = await Formatter.FormatAsync(document, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
document = await Formatter.FormatAsync(document, Formatter.Annotation, formattingOptions, cancellationToken).ConfigureAwait(false);
document = await CaseCorrector.CaseCorrectAsync(document, CaseCorrector.Annotation, cancellationToken).ConfigureAwait(false);
return document;
}
Expand Down
Loading

0 comments on commit 40c6af1

Please sign in to comment.