Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Mar 13, 2022
1 parent 3d0c2c9 commit 77a63af
Show file tree
Hide file tree
Showing 19 changed files with 63 additions and 48 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 @@ -98,7 +98,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 @@ -179,8 +180,10 @@ private TextSpan ComputeDestinationSpan(SyntaxNode insertionRoot)
private async Task<string> GenerateInsertionTextAsync(
Document memberContainingDocument, CancellationToken cancellationToken)
{
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(memberContainingDocument, cancellationToken).ConfigureAwait(false);

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();
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,8 @@ private static async Task<MoveToNamespaceResult> MoveTypeToNamespaceAsync(
private static async Task<Solution> PropagateChangeToLinkedDocumentsAsync(Document document, CancellationToken cancellationToken)
{
// Need to make sure elastic trivia is formatted properly before pushing the text to other documents.
var formattedDocument = await Formatter.FormatAsync(document, SyntaxAnnotation.ElasticAnnotation, cancellationToken: cancellationToken).ConfigureAwait(false);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).ConfigureAwait(false);
var formattedDocument = await Formatter.FormatAsync(document, SyntaxAnnotation.ElasticAnnotation, formattingOptions, cancellationToken).ConfigureAwait(false);
var formattedText = await formattedDocument.GetTextAsync(cancellationToken).ConfigureAwait(false);
var solution = formattedDocument.Project.Solution;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ internal static class ExtractTypeHelpers

newTypeDocument = newTypeDocument.WithSyntaxRoot(annotatedRoot);

var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(newTypeDocument, cancellationToken).ConfigureAwait(false);
var simplified = await Simplifier.ReduceAsync(newTypeDocument, cancellationToken: cancellationToken).ConfigureAwait(false);
var formattedDocument = await Formatter.FormatAsync(simplified, cancellationToken: cancellationToken).ConfigureAwait(false);
var formattedDocument = await Formatter.FormatAsync(simplified, formattingOptions, cancellationToken).ConfigureAwait(false);

return (formattedDocument, typeAnnotation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private async Task<Document> FormatDocumentAsync(SyntaxNode rewrittenRoot, TextS
{
var newDocument = OriginalDocument.WithSyntaxRoot(rewrittenRoot);
var formattedDocument = await Formatter.FormatAsync(
newDocument, spanToFormat, cancellationToken: CancellationToken).ConfigureAwait(false);
newDocument, spanToFormat, Options.FormattingOptions, CancellationToken).ConfigureAwait(false);
return formattedDocument;
}

Expand Down
18 changes: 7 additions & 11 deletions src/Features/Core/Portable/Wrapping/SyntaxWrappingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,29 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Formatting;

namespace Microsoft.CodeAnalysis.Wrapping
{
internal abstract class SyntaxWrappingOptions
{
public readonly bool UseTabs;
public readonly int TabSize;
public readonly string NewLine;
public readonly SyntaxFormattingOptions FormattingOptions;
public readonly int WrappingColumn;
public readonly OperatorPlacementWhenWrappingPreference OperatorPlacement;

protected SyntaxWrappingOptions(
bool useTabs,
int tabSize,
string newLine,
SyntaxFormattingOptions formattingOptions,
int wrappingColumn,
OperatorPlacementWhenWrappingPreference operatorPlacement)
{
UseTabs = useTabs;
TabSize = tabSize;
NewLine = newLine;
FormattingOptions = formattingOptions;
WrappingColumn = wrappingColumn;
OperatorPlacement = operatorPlacement;
}

public bool UseTabs => FormattingOptions.UseTabs;
public int TabSize => FormattingOptions.TabSize;
public string NewLine => FormattingOptions.NewLine;
}
}
Loading

0 comments on commit 77a63af

Please sign in to comment.