本文整理汇总了C#中SyntaxTree.GetText方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxTree.GetText方法的具体用法?C# SyntaxTree.GetText怎么用?C# SyntaxTree.GetText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxTree
的用法示例。
在下文中一共展示了SyntaxTree.GetText方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetSpanIn
private TextSpan GetSpanIn(SyntaxTree syntaxTree, string textToFind)
{
string s = syntaxTree.GetText().ToString();
int index = s.IndexOf(textToFind);
Assert.True(index >= 0, "textToFind not found in the tree");
return new TextSpan(index, textToFind.Length);
}
示例2: VerifyNotEquivalent
private void VerifyNotEquivalent(SyntaxTree tree1, SyntaxTree tree2, bool topLevel)
{
Assert.False(SyntaxFactory.AreEquivalent(tree1, tree2, topLevel));
// now try as if the second tree were created from scratch.
var tree3 = SyntaxFactory.ParseSyntaxTree(tree2.GetText().ToString());
Assert.False(SyntaxFactory.AreEquivalent(tree1, tree3, topLevel));
}
示例3: VerifyWholeLineIsInactive
private static void VerifyWholeLineIsInactive(SyntaxTree tree, int lineNumber)
{
var line = tree.GetText().Lines[lineNumber];
for (int pos = line.Start; pos < line.EndIncludingLineBreak; pos++)
{
Assert.True(tree.IsInInactiveRegion(pos, CancellationToken.None));
}
}
示例4: Compare
private void Compare(SyntaxTree src, string rule, string test)
{
string expectedPath = Path.Combine(TestFilePath, rule, test + ".expected.cs");
string actualPath = Path.Combine(TestFilePath, rule, test + ".actual.cs");
string expected = File.ReadAllText(expectedPath);
string actual = src.GetText().ToString();
File.WriteAllText(actualPath, actual);
Assert.AreEqual(expected, actual);
}
示例5: VerifyIdenticalStructure
private void VerifyIdenticalStructure(SyntaxTree syntaxTree)
{
var incrementalRoot = syntaxTree.GetCompilationUnitRoot();
var parsedRoot = SyntaxFactory.ParseCompilationUnit(syntaxTree.GetText().ToString());
AssertNodesAreEquivalent(parsedRoot, incrementalRoot);
}
示例6: CollectBlockSpans
public void CollectBlockSpans(
SyntaxTree syntaxTree, SyntaxTrivia trivia,
ArrayBuilder<BlockSpan> spans, CancellationToken cancellationToken)
{
// We'll always be leading trivia of some token.
var startPos = trivia.FullSpan.Start;
var endPos = trivia.FullSpan.End;
// Look through our parent token's trivia, to:
// 1. See if we're the first disabled trivia attached to the token.
// 2. To extend the span to the end of the last disabled trivia.
//
// The issue is that if there are other pre-processor directives (like #regions or
// #lines) mixed in the disabled code, they will be interleaved. Keep walking past
// them to the next thing that will actually end a disabled block. When we encounter
// one, we must also consider which opening block they end. In case of nested pre-processor
// directives, the inner most end block should match the inner most open block and so on.
var parentTriviaList = trivia.Token.LeadingTrivia;
var indexInParent = parentTriviaList.IndexOf(trivia);
// Note: in some error cases (for example when all future tokens end up being skipped)
// the parser may end up attaching pre-processor directives as trailing trivia to a
// preceding token.
if (indexInParent < 0)
{
parentTriviaList = trivia.Token.TrailingTrivia;
indexInParent = parentTriviaList.IndexOf(trivia);
}
if (indexInParent <= 0 ||
(!parentTriviaList[indexInParent - 1].IsKind(SyntaxKind.IfDirectiveTrivia) &&
!parentTriviaList[indexInParent - 1].IsKind(SyntaxKind.ElifDirectiveTrivia) &&
!parentTriviaList[indexInParent - 1].IsKind(SyntaxKind.ElseDirectiveTrivia)))
{
return;
}
var nestedIfDirectiveTrivia = 0;
for (int i = indexInParent; i < parentTriviaList.Count; i++)
{
if (parentTriviaList[i].IsKind(SyntaxKind.IfDirectiveTrivia))
{
nestedIfDirectiveTrivia++;
}
if (parentTriviaList[i].IsKind(SyntaxKind.EndIfDirectiveTrivia) ||
parentTriviaList[i].IsKind(SyntaxKind.ElifDirectiveTrivia) ||
parentTriviaList[i].IsKind(SyntaxKind.ElseDirectiveTrivia))
{
if (nestedIfDirectiveTrivia > 0)
{
nestedIfDirectiveTrivia--;
}
else
{
endPos = parentTriviaList[i - 1].FullSpan.End;
break;
}
}
}
// Now, exclude the last newline if there is one.
var text = syntaxTree.GetText(cancellationToken);
if (endPos > 1 && text[endPos - 1] == '\n' && text[endPos - 2] == '\r')
{
endPos -= 2;
}
else if (endPos > 0 && SyntaxFacts.IsNewLine(text[endPos - 1]))
{
endPos--;
}
var span = TextSpan.FromBounds(startPos, endPos);
spans.Add(new BlockSpan(
isCollapsible: true,
textSpan: span,
bannerText: CSharpStructureHelpers.Ellipsis,
autoCollapse: true));
}
示例7: ToDiagnostic
public Diagnostic ToDiagnostic(SyntaxTree tree)
{
var location = Location.None;
if (tree != null)
{
var span = _textSpan.HasValue ? _textSpan.Value : GetTextSpan(tree.GetText());
location = tree.GetLocation(span);
}
else if (OriginalFilePath != null && _textSpan != null)
{
var span = _textSpan.Value;
location = Location.Create(OriginalFilePath, span, new LinePositionSpan(
new LinePosition(OriginalStartLine, OriginalStartColumn),
new LinePosition(OriginalEndLine, OriginalEndColumn)));
}
return Diagnostic.Create(this.Id, this.Category, this.Message, this.Severity, this.DefaultSeverity, this.IsEnabledByDefault, this.WarningLevel, this.Title, this.Description, this.HelpLink, location, customTags: this.CustomTags, properties: this.Properties);
}
示例8: WithReplace
public static SyntaxTree WithReplace(SyntaxTree syntaxTree, int startIndex, string oldText, string newText)
{ // Use the offset to find the first element to replace at
return WithReplace(syntaxTree,
offset: syntaxTree.GetText().ToString().IndexOf(oldText, startIndex, StringComparison.Ordinal),
length: oldText.Length,
newText: newText);
}
示例9: ComputeGeneratedCodeSymbolsInTree
private ImmutableHashSet<ISymbol> ComputeGeneratedCodeSymbolsInTree(SyntaxTree tree, Compilation compilation, CancellationToken cancellationToken)
{
// PERF: Bail out early if file doesn't have "GeneratedCode" text.
var text = tree.GetText(cancellationToken).ToString();
if (!text.Contains("GeneratedCode"))
{
return ImmutableHashSet<ISymbol>.Empty;
}
var model = compilation.GetSemanticModel(tree);
var root = tree.GetRoot(cancellationToken);
var span = root.FullSpan;
var builder = new List<DeclarationInfo>();
model.ComputeDeclarationsInSpan(span, getSymbol: true, builder: builder, cancellationToken: cancellationToken);
ImmutableHashSet<ISymbol>.Builder generatedSymbolsBuilderOpt = null;
foreach (var declarationInfo in builder)
{
var symbol = declarationInfo.DeclaredSymbol;
if (symbol != null &&
GeneratedCodeUtilities.IsGeneratedSymbolWithGeneratedCodeAttribute(symbol, _generatedCodeAttribute))
{
generatedSymbolsBuilderOpt = generatedSymbolsBuilderOpt ?? ImmutableHashSet.CreateBuilder<ISymbol>();
generatedSymbolsBuilderOpt.Add(symbol);
}
}
return generatedSymbolsBuilderOpt != null ? generatedSymbolsBuilderOpt.ToImmutable() : ImmutableHashSet<ISymbol>.Empty;
}
示例10: ToDiagnostic
public Diagnostic ToDiagnostic(SyntaxTree tree)
{
var location = Location.None;
if (tree != null)
{
var span = _textSpan.HasValue ? _textSpan.Value : GetTextSpan(tree.GetText());
location = tree.GetLocation(span);
}
return Diagnostic.Create(this.Id, this.Category, this.Message, this.Severity, this.DefaultSeverity, this.IsEnabledByDefault, this.WarningLevel, this.Title, this.Description, this.HelpLink, location, customTags: this.CustomTags);
}