未验证 提交 648893fd 编写于 作者: G Gen Lu 提交者: GitHub

Merge pull request #36012 from CyrusNajmabadi/noIndenter

Remove the AbstractIndenter type (and subtypes).
......@@ -35,9 +35,6 @@ protected override AbstractFormattingRule GetSpecializedIndentationFormattingRul
return s_instance;
}
protected override AbstractIndenter GetIndenter(SyntacticDocument document, TextLine lineToBeIndented, IEnumerable<AbstractFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken)
=> new Indenter(document, formattingRules, optionSet, lineToBeIndented, cancellationToken);
public static bool ShouldUseSmartTokenFormatterInsteadOfIndenter(
IEnumerable<AbstractFormattingRule> formattingRules,
CompilationUnitSyntax root,
......
......@@ -17,25 +17,28 @@ namespace Microsoft.CodeAnalysis.Indentation
{
internal abstract partial class AbstractIndentationService<TSyntaxRoot>
{
internal abstract class AbstractIndenter
protected struct Indenter
{
private readonly AbstractIndentationService<TSyntaxRoot> _service;
public readonly OptionSet OptionSet;
public readonly TextLine LineToBeIndented;
protected readonly int TabSize;
protected readonly CancellationToken CancellationToken;
public readonly CancellationToken CancellationToken;
protected readonly SyntacticDocument Document;
protected readonly TSyntaxRoot Root;
protected SyntaxTree Tree => Document.SyntaxTree;
protected readonly IEnumerable<AbstractFormattingRule> Rules;
protected readonly BottomUpBaseIndentationFinder Finder;
public readonly SyntacticDocument Document;
public readonly TSyntaxRoot Root;
public SyntaxTree Tree => Document.SyntaxTree;
public readonly IEnumerable<AbstractFormattingRule> Rules;
public readonly BottomUpBaseIndentationFinder Finder;
private static readonly Func<SyntaxToken, bool> s_tokenHasDirective = tk => tk.ContainsDirectives &&
(tk.LeadingTrivia.Any(tr => tr.IsDirective) || tk.TrailingTrivia.Any(tr => tr.IsDirective));
private readonly ISyntaxFactsService _syntaxFacts;
private readonly int TabSize;
public AbstractIndenter(
public Indenter(
AbstractIndentationService<TSyntaxRoot> service,
SyntacticDocument document,
IEnumerable<AbstractFormattingRule> rules,
OptionSet optionSet,
......@@ -44,6 +47,7 @@ internal abstract class AbstractIndenter
{
Document = document;
this._service = service;
this._syntaxFacts = document.Document.GetLanguageService<ISyntaxFactsService>();
this.OptionSet = optionSet;
this.Root = (TSyntaxRoot)document.Root;
......@@ -59,18 +63,6 @@ internal abstract class AbstractIndenter
tokenStream: null);
}
/// <summary>
/// Returns <see langword="true"/> if the language specific <see
/// cref="ISmartTokenFormatter"/> should be deferred to figure out indentatation. If
/// so, it will be asked to <see cref="ISmartTokenFormatter.FormatTokenAsync"/> the
/// resultant <paramref name="token"/> provided by this method.
/// </summary>
protected abstract bool ShouldUseTokenIndenter(out SyntaxToken token);
protected abstract ISmartTokenFormatter CreateSmartTokenFormatter();
protected abstract IndentationResult GetDesiredIndentationWorker(
SyntaxToken token, TextLine previousLine, int lastNonWhitespacePosition);
public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle indentStyle)
{
// If the caller wants no indent, then we'll return an effective '0' indent.
......@@ -115,18 +107,18 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind
var token = Root.FindToken(lastNonWhitespacePosition);
Debug.Assert(token.RawKind != 0, "FindToken should always return a valid token");
return GetDesiredIndentationWorker(
token, previousNonWhitespaceOrPreprocessorLine, lastNonWhitespacePosition);
return _service.GetDesiredIndentationWorker(
this, token, previousNonWhitespaceOrPreprocessorLine, lastNonWhitespacePosition);
}
public bool TryGetSmartTokenIndentation(out IndentationResult indentationResult)
{
if (ShouldUseTokenIndenter(out var token))
if (_service.ShouldUseTokenIndenter(this, out var token))
{
// var root = document.GetSyntaxRootSynchronously(cancellationToken);
var sourceText = Tree.GetText(CancellationToken);
var formatter = CreateSmartTokenFormatter();
var formatter = _service.CreateSmartTokenFormatter(this);
var changes = formatter.FormatTokenAsync(Document.Project.Solution.Workspace, token, CancellationToken)
.WaitAndGetResult(CancellationToken);
......@@ -149,19 +141,19 @@ public bool TryGetSmartTokenIndentation(out IndentationResult indentationResult)
return false;
}
protected IndentationResult IndentFromStartOfLine(int addedSpaces)
public IndentationResult IndentFromStartOfLine(int addedSpaces)
=> new IndentationResult(this.LineToBeIndented.Start, addedSpaces);
protected IndentationResult GetIndentationOfToken(SyntaxToken token)
public IndentationResult GetIndentationOfToken(SyntaxToken token)
=> GetIndentationOfToken(token, addedSpaces: 0);
protected IndentationResult GetIndentationOfToken(SyntaxToken token, int addedSpaces)
public IndentationResult GetIndentationOfToken(SyntaxToken token, int addedSpaces)
=> GetIndentationOfPosition(token.SpanStart, addedSpaces);
protected IndentationResult GetIndentationOfLine(TextLine lineToMatch)
public IndentationResult GetIndentationOfLine(TextLine lineToMatch)
=> GetIndentationOfLine(lineToMatch, addedSpaces: 0);
protected IndentationResult GetIndentationOfLine(TextLine lineToMatch, int addedSpaces)
public IndentationResult GetIndentationOfLine(TextLine lineToMatch, int addedSpaces)
{
var firstNonWhitespace = lineToMatch.GetFirstNonWhitespacePosition();
firstNonWhitespace ??= lineToMatch.End;
......@@ -169,7 +161,7 @@ protected IndentationResult GetIndentationOfLine(TextLine lineToMatch, int added
return GetIndentationOfPosition(firstNonWhitespace.Value, addedSpaces);
}
protected IndentationResult GetIndentationOfPosition(int position, int addedSpaces)
private IndentationResult GetIndentationOfPosition(int position, int addedSpaces)
{
if (this.Tree.OverlapsHiddenPosition(GetNormalizedSpan(position), CancellationToken))
{
......@@ -194,7 +186,7 @@ private TextSpan GetNormalizedSpan(int position)
return TextSpan.FromBounds(position, LineToBeIndented.Start);
}
protected TextLine? GetPreviousNonBlankOrPreprocessorLine()
private TextLine? GetPreviousNonBlankOrPreprocessorLine()
{
if (LineToBeIndented.LineNumber <= 0)
{
......@@ -246,10 +238,10 @@ private TextSpan GetNormalizedSpan(int position)
return null;
}
protected int GetCurrentPositionNotBelongToEndOfFileToken(int position)
public int GetCurrentPositionNotBelongToEndOfFileToken(int position)
=> Math.Min(Root.EndOfFileToken.FullSpan.Start, position);
protected bool HasPreprocessorCharacter(TextLine currentLine)
private bool HasPreprocessorCharacter(TextLine currentLine)
{
var text = currentLine.ToString();
Debug.Assert(!string.IsNullOrWhiteSpace(text));
......
......@@ -47,14 +47,7 @@ private IEnumerable<AbstractFormattingRule> GetFormattingRules(Document document
return indenter.GetDesiredIndentation(indentStyle);
}
public IndentationResult GetBlankLineIndentation(
Document document, int lineNumber, FormattingOptions.IndentStyle indentStyle, CancellationToken cancellationToken)
{
var indenter = GetIndenter(document, lineNumber, cancellationToken);
return indenter.GetDesiredIndentation(indentStyle);
}
private AbstractIndenter GetIndenter(Document document, int lineNumber, CancellationToken cancellationToken)
private Indenter GetIndenter(Document document, int lineNumber, CancellationToken cancellationToken)
{
var documentOptions = document.GetOptionsAsync(cancellationToken).WaitAndGetResult_CanCallOnBackground(cancellationToken);
var syntacticDoc = SyntacticDocument.CreateAsync(document, cancellationToken).WaitAndGetResult_CanCallOnBackground(cancellationToken);
......@@ -64,10 +57,19 @@ private AbstractIndenter GetIndenter(Document document, int lineNumber, Cancella
var formattingRules = GetFormattingRules(document, lineToBeIndented.Start);
return GetIndenter(syntacticDoc, lineToBeIndented, formattingRules, documentOptions, cancellationToken);
return new Indenter(this, syntacticDoc, formattingRules, documentOptions, lineToBeIndented, cancellationToken);
}
protected abstract AbstractIndenter GetIndenter(
SyntacticDocument document, TextLine lineToBeIndented, IEnumerable<AbstractFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken);
/// <summary>
/// Returns <see langword="true"/> if the language specific <see
/// cref="ISmartTokenFormatter"/> should be deferred to figure out indentation. If so, it
/// will be asked to <see cref="ISmartTokenFormatter.FormatTokenAsync"/> the resultant
/// <paramref name="token"/> provided by this method.
/// </summary>
protected abstract bool ShouldUseTokenIndenter(Indenter indenter, out SyntaxToken token);
protected abstract ISmartTokenFormatter CreateSmartTokenFormatter(Indenter indenter);
protected abstract IndentationResult GetDesiredIndentationWorker(
Indenter indenter, SyntaxToken token, TextLine previousLine, int lastNonWhitespacePosition);
}
}
......@@ -32,14 +32,6 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Indentation
Return _specializedIndentationRule
End Function
Protected Overrides Function GetIndenter(document As SyntacticDocument,
lineToBeIndented As TextLine,
formattingRules As IEnumerable(Of AbstractFormattingRule),
optionSet As OptionSet,
cancellationToken As CancellationToken) As AbstractIndenter
Return New Indenter(document, formattingRules, optionSet, lineToBeIndented, cancellationToken)
End Function
Public Overloads Shared Function ShouldUseSmartTokenFormatterInsteadOfIndenter(
formattingRules As IEnumerable(Of AbstractFormattingRule),
root As CompilationUnitSyntax,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册