Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Apr 10, 2022
1 parent b7f72a7 commit 700f096
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private Solution CreateSolutionWithEventHandler(

var documentWithNameAndAnnotationsAdded = AddMethodNameAndAnnotationsToSolution(document, eventHandlerMethodName, position, plusEqualsTokenAnnotation, cancellationToken);
var semanticDocument = SemanticDocument.CreateAsync(documentWithNameAndAnnotationsAdded, cancellationToken).WaitAndGetResult(cancellationToken);
var preferences = CSharpCodeGenerationPreferences.FromDocumentAsync(semanticDocument.Document, cancellationToken).WaitAndGetResult(cancellationToken);
var preferences = CSharpCodeGenerationPreferences.FromDocumentAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);
var updatedRoot = AddGeneratedHandlerMethodToSolution(semanticDocument, preferences, eventHandlerMethodName, plusEqualsTokenAnnotation, cancellationToken);

if (updatedRoot == null)
Expand All @@ -167,8 +167,10 @@ private Solution CreateSolutionWithEventHandler(
return null;
}

var formattingOptions = SyntaxFormattingOptions.FromDocumentAsync(document, cancellationToken).WaitAndGetResult(cancellationToken);

var simplifiedDocument = Simplifier.ReduceAsync(documentWithNameAndAnnotationsAdded.WithSyntaxRoot(updatedRoot), Simplifier.Annotation, cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);
var formattedDocument = Formatter.FormatAsync(simplifiedDocument, Formatter.Annotation, cancellationToken: cancellationToken).WaitAndGetResult(cancellationToken);
var formattedDocument = Formatter.FormatAsync(simplifiedDocument, Formatter.Annotation, formattingOptions, cancellationToken).WaitAndGetResult(cancellationToken);

var newRoot = formattedDocument.GetSyntaxRootSynchronously(cancellationToken);
plusEqualTokenEndPosition = newRoot.GetAnnotatedNodesAndTokens(plusEqualsTokenAnnotation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.Utilities.CommandHandlers
Return False
End If

Dim formattingOptions = SyntaxFormattingOptions.FromDocumentAsync(newDocument, cancellationToken).WaitAndGetResult(cancellationToken)

newDocument = Simplifier.ReduceAsync(newDocument, Simplifier.Annotation, Nothing, cancellationToken).WaitAndGetResult(cancellationToken)
newDocument = Formatter.FormatAsync(newDocument, Formatter.Annotation, cancellationToken:=cancellationToken).WaitAndGetResult(cancellationToken)
newDocument = Formatter.FormatAsync(newDocument, Formatter.Annotation, formattingOptions, cancellationToken).WaitAndGetResult(cancellationToken)

newDocument.Project.Solution.Workspace.ApplyDocumentChanges(newDocument, cancellationToken)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

#nullable disable

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand Down Expand Up @@ -34,7 +36,6 @@ private static Document GetDocument(string code)
private static async Task TestAsync(string initialText, string attributeAddedText)
{
var doc = GetDocument(initialText);
var options = await doc.GetOptionsAsync();

var attributeList =
SyntaxFactory.AttributeList(
Expand All @@ -54,7 +55,7 @@ private static async Task TestAsync(string initialText, string attributeAddedTex

if (attributeAddedText != null)
{
var formatted = await Formatter.FormatAsync(changedDoc, SyntaxAnnotation.ElasticAnnotation, options);
var formatted = await Formatter.FormatAsync(changedDoc, SyntaxAnnotation.ElasticAnnotation, CSharpSyntaxFormattingOptions.Default, CancellationToken.None);
var actualText = (await formatted.GetTextAsync()).ToString();

Assert.Equal(attributeAddedText, actualText);
Expand Down
12 changes: 7 additions & 5 deletions src/Workspaces/CSharpTest/CodeGeneration/AddImportsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.AddImport;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
Expand Down Expand Up @@ -78,21 +79,23 @@ private static async Task TestAsync(
PlaceImportsInsideNamespaces: placeImportsInsideNamespaces,
AllowInHiddenRegions: false);

var formattingOptions = CSharpSyntaxFormattingOptions.Default;

var imported = useSymbolAnnotations
? await ImportAdder.AddImportsFromSymbolAnnotationAsync(doc, addImportOptions, CancellationToken.None)
: await ImportAdder.AddImportsFromSyntaxesAsync(doc, addImportOptions, CancellationToken.None);

if (importsAddedText != null)
{
var formatted = await Formatter.FormatAsync(imported, SyntaxAnnotation.ElasticAnnotation);
var formatted = await Formatter.FormatAsync(imported, SyntaxAnnotation.ElasticAnnotation, formattingOptions, CancellationToken.None);
var actualText = (await formatted.GetTextAsync()).ToString();
Assert.Equal(importsAddedText, actualText);
}

if (simplifiedText != null)
{
var reduced = await Simplifier.ReduceAsync(imported);
var formatted = await Formatter.FormatAsync(reduced, SyntaxAnnotation.ElasticAnnotation);
var formatted = await Formatter.FormatAsync(reduced, SyntaxAnnotation.ElasticAnnotation, formattingOptions, CancellationToken.None);

var actualText = (await formatted.GetTextAsync()).ToString();
Assert.Equal(simplifiedText, actualText);
Expand Down Expand Up @@ -780,8 +783,6 @@ public void M2([A] String p2) { }
project = project.AddDocument("duplicate.cs", externalCode).Project;
var document = project.AddDocument("test.cs", code);

var options = document.Project.Solution.Workspace.Options;

var compilation = await document.Project.GetCompilationAsync(CancellationToken.None);
var compilerDiagnostics = compilation.GetDiagnostics(CancellationToken.None);
Assert.Empty(compilerDiagnostics.Where(d => d.Severity == DiagnosticSeverity.Error));
Expand All @@ -799,13 +800,14 @@ public void M2([A] String p2) { }
var documentWithAttribute = editor.GetChangedDocument();

var addImportOptions = new AddImportPlacementOptions();
var formattingOptions = CSharpSyntaxFormattingOptions.Default;

// Add namespace import.
var imported = useSymbolAnnotations
? await ImportAdder.AddImportsFromSymbolAnnotationAsync(documentWithAttribute, addImportOptions, CancellationToken.None).ConfigureAwait(false)
: await ImportAdder.AddImportsFromSyntaxesAsync(documentWithAttribute, addImportOptions, CancellationToken.None).ConfigureAwait(false);

var formatted = await Formatter.FormatAsync(imported, options);
var formatted = await Formatter.FormatAsync(imported, formattingOptions, CancellationToken.None);
var actualText = (await formatted.GetTextAsync()).ToString();

Assert.Equal(@"using System;
Expand Down
6 changes: 4 additions & 2 deletions src/Workspaces/CSharpTest/CodeGeneration/SymbolEditorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Simplification;
Expand Down Expand Up @@ -51,8 +53,8 @@ private static async Task<IEnumerable<ISymbol>> GetSymbolsAsync(Solution solutio
private static async Task<string> GetActualAsync(Document document)
{
document = await Simplifier.ReduceAsync(document);
document = await Formatter.FormatAsync(document, Formatter.Annotation);
document = await Formatter.FormatAsync(document, SyntaxAnnotation.ElasticAnnotation);
document = await Formatter.FormatAsync(document, Formatter.Annotation, CSharpSyntaxFormattingOptions.Default, CancellationToken.None);
document = await Formatter.FormatAsync(document, SyntaxAnnotation.ElasticAnnotation, CSharpSyntaxFormattingOptions.Default, CancellationToken.None);
return (await document.GetSyntaxRootAsync()).ToFullString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Imports Microsoft.CodeAnalysis.Editing
Imports Microsoft.CodeAnalysis.Formatting
Imports Microsoft.CodeAnalysis.Simplification
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.VisualBasic.Formatting
Imports Microsoft.CodeAnalysis.VisualBasic.Syntax
Imports Roslyn.Test.Utilities
Imports Xunit
Expand Down Expand Up @@ -86,20 +87,22 @@ End NameSpace"
PlaceImportsInsideNamespaces:=False,
AllowInHiddenRegions:=False)

Dim formattingOptions = VisualBasicSyntaxFormattingOptions.Default

Dim imported = If(
useSymbolAnnotations,
Await ImportAdder.AddImportsFromSymbolAnnotationAsync(doc, addImportOptions, CancellationToken.None),
Await ImportAdder.AddImportsFromSyntaxesAsync(doc, addImportOptions, CancellationToken.None))

If importsAddedText IsNot Nothing Then
Dim formatted = Await Formatter.FormatAsync(imported, SyntaxAnnotation.ElasticAnnotation)
Dim formatted = Await Formatter.FormatAsync(imported, SyntaxAnnotation.ElasticAnnotation, formattingOptions, CancellationToken.None)
Dim actualText = (Await formatted.GetTextAsync()).ToString()
Assert.Equal(importsAddedText, actualText)
End If

If simplifiedText IsNot Nothing Then
Dim reduced = Await Simplifier.ReduceAsync(imported)
Dim formatted = Await Formatter.FormatAsync(reduced, SyntaxAnnotation.ElasticAnnotation)
Dim formatted = Await Formatter.FormatAsync(reduced, SyntaxAnnotation.ElasticAnnotation, formattingOptions, CancellationToken.None)
Dim actualText = (Await formatted.GetTextAsync()).ToString()
Assert.Equal(simplifiedText, actualText)
End If
Expand Down
7 changes: 5 additions & 2 deletions src/Workspaces/VisualBasicTest/Formatting/FormattingTests.vb
Original file line number Diff line number Diff line change
Expand Up @@ -4338,8 +4338,11 @@ End Module
End Class</text>.Value)

Dim propertyBlock = (Await document.GetSyntaxRootAsync()).DescendantNodes().OfType(Of PropertyBlockSyntax).Single()
document = Await Formatter.FormatAsync(document.WithSyntaxRoot(
(Await document.GetSyntaxRootAsync()).ReplaceNode(propertyBlock, propertyBlock.WithAccessors(SyntaxFactory.SingletonList(setter)))))

Dim newDocument = document.WithSyntaxRoot(
(Await document.GetSyntaxRootAsync()).ReplaceNode(propertyBlock, propertyBlock.WithAccessors(SyntaxFactory.SingletonList(setter))))

document = Await Formatter.FormatAsync(newDocument, VisualBasicSyntaxFormattingOptions.Default, CancellationToken.None)

Dim actual = (Await document.GetTextAsync()).ToString()
Assert.Equal(actual, actual)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Imports System.Threading
Imports Microsoft.CodeAnalysis.Editing
Imports Microsoft.CodeAnalysis.Formatting
Imports Microsoft.CodeAnalysis.VisualBasic.Formatting
Imports Microsoft.CodeAnalysis.Test.Utilities
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.Options
Expand Down Expand Up @@ -45,11 +46,14 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Workspaces.UnitTests.OrganizeImport
Dim document = project.AddDocument("Document", SourceText.From(initial.Value.NormalizeLineEndings()))

Dim options = workspace.Options.WithChangedOption(New OptionKey(GenerationOptions.PlaceSystemNamespaceFirst, document.Project.Language), placeSystemNamespaceFirst)
options = options.WithChangedOption(New OptionKey(GenerationOptions.SeparateImportDirectiveGroups, document.Project.Language), separateImportGroups)
document = document.WithSolutionOptions(options)

Dim formattingOptions = New VisualBasicSyntaxFormattingOptions(
LineFormattingOptions.Default,
separateImportDirectiveGroups:=separateImportGroups)

Dim organizedDocument = Await Formatter.OrganizeImportsAsync(document, CancellationToken.None)
Dim formattedDocument = Await Formatter.FormatAsync(organizedDocument, workspace.Options, CancellationToken.None)
Dim formattedDocument = Await Formatter.FormatAsync(organizedDocument, formattingOptions, CancellationToken.None)

Dim newRoot = Await formattedDocument.GetSyntaxRootAsync()
Assert.Equal(final.Value.NormalizeLineEndings(), newRoot.ToFullString())
Expand Down

0 comments on commit 700f096

Please sign in to comment.