提交 e9536ef1 编写于 作者: S Sam Harwell

Remove unnecessary asynchronous Formatter operations

上级 259cae04
......@@ -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);
......
......@@ -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)
......@@ -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)
......@@ -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;
}
......
......@@ -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);
......@@ -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)
......@@ -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
......
......@@ -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);
......
......@@ -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")]
......
......@@ -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);
}
......
......@@ -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
......@@ -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);
......@@ -80,7 +80,7 @@ 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();
......@@ -88,7 +88,7 @@ protected override async Task<SyntaxNode> AddGlobalSuppressMessageAttributeAsync
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);
}
......
......@@ -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(
......
......@@ -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;
}
......
......@@ -2,7 +2,6 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
namespace Microsoft.CodeAnalysis.CodeFixes.Suppression
{
......@@ -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);
}
......
......@@ -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);
}
}
......
......@@ -21,8 +21,8 @@ private static class PragmaHelpers
Document document,
TextSpan diagnosticSpan,
SuppressionTargetInfo suppressionTargetInfo,
Func<SyntaxToken, TextSpan, Task<SyntaxToken>> getNewStartToken,
Func<SyntaxToken, TextSpan, Task<SyntaxToken>> getNewEndToken,
Func<SyntaxToken, TextSpan, SyntaxToken> getNewStartToken,
Func<SyntaxToken, TextSpan, SyntaxToken> getNewEndToken,
CancellationToken cancellationToken)
{
var startToken = suppressionTargetInfo.StartToken;
......@@ -31,7 +31,7 @@ private static class PragmaHelpers
var root = await nodeWithTokens.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var startAndEndTokenAreTheSame = startToken == endToken;
SyntaxToken newStartToken = await getNewStartToken(startToken, diagnosticSpan).ConfigureAwait(false);
SyntaxToken newStartToken = getNewStartToken(startToken, diagnosticSpan);
SyntaxToken newEndToken = endToken;
if (startAndEndTokenAreTheSame)
......@@ -42,7 +42,7 @@ private static class PragmaHelpers
diagnosticSpan = new TextSpan(diagnosticSpan.Start + spanChange, diagnosticSpan.Length);
}
newEndToken = await getNewEndToken(newEndToken, diagnosticSpan).ConfigureAwait(false);
newEndToken = getNewEndToken(newEndToken, diagnosticSpan);
SyntaxNode newNode;
if (startAndEndTokenAreTheSame)
......@@ -95,12 +95,12 @@ private static int GetPositionForPragmaInsertion(ImmutableArray<SyntaxTrivia> tr
return index;
}
internal static async Task<SyntaxToken> GetNewStartTokenWithAddedPragmaAsync(
internal static SyntaxToken GetNewStartTokenWithAddedPragma(
SyntaxToken startToken,
TextSpan currentDiagnosticSpan,
Diagnostic diagnostic,
AbstractSuppressionCodeFixProvider fixer,
Func<SyntaxNode, Task<SyntaxNode>> formatNode,
Func<SyntaxNode, SyntaxNode> formatNode,
bool isRemoveSuppression = false)
{
var trivia = startToken.LeadingTrivia.ToImmutableArray();
......@@ -122,8 +122,8 @@ private static int GetPositionForPragmaInsertion(ImmutableArray<SyntaxTrivia> tr
}
var pragmaTrivia = !isRemoveSuppression
? await fixer.CreatePragmaDisableDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true).ConfigureAwait(false)
: await fixer.CreatePragmaRestoreDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true).ConfigureAwait(false);
? fixer.CreatePragmaDisableDirectiveTrivia(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true)
: fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, formatNode, needsLeadingEOL, needsTrailingEndOfLine: true);
return startToken.WithLeadingTrivia(trivia.InsertRange(index, pragmaTrivia));
}
......@@ -146,12 +146,12 @@ private static bool IsEndOfLineOrContainsEndOfLine(SyntaxTrivia trivia, Abstract
(trivia.HasStructure && trivia.GetStructure().DescendantTrivia().Any(t => fixer.IsEndOfLine(t)));
}
internal static async Task<SyntaxToken> GetNewEndTokenWithAddedPragmaAsync(
internal static SyntaxToken GetNewEndTokenWithAddedPragma(
SyntaxToken endToken,
TextSpan currentDiagnosticSpan,
Diagnostic diagnostic,
AbstractSuppressionCodeFixProvider fixer,
Func<SyntaxNode, Task<SyntaxNode>> formatNode,
Func<SyntaxNode, SyntaxNode> formatNode,
bool isRemoveSuppression = false)
{
ImmutableArray<SyntaxTrivia> trivia;
......@@ -182,8 +182,8 @@ private static bool IsEndOfLineOrContainsEndOfLine(SyntaxTrivia trivia, Abstract
}
var pragmaTrivia = !isRemoveSuppression
? await fixer.CreatePragmaRestoreDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL).ConfigureAwait(false)
: await fixer.CreatePragmaDisableDirectiveTriviaAsync(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL).ConfigureAwait(false);
? fixer.CreatePragmaRestoreDirectiveTrivia(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL)
: fixer.CreatePragmaDisableDirectiveTrivia(diagnostic, formatNode, needsLeadingEndOfLine: true, needsTrailingEndOfLine: needsTrailingEOL);
if (isEOF)
{
......
......@@ -60,16 +60,16 @@ public async Task<Document> GetChangedDocumentAsync(bool includeStartTokenChange
_document,
_diagnostic.Location.SourceSpan,
_suppressionTargetInfo,
async (startToken, currentDiagnosticSpan) =>
(startToken, currentDiagnosticSpan) =>
{
return includeStartTokenChange
? await PragmaHelpers.GetNewStartTokenWithAddedPragmaAsync(startToken, currentDiagnosticSpan, _diagnostic, Fixer, FormatNodeAsync).ConfigureAwait(false)
? PragmaHelpers.GetNewStartTokenWithAddedPragma(startToken, currentDiagnosticSpan, _diagnostic, Fixer, FormatNode)
: startToken;
},
async (endToken, currentDiagnosticSpan) =>
(endToken, currentDiagnosticSpan) =>
{
return includeEndTokenChange
? await PragmaHelpers.GetNewEndTokenWithAddedPragmaAsync(endToken, currentDiagnosticSpan, _diagnostic, Fixer, FormatNodeAsync).ConfigureAwait(false)
? PragmaHelpers.GetNewEndTokenWithAddedPragma(endToken, currentDiagnosticSpan, _diagnostic, Fixer, FormatNode)
: endToken;
},
cancellationToken).ConfigureAwait(false);
......@@ -78,9 +78,9 @@ public async Task<Document> GetChangedDocumentAsync(bool includeStartTokenChange
public SyntaxToken StartToken_TestOnly => _suppressionTargetInfo.StartToken;
public SyntaxToken EndToken_TestOnly => _suppressionTargetInfo.EndToken;
private Task<SyntaxNode> FormatNodeAsync(SyntaxNode node)
private SyntaxNode FormatNode(SyntaxNode node)
{
return Formatter.FormatAsync(node, _document.Project.Solution.Workspace);
return Formatter.Format(node, _document.Project.Solution.Workspace);
}
}
}
......
......@@ -80,13 +80,13 @@ public async Task<Document> GetChangedDocumentAsync(bool includeStartTokenChange
add = true;
}
Task<SyntaxToken> getNewStartToken(SyntaxToken startToken, TextSpan currentDiagnosticSpan) => includeStartTokenChange
? GetNewTokenWithModifiedPragmaAsync(startToken, currentDiagnosticSpan, add, toggle, indexOfLeadingPragmaDisableToRemove, isStartToken: true)
: Task.FromResult(startToken);
SyntaxToken getNewStartToken(SyntaxToken startToken, TextSpan currentDiagnosticSpan) => includeStartTokenChange
? GetNewTokenWithModifiedPragma(startToken, currentDiagnosticSpan, add, toggle, indexOfLeadingPragmaDisableToRemove, isStartToken: true)
: startToken;
Task<SyntaxToken> getNewEndToken(SyntaxToken endToken, TextSpan currentDiagnosticSpan) => includeEndTokenChange
? GetNewTokenWithModifiedPragmaAsync(endToken, currentDiagnosticSpan, add, toggle, indexOfTrailingPragmaEnableToRemove, isStartToken: false)
: Task.FromResult(endToken);
SyntaxToken getNewEndToken(SyntaxToken endToken, TextSpan currentDiagnosticSpan) => includeEndTokenChange
? GetNewTokenWithModifiedPragma(endToken, currentDiagnosticSpan, add, toggle, indexOfTrailingPragmaEnableToRemove, isStartToken: false)
: endToken;
return await PragmaHelpers.GetChangeDocumentWithPragmaAdjustedAsync(
_document,
......@@ -151,30 +151,28 @@ private static bool CanRemovePragmaTrivia(SyntaxToken token, Diagnostic diagnost
return false;
}
private Task<SyntaxToken> GetNewTokenWithModifiedPragmaAsync(SyntaxToken token, TextSpan currentDiagnosticSpan, bool add, bool toggle, int indexOfTriviaToRemoveOrToggle, bool isStartToken)
private SyntaxToken GetNewTokenWithModifiedPragma(SyntaxToken token, TextSpan currentDiagnosticSpan, bool add, bool toggle, int indexOfTriviaToRemoveOrToggle, bool isStartToken)
{
return add
? GetNewTokenWithAddedPragmaAsync(token, currentDiagnosticSpan, isStartToken)
: GetNewTokenWithRemovedOrToggledPragmaAsync(token, indexOfTriviaToRemoveOrToggle, isStartToken, toggle);
? GetNewTokenWithAddedPragma(token, currentDiagnosticSpan, isStartToken)
: GetNewTokenWithRemovedOrToggledPragma(token, indexOfTriviaToRemoveOrToggle, isStartToken, toggle);
}
private Task<SyntaxToken> GetNewTokenWithAddedPragmaAsync(SyntaxToken token, TextSpan currentDiagnosticSpan, bool isStartToken)
private SyntaxToken GetNewTokenWithAddedPragma(SyntaxToken token, TextSpan currentDiagnosticSpan, bool isStartToken)
{
if (isStartToken)
{
return PragmaHelpers.GetNewStartTokenWithAddedPragmaAsync(token, currentDiagnosticSpan, _diagnostic, Fixer, FormatNode, isRemoveSuppression: true);
return PragmaHelpers.GetNewStartTokenWithAddedPragma(token, currentDiagnosticSpan, _diagnostic, Fixer, FormatNode, isRemoveSuppression: true);
}
else
{
return PragmaHelpers.GetNewEndTokenWithAddedPragmaAsync(token, currentDiagnosticSpan, _diagnostic, Fixer, FormatNode, isRemoveSuppression: true);
return PragmaHelpers.GetNewEndTokenWithAddedPragma(token, currentDiagnosticSpan, _diagnostic, Fixer, FormatNode, isRemoveSuppression: true);
}
}
private Task<SyntaxToken> GetNewTokenWithRemovedOrToggledPragmaAsync(SyntaxToken token, int indexOfTriviaToRemoveOrToggle, bool isStartToken, bool toggle)
private SyntaxToken GetNewTokenWithRemovedOrToggledPragma(SyntaxToken token, int indexOfTriviaToRemoveOrToggle, bool isStartToken, bool toggle)
{
var result = GetNewTokenWithPragmaUnsuppress(token, indexOfTriviaToRemoveOrToggle, _diagnostic, Fixer, isStartToken, toggle);
return Task.FromResult(result);
return GetNewTokenWithPragmaUnsuppress(token, indexOfTriviaToRemoveOrToggle, _diagnostic, Fixer, isStartToken, toggle);
}
private static SyntaxToken GetNewTokenWithPragmaUnsuppress(SyntaxToken token, int indexOfTriviaToRemoveOrToggle, Diagnostic diagnostic, AbstractSuppressionCodeFixProvider fixer, bool isStartToken, bool toggle)
......@@ -217,9 +215,9 @@ private async Task<bool> IsDiagnosticSuppressedBeforeLeadingPragmaAsync(int inde
public SyntaxToken StartToken_TestOnly => _suppressionTargetInfo.StartToken;
public SyntaxToken EndToken_TestOnly => _suppressionTargetInfo.EndToken;
private Task<SyntaxNode> FormatNode(SyntaxNode node)
private SyntaxNode FormatNode(SyntaxNode node)
{
return Formatter.FormatAsync(node, _document.Project.Solution.Workspace);
return Formatter.Format(node, _document.Project.Solution.Workspace);
}
}
}
......
......@@ -41,10 +41,10 @@ public bool CanBeSuppressedOrUnsuppressed(Diagnostic diagnostic)
return SuppressionHelpers.CanBeSuppressed(diagnostic) || SuppressionHelpers.CanBeUnsuppressed(diagnostic);
}
protected abstract Task<SyntaxTriviaList> CreatePragmaDisableDirectiveTriviaAsync(Diagnostic diagnostic, Func<SyntaxNode, Task<SyntaxNode>> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine);
protected abstract Task<SyntaxTriviaList> CreatePragmaRestoreDirectiveTriviaAsync(Diagnostic diagnostic, Func<SyntaxNode, Task<SyntaxNode>> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine);
protected abstract SyntaxTriviaList CreatePragmaDisableDirectiveTrivia(Diagnostic diagnostic, Func<SyntaxNode, SyntaxNode> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine);
protected abstract SyntaxTriviaList CreatePragmaRestoreDirectiveTrivia(Diagnostic diagnostic, Func<SyntaxNode, SyntaxNode> formatNode, bool needsLeadingEndOfLine, bool needsTrailingEndOfLine);
protected abstract Task<SyntaxNode> AddGlobalSuppressMessageAttributeAsync(SyntaxNode newRoot, ISymbol targetSymbol, Diagnostic diagnostic, Workspace workspace, CancellationToken cancellationToken);
protected abstract SyntaxNode AddGlobalSuppressMessageAttribute(SyntaxNode newRoot, ISymbol targetSymbol, Diagnostic diagnostic, Workspace workspace, CancellationToken cancellationToken);
protected abstract string DefaultFileExtension { get; }
protected abstract string SingleLineCommentStart { get; }
......
......@@ -359,8 +359,7 @@ private static async Task<Solution> MergeDocumentChangesAsync(Solution originalS
.WithAdditionalAnnotations(Formatter.Annotation);
// Need to invoke formatter explicitly since we are doing the diff merge ourselves.
root = await Formatter.FormatAsync(root, Formatter.Annotation, documentWithAddedImports.Project.Solution.Workspace, optionSet, cancellationToken)
.ConfigureAwait(false);
root = Formatter.Format(root, Formatter.Annotation, documentWithAddedImports.Project.Solution.Workspace, optionSet, cancellationToken);
root = root.WithAdditionalAnnotations(Simplifier.Annotation);
var formattedDocument = documentWithAddedImports.WithSyntaxRoot(root);
......
......@@ -193,7 +193,7 @@ private async Task<Solution> ProcessResultAsync(CodeFixContext context, Diagnost
editor.RemoveNode(nodeToRemove, syntaxRemoveOptions);
var newRoot = editor.GetChangedRoot();
newRoot = await FormatAsync(newRoot, fieldDocument, cancellationToken).ConfigureAwait(false);
newRoot = Format(newRoot, fieldDocument, cancellationToken);
return solution.WithDocumentSyntaxRoot(fieldDocument.Id, newRoot);
}
......@@ -206,8 +206,8 @@ private async Task<Solution> ProcessResultAsync(CodeFixContext context, Diagnost
var newFieldTreeRoot = fieldTreeRoot.RemoveNode(nodeToRemove, syntaxRemoveOptions);
var newPropertyTreeRoot = propertyTreeRoot.ReplaceNode(property, updatedProperty);
newFieldTreeRoot = await FormatAsync(newFieldTreeRoot, fieldDocument, cancellationToken).ConfigureAwait(false);
newPropertyTreeRoot = await FormatAsync(newPropertyTreeRoot, propertyDocument, cancellationToken).ConfigureAwait(false);
newFieldTreeRoot = Format(newFieldTreeRoot, fieldDocument, cancellationToken);
newPropertyTreeRoot = Format(newPropertyTreeRoot, propertyDocument, cancellationToken);
updatedSolution = solution.WithDocumentSyntaxRoot(fieldDocument.Id, newFieldTreeRoot);
updatedSolution = updatedSolution.WithDocumentSyntaxRoot(propertyDocument.Id, newPropertyTreeRoot);
......@@ -256,7 +256,7 @@ private SyntaxRemoveOptions CreateSyntaxRemoveOptions(SyntaxNode nodeToRemove)
return canEdit[sourceTree];
}
private async Task<SyntaxNode> FormatAsync(SyntaxNode newRoot, Document document, CancellationToken cancellationToken)
private SyntaxNode Format(SyntaxNode newRoot, Document document, CancellationToken cancellationToken)
{
var formattingRules = GetFormattingRules(document);
if (formattingRules == null)
......@@ -264,7 +264,7 @@ private async Task<SyntaxNode> FormatAsync(SyntaxNode newRoot, Document document
return newRoot;
}
return await Formatter.FormatAsync(newRoot, SpecializedFormattingAnnotation, document.Project.Solution.Workspace, options: null, rules: formattingRules, cancellationToken: cancellationToken).ConfigureAwait(false);
return Formatter.Format(newRoot, SpecializedFormattingAnnotation, document.Project.Solution.Workspace, options: null, rules: formattingRules, cancellationToken: cancellationToken);
}
private static bool IsWrittenToOutsideOfConstructorOrProperty(
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
......@@ -9,7 +8,6 @@
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Simplification;
......@@ -58,11 +56,11 @@ internal abstract class AbstractUseConditionalExpressionCodeFixProvider<
// annotation on it.
var rules = new List<IFormattingRule> { GetMultiLineFormattingRule() };
var formattedRoot = await Formatter.FormatAsync(changedRoot,
var formattedRoot = Formatter.Format(changedRoot,
SpecializedFormattingAnnotation,
document.Project.Solution.Workspace,
await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false),
rules, cancellationToken).ConfigureAwait(false);
rules, cancellationToken);
changedRoot = formattedRoot;
editor.ReplaceNode(root, changedRoot);
......
......@@ -14,16 +14,16 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Suppression
Friend Class VisualBasicSuppressionCodeFixProvider
Inherits AbstractSuppressionCodeFixProvider
Protected Overrides Function CreatePragmaRestoreDirectiveTriviaAsync(diagnostic As Diagnostic, formatNode As Func(Of SyntaxNode, Task(Of SyntaxNode)), needsLeadingEndOfLine As Boolean, needsTrailingEndOfLine As Boolean) As Task(Of SyntaxTriviaList)
Protected Overrides Function CreatePragmaRestoreDirectiveTrivia(diagnostic As Diagnostic, formatNode As Func(Of SyntaxNode, SyntaxNode), needsLeadingEndOfLine As Boolean, needsTrailingEndOfLine As Boolean) As SyntaxTriviaList
Dim errorCodes = GetErrorCodes(diagnostic)
Dim pragmaDirective = SyntaxFactory.EnableWarningDirectiveTrivia(errorCodes)
Return CreatePragmaDirectiveTriviaAsync(pragmaDirective, diagnostic, formatNode, needsLeadingEndOfLine, needsTrailingEndOfLine)
Return CreatePragmaDirectiveTrivia(pragmaDirective, diagnostic, formatNode, needsLeadingEndOfLine, needsTrailingEndOfLine)
End Function
Protected Overrides Function CreatePragmaDisableDirectiveTriviaAsync(diagnostic As Diagnostic, formatNode As Func(Of SyntaxNode, Task(Of SyntaxNode)), needsLeadingEndOfLine As Boolean, needsTrailingEndOfLine As Boolean) As Task(Of SyntaxTriviaList)
Protected Overrides Function CreatePragmaDisableDirectiveTrivia(diagnostic As Diagnostic, formatNode As Func(Of SyntaxNode, SyntaxNode), needsLeadingEndOfLine As Boolean, needsTrailingEndOfLine As Boolean) As SyntaxTriviaList
Dim errorCodes = GetErrorCodes(diagnostic)
Dim pragmaDirective = SyntaxFactory.DisableWarningDirectiveTrivia(errorCodes)
Return CreatePragmaDirectiveTriviaAsync(pragmaDirective, diagnostic, formatNode, needsLeadingEndOfLine, needsTrailingEndOfLine)
Return CreatePragmaDirectiveTrivia(pragmaDirective, diagnostic, formatNode, needsLeadingEndOfLine, needsTrailingEndOfLine)
End Function
Private Shared Function GetErrorCodes(diagnostic As Diagnostic) As SeparatedSyntaxList(Of IdentifierNameSyntax)
......@@ -34,8 +34,8 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Suppression
Return New SeparatedSyntaxList(Of IdentifierNameSyntax)().Add(SyntaxFactory.IdentifierName(text))
End Function
Private Async Function CreatePragmaDirectiveTriviaAsync(enableOrDisablePragmaDirective As StructuredTriviaSyntax, diagnostic As Diagnostic, formatNode As Func(Of SyntaxNode, Task(Of SyntaxNode)), needsLeadingEndOfLine As Boolean, needsTrailingEndOfLine As Boolean) As Task(Of SyntaxTriviaList)
enableOrDisablePragmaDirective = CType(Await formatNode(enableOrDisablePragmaDirective).ConfigureAwait(False), StructuredTriviaSyntax)
Private Function CreatePragmaDirectiveTrivia(enableOrDisablePragmaDirective As StructuredTriviaSyntax, diagnostic As Diagnostic, formatNode As Func(Of SyntaxNode, SyntaxNode), needsLeadingEndOfLine As Boolean, needsTrailingEndOfLine As Boolean) As SyntaxTriviaList
enableOrDisablePragmaDirective = CType(formatNode(enableOrDisablePragmaDirective), StructuredTriviaSyntax)
Dim pragmaDirectiveTrivia = SyntaxFactory.Trivia(enableOrDisablePragmaDirective)
Dim endOfLineTrivia = SyntaxFactory.ElasticCarriageReturnLineFeed
Dim triviaList = SyntaxFactory.TriviaList(pragmaDirectiveTrivia)
......@@ -119,7 +119,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Suppression
Return token.Kind = SyntaxKind.EndOfFileToken
End Function
Protected Overrides Async Function AddGlobalSuppressMessageAttributeAsync(newRoot As SyntaxNode, targetSymbol As ISymbol, diagnostic As Diagnostic, workspace As Workspace, cancellationToken As CancellationToken) As Task(Of SyntaxNode)
Protected Overrides Function AddGlobalSuppressMessageAttribute(newRoot As SyntaxNode, targetSymbol As ISymbol, diagnostic As Diagnostic, workspace As Workspace, cancellationToken As CancellationToken) As SyntaxNode
Dim compilationRoot = DirectCast(newRoot, CompilationUnitSyntax)
Dim isFirst = Not compilationRoot.Attributes.Any()
Dim attributeList = CreateAttributeList(targetSymbol, diagnostic)
......@@ -134,7 +134,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.CodeFixes.Suppression
End If
End If
attributeStatement = CType(Await Formatter.FormatAsync(attributeStatement, workspace, cancellationToken:=cancellationToken).ConfigureAwait(False), AttributesStatementSyntax)
attributeStatement = CType(Formatter.Format(attributeStatement, workspace, cancellationToken:=cancellationToken), AttributesStatementSyntax)
Dim leadingTrivia = If(isFirst AndAlso Not compilationRoot.HasLeadingTrivia,
SyntaxFactory.TriviaList(SyntaxFactory.CommentTrivia(GlobalSuppressionsFileHeaderComment)),
......
......@@ -219,13 +219,13 @@ public static string GetEventHandlerMemberId(Document document, string className
var formattingRules = additionalFormattingRule.Concat(Formatter.GetDefaultFormattingRules(targetDocument));
newRoot = Formatter.FormatAsync(
newRoot = Formatter.Format(
newRoot,
Formatter.Annotation,
targetDocument.Project.Solution.Workspace,
targetDocument.GetOptionsAsync(cancellationToken).WaitAndGetResult_Venus(cancellationToken),
formattingRules,
cancellationToken).WaitAndGetResult_Venus(cancellationToken);
cancellationToken);
var newMember = newRoot.GetAnnotatedNodesAndTokens(annotation).Single();
var newMemberText = newMember.ToFullString();
......
......@@ -3297,7 +3297,7 @@ public void M()
[WorkItem(293, "https://github.com/dotnet/roslyn/issues/293")]
[Fact]
[Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task IntroduceBaseList()
public void IntroduceBaseList()
{
var text = @"
public class C
......@@ -3315,7 +3315,7 @@ public class C : IDisposable
var newDecl = Generator.AddInterfaceType(decl, Generator.IdentifierName("IDisposable"));
var newRoot = root.ReplaceNode(decl, newDecl);
var elasticOnlyFormatted = (await Formatter.FormatAsync(newRoot, SyntaxAnnotation.ElasticAnnotation, _ws)).ToFullString();
var elasticOnlyFormatted = Formatter.Format(newRoot, SyntaxAnnotation.ElasticAnnotation, _ws).ToFullString();
Assert.Equal(expected, elasticOnlyFormatted);
}
......
......@@ -15,7 +15,7 @@ public class FormattingEngineElasticTriviaTests : CSharpFormattingTestBase
{
[Fact(Skip = "530167")]
[Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task FormatElasticTrivia()
public void FormatElasticTrivia()
{
var expected = @"extern alias A1;
......@@ -85,14 +85,14 @@ class B
Assert.NotNull(compilation);
var newCompilation = await Formatter.FormatAsync(compilation, new AdhocWorkspace());
var newCompilation = Formatter.Format(compilation, new AdhocWorkspace());
Assert.Equal(expected, newCompilation.ToFullString());
}
[WorkItem(1947, "https://github.com/dotnet/roslyn/issues/1947")]
[Fact]
[Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task ElasticLineBreaksBetweenMembers()
public void ElasticLineBreaksBetweenMembers()
{
var text = @"
public class C
......@@ -126,20 +126,20 @@ public class C
public class SomeAttribute : System.Attribute { }
";
var formatted = (await Formatter.FormatAsync(newRoot, ws)).ToFullString();
var formatted = Formatter.Format(newRoot, ws).ToFullString();
Assert.Equal(expected, formatted);
var elasticOnlyFormatted = (await Formatter.FormatAsync(newRoot, SyntaxAnnotation.ElasticAnnotation, ws)).ToFullString();
var elasticOnlyFormatted = Formatter.Format(newRoot, SyntaxAnnotation.ElasticAnnotation, ws).ToFullString();
Assert.Equal(expected, elasticOnlyFormatted);
var annotationFormatted = (await Formatter.FormatAsync(newRoot, Formatter.Annotation, ws)).ToFullString();
var annotationFormatted = Formatter.Format(newRoot, Formatter.Annotation, ws).ToFullString();
Assert.Equal(expected, annotationFormatted);
}
[WorkItem(408, "https://roslyn.codeplex.com/workitem/408")]
[Fact]
[Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task FormatElasticTriviaBetweenPropertiesWithoutAccessors()
public void FormatElasticTriviaBetweenPropertiesWithoutAccessors()
{
var expected = @"class PropertyTest
{
......@@ -188,7 +188,7 @@ public async Task FormatElasticTriviaBetweenPropertiesWithoutAccessors()
Assert.NotNull(compilation);
var newCompilation = await Formatter.FormatAsync(compilation, new AdhocWorkspace());
var newCompilation = Formatter.Format(compilation, new AdhocWorkspace());
Assert.Equal(expected, newCompilation.ToFullString());
}
}
......
......@@ -165,7 +165,7 @@ public async Task EmptySpan()
var document = project.AddDocument("Document", SourceText.From(""));
var syntaxTree = await document.GetSyntaxTreeAsync();
var result = await Formatter.FormatAsync(await syntaxTree.GetRootAsync(), TextSpan.FromBounds(0, 0), workspace, cancellationToken: CancellationToken.None);
var result = Formatter.Format(await syntaxTree.GetRootAsync(), TextSpan.FromBounds(0, 0), workspace, cancellationToken: CancellationToken.None);
}
}
......
......@@ -4526,7 +4526,7 @@ public static void Main()
[Fact]
[WorkItem(543112, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/543112")]
[Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task FormatArbitaryNode()
public void FormatArbitaryNode()
{
var expected = @"public int Prop
{
......@@ -4561,7 +4561,7 @@ public async Task FormatArbitaryNode()
Assert.NotNull(property);
var newProperty = await Formatter.FormatAsync(property, new AdhocWorkspace());
var newProperty = Formatter.Format(property, new AdhocWorkspace());
Assert.Equal(expected, newProperty.ToFullString());
}
......@@ -7062,10 +7062,10 @@ static void Main(string[] args)
[WorkItem(1118, "https://github.com/dotnet/roslyn/issues/1118")]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task DontAssumeCertainNodeAreAlwaysParented()
public void DontAssumeCertainNodeAreAlwaysParented()
{
var block = SyntaxFactory.Block();
await Formatter.FormatAsync(block, new AdhocWorkspace());
Formatter.Format(block, new AdhocWorkspace());
}
[WorkItem(776, "https://github.com/dotnet/roslyn/issues/776")]
......@@ -8126,9 +8126,9 @@ public void M()
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
[WorkItem(25098, "https://github.com/dotnet/roslyn/issues/25098")]
public async Task FormatSingleStructDeclaration()
public void FormatSingleStructDeclaration()
{
await Formatter.FormatAsync(SyntaxFactory.StructDeclaration("S"), DefaultWorkspace);
Formatter.Format(SyntaxFactory.StructDeclaration("S"), DefaultWorkspace);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
using System.Linq;
using System.Threading.Tasks;
namespace Microsoft.CodeAnalysis.CSharp.UnitTests.Formatting
{
......@@ -44,8 +39,8 @@ public class C
{
void M([MyAttr] int? p) { }
}
", (await Formatter.FormatAsync(root.ReplaceNode(param, g.AddAttributes(param, g.Attribute("MyAttr"))),
document.Project.Solution.Workspace)).ToFullString());
", Formatter.Format(root.ReplaceNode(param, g.AddAttributes(param, g.Attribute("MyAttr"))),
document.Project.Solution.Workspace).ToFullString());
// verify change doesn't affect how attributes appear before other kinds of declarations
var method = root.DescendantNodes().OfType<MethodDeclarationSyntax>().First();
......@@ -55,8 +50,8 @@ public class C
[MyAttr]
void M(int? p) { }
}
", (await Formatter.FormatAsync(root.ReplaceNode(method, g.AddAttributes(method, g.Attribute("MyAttr"))),
document.Project.Solution.Workspace)).ToFullString());
", Formatter.Format(root.ReplaceNode(method, g.AddAttributes(method, g.Attribute("MyAttr"))),
document.Project.Solution.Workspace).ToFullString());
}
}
}
......@@ -1735,14 +1735,14 @@ static void Main(string[] args)
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task NewLineOptions_LineFeedOnly()
public void NewLineOptions_LineFeedOnly()
{
var tree = SyntaxFactory.ParseCompilationUnit("class C\r\n{\r\n}");
// replace all EOL trivia with elastic markers to force the formatter to add EOL back
tree = tree.ReplaceTrivia(tree.DescendantTrivia().Where(tr => tr.IsKind(SyntaxKind.EndOfLineTrivia)), (o, r) => SyntaxFactory.ElasticMarker);
var formatted = await Formatter.FormatAsync(tree, DefaultWorkspace, DefaultWorkspace.Options.WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, "\n"));
var formatted = Formatter.Format(tree, DefaultWorkspace, DefaultWorkspace.Options.WithChangedOption(FormattingOptions.NewLine, LanguageNames.CSharp, "\n"));
var actual = formatted.ToFullString();
var expected = "class C\n{\n}";
......@@ -1752,7 +1752,7 @@ public async Task NewLineOptions_LineFeedOnly()
[WorkItem(4019, "https://github.com/dotnet/roslyn/issues/4019")]
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task FormatWithTabs()
public void FormatWithTabs()
{
var code = @"#region Assembly mscorlib
// C:\
......@@ -1783,7 +1783,7 @@ class F
.WithLeadingTrivia(SyntaxFactory.TriviaList())
.WithAdditionalAnnotations(SyntaxAnnotation.ElasticAnnotation));
var formatted = await Formatter.FormatAsync(tree, DefaultWorkspace, DefaultWorkspace.Options.WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, true));
var formatted = Formatter.Format(tree, DefaultWorkspace, DefaultWorkspace.Options.WithChangedOption(FormattingOptions.UseTabs, LanguageNames.CSharp, true));
var actual = formatted.ToFullString();
Assert.Equal(expected, actual);
......
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
......@@ -19,7 +18,7 @@ public async Task<Document> CleanupAsync(Document document, ImmutableArray<TextS
if (document.TryGetText(out var oldText))
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var textChanges = await Formatter.GetFormattedTextChangesAsync(root, spans, document.Project.Solution.Workspace, cancellationToken: cancellationToken).ConfigureAwait(false);
var textChanges = Formatter.GetFormattedTextChanges(root, spans, document.Project.Solution.Workspace, cancellationToken: cancellationToken);
if (textChanges.Count == 0)
{
return document;
......@@ -37,8 +36,7 @@ public async Task<SyntaxNode> CleanupAsync(SyntaxNode root, ImmutableArray<TextS
// If the old text already exists, use the fast path for formatting.
if (root.SyntaxTree != null && root.SyntaxTree.TryGetText(out var oldText))
{
var changes = await Formatter.GetFormattedTextChangesAsync(root, spans, workspace, cancellationToken: cancellationToken).ConfigureAwait(false);
var changes = Formatter.GetFormattedTextChanges(root, spans, workspace, cancellationToken: cancellationToken);
if (changes.Count == 0)
{
return root;
......@@ -47,7 +45,7 @@ public async Task<SyntaxNode> CleanupAsync(SyntaxNode root, ImmutableArray<TextS
return await root.SyntaxTree.WithChangedText(oldText.WithChanges(changes)).GetRootAsync(cancellationToken).ConfigureAwait(false);
}
return await Formatter.FormatAsync(root, spans, workspace, cancellationToken: cancellationToken).ConfigureAwait(false);
return Formatter.Format(root, spans, workspace, cancellationToken: cancellationToken);
}
}
}
......@@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
......@@ -118,7 +117,7 @@ internal static async Task<Document> FormatAsync(Document document, IEnumerable<
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var documentOptions = options ?? await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
return document.WithSyntaxRoot(await FormatAsync(root, spans, document.Project.Solution.Workspace, documentOptions, rules, cancellationToken).ConfigureAwait(false));
return document.WithSyntaxRoot(Format(root, spans, document.Project.Solution.Workspace, documentOptions, rules, cancellationToken));
}
/// <summary>
......@@ -146,7 +145,7 @@ internal static async Task<Document> FormatAsync(Document document, SyntaxAnnota
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var documentOptions = options ?? await document.GetOptionsAsync(cancellationToken).ConfigureAwait(false);
return document.WithSyntaxRoot(await FormatAsync(root, annotation, document.Project.Solution.Workspace, documentOptions, rules, cancellationToken).ConfigureAwait(false));
return document.WithSyntaxRoot(Format(root, annotation, document.Project.Solution.Workspace, documentOptions, rules, cancellationToken));
}
/// <summary>
......@@ -159,17 +158,9 @@ internal static async Task<Document> FormatAsync(Document document, SyntaxAnnota
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The formatted tree's root node.</returns>
public static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
{
options = options ?? workspace.Options;
var resultTask = FormatAsync(node, annotation, workspace, options, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
=> Format(node, annotation, workspace, options, rules: null, cancellationToken: cancellationToken);
internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, SyntaxAnnotation annotation, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
=> FormatAsync(node, annotation, workspace, options, rules: null, cancellationToken: cancellationToken);
internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, SyntaxAnnotation annotation, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
internal static SyntaxNode Format(SyntaxNode node, SyntaxAnnotation annotation, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
{
if (workspace == null)
{
......@@ -190,7 +181,7 @@ internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, SyntaxAnnotation a
? GetElasticSpans(node)
: GetAnnotatedSpans(node, annotation);
return FormatAsync(node, spans, workspace, options, rules, cancellationToken);
return Format(node, spans, workspace, options, rules, cancellationToken);
}
/// <summary>
......@@ -202,15 +193,7 @@ internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, SyntaxAnnotation a
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The formatted tree's root node.</returns>
public static SyntaxNode Format(SyntaxNode node, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
{
options = options ?? workspace.Options;
var resultTask = FormatAsync(node, workspace, options, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
=> FormatAsync(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), workspace, options, rules: null, cancellationToken: cancellationToken);
=> Format(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), workspace, options, rules: null, cancellationToken: cancellationToken);
/// <summary>
/// Formats the whitespace in areas of a syntax tree identified by a span.
......@@ -222,15 +205,7 @@ internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, Workspace workspac
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The formatted tree's root node.</returns>
public static SyntaxNode Format(SyntaxNode node, TextSpan span, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
{
options = options ?? workspace.Options;
var resultTask = FormatAsync(node, span, workspace, options, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, TextSpan span, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
=> FormatAsync(node, SpecializedCollections.SingletonEnumerable(span), workspace, options, rules: null, cancellationToken: cancellationToken);
=> Format(node, SpecializedCollections.SingletonEnumerable(span), workspace, options, rules: null, cancellationToken: cancellationToken);
/// <summary>
/// Formats the whitespace in areas of a syntax tree identified by multiple non-overlapping spans.
......@@ -242,39 +217,15 @@ internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, TextSpan span, Wor
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The formatted tree's root node.</returns>
public static SyntaxNode Format(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
{
options = options ?? workspace.Options;
var resultTask = FormatAsync(node, spans, workspace, options, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal static Task<SyntaxNode> FormatAsync(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
=> FormatAsync(node, spans, workspace, options, rules: null, cancellationToken: cancellationToken);
=> Format(node, spans, workspace, options, rules: null, cancellationToken: cancellationToken);
internal static SyntaxNode Format(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
{
options = options ?? workspace.Options;
var resultTask = FormatAsync(node, spans, workspace, options, rules, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal static async Task<SyntaxNode> FormatAsync(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
{
var formattingResult = await GetFormattingResult(node, spans, workspace, options, rules, cancellationToken).ConfigureAwait(false);
var formattingResult = GetFormattingResult(node, spans, workspace, options, rules, cancellationToken);
return formattingResult == null ? node : formattingResult.GetFormattedRoot(cancellationToken);
}
internal static async Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
{
var formattingResult = await GetFormattingResult(node, spans, workspace, options, rules, cancellationToken).ConfigureAwait(false);
return formattingResult == null
? SpecializedCollections.EmptyList<TextChange>()
: formattingResult.GetTextChanges(cancellationToken);
}
internal static Task<IFormattingResult> GetFormattingResult(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
internal static IFormattingResult GetFormattingResult(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
{
if (workspace == null)
{
......@@ -289,13 +240,13 @@ internal static Task<IFormattingResult> GetFormattingResult(SyntaxNode node, IEn
var languageFormatter = workspace.Services.GetLanguageServices(node.Language).GetService<ISyntaxFormattingService>();
if (languageFormatter == null)
{
return SpecializedTasks.Default<IFormattingResult>();
return null;
}
options = options ?? workspace.Options;
rules = rules ?? GetDefaultFormattingRules(workspace, node.Language);
spans = spans ?? SpecializedCollections.SingletonEnumerable(node.FullSpan);
return Task.FromResult(languageFormatter.Format(node, spans, options, rules, cancellationToken));
return languageFormatter.Format(node, spans, options, rules, cancellationToken);
}
/// <summary>
......@@ -307,15 +258,7 @@ internal static Task<IFormattingResult> GetFormattingResult(SyntaxNode node, IEn
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The changes necessary to format the tree.</returns>
public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
{
options = options ?? workspace.Options;
var resultTask = GetFormattedTextChangesAsync(node, workspace, options, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal static Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode node, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
=> GetFormattedTextChangesAsync(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), workspace, options, rules: null, cancellationToken: cancellationToken);
=> GetFormattedTextChanges(node, SpecializedCollections.SingletonEnumerable(node.FullSpan), workspace, options, rules: null, cancellationToken: cancellationToken);
/// <summary>
/// Determines the changes necessary to format the whitespace of a syntax tree.
......@@ -327,15 +270,7 @@ internal static Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The changes necessary to format the tree.</returns>
public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, TextSpan span, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
{
options = options ?? workspace.Options;
var resultTask = GetFormattedTextChangesAsync(node, span, workspace, options, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal static Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode node, TextSpan span, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
=> GetFormattedTextChangesAsync(node, SpecializedCollections.SingletonEnumerable(span), workspace, options, rules: null, cancellationToken: cancellationToken);
=> GetFormattedTextChanges(node, SpecializedCollections.SingletonEnumerable(span), workspace, options, rules: null, cancellationToken: cancellationToken);
/// <summary>
/// Determines the changes necessary to format the whitespace of a syntax tree.
......@@ -347,22 +282,14 @@ internal static Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode
/// <param name="cancellationToken">An optional cancellation token.</param>
/// <returns>The changes necessary to format the tree.</returns>
public static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
{
options = options ?? workspace.Options;
var resultTask = GetFormattedTextChangesAsync(node, spans, workspace, options, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
}
internal static Task<IList<TextChange>> GetFormattedTextChangesAsync(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options = null, CancellationToken cancellationToken = default)
=> GetFormattedTextChangesAsync(node, spans, workspace, options, rules: null, cancellationToken: cancellationToken);
=> GetFormattedTextChanges(node, spans, workspace, options, rules: null, cancellationToken: cancellationToken);
internal static IList<TextChange> GetFormattedTextChanges(SyntaxNode node, IEnumerable<TextSpan> spans, Workspace workspace, OptionSet options, IEnumerable<IFormattingRule> rules, CancellationToken cancellationToken)
{
options = options ?? workspace.Options;
var resultTask = GetFormattedTextChangesAsync(node, spans, workspace, options, rules, cancellationToken);
Debug.Assert(resultTask.IsCompleted);
return resultTask.WaitAndGetResult_CanCallOnBackground(cancellationToken);
var formattingResult = GetFormattingResult(node, spans, workspace, options, rules, cancellationToken);
return formattingResult == null
? SpecializedCollections.EmptyList<TextChange>()
: formattingResult.GetTextChanges(cancellationToken);
}
private static IEnumerable<TextSpan> GetAnnotatedSpans(SyntaxNode node, SyntaxAnnotation annotation)
......
......@@ -17,10 +17,10 @@ public class SyntaxEditorTests
private Workspace EmptyWorkspace
=> _emptyWorkspace ?? (_emptyWorkspace = new AdhocWorkspace());
private async Task VerifySyntaxAsync<TSyntax>(SyntaxNode node, string expectedText) where TSyntax : SyntaxNode
private void VerifySyntax<TSyntax>(SyntaxNode node, string expectedText) where TSyntax : SyntaxNode
{
Assert.IsAssignableFrom(typeof(TSyntax), node);
var formatted = await Formatter.FormatAsync(node, EmptyWorkspace);
var formatted = Formatter.Format(node, EmptyWorkspace);
var actualText = formatted.ToFullString();
Assert.Equal(expectedText, actualText);
}
......@@ -31,7 +31,7 @@ private SyntaxEditor GetEditor(SyntaxNode root)
}
[Fact]
public async Task TestReplaceNode()
public void TestReplaceNode()
{
var code = @"
public class C
......@@ -47,7 +47,7 @@ public class C
editor.ReplaceNode(fieldX, editor.Generator.FieldDeclaration("Y", editor.Generator.TypeExpression(SpecialType.System_String), Accessibility.Public));
var newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
VerifySyntax<CompilationUnitSyntax>(
newRoot,
@"
public class C
......@@ -57,7 +57,7 @@ public class C
}
[Fact]
public async Task TestRemoveNode()
public void TestRemoveNode()
{
var code = @"
public class C
......@@ -73,7 +73,7 @@ public class C
editor.RemoveNode(fieldX);
var newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
VerifySyntax<CompilationUnitSyntax>(
newRoot,
@"
public class C
......@@ -82,7 +82,7 @@ public class C
}
[Fact]
public async Task TestInterAfter()
public void TestInterAfter()
{
var code = @"
public class C
......@@ -98,7 +98,7 @@ public class C
editor.InsertAfter(fieldX, editor.Generator.FieldDeclaration("Y", editor.Generator.TypeExpression(SpecialType.System_String), Accessibility.Public));
var newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
VerifySyntax<CompilationUnitSyntax>(
newRoot,
@"
public class C
......@@ -109,7 +109,7 @@ public class C
}
[Fact]
public async Task TestInterBefore()
public void TestInterBefore()
{
var code = @"
public class C
......@@ -125,7 +125,7 @@ public class C
editor.InsertBefore(fieldX, editor.Generator.FieldDeclaration("Y", editor.Generator.TypeExpression(SpecialType.System_String), Accessibility.Public));
var newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
VerifySyntax<CompilationUnitSyntax>(
newRoot,
@"
public class C
......@@ -157,7 +157,7 @@ public class C
}
[Fact]
public async Task TestMultipleEdits()
public void TestMultipleEdits()
{
var code = @"
public class C
......@@ -175,7 +175,7 @@ public class C
editor.RemoveNode(fieldX);
var newRoot = editor.GetChangedRoot();
await VerifySyntaxAsync<CompilationUnitSyntax>(
VerifySyntax<CompilationUnitSyntax>(
newRoot,
@"
public class C
......
......@@ -14,12 +14,12 @@ namespace Microsoft.CodeAnalysis.UnitTests
public partial class FormattingTests : TestBase
{
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task TestCSharpFormatting()
public void TestCSharpFormatting()
{
var text = @"public class C{public int X;}";
var expectedFormattedText = @"public class C { public int X; }";
await AssertFormatCSharpAsync(expectedFormattedText, text);
AssertFormatCSharp(expectedFormattedText, text);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
......@@ -32,7 +32,7 @@ public void TestCSharpDefaultRules()
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
public async Task TestVisualBasicFormatting()
public void TestVisualBasicFormatting()
{
var text = @"
Public Class C
......@@ -45,7 +45,7 @@ End Class
End Class
";
await AssertFormatVBAsync(expectedFormattedText, text);
AssertFormatVB(expectedFormattedText, text);
}
[Fact, Trait(Traits.Feature, Traits.Features.Formatting)]
......@@ -57,23 +57,23 @@ public void TestVisualBasicDefaultFormattingRules()
Assert.NotEmpty(rules);
}
private Task AssertFormatCSharpAsync(string expected, string input)
private void AssertFormatCSharp(string expected, string input)
{
var tree = CS.SyntaxFactory.ParseSyntaxTree(input);
return AssertFormatAsync(expected, tree);
AssertFormat(expected, tree);
}
private Task AssertFormatVBAsync(string expected, string input)
private void AssertFormatVB(string expected, string input)
{
var tree = VB.SyntaxFactory.ParseSyntaxTree(input);
return AssertFormatAsync(expected, tree);
AssertFormat(expected, tree);
}
private async Task AssertFormatAsync(string expected, SyntaxTree tree)
private void AssertFormat(string expected, SyntaxTree tree)
{
using (var workspace = new AdhocWorkspace())
{
var formattedRoot = await Formatter.FormatAsync(tree.GetRoot(), workspace);
var formattedRoot = Formatter.Format(tree.GetRoot(), workspace);
var actualFormattedText = formattedRoot.ToFullString();
Assert.Equal(expected, actualFormattedText);
......
......@@ -57,19 +57,19 @@ public abstract class FormattingTestBase
}
var root = await syntaxTree.GetRootAsync();
await AssertFormatAsync(workspace, expected, root, spans, options, await document.GetTextAsync());
AssertFormat(workspace, expected, root, spans, options, await document.GetTextAsync());
// format with node and transform
await AssertFormatWithTransformationAsync(workspace, expected, root, spans, options, treeCompare, parseOptions);
AssertFormatWithTransformation(workspace, expected, root, spans, options, treeCompare, parseOptions);
}
}
protected abstract SyntaxNode ParseCompilation(string text, ParseOptions parseOptions);
protected async Task AssertFormatWithTransformationAsync(
protected void AssertFormatWithTransformation(
Workspace workspace, string expected, SyntaxNode root, IEnumerable<TextSpan> spans, OptionSet optionSet, bool treeCompare = true, ParseOptions parseOptions = null)
{
var newRootNode = await Formatter.FormatAsync(root, spans, workspace, optionSet, CancellationToken.None);
var newRootNode = Formatter.Format(root, spans, workspace, optionSet, CancellationToken.None);
Assert.Equal(expected, newRootNode.ToFullString());
......@@ -83,9 +83,9 @@ public abstract class FormattingTestBase
}
}
protected static async Task AssertFormatAsync(Workspace workspace, string expected, SyntaxNode root, IEnumerable<TextSpan> spans, OptionSet optionSet, SourceText sourceText)
protected static void AssertFormat(Workspace workspace, string expected, SyntaxNode root, IEnumerable<TextSpan> spans, OptionSet optionSet, SourceText sourceText)
{
var result = await Formatter.GetFormattedTextChangesAsync(root, spans, workspace, optionSet);
var result = Formatter.GetFormattedTextChanges(root, spans, workspace, optionSet);
AssertResult(expected, sourceText, result);
}
......
......@@ -2797,14 +2797,14 @@ End Class</Code>
Dim root = Await document.GetSyntaxRootAsync()
' format first time
Dim result = Await Formatter.GetFormattedTextChangesAsync(root, workspace)
Dim result = Formatter.GetFormattedTextChanges(root, workspace)
AssertResult(My.Resources.XmlLiterals.XmlTest4_Input_Output, Await document.GetTextAsync(), result)
Dim document2 = document.WithText((Await document.GetTextAsync()).WithChanges(result))
Dim root2 = Await document2.GetSyntaxRootAsync()
' format second time
Dim result2 = Await Formatter.GetFormattedTextChangesAsync(root, workspace)
Dim result2 = Formatter.GetFormattedTextChanges(root, workspace)
AssertResult(My.Resources.XmlLiterals.XmlTest4_Input_Output, Await document2.GetTextAsync(), result2)
End Using
End Function
......@@ -3452,7 +3452,7 @@ End Module</Code>
End Function
<Fact, Trait(Traits.Feature, Traits.Features.Formatting)>
Public Async Function ElasticNewlines() As Task
Public Sub ElasticNewlines()
Dim text = <text>Class C
Implements INotifyPropertyChanged
Dim _p As Integer
......@@ -3510,10 +3510,10 @@ End Class</text>.Value.Replace(vbLf, vbCrLf)
root = root.ReplaceNode(method, method.NormalizeWhitespace(elasticTrivia:=True).WithAdditionalAnnotations(goo))
Using workspace = New AdhocWorkspace()
Dim result = (Await Formatter.FormatAsync(root, goo, workspace)).ToString()
Dim result = Formatter.Format(root, goo, workspace).ToString()
Assert.Equal(expected, result)
End Using
End Function
End Sub
<Fact>
<WorkItem(545630, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/545630")>
......@@ -4327,19 +4327,19 @@ End Class
End Function
<Fact, Trait(Traits.Feature, Traits.Features.Formatting)>
Public Async Function NewLineOption_LineFeedOnly() As Task
Public Sub NewLineOption_LineFeedOnly()
Dim tree = SyntaxFactory.ParseCompilationUnit("Class C" & vbCrLf & "End Class")
' replace all EOL trivia with elastic markers to force the formatter to add EOL back
tree = tree.ReplaceTrivia(tree.DescendantTrivia().Where(Function(tr) tr.IsKind(SyntaxKind.EndOfLineTrivia)), Function(o, r) SyntaxFactory.ElasticMarker)
Dim formatted = Await Formatter.FormatAsync(tree, DefaultWorkspace, DefaultWorkspace.Options.WithChangedOption(FormattingOptions.NewLine, LanguageNames.VisualBasic, vbLf))
Dim formatted = Formatter.Format(tree, DefaultWorkspace, DefaultWorkspace.Options.WithChangedOption(FormattingOptions.NewLine, LanguageNames.VisualBasic, vbLf))
Dim actual = formatted.ToFullString()
Dim expected = "Class C" & vbLf & "End Class"
Assert.Equal(expected, actual)
End Function
End Sub
<Fact, Trait(Traits.Feature, Traits.Features.Formatting)>
<WorkItem(2822, "https://github.com/dotnet/roslyn/issues/2822")>
......
......@@ -59,20 +59,20 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.UnitTests.Formatting
Dim spans = New List(Of TextSpan)()
spans.Add(syntaxTree.GetRoot().FullSpan)
Dim changes = Await Formatter.GetFormattedTextChangesAsync(Await syntaxTree.GetRootAsync(), workspace, cancellationToken:=CancellationToken.None).ConfigureAwait(True)
Dim changes = Formatter.GetFormattedTextChanges(Await syntaxTree.GetRootAsync(), workspace, cancellationToken:=CancellationToken.None)
AssertResult(expected, Await document.GetTextAsync(), changes)
changes = Await Formatter.GetFormattedTextChangesAsync(Await syntaxTree.GetRootAsync(), (Await syntaxTree.GetRootAsync()).FullSpan, workspace, cancellationToken:=CancellationToken.None).ConfigureAwait(True)
changes = Formatter.GetFormattedTextChanges(Await syntaxTree.GetRootAsync(), (Await syntaxTree.GetRootAsync()).FullSpan, workspace, cancellationToken:=CancellationToken.None)
AssertResult(expected, Await document.GetTextAsync(), changes)
spans = New List(Of TextSpan)()
spans.Add(syntaxTree.GetRoot().FullSpan)
changes = Await Formatter.GetFormattedTextChangesAsync(Await syntaxTree.GetRootAsync(), spans, workspace, cancellationToken:=CancellationToken.None).ConfigureAwait(True)
changes = Formatter.GetFormattedTextChanges(Await syntaxTree.GetRootAsync(), spans, workspace, cancellationToken:=CancellationToken.None)
AssertResult(expected, Await document.GetTextAsync(), changes)
' format with node and transform
Await AssertFormatWithTransformationAsync(workspace, expected, syntaxTree.GetRoot(), spans, Nothing, False)
AssertFormatWithTransformation(workspace, expected, syntaxTree.GetRoot(), spans, Nothing, False)
End Using
End Function
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册