提交 42f069ac 编写于 作者: C Cyrus Najmabadi

Use NRT

上级 ecda4320
......@@ -31,9 +31,9 @@ protected override ISmartTokenFormatter CreateSmartTokenFormatter(Indenter inden
return new CSharpSmartTokenFormatter(indenter.OptionSet, rules, indenter.Root);
}
protected override IndentationResult GetDesiredIndentationWorker(Indenter indenter, SyntaxToken? tokenOpt, SyntaxTrivia? triviaOpt)
protected override IndentationResult? GetDesiredIndentationWorker(Indenter indenter, SyntaxToken? tokenOpt, SyntaxTrivia? triviaOpt)
=> TryGetDesiredIndentation(indenter, triviaOpt) ??
TryGetDesiredIndentation(indenter, tokenOpt) ?? default;
TryGetDesiredIndentation(indenter, tokenOpt);
private IndentationResult? TryGetDesiredIndentation(Indenter indenter, SyntaxTrivia? triviaOpt)
{
......
......@@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
......@@ -50,7 +52,7 @@ protected struct Indenter
Document = document;
_service = service;
_syntaxFacts = document.Document.GetLanguageService<ISyntaxFactsService>();
_syntaxFacts = document.Document.GetRequiredLanguageService<ISyntaxFactsService>();
OptionSet = optionSet;
OptionService = document.Document.Project.Solution.Workspace.Services.GetRequiredService<IOptionService>();
Root = (TSyntaxRoot)document.Root;
......@@ -66,11 +68,11 @@ protected struct Indenter
tokenStream: null);
}
public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle indentStyle)
public IndentationResult? GetDesiredIndentation(FormattingOptions.IndentStyle indentStyle)
{
// If the caller wants no indent, then we'll return an effective '0' indent.
if (indentStyle == FormattingOptions.IndentStyle.None)
return default;
return null;
// If the user has explicitly set 'block' indentation, or they're in an inactive preprocessor region,
// then just do simple block indentation.
......@@ -84,7 +86,7 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind
return GetDesiredSmartIndentation();
}
private readonly IndentationResult GetDesiredSmartIndentation()
private readonly IndentationResult? GetDesiredSmartIndentation()
{
// For smart indent, we generally will be computing from either the previous token in the code, or in a
// few special cases, the previous trivia.
......@@ -97,7 +99,7 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind
var trivia = TryGetImmediatelyPrecedingVisibleTrivia();
if (token == null && trivia == null)
return default;
return null;
return _service.GetDesiredIndentationWorker(this, token, trivia);
}
......@@ -143,7 +145,7 @@ public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle ind
return token;
}
private IndentationResult GetDesiredBlockIndentation()
private IndentationResult? GetDesiredBlockIndentation()
{
// Block indentation is simple, we keep walking back lines until we find a line with any sort of
// text on it. We then set our indentation to whatever the indentation of that line was.
......@@ -158,8 +160,8 @@ private IndentationResult GetDesiredBlockIndentation()
return new IndentationResult(basePosition: line.Start + offset.Value, offset: 0);
}
// Couldn't find a previous non-blank line. Don't indent at all.
return default;
// Couldn't find a previous non-blank line.
return null;
}
public bool TryGetSmartTokenIndentation(out IndentationResult indentationResult)
......
......@@ -2,12 +2,13 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Indentation
......@@ -45,7 +46,8 @@ private IEnumerable<AbstractFormattingRule> GetFormattingRules(Document document
return indentationResult;
}
return indenter.GetDesiredIndentation(indentStyle);
// If the indenter can't produce a valid result, just default to 0 as our indentation.
return indenter.GetDesiredIndentation(indentStyle) ?? default;
}
private Indenter GetIndenter(Document document, int lineNumber, FormattingOptions.IndentStyle indentStyle, CancellationToken cancellationToken)
......@@ -70,7 +72,7 @@ private Indenter GetIndenter(Document document, int lineNumber, FormattingOption
protected abstract bool ShouldUseTokenIndenter(Indenter indenter, out SyntaxToken token);
protected abstract ISmartTokenFormatter CreateSmartTokenFormatter(Indenter indenter);
protected abstract IndentationResult GetDesiredIndentationWorker(
protected abstract IndentationResult? GetDesiredIndentationWorker(
Indenter indenter, SyntaxToken? token, SyntaxTrivia? trivia);
}
}
......@@ -27,7 +27,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Indentation
Protected Overrides Function GetDesiredIndentationWorker(
indenter As Indenter,
tokenOpt As SyntaxToken?,
triviaOpt As SyntaxTrivia?) As IndentationResult
triviaOpt As SyntaxTrivia?) As IndentationResult?
If triviaOpt.HasValue Then
Dim trivia = triviaOpt.Value
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册