本文整理汇总了C#中SyntaxTree.GetRoot方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxTree.GetRoot方法的具体用法?C# SyntaxTree.GetRoot怎么用?C# SyntaxTree.GetRoot使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxTree
的用法示例。
在下文中一共展示了SyntaxTree.GetRoot方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TreeFullSpan
bool TreeFullSpan(SyntaxTree tree, string codeText, string filename, ref string errorText)
{
var retVal = true;
if (tree.GetRoot().FullSpan.Length != codeText.Length)
{
retVal = false;
errorText = "FullSpan width of tree (" + tree.GetRoot().FullSpan.Length + ") does not match length of the code (" + codeText.Length + ")";
}
return retVal;
}
示例2: BeginsWithAutoGeneratedComment
private static bool BeginsWithAutoGeneratedComment(SyntaxTree tree, Func<SyntaxTrivia, bool> isComment, CancellationToken cancellationToken)
{
var root = tree.GetRoot(cancellationToken);
if (root.HasLeadingTrivia)
{
var leadingTrivia = root.GetLeadingTrivia();
foreach (var trivia in leadingTrivia)
{
if (!isComment(trivia))
{
continue;
}
var text = trivia.ToString();
// Check to see if the text of the comment contains an auto generated comment.
foreach (var autoGenerated in s_autoGeneratedStrings)
{
if (text.Contains(autoGenerated))
{
return true;
}
}
}
}
return false;
}
示例3: AbstractIndenter
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, syntaxRoot.Language);
this.CancellationToken = cancellationToken;
this.Rules = rules;
this.Finder = new BottomUpBaseIndentationFinder(
new ChainedFormattingRules(this.Rules, OptionSet),
this.TabSize,
this.OptionSet.GetOption(FormattingOptions.IndentationSize, syntaxRoot.Language),
tokenStream: null,
lastToken: default(SyntaxToken));
}
示例4: RecoverNode
protected static SyntaxNode RecoverNode(SyntaxTree tree, TextSpan textSpan, int kind)
{
var token = tree.GetRoot().FindToken(textSpan.Start, findInsideTrivia: true);
var node = token.Parent;
while (node != null)
{
if (node.Span == textSpan && node.RawKind == kind)
{
return node;
}
var structuredTrivia = node as IStructuredTriviaSyntax;
if (structuredTrivia != null)
{
node = structuredTrivia.ParentTrivia.Token.Parent;
}
else
{
node = node.Parent;
}
}
throw Contract.Unreachable;
}
示例5: ReportUnprocessed
public static void ReportUnprocessed(SyntaxTree tree, TextSpan? filterSpanWithinTree, DiagnosticBag diagnostics, CancellationToken cancellationToken)
{
if (tree.ReportDocumentationCommentDiagnostics())
{
UnprocessedDocumentationCommentFinder finder = new UnprocessedDocumentationCommentFinder(diagnostics, filterSpanWithinTree, cancellationToken);
finder.Visit(tree.GetRoot());
}
}
示例6: CallGraphTest
public CallGraphTest()
{
source = FileUtil.ReadAllText(path);
tree = ASTUtil.GetSyntaxTreeFromSource(source);
classDeclaration = tree.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().
First(c => c.Identifier.Value.Equals("CallGraphTest"));
graph = new CallGraphBuilder(classDeclaration, tree).BuildCallGraph();
}
示例7: AssertFormatAsync
private async Task AssertFormatAsync(string expected, SyntaxTree tree)
{
using (var workspace = new TestWorkspace())
{
var formattedRoot = await Formatter.FormatAsync(tree.GetRoot(), workspace);
var actualFormattedText = formattedRoot.ToFullString();
Assert.Equal(expected, actualFormattedText);
}
}
示例8: RoundTrip
bool RoundTrip(SyntaxTree tree, string codeText, string filename, ref string errorText)
{
var retVal = true;
if (tree.GetRoot().ToFullString() != codeText)
{
retVal = false;
errorText = "FullText for tree parsed from '" + filename + "' does match actual text";
}
return retVal;
}
示例9: GetAllMethodDeclarations
public static List<MethodDeclarationSyntax> GetAllMethodDeclarations(SyntaxTree tree)
{
SyntaxNode root = tree.GetRoot();
var methods = new List<MethodDeclarationSyntax>();
IEnumerable<SyntaxNode> ite = root.DescendantNodes();
foreach (SyntaxNode node in ite)
{
if (node.Kind == SyntaxKind.MethodDeclaration)
methods.Add((MethodDeclarationSyntax)node);
}
return methods;
}
示例10: AreEquivalent
internal static bool AreEquivalent(SyntaxTree before, SyntaxTree after, Func<SyntaxKind, bool> ignoreChildNode, bool topLevel)
{
if (before == after)
{
return true;
}
if (before == null || after == null)
{
return false;
}
return AreEquivalent(before.GetRoot(), after.GetRoot(), ignoreChildNode, topLevel);
}
示例11: GetAllPragmaWarningDirectives
// Add all active #pragma warn directives under trivia into the list, in source code order.
private static void GetAllPragmaWarningDirectives(SyntaxTree syntaxTree, ArrayBuilder<PragmaWarningDirectiveTriviaSyntax> directiveList)
{
foreach (var d in syntaxTree.GetRoot().GetDirectives())
{
if (d.Kind() == SyntaxKind.PragmaWarningDirectiveTrivia)
{
var w = d as PragmaWarningDirectiveTriviaSyntax;
// Ignore directives with errors (i.e., Unrecognized #pragma directive) and
// directives inside disabled code (by #if and #endif)
if (!w.DisableOrRestoreKeyword.IsMissing && !w.WarningKeyword.IsMissing && w.IsActive)
directiveList.Add(w);
}
}
}
示例12: TryGetStringLiteralToken
protected override bool TryGetStringLiteralToken(SyntaxTree tree, int position, out SyntaxToken stringLiteral, CancellationToken cancellationToken)
{
if (tree.IsEntirelyWithinStringLiteral(position, cancellationToken))
{
var token = tree.GetRoot(cancellationToken).FindToken(position, findInsideTrivia: true);
if (token.Kind() == SyntaxKind.EndOfDirectiveToken || token.Kind() == SyntaxKind.EndOfFileToken)
{
token = token.GetPreviousToken(includeSkipped: true, includeDirectives: true);
}
if (token.Kind() == SyntaxKind.StringLiteralToken &&
token.Parent.Kind() == SyntaxKind.ReferenceDirectiveTrivia)
{
stringLiteral = token;
return true;
}
}
stringLiteral = default(SyntaxToken);
return false;
}
示例13: NullsAndCollections
bool NullsAndCollections(SyntaxTree tree, string codeText, string filename, ref string errorText)
{
var retVal = true;
if (tree.GetDiagnostics() == null)
{
retVal = false;
errorText = "Diagnostics collection for this tree is null";
}
else if ((
from e in tree.GetDiagnostics()
where e == null
select e).Any())
{
retVal = false;
errorText = "Diagnostics collection for this tree contains a null element";
}
else if (tree.GetRoot().DescendantTokens() == null)
{
retVal = false;
errorText = "Tokens collection for this tree is null";
}
return retVal;
}
示例14: GetMethodBodyDiagnosticsForTree
internal static ImmutableArray<Diagnostic> GetMethodBodyDiagnosticsForTree(CSharpCompilation compilation, SyntaxTree tree, TextSpan? span, CancellationToken cancellationToken)
{
DiagnosticBag diagnostics = DiagnosticBag.GetInstance();
CompileMethodBodies(
compilation: compilation,
moduleBeingBuilt: null,
generateDebugInfo: false,
hasDeclarationErrors: false,
filter: null,
filterTree: tree,
filterSpanWithinTree: span,
diagnostics: diagnostics,
cancellationToken: cancellationToken);
DocumentationCommentCompiler.WriteDocumentationCommentXml(compilation, null, null, diagnostics, cancellationToken, tree, span);
// Report unused directives only if computing diagnostics for the entire tree.
// Otherwise we cannot determine if a particular directive is used outside of the given sub-span within the tree.
if (!span.HasValue || span.Value == tree.GetRoot(cancellationToken).FullSpan)
{
compilation.ReportUnusedImports(diagnostics, cancellationToken, tree);
}
return diagnostics.ToReadOnlyAndFree();
}
示例15: FindTriviaAndAdjustForEndOfFile
private static SyntaxTrivia FindTriviaAndAdjustForEndOfFile(
SyntaxTree syntaxTree, int position, CancellationToken cancellationToken, bool findInsideTrivia = false)
{
var root = syntaxTree.GetRoot(cancellationToken) as CompilationUnitSyntax;
var trivia = root.FindTrivia(position, findInsideTrivia);
// If we ask right at the end of the file, we'll get back nothing.
// We handle that case specially for now, though SyntaxTree.FindTrivia should
// work at the end of a file.
if (position == root.FullWidth())
{
var endOfFileToken = root.EndOfFileToken;
if (endOfFileToken.HasLeadingTrivia)
{
trivia = endOfFileToken.LeadingTrivia.Last();
}
else
{
var token = endOfFileToken.GetPreviousToken(includeSkipped: true);
if (token.HasTrailingTrivia)
{
trivia = token.TrailingTrivia.Last();
}
}
}
return trivia;
}