Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Apr 10, 2022
1 parent f33deb3 commit 2d69068
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeRefactorings.MoveType;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
Expand Down Expand Up @@ -931,7 +932,7 @@ private async Task TestNamespaceMove(string originalCode, string expectedCode, b
}

var modifiedDocument = modifiedSolution.GetDocument(documentToModifyId);
var formattedDocument = await Formatter.FormatAsync(modifiedDocument).ConfigureAwait(false);
var formattedDocument = await Formatter.FormatAsync(modifiedDocument, CSharpSyntaxFormattingOptions.Default, CancellationToken.None).ConfigureAwait(false);

var formattedText = await formattedDocument.GetTextAsync().ConfigureAwait(false);
Assert.Equal(expectedCode, formattedText.ToString());
Expand Down
8 changes: 6 additions & 2 deletions src/EditorFeatures/Test/CodeGeneration/CodeGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeGeneration;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
Expand All @@ -20,6 +21,7 @@
using Microsoft.CodeAnalysis.Simplification;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.VisualBasic.Formatting;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Xunit;
Expand Down Expand Up @@ -882,8 +884,10 @@ public void Dispose()
{
this.Document = this.Result;

var actual = Formatter.FormatAsync(Simplifier.ReduceAsync(this.Document, Simplifier.Annotation).Result, Formatter.Annotation).Result
.GetSyntaxRootAsync().Result.ToFullString();
var formattingOptions = IsVisualBasic ? (SyntaxFormattingOptions)VisualBasicSyntaxFormattingOptions.Default : CSharpSyntaxFormattingOptions.Default;

var simplified = Simplifier.ReduceAsync(this.Document, Simplifier.Annotation).Result;
var actual = Formatter.FormatAsync(simplified, Formatter.Annotation, formattingOptions, CancellationToken.None).Result.GetSyntaxRootAsync().Result.ToFullString();

Assert.Equal(_expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using CS = Microsoft.CodeAnalysis.CSharp;
using VB = Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using System.Threading;

namespace Microsoft.CodeAnalysis.Editor.UnitTests.MetadataAsSource
{
Expand Down Expand Up @@ -1786,7 +1788,7 @@ public async Task FormatMetadataAsSource()
using var context = TestContext.Create(LanguageNames.CSharp);
var file = await context.GenerateSourceAsync("System.Console", project: context.DefaultProject);
var document = context.GetDocument(file);
await Formatter.FormatAsync(document);
await Formatter.FormatAsync(document, CSharpSyntaxFormattingOptions.Default, CancellationToken.None);
}

[WorkItem(530829, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/530829")]
Expand Down
16 changes: 11 additions & 5 deletions src/Tools/IdeCoreBenchmarks/FormatterBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class FormatterBenchmarks
private readonly int _iterationCount = 5;

private Document _document;
private OptionSet _options;
private SyntaxFormattingOptions _options;

[GlobalSetup]
public void GlobalSetup()
Expand Down Expand Up @@ -55,10 +55,16 @@ public void GlobalSetup()
solution = solution.WithDocumentSyntaxRoot(documentId, root);

_document = solution.GetDocument(documentId);
_options = _document.GetOptionsAsync().Result
.WithChangedOption(CSharpFormattingOptions.NewLinesForBracesInTypes, true)
.WithChangedOption(CSharpFormattingOptions.WrappingKeepStatementsOnSingleLine, false)
.WithChangedOption(CSharpFormattingOptions.WrappingPreserveSingleLine, false);
_options = new CSharpSyntaxFormattingOptions(
LineFormattingOptions.Default,
separateImportDirectiveGroups: CSharpSyntaxFormattingOptions.Default.SeparateImportDirectiveGroups,
spacing: CSharpSyntaxFormattingOptions.Default.Spacing,
spacingAroundBinaryOperator: CSharpSyntaxFormattingOptions.Default.SpacingAroundBinaryOperator,
CSharpSyntaxFormattingOptions.Default.NewLines | NewLinePlacement.BeforeOpenBraceInTypes,
labelPositioning: CSharpSyntaxFormattingOptions.Default.LabelPositioning,
indentation: CSharpSyntaxFormattingOptions.Default.Indentation,
wrappingKeepStatementsOnSingleLine: false,
wrappingPreserveSingleLine: false);
}

[Benchmark]
Expand Down
5 changes: 3 additions & 2 deletions src/VisualStudio/Core/Impl/CodeModel/FileCodeModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ internal void PerformEdit(Func<Document, Document> action)
var formatted = State.ThreadingContext.JoinableTaskFactory.Run(async () =>
{
var formatted = await Formatter.FormatAsync(result, Formatter.Annotation).ConfigureAwait(true);
formatted = await Formatter.FormatAsync(formatted, SyntaxAnnotation.ElasticAnnotation).ConfigureAwait(true);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(result, CancellationToken.None).ConfigureAwait(false);
var formatted = await Formatter.FormatAsync(result, Formatter.Annotation, formattingOptions, CancellationToken.None).ConfigureAwait(true);
formatted = await Formatter.FormatAsync(formatted, SyntaxAnnotation.ElasticAnnotation, formattingOptions, CancellationToken.None).ConfigureAwait(true);
return formatted;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ public void UpdatePreview(string text)
project = project.WithMetadataReferences(referenceAssemblies);

var document = project.AddDocument("document", SourceText.From(text, Encoding.UTF8));
var formatted = Formatter.FormatAsync(document, OptionStore.GetOptions()).WaitAndGetResult(CancellationToken.None);
var formattingOptions = SyntaxFormattingOptions.Create(OptionStore.GetOptions(), document.Project.Solution.Workspace.Services, document.Project.Language);
var formatted = Formatter.FormatAsync(document, formattingOptions, CancellationToken.None).WaitAndGetResult(CancellationToken.None);

var textBuffer = _textBufferFactoryService.CreateTextBuffer(formatted.GetTextSynchronously(CancellationToken.None).ToString(), _contentType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.EditorFeatures.DiagnosticsTests.Utilities" />
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.OmniSharp.CSharp"/>
<InternalsVisibleTo Include="Microsoft.CodeAnalysis.ExternalAccess.Razor"/>
<InternalsVisibleTo Include="IdeCoreBenchmarks"/>
<InternalsVisibleTo Include="Roslyn.VisualStudio.Next.UnitTests" />
<InternalsVisibleTo Include="Microsoft.VisualStudio.Completion.Tests" Key="$(IntelliCodeCSharpKey)" WorkItem="https://github.com/dotnet/roslyn/issues/35081" />
<RestrictedInternalsVisibleTo Include="Microsoft.VisualStudio.IntelliCode.CSharp" Partner="Pythia" Key="$(IntelliCodeCSharpKey)" />
Expand Down
3 changes: 2 additions & 1 deletion src/Workspaces/CoreTest/SolutionTests/SolutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
Expand Down Expand Up @@ -1816,7 +1817,7 @@ public async Task TestUpdateDocumentAsync()
.AddDocument(documentId, "DocumentName", SourceText.From("class Class{}"));

var document = solution1.GetDocument(documentId);
var newRoot = await Formatter.FormatAsync(document).Result.GetSyntaxRootAsync();
var newRoot = await Formatter.FormatAsync(document, CSharpSyntaxFormattingOptions.Default, CancellationToken.None).Result.GetSyntaxRootAsync();
var solution2 = solution1.WithDocumentSyntaxRoot(documentId, newRoot);

Assert.NotEqual(solution1, solution2);
Expand Down

0 comments on commit 2d69068

Please sign in to comment.