当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxTree.GetText方法代码示例

本文整理汇总了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);
 }
开发者ID:riversky,项目名称:roslyn,代码行数:7,代码来源:LocationsTests.cs

示例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));
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:8,代码来源:SyntaxEquivalenceTests.cs

示例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));
     }
 }
开发者ID:GloryChou,项目名称:roslyn,代码行数:8,代码来源:SyntaxTreeExtensionsTests.cs

示例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);
        }
开发者ID:grokys,项目名称:StyleCopMagic,代码行数:11,代码来源:TestBase.cs

示例5: VerifyIdenticalStructure

        private void VerifyIdenticalStructure(SyntaxTree syntaxTree)
        {
            var incrementalRoot = syntaxTree.GetCompilationUnitRoot();
            var parsedRoot = SyntaxFactory.ParseCompilationUnit(syntaxTree.GetText().ToString());

            AssertNodesAreEquivalent(parsedRoot, incrementalRoot);
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:7,代码来源:GrammarAmbiguities.cs

示例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));
        }
开发者ID:orthoxerox,项目名称:roslyn,代码行数:79,代码来源:DisabledTextTriviaStructureProvider.cs

示例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);
        }
开发者ID:reidwooten99apps,项目名称:roslyn,代码行数:18,代码来源:DiagnosticData.cs

示例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);
 }
开发者ID:GuilhermeSa,项目名称:roslyn,代码行数:7,代码来源:SyntaxPathTests.cs

示例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;
        }
开发者ID:RoryVL,项目名称:roslyn,代码行数:29,代码来源:AnalyzerDriver.cs

示例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);
        }
开发者ID:elemk0vv,项目名称:roslyn-1,代码行数:11,代码来源:DiagnosticData.cs


注:本文中的SyntaxTree.GetText方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。