提交 b80b7040 编写于 作者: C CyrusNajmabadi

Make 'SplitStringLiteral' fully synchronous.

上级 1b69ef7a
......@@ -24,12 +24,12 @@ internal partial class CSharpIndentationService
internal class Indenter : AbstractIndenter
{
public Indenter(
SyntacticDocument document,
SyntaxTree syntaxTree,
IEnumerable<IFormattingRule> rules,
OptionSet optionSet,
TextLine line,
CancellationToken cancellationToken) :
base(document, rules, optionSet, line, cancellationToken)
base(syntaxTree, rules, optionSet, line, cancellationToken)
{
}
......
......@@ -30,11 +30,10 @@ protected override IFormattingRule GetSpecializedIndentationFormattingRule()
return s_instance;
}
protected override async Task<AbstractIndenter> GetIndenterAsync(
Document document, TextLine lineToBeIndented, IEnumerable<IFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken)
protected override AbstractIndenter GetIndenter(
SyntaxTree syntaxTree, TextLine lineToBeIndented, IEnumerable<IFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken)
{
var syntacticDocument = await SyntacticDocument.CreateAsync(document, cancellationToken).ConfigureAwait(false);
return new Indenter(syntacticDocument, formattingRules, optionSet, lineToBeIndented, cancellationToken);
return new Indenter(syntaxTree, formattingRules, optionSet, lineToBeIndented, cancellationToken);
}
protected override bool ShouldUseSmartTokenFormatterInsteadOfIndenter(
......
......@@ -15,10 +15,10 @@ private class InterpolatedStringSplitter : StringSplitter
public InterpolatedStringSplitter(
Document document, int position,
SyntaxTree syntaxTree, SyntaxNode root, SourceText sourceText,
SyntaxNode root, SourceText sourceText,
InterpolatedStringExpressionSyntax interpolatedStringExpression,
bool useTabs, int tabSize, CancellationToken cancellationToken)
: base(document, position, syntaxTree, root, sourceText, useTabs, tabSize, cancellationToken)
: base(document, position, root, sourceText, useTabs, tabSize, cancellationToken)
{
_interpolatedStringExpression = interpolatedStringExpression;
}
......
......@@ -12,8 +12,8 @@ private class SimpleStringSplitter : StringSplitter
private const char QuoteCharacter = '"';
private readonly SyntaxToken _token;
public SimpleStringSplitter(Document document, int position, SyntaxTree syntaxTree, SyntaxNode root, SourceText sourceText, SyntaxToken token, bool useTabs, int tabSize, CancellationToken cancellationToken)
: base(document, position, syntaxTree, root, sourceText, useTabs, tabSize, cancellationToken)
public SimpleStringSplitter(Document document, int position, SyntaxNode root, SourceText sourceText, SyntaxToken token, bool useTabs, int tabSize, CancellationToken cancellationToken)
: base(document, position, root, sourceText, useTabs, tabSize, cancellationToken)
{
_token = token;
}
......
......@@ -19,17 +19,15 @@ private abstract class StringSplitter
protected readonly Document Document;
protected readonly int CursorPosition;
protected readonly SourceText SourceText;
protected readonly SyntaxTree SyntaxTree;
protected readonly SyntaxNode Root;
protected readonly int TabSize;
protected readonly bool UseTabs;
protected readonly CancellationToken CancellationToken;
public StringSplitter(Document document, int position, SyntaxTree syntaxTree, SyntaxNode root, SourceText sourceText, bool useTabs, int tabSize, CancellationToken cancellationToken)
public StringSplitter(Document document, int position, SyntaxNode root, SourceText sourceText, bool useTabs, int tabSize, CancellationToken cancellationToken)
{
Document = document;
CursorPosition = position;
SyntaxTree = syntaxTree;
Root = root;
SourceText = sourceText;
UseTabs = useTabs;
......@@ -39,7 +37,7 @@ public StringSplitter(Document document, int position, SyntaxTree syntaxTree, Sy
public static StringSplitter Create(
Document document, int position,
SyntaxTree syntaxTree, SyntaxNode root, SourceText sourceText,
SyntaxNode root, SourceText sourceText,
bool useTabs, int tabSize, CancellationToken cancellationToken)
{
var token = root.FindToken(position);
......@@ -47,7 +45,7 @@ public StringSplitter(Document document, int position, SyntaxTree syntaxTree, Sy
if (token.IsKind(SyntaxKind.StringLiteralToken))
{
return new SimpleStringSplitter(
document, position, syntaxTree, root,
document, position, root,
sourceText, token, useTabs, tabSize,
cancellationToken);
}
......@@ -56,7 +54,7 @@ public StringSplitter(Document document, int position, SyntaxTree syntaxTree, Sy
if (interpolatedStringExpression != null)
{
return new InterpolatedStringSplitter(
document, position, syntaxTree, root,
document, position, root,
sourceText, interpolatedStringExpression,
useTabs, tabSize, cancellationToken);
}
......@@ -92,19 +90,19 @@ private static bool IsInterpolationOpenBrace(SyntaxToken token, int position)
protected abstract BinaryExpressionSyntax CreateSplitString();
public async Task<int?> TrySplitAsync()
public int? TrySplit()
{
if (!CheckToken())
{
return null;
}
return await TrySplitWorkerAsync().ConfigureAwait(false);
return TrySplitWorker();
}
private async Task<int?> TrySplitWorkerAsync()
private int? TrySplitWorker()
{
var newDocumentAndCaretPosition = await SplitStringAsync().ConfigureAwait(false);
var newDocumentAndCaretPosition = SplitString();
if (newDocumentAndCaretPosition == null)
{
return null;
......@@ -127,7 +125,7 @@ protected static SyntaxToken GetPlusToken()
SyntaxFactory.TriviaList(SyntaxFactory.ElasticCarriageReturnLineFeed));
}
private async Task<Tuple<Document, int>> SplitStringAsync()
private Tuple<Document, int> SplitString()
{
var splitString = CreateSplitString();
......@@ -135,7 +133,7 @@ protected static SyntaxToken GetPlusToken()
var newRoot = Root.ReplaceNode(nodeToReplace, splitString);
var rightExpression = newRoot.GetAnnotatedNodes(RightNodeAnnotation).Single();
var indentString = await GetIndentStringAsync(newRoot).ConfigureAwait(false);
var indentString = GetIndentString(newRoot);
if (indentString == null)
{
return null;
......@@ -148,21 +146,21 @@ protected static SyntaxToken GetPlusToken()
return Tuple.Create(newDocument2, rightExpression.Span.Start + indentString.Length + StringOpenQuoteLength());
}
private async Task<string> GetIndentStringAsync(SyntaxNode newRoot)
private string GetIndentString(SyntaxNode newRoot)
{
var newDocument = Document.WithSyntaxRoot(newRoot);
var indentationService = newDocument.GetLanguageService<IIndentationService>();
var originalLineNumber = SourceText.Lines.GetLineFromPosition(CursorPosition).LineNumber;
var desiredIndentation = await indentationService.GetDesiredIndentationAsync(
newDocument, originalLineNumber + 1, CancellationToken).ConfigureAwait(false);
var desiredIndentation = indentationService.GetDesiredIndentation(
newDocument, originalLineNumber + 1, CancellationToken);
if (desiredIndentation == null)
{
return null;
}
var newSourceText = await newDocument.GetTextAsync(CancellationToken).ConfigureAwait(false);
var newSourceText = newDocument.GetSyntaxRootSynchronously(CancellationToken).SyntaxTree.GetText(CancellationToken);
var baseLine = newSourceText.Lines.GetLineFromPosition(desiredIndentation.Value.BasePosition);
var baseOffsetInLine = desiredIndentation.Value.BasePosition - baseLine.Start;
......
......@@ -57,8 +57,8 @@ private bool SplitString(ITextView textView, ITextBuffer subjectBuffer, Snapshot
if (enabled)
{
var cursorPosition = SplitStringLiteralAsync(
subjectBuffer, document, caret.Value.Position, CancellationToken.None).GetAwaiter().GetResult();
var cursorPosition = SplitStringLiteral(
subjectBuffer, document, caret.Value.Position, CancellationToken.None);
if (cursorPosition != null)
{
......@@ -95,23 +95,22 @@ private bool LineContainsQuote(ITextSnapshotLine line, int caretPosition)
return false;
}
private async Task<int?> SplitStringLiteralAsync(
private int? SplitStringLiteral(
ITextBuffer subjectBuffer, Document document, int position, CancellationToken cancellationToken)
{
var useTabs = subjectBuffer.GetOption(FormattingOptions.UseTabs);
var tabSize = subjectBuffer.GetOption(FormattingOptions.TabSize);
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
var root = await syntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
var sourceText = await syntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);
var root = document.GetSyntaxRootSynchronously(cancellationToken);
var sourceText = root.SyntaxTree.GetText(cancellationToken);
var splitter = StringSplitter.Create(document, position, syntaxTree, root, sourceText, useTabs, tabSize, cancellationToken);
var splitter = StringSplitter.Create(document, position, root, sourceText, useTabs, tabSize, cancellationToken);
if (splitter == null)
{
return null;
}
return await splitter.TrySplitAsync().ConfigureAwait(false);
return splitter.TrySplit();
}
}
}
\ No newline at end of file
......@@ -118,7 +118,7 @@ private static void ApplyChanges(ITextBuffer buffer, IList<TextChange> changes)
}
}
internal static async Task TestIndentationAsync(int point, int? expectedIndentation, ITextView textView, TestHostDocument subjectDocument)
internal static void TestIndentation(int point, int? expectedIndentation, ITextView textView, TestHostDocument subjectDocument)
{
var textUndoHistory = new Mock<ITextUndoHistoryRegistry>();
var editorOperationsFactory = new Mock<IEditorOperationsFactoryService>();
......@@ -140,13 +140,13 @@ internal static async Task TestIndentationAsync(int point, int? expectedIndentat
else
{
var provider = new SmartIndent(textView);
actualIndentation = await provider.GetDesiredIndentationAsync(indentationLineFromBuffer);
actualIndentation = provider.GetDesiredIndentation(indentationLineFromBuffer);
}
Assert.Equal(expectedIndentation, actualIndentation.Value);
}
public static async Task TestIndentationAsync(int indentationLine, int? expectedIndentation, TestWorkspace workspace)
public static void TestIndentation(int indentationLine, int? expectedIndentation, TestWorkspace workspace)
{
var snapshot = workspace.Documents.First().TextBuffer.CurrentSnapshot;
var bufferGraph = new Mock<IBufferGraph>(MockBehavior.Strict);
......@@ -180,7 +180,7 @@ public static async Task TestIndentationAsync(int indentationLine, int? expected
var provider = new SmartIndent(textView.Object);
var indentationLineFromBuffer = snapshot.GetLineFromLineNumber(indentationLine);
var actualIndentation = await provider.GetDesiredIndentationAsync(indentationLineFromBuffer);
var actualIndentation = provider.GetDesiredIndentation(indentationLineFromBuffer);
Assert.Equal(expectedIndentation, actualIndentation);
}
......
......@@ -1395,7 +1395,7 @@ void Main(object o)
Formatter.GetDefaultFormattingRules(workspace, root.Language),
root, line.AsTextLine(), document.Options, CancellationToken.None));
await TestIndentationAsync(indentationLine, expectedIndentation, workspace);
TestIndentation(indentationLine, expectedIndentation, workspace);
}
}
}
......
......@@ -2709,7 +2709,7 @@ private static async Task AssertSmartIndentInProjectionAsync(string markup, int
var indentationLine = projectedDocument.TextBuffer.CurrentSnapshot.GetLineFromPosition(projectedDocument.CursorPosition.Value);
var point = projectedDocument.GetTextView().BufferGraph.MapDownToBuffer(indentationLine.Start, PointTrackingMode.Negative, subjectDocument.TextBuffer, PositionAffinity.Predecessor);
await TestIndentationAsync(point.Value, expectedIndentation, projectedDocument.GetTextView(), subjectDocument);
TestIndentation(point.Value, expectedIndentation, projectedDocument.GetTextView(), subjectDocument);
}
}
}
......@@ -2728,7 +2728,7 @@ private static async Task AssertSmartIndentInProjectionAsync(string markup, int
{
using (var workspace = await TestWorkspace.CreateCSharpAsync(code, parseOptions: option))
{
await TestIndentationAsync(indentationLine, expectedIndentation, workspace);
TestIndentation(indentationLine, expectedIndentation, workspace);
}
}
}
......@@ -2748,7 +2748,7 @@ private static async Task AssertSmartIndentInProjectionAsync(string markup, int
{
var wpfTextView = workspace.Documents.First().GetTextView();
var line = wpfTextView.TextBuffer.CurrentSnapshot.GetLineFromPosition(wpfTextView.Caret.Position.BufferPosition).LineNumber;
await TestIndentationAsync(line, expectedIndentation, workspace);
TestIndentation(line, expectedIndentation, workspace);
}
}
}
......
......@@ -129,8 +129,8 @@ internal void ExecuteCommandWorker(ReturnKeyCommandArgs args, CancellationToken
}
var indentationService = document.GetLanguageService<IIndentationService>();
var indentation = indentationService.GetDesiredIndentationAsync(document,
currentPosition.GetContainingLine().LineNumber, cancellationToken).WaitAndGetResult(cancellationToken);
var indentation = indentationService.GetDesiredIndentation(document,
currentPosition.GetContainingLine().LineNumber, cancellationToken);
// looks like we can't.
if (!indentation.HasValue)
......
......@@ -34,20 +34,19 @@ internal abstract class AbstractIndenter
(tk.LeadingTrivia.Any(tr => tr.IsDirective) || tk.TrailingTrivia.Any(tr => tr.IsDirective));
public AbstractIndenter(
SyntacticDocument document,
SyntaxTree syntaxTree,
IEnumerable<IFormattingRule> rules,
OptionSet optionSet,
TextLine lineToBeIndented,
CancellationToken cancellationToken)
{
this.OptionSet = optionSet;
this.Document = document;
this.Tree = syntaxTree;
this.LineToBeIndented = lineToBeIndented;
this.TabSize = this.OptionSet.GetOption(FormattingOptions.TabSize, this.Document.Root.Language);
this.CancellationToken = cancellationToken;
this.Rules = rules;
this.Tree = this.Document.SyntaxTree;
this.Finder = new BottomUpBaseIndentationFinder(
new ChainedFormattingRules(this.Rules, OptionSet),
this.TabSize,
......
......@@ -27,10 +27,10 @@ private IEnumerable<IFormattingRule> GetFormattingRules(Document document, int p
return formattingRules;
}
public async Task<IndentationResult?> GetDesiredIndentationAsync(Document document, int lineNumber, CancellationToken cancellationToken)
public IndentationResult? GetDesiredIndentation(Document document, int lineNumber, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
var sourceText = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var root = document.GetSyntaxRootSynchronously(cancellationToken);
var sourceText = root.SyntaxTree.GetText(cancellationToken);
var lineToBeIndented = sourceText.Lines[lineNumber];
......@@ -42,12 +42,12 @@ private IEnumerable<IFormattingRule> GetFormattingRules(Document document, int p
return null;
}
var indenter = await GetIndenterAsync(document, lineToBeIndented, formattingRules, document.Options, cancellationToken).ConfigureAwait(false);
var indenter = GetIndenter(root.SyntaxTree, lineToBeIndented, formattingRules, document.Options, cancellationToken);
return indenter.GetDesiredIndentation();
}
protected abstract Task<AbstractIndenter> GetIndenterAsync(
Document document, TextLine lineToBeIndented, IEnumerable<IFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken);
protected abstract AbstractIndenter GetIndenter(
SyntaxTree syntaxTree, TextLine lineToBeIndented, IEnumerable<IFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken);
protected abstract bool ShouldUseSmartTokenFormatterInsteadOfIndenter(
IEnumerable<IFormattingRule> formattingRules, SyntaxNode root, TextLine line, OptionSet optionSet, CancellationToken cancellationToken);
......
......@@ -40,6 +40,6 @@ public IndentationResult(int basePosition, int offset) : this()
internal interface IIndentationService : ILanguageService
{
Task<IndentationResult?> GetDesiredIndentationAsync(Document document, int lineNumber, CancellationToken cancellationToken);
IndentationResult? GetDesiredIndentation(Document document, int lineNumber, CancellationToken cancellationToken);
}
}
......@@ -2,14 +2,12 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Internal.Log;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editor.Implementation.SmartIndent
{
......@@ -29,19 +27,14 @@ public SmartIndent(ITextView textView)
public int? GetDesiredIndentation(ITextSnapshotLine line)
{
return GetDesiredIndentationAsync(line).WaitAndGetResult(CancellationToken.None);
}
internal Task<int?> GetDesiredIndentationAsync(ITextSnapshotLine line)
{
return GetDesiredIndentationAsync(line, CancellationToken.None);
return GetDesiredIndentation(line, CancellationToken.None);
}
public void Dispose()
{
}
private async Task<int?> GetDesiredIndentationAsync(ITextSnapshotLine lineToBeIndented, CancellationToken cancellationToken)
private int? GetDesiredIndentation(ITextSnapshotLine lineToBeIndented, CancellationToken cancellationToken)
{
if (lineToBeIndented == null)
{
......@@ -51,24 +44,9 @@ public void Dispose()
using (Logger.LogBlock(FunctionId.SmartIndentation_Start, cancellationToken))
{
var document = lineToBeIndented.Snapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document == null)
{
return null;
}
var service = document.GetLanguageService<IIndentationService>();
if (service == null)
{
return null;
}
var result = await service.GetDesiredIndentationAsync(document, lineToBeIndented.LineNumber, cancellationToken).ConfigureAwait(false);
if (result == null)
{
return null;
}
return result.Value.GetIndentation(_textView, lineToBeIndented);
var service = document?.GetLanguageService<IIndentationService>();
var result = service?.GetDesiredIndentation(document, lineToBeIndented.LineNumber, cancellationToken);
return result?.GetIndentation(_textView, lineToBeIndented);
}
}
}
......
......@@ -14,12 +14,12 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.Formatting.Indentation
Private Class Indenter
Inherits AbstractIndenter
Public Sub New(document As SyntacticDocument,
Public Sub New(syntaxTree As SyntaxTree,
rules As IEnumerable(Of IFormattingRule),
optionSet As OptionSet,
line As TextLine,
cancellationToken As CancellationToken)
MyBase.New(document, rules, optionSet, line, cancellationToken)
MyBase.New(syntaxTree, rules, optionSet, line, cancellationToken)
End Sub
Public Overrides Function GetDesiredIndentation() As IndentationResult?
......
......@@ -23,9 +23,8 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.Formatting.Indentation
Return s_instance
End Function
Protected Overrides Async Function GetIndenterAsync(document As Document, lineToBeIndented As TextLine, formattingRules As IEnumerable(Of IFormattingRule), optionSet As OptionSet, cancellationToken As CancellationToken) As Tasks.Task(Of AbstractIndenter)
Dim synDocument = Await SyntacticDocument.CreateAsync(document, cancellationToken).ConfigureAwait(False)
Return New Indenter(synDocument, formattingRules, optionSet, lineToBeIndented, cancellationToken)
Protected Overrides Function GetIndenter(syntaxTree As SyntaxTree, lineToBeIndented As TextLine, formattingRules As IEnumerable(Of IFormattingRule), optionSet As OptionSet, cancellationToken As CancellationToken) As AbstractIndenter
Return New Indenter(syntaxTree, formattingRules, optionSet, lineToBeIndented, cancellationToken)
End Function
Protected Overrides Function ShouldUseSmartTokenFormatterInsteadOfIndenter(formattingRules As IEnumerable(Of IFormattingRule),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册