提交 00181a01 编写于 作者: C CyrusNajmabadi

Pass along a syntax facts service.

上级 b80b7040
......@@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.Editor.Implementation.Formatting.Indentation;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
......@@ -24,12 +25,13 @@ internal partial class CSharpIndentationService
internal class Indenter : AbstractIndenter
{
public Indenter(
ISyntaxFactsService syntaxFacts,
SyntaxTree syntaxTree,
IEnumerable<IFormattingRule> rules,
OptionSet optionSet,
TextLine line,
CancellationToken cancellationToken) :
base(syntaxTree, rules, optionSet, line, cancellationToken)
base(syntaxFacts, syntaxTree, rules, optionSet, line, cancellationToken)
{
}
......
......@@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
......@@ -31,9 +32,11 @@ protected override IFormattingRule GetSpecializedIndentationFormattingRule()
}
protected override AbstractIndenter GetIndenter(
SyntaxTree syntaxTree, TextLine lineToBeIndented, IEnumerable<IFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken)
ISyntaxFactsService syntaxFacts, SyntaxTree syntaxTree, TextLine lineToBeIndented, IEnumerable<IFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken)
{
return new Indenter(syntaxTree, formattingRules, optionSet, lineToBeIndented, cancellationToken);
return new Indenter(
syntaxFacts, syntaxTree, formattingRules,
optionSet, lineToBeIndented, cancellationToken);
}
protected override bool ShouldUseSmartTokenFormatterInsteadOfIndenter(
......
......@@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Editor.Shared.Extensions;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.LanguageServices;
......@@ -12,7 +11,6 @@
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.CodeAnalysis.Text.Shared.Extensions;
using Microsoft.VisualStudio.Text;
namespace Microsoft.CodeAnalysis.Editor.Implementation.SmartIndent
{
......@@ -21,7 +19,6 @@ internal abstract partial class AbstractIndentationService
internal abstract class AbstractIndenter
{
protected readonly OptionSet OptionSet;
protected readonly SyntacticDocument Document;
protected readonly TextLine LineToBeIndented;
protected readonly int TabSize;
protected readonly CancellationToken CancellationToken;
......@@ -32,25 +29,30 @@ internal abstract class AbstractIndenter
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;
public AbstractIndenter(
ISyntaxFactsService syntaxFacts,
SyntaxTree syntaxTree,
IEnumerable<IFormattingRule> rules,
OptionSet optionSet,
TextLine lineToBeIndented,
CancellationToken cancellationToken)
{
var syntaxRoot = syntaxTree.GetRoot(cancellationToken);
this._syntaxFacts = syntaxFacts;
this.OptionSet = optionSet;
this.Tree = syntaxTree;
this.LineToBeIndented = lineToBeIndented;
this.TabSize = this.OptionSet.GetOption(FormattingOptions.TabSize, this.Document.Root.Language);
this.TabSize = this.OptionSet.GetOption(FormattingOptions.TabSize, syntaxRoot.Language);
this.CancellationToken = cancellationToken;
this.Rules = rules;
this.Finder = new BottomUpBaseIndentationFinder(
new ChainedFormattingRules(this.Rules, OptionSet),
this.TabSize,
this.OptionSet.GetOption(FormattingOptions.IndentationSize, this.Document.Root.Language),
this.OptionSet.GetOption(FormattingOptions.IndentationSize, syntaxRoot.Language),
tokenStream: null,
lastToken: default(SyntaxToken));
}
......@@ -87,15 +89,13 @@ protected IndentationResult GetIndentationOfLine(TextLine lineToMatch, int added
protected IndentationResult GetIndentationOfPosition(int position, int addedSpaces)
{
var tree = Document.SyntaxTree;
if (tree.OverlapsHiddenPosition(GetNormalizedSpan(position), CancellationToken))
if (this.Tree.OverlapsHiddenPosition(GetNormalizedSpan(position), CancellationToken))
{
// Oops, the line we want to line up to is either hidden, or is in a different
// visible region.
var root = tree.GetRoot(CancellationToken.None);
var root = this.Tree.GetRoot(CancellationToken.None);
var token = root.FindTokenFromEnd(LineToBeIndented.Start);
var indentation = Finder.GetIndentationOfCurrentPosition(tree, token, LineToBeIndented.Start, CancellationToken.None);
var indentation = Finder.GetIndentationOfCurrentPosition(this.Tree, token, LineToBeIndented.Start, CancellationToken.None);
return new IndentationResult(LineToBeIndented.Start, indentation);
}
......@@ -125,7 +125,6 @@ private TextSpan GetNormalizedSpan(int position)
return null;
}
var syntaxFacts = this.Document.Document.GetLanguageService<ISyntaxFactsService>();
var sourceText = this.LineToBeIndented.Text;
var lineNumber = this.LineToBeIndented.LineNumber - 1;
......@@ -150,7 +149,7 @@ private TextSpan GetNormalizedSpan(int position)
// This line is inside an inactive region. Examine the
// first preceding line not in an inactive region.
var disabledSpan = syntaxFacts.GetInactiveRegionSpanAroundPosition(this.Tree, actualLine.Span.Start, CancellationToken);
var disabledSpan = _syntaxFacts.GetInactiveRegionSpanAroundPosition(this.Tree, actualLine.Span.Start, CancellationToken);
if (disabledSpan != default(TextSpan))
{
var targetLine = sourceText.Lines.GetLineFromPosition(disabledSpan.Start).LineNumber;
......
......@@ -3,12 +3,12 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Formatting.Rules;
using Microsoft.CodeAnalysis.LanguageServices;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Editor.Implementation.SmartIndent
......@@ -42,12 +42,16 @@ private IEnumerable<IFormattingRule> GetFormattingRules(Document document, int p
return null;
}
var indenter = GetIndenter(root.SyntaxTree, lineToBeIndented, formattingRules, document.Options, cancellationToken);
var indenter = GetIndenter(
document.GetLanguageService<ISyntaxFactsService>(),
root.SyntaxTree, lineToBeIndented, formattingRules,
document.Options, cancellationToken);
return indenter.GetDesiredIndentation();
}
protected abstract AbstractIndenter GetIndenter(
SyntaxTree syntaxTree, TextLine lineToBeIndented, IEnumerable<IFormattingRule> formattingRules, OptionSet optionSet, CancellationToken cancellationToken);
ISyntaxFactsService syntaxFacts, 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);
......
......@@ -3,6 +3,7 @@
Imports System.Threading
Imports Microsoft.CodeAnalysis.Formatting
Imports Microsoft.CodeAnalysis.Formatting.Rules
Imports Microsoft.CodeAnalysis.LanguageServices
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.Text.Shared.Extensions
......@@ -14,12 +15,13 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.Formatting.Indentation
Private Class Indenter
Inherits AbstractIndenter
Public Sub New(syntaxTree As SyntaxTree,
Public Sub New(syntaxFacts As ISyntaxFactsService,
syntaxTree As SyntaxTree,
rules As IEnumerable(Of IFormattingRule),
optionSet As OptionSet,
line As TextLine,
cancellationToken As CancellationToken)
MyBase.New(syntaxTree, rules, optionSet, line, cancellationToken)
MyBase.New(syntaxFacts, syntaxTree, rules, optionSet, line, cancellationToken)
End Sub
Public Overrides Function GetDesiredIndentation() As IndentationResult?
......
......@@ -6,6 +6,7 @@ Imports Microsoft.CodeAnalysis.Editor.Implementation.SmartIndent
Imports Microsoft.CodeAnalysis.Formatting.Rules
Imports Microsoft.CodeAnalysis.Host
Imports Microsoft.CodeAnalysis.Host.Mef
Imports Microsoft.CodeAnalysis.LanguageServices
Imports Microsoft.CodeAnalysis.Options
Imports Microsoft.CodeAnalysis.Text
Imports Microsoft.CodeAnalysis.Text.Shared.Extensions
......@@ -23,8 +24,13 @@ Namespace Microsoft.CodeAnalysis.Editor.VisualBasic.Formatting.Indentation
Return s_instance
End Function
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)
Protected Overrides Function GetIndenter(syntaxFacts As ISyntaxFactsService,
syntaxTree As SyntaxTree,
lineToBeIndented As TextLine,
formattingRules As IEnumerable(Of IFormattingRule),
optionSet As OptionSet,
cancellationToken As CancellationToken) As AbstractIndenter
Return New Indenter(syntaxFacts, 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.
先完成此消息的编辑!
想要评论请 注册