Skip to content
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

Remove unnecessary asynchronous Formatter operations #31413

Merged
merged 1 commit into from Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CodeStyle/Core/CodeFixes/FormattingCodeFixHelper.cs
Expand Up @@ -35,7 +35,7 @@ internal static async Task<SyntaxTree> FixOneAsync(SyntaxTree syntaxTree, Format
#if CODE_STYLE
var formattedRoot = Formatter.Format(root, formatterState, new[] { spanToFormat }, options, Formatter.GetDefaultFormattingRules(formatterState), cancellationToken);
#else
var formattedRoot = await Formatter.FormatAsync(root, spanToFormat, formatterState, options, cancellationToken).ConfigureAwait(false);
var formattedRoot = Formatter.Format(root, spanToFormat, formatterState, options, cancellationToken);
#endif

return syntaxTree.WithRootAndOptions(formattedRoot, syntaxTree.Options);
Expand Down
Expand Up @@ -98,9 +98,9 @@ public async Task<IList<TextChange>> GetFormattingChangesAsync(Document document

var span = textSpan ?? new TextSpan(0, root.FullSpan.Length);
var formattingSpan = CommonFormattingHelpers.GetFormattingSpan(root, span);
return await Formatter.GetFormattedTextChangesAsync(root,
return Formatter.GetFormattedTextChanges(root,
SpecializedCollections.SingletonEnumerable(formattingSpan),
document.Project.Solution.Workspace, options, cancellationToken).ConfigureAwait(false);
document.Project.Solution.Workspace, options, cancellationToken);
}

public async Task<IList<TextChange>> GetFormattingChangesOnPasteAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken)
Expand All @@ -118,7 +118,7 @@ public async Task<IList<TextChange>> GetFormattingChangesOnPasteAsync(Document d
var rules = new List<IFormattingRule>() { new PasteFormattingRule() };
rules.AddRange(service.GetDefaultFormattingRules());

return await Formatter.GetFormattedTextChangesAsync(root, SpecializedCollections.SingletonEnumerable(formattingSpan), document.Project.Solution.Workspace, options, rules, cancellationToken).ConfigureAwait(false);
return Formatter.GetFormattedTextChanges(root, SpecializedCollections.SingletonEnumerable(formattingSpan), document.Project.Solution.Workspace, options, rules, cancellationToken);
}

private IEnumerable<IFormattingRule> GetFormattingRules(Document document, int position, SyntaxToken tokenBeforeCaret)
Expand Down Expand Up @@ -350,7 +350,7 @@ private ISmartTokenFormatter CreateSmartTokenFormatter(OptionSet optionSet, IEnu

var formatter = new SmartTokenFormatter(options, formattingRules, (CompilationUnitSyntax)root);

var changes = await formatter.FormatRangeAsync(document.Project.Solution.Workspace, tokenRange.Value.Item1, tokenRange.Value.Item2, cancellationToken).ConfigureAwait(false);
var changes = formatter.FormatRange(document.Project.Solution.Workspace, tokenRange.Value.Item1, tokenRange.Value.Item2, cancellationToken);
return changes;
}

Expand Down
Expand Up @@ -40,7 +40,7 @@ internal class SmartTokenFormatter : ISmartTokenFormatter
_root = root;
}

public Task<IList<TextChange>> FormatRangeAsync(
public IList<TextChange> FormatRange(
Workspace workspace, SyntaxToken startToken, SyntaxToken endToken, CancellationToken cancellationToken)
{
Contract.ThrowIfTrue(startToken.Kind() == SyntaxKind.None || startToken.Kind() == SyntaxKind.EndOfFileToken);
Expand All @@ -58,7 +58,7 @@ internal class SmartTokenFormatter : ISmartTokenFormatter
smartTokenformattingRules = (new NoLineChangeFormattingRule()).Concat(_formattingRules);
}

return Formatter.GetFormattedTextChangesAsync(_root, new TextSpan[] { TextSpan.FromBounds(startToken.SpanStart, endToken.Span.End) }, workspace, _optionSet, smartTokenformattingRules, cancellationToken);
return Formatter.GetFormattedTextChanges(_root, new TextSpan[] { TextSpan.FromBounds(startToken.SpanStart, endToken.Span.End) }, workspace, _optionSet, smartTokenformattingRules, cancellationToken);
}

private bool CloseBraceOfTryOrDoBlock(SyntaxToken endToken)
Expand Down Expand Up @@ -111,9 +111,9 @@ private bool CloseBraceOfTryOrDoBlock(SyntaxToken endToken)
}
}

return await Formatter.GetFormattedTextChangesAsync(_root,
return Formatter.GetFormattedTextChanges(_root,
new TextSpan[] { TextSpan.FromBounds(adjustedStartPosition, adjustedEndPosition) },
workspace, _optionSet, smartTokenformattingRules, cancellationToken).ConfigureAwait(false);
workspace, _optionSet, smartTokenformattingRules, cancellationToken);
}

private class NoLineChangeFormattingRule : AbstractFormattingRule
Expand Down
Expand Up @@ -214,9 +214,9 @@ protected async Task AssertFormatWithBaseIndentAsync(string expected, string mar
/// </summary>
/// <param name="node">the <see cref="SyntaxNode"/> to format.</param>
/// <remarks>uses an <see cref="AdhocWorkspace"/> for formatting context, since the <paramref name="node"/> is not associated with a <see cref="SyntaxTree"/> </remarks>
protected async Task AssertFormatOnArbitraryNodeAsync(SyntaxNode node, string expected)
protected void AssertFormatOnArbitraryNode(SyntaxNode node, string expected)
{
var result = await Formatter.FormatAsync(node, new AdhocWorkspace());
var result = Formatter.Format(node, new AdhocWorkspace());
var actual = result.GetText().ToString();

Assert.Equal(expected, actual);
Expand Down
Expand Up @@ -1650,14 +1650,14 @@ public void M()

[WorkItem(11642, "https://github.com/dotnet/roslyn/issues/11642")]
[WpfFact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task FormatArbitraryNodeParenthesizedLambdaExpression()
public void FormatArbitraryNodeParenthesizedLambdaExpression()
{
// code equivalent to an expression synthesized like so:
// ParenthesizedExpression(ParenthesizedLambdaExpression(ParameterList(), Block()))
var code = @"(()=>{})";
var node = SyntaxFactory.ParseExpression(code);
var expected = @"(() => { })";
await AssertFormatOnArbitraryNodeAsync(node, expected);
AssertFormatOnArbitraryNode(node, expected);
}

[WorkItem(30787, "https://github.com/dotnet/roslyn/issues/30787")]
Expand Down
Expand Up @@ -3508,7 +3508,7 @@ private async Task AutoFormatOnMarkerAsync(string initialMarkup, string expected
return;
}

var changes = await formatter.FormatRangeAsync(workspace, tokenRange.Value.Item1, tokenRange.Value.Item2, CancellationToken.None).ConfigureAwait(false);
var changes = formatter.FormatRange(workspace, tokenRange.Value.Item1, tokenRange.Value.Item2, CancellationToken.None);
var actual = GetFormattedText(buffer, changes);
Assert.Equal(expected, actual);
}
Expand Down
Expand Up @@ -37,7 +37,7 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.Formatting.Indentation
Dim previousToken = token.GetPreviousToken()

Dim spans = SpecializedCollections.SingletonEnumerable(TextSpan.FromBounds(previousToken.SpanStart, token.Span.End))
Return Formatter.GetFormattedTextChangesAsync(_root, spans, workspace, _optionSet, _formattingRules, cancellationToken)
Return Task.FromResult(Formatter.GetFormattedTextChanges(_root, spans, workspace, _optionSet, _formattingRules, cancellationToken))
End Function
End Class
End Namespace
Expand Up @@ -18,26 +18,26 @@ namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.Suppression
[ExportSuppressionFixProvider(PredefinedCodeFixProviderNames.Suppression, LanguageNames.CSharp), Shared]
internal class CSharpSuppressionCodeFixProvider : AbstractSuppressionCodeFixProvider
{
protected override Task<SyntaxTriviaList> CreatePragmaRestoreDirectiveTriviaAsync(Diagnostic diagnostic, Func<SyntaxNode, Task<SyntaxNode>> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine)
protected override SyntaxTriviaList CreatePragmaRestoreDirectiveTrivia(Diagnostic diagnostic, Func<SyntaxNode, SyntaxNode> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine)
{
var restoreKeyword = SyntaxFactory.Token(SyntaxKind.RestoreKeyword);
return CreatePragmaDirectiveTriviaAsync(restoreKeyword, diagnostic, formatNode, needsLeadingEndOfLine, needsTrailingEndOfLine);
return CreatePragmaDirectiveTrivia(restoreKeyword, diagnostic, formatNode, needsLeadingEndOfLine, needsTrailingEndOfLine);
}

protected override Task<SyntaxTriviaList> CreatePragmaDisableDirectiveTriviaAsync(
Diagnostic diagnostic, Func<SyntaxNode, Task<SyntaxNode>> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine)
protected override SyntaxTriviaList CreatePragmaDisableDirectiveTrivia(
Diagnostic diagnostic, Func<SyntaxNode, SyntaxNode> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine)
{
var disableKeyword = SyntaxFactory.Token(SyntaxKind.DisableKeyword);
return CreatePragmaDirectiveTriviaAsync(disableKeyword, diagnostic, formatNode, needsLeadingEndOfLine, needsTrailingEndOfLine);
return CreatePragmaDirectiveTrivia(disableKeyword, diagnostic, formatNode, needsLeadingEndOfLine, needsTrailingEndOfLine);
}

private async Task<SyntaxTriviaList> CreatePragmaDirectiveTriviaAsync(
SyntaxToken disableOrRestoreKeyword, Diagnostic diagnostic, Func<SyntaxNode, Task<SyntaxNode>> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine)
private SyntaxTriviaList CreatePragmaDirectiveTrivia(
SyntaxToken disableOrRestoreKeyword, Diagnostic diagnostic, Func<SyntaxNode, SyntaxNode> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine)
{
var id = SyntaxFactory.IdentifierName(diagnostic.Id);
var ids = new SeparatedSyntaxList<ExpressionSyntax>().Add(id);
var pragmaDirective = SyntaxFactory.PragmaWarningDirectiveTrivia(disableOrRestoreKeyword, ids, true);
pragmaDirective = (PragmaWarningDirectiveTriviaSyntax)await formatNode(pragmaDirective).ConfigureAwait(false);
pragmaDirective = (PragmaWarningDirectiveTriviaSyntax)formatNode(pragmaDirective);
var pragmaDirectiveTrivia = SyntaxFactory.Trivia(pragmaDirective);
var endOfLineTrivia = SyntaxFactory.ElasticCarriageReturnLineFeed;
var triviaList = SyntaxFactory.TriviaList(pragmaDirectiveTrivia);
Expand Down Expand Up @@ -80,15 +80,15 @@ protected override bool IsEndOfLine(SyntaxTrivia trivia)
protected override bool IsEndOfFileToken(SyntaxToken token)
=> token.Kind() == SyntaxKind.EndOfFileToken;

protected override async Task<SyntaxNode> AddGlobalSuppressMessageAttributeAsync(SyntaxNode newRoot, ISymbol targetSymbol, Diagnostic diagnostic, Workspace workspace, CancellationToken cancellationToken)
protected override SyntaxNode AddGlobalSuppressMessageAttribute(SyntaxNode newRoot, ISymbol targetSymbol, Diagnostic diagnostic, Workspace workspace, CancellationToken cancellationToken)
{
var compilationRoot = (CompilationUnitSyntax)newRoot;
var isFirst = !compilationRoot.AttributeLists.Any();
var leadingTriviaForAttributeList = isFirst && !compilationRoot.HasLeadingTrivia ?
SyntaxFactory.TriviaList(SyntaxFactory.Comment(GlobalSuppressionsFileHeaderComment)) :
default;
var attributeList = CreateAttributeList(targetSymbol, diagnostic, leadingTrivia: leadingTriviaForAttributeList, needsLeadingEndOfLine: !isFirst);
attributeList = (AttributeListSyntax)await Formatter.FormatAsync(attributeList, workspace, cancellationToken: cancellationToken).ConfigureAwait(false);
attributeList = (AttributeListSyntax)Formatter.Format(attributeList, workspace, cancellationToken: cancellationToken);
return compilationRoot.AddAttributeLists(attributeList);
}

Expand Down
Expand Up @@ -77,7 +77,7 @@ private async Task<SyntaxNode> FormatResultAsync(Document document, CompilationU
var spans = new List<TextSpan>();
AddFormattingSpans(newRoot, spans, cancellationToken);
var options = await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
return await Formatter.FormatAsync(newRoot, spans, document.Project.Solution.Workspace, options, cancellationToken: cancellationToken).ConfigureAwait(false);
return Formatter.Format(newRoot, spans, document.Project.Solution.Workspace, options, cancellationToken: cancellationToken);
}

private void AddFormattingSpans(
Expand Down
Expand Up @@ -339,13 +339,13 @@ internal ChangeSignatureResult ChangeSignatureWithContext(ChangeSignatureAnalyze

var annotatedNodes = newRoot.GetAnnotatedNodes<SyntaxNode>(syntaxAnnotation: changeSignatureFormattingAnnotation);

var formattedRoot = Formatter.FormatAsync(
var formattedRoot = Formatter.Format(
newRoot,
changeSignatureFormattingAnnotation,
doc.Project.Solution.Workspace,
options: null,
rules: GetFormattingRules(doc),
cancellationToken: CancellationToken.None).WaitAndGetResult(CancellationToken.None);
cancellationToken: CancellationToken.None);

updatedRoots[docId] = formattedRoot;
}
Expand Down
Expand Up @@ -2,7 +2,6 @@

using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;

namespace Microsoft.CodeAnalysis.CodeFixes.Suppression
{
Expand All @@ -26,7 +25,7 @@ protected override async Task<Document> GetChangedSuppressionDocumentAsync(Cance
var workspace = suppressionsDoc.Project.Solution.Workspace;
var suppressionsRoot = await suppressionsDoc.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var semanticModel = await suppressionsDoc.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
suppressionsRoot = await Fixer.AddGlobalSuppressMessageAttributeAsync(suppressionsRoot, _targetSymbol, _diagnostic, workspace, cancellationToken).ConfigureAwait(false);
suppressionsRoot = Fixer.AddGlobalSuppressMessageAttribute(suppressionsRoot, _targetSymbol, _diagnostic, workspace, cancellationToken);
return suppressionsDoc.WithSyntaxRoot(suppressionsRoot);
}

Expand Down
Expand Up @@ -107,7 +107,7 @@ protected override async Task<Document> GetChangedSuppressionDocumentAsync(Cance
foreach (var diagnostic in diagnostics)
{
Contract.ThrowIfFalse(!diagnostic.IsSuppressed);
suppressionsRoot = await Fixer.AddGlobalSuppressMessageAttributeAsync(suppressionsRoot, targetSymbol, diagnostic, workspace, cancellationToken).ConfigureAwait(false);
suppressionsRoot = Fixer.AddGlobalSuppressMessageAttribute(suppressionsRoot, targetSymbol, diagnostic, workspace, cancellationToken);
}
}

Expand Down