Пример #1
0
        public void PreprocessorDirectives()
        {
            SyntaxTree  tree = SyntaxFactory.ParseSyntaxTree(@"#if true
class A { }
#else
class B { }
#endif");
            SyntaxToken eof  = tree.GetRoot().FindToken(tree.GetText().Length, false);

            Assert.AreEqual(true, eof.HasLeadingTrivia);
            Assert.AreEqual(false, eof.HasTrailingTrivia);
            Assert.AreEqual(true, eof.ContainsDirectives);

            SyntaxTriviaList trivia = eof.LeadingTrivia;

            Assert.AreEqual(3, trivia.Count);
            Assert.AreEqual("#else", trivia.ElementAt(0).ToString());
            Assert.AreEqual(SyntaxKind.DisabledTextTrivia, trivia.ElementAt(1).CSharpKind());
            Assert.AreEqual("#endif", trivia.ElementAt(2).ToString());

            DirectiveTriviaSyntax directive = tree.GetRoot().GetLastDirective();

            Assert.AreEqual("endif", directive.DirectiveNameToken.Value);

            directive = directive.GetPreviousDirective();
            Assert.AreEqual("else", directive.DirectiveNameToken.Value);

            // List<DirectiveSyntax> relatedDirectives = directive.GetRelatedDirectives();
            // Assert.AreEqual(3, relatedDirectives.Count);
        }
Пример #2
0
        private static List <SyntaxTrivia> RemoveBlankLineTrivia(SyntaxTriviaList trivia, ref bool changed)
        {
            List <SyntaxTrivia> newTrivia = new List <SyntaxTrivia>();

            for (int i = 0; i < trivia.Count;)
            {
                SyntaxTrivia trivia1 = trivia.ElementAt(i);
                newTrivia.Add(trivia1);

                if (i < trivia.Count - 2)
                {
                    SyntaxTrivia trivia2 = trivia.ElementAt(i + 1);
                    SyntaxTrivia trivia3 = trivia.ElementAt(i + 2);

                    if (trivia1.IsKind(SyntaxKind.EndOfLineTrivia) &&
                        trivia2.IsKind(SyntaxKind.WhitespaceTrivia) &&
                        trivia3.IsKind(SyntaxKind.EndOfLineTrivia))
                    {
                        // Skip the whitespace with a newline.
                        newTrivia.Add(trivia3);
                        changed = true;
                        i      += 3;
                        continue;
                    }
                }

                i++;
            }

            return(newTrivia);
        }
        private static List <SyntaxTrivia> RemoveBlankLines(SyntaxTriviaList trivia, ref bool changed)
        {
            List <SyntaxTrivia> newTrivia = new List <SyntaxTrivia>();

            for (int i = 0; i < trivia.Count;)
            {
                SyntaxTrivia trivia1 = trivia.ElementAt(i);
                newTrivia.Add(trivia1);

                if (i < trivia.Count - 1)
                {
                    SyntaxTrivia trivia2 = trivia.ElementAt(i + 1);

                    if (trivia1.IsKind(SyntaxKind.EndOfLineTrivia) &&
                        trivia2.IsKind(SyntaxKind.EndOfLineTrivia))
                    {
                        changed = true;
                        i      += 2;
                        continue;
                    }
                }

                i++;
            }

            return(newTrivia);
        }
Пример #4
0
            private static XmlNameAttributeSyntax ParseNameAttribute(
                string attributeText,
                string elementName
                )
            {
                // NOTE: Rather than introducing a new code path that will have to be kept in
                // sync with other mode changes distributed throughout Lexer, SyntaxParser, and
                // DocumentationCommentParser, we'll just wrap the text in some lexable syntax
                // and then extract the piece we want.
                string commentText = string.Format(@"/// <{0} {1}/>", elementName, attributeText);

                SyntaxTriviaList leadingTrivia = SyntaxFactory.ParseLeadingTrivia(
                    commentText,
                    CSharpParseOptions.Default.WithDocumentationMode(DocumentationMode.Diagnose)
                    );

                Debug.Assert(leadingTrivia.Count == 1);
                SyntaxTrivia trivia = leadingTrivia.ElementAt(0);
                DocumentationCommentTriviaSyntax structure =
                    (DocumentationCommentTriviaSyntax)trivia.GetStructure();

                Debug.Assert(structure.Content.Count == 2);
                XmlEmptyElementSyntax elementSyntax = (XmlEmptyElementSyntax)structure.Content[1];

                Debug.Assert(elementSyntax.Attributes.Count == 1);
                return((XmlNameAttributeSyntax)elementSyntax.Attributes[0]);
            }
Пример #5
0
        private static SyntaxTriviaList RemoveTriviaAtIndex(SyntaxTriviaList newTrivia, int index)
        {
            // Remove trivia
            newTrivia = newTrivia.RemoveAt(index);

            // Remove end of line after trivia
            if (index < newTrivia.Count && newTrivia.ElementAt(index).Kind() == SyntaxKind.EndOfLineTrivia)
            {
                newTrivia = newTrivia.RemoveAt(index);
            }

            return(newTrivia);
        }
Пример #6
0
        internal static SyntaxTriviaList Normalize(SyntaxTriviaList trivia, string indentWhitespace, string eolWhitespace, bool useElasticTrivia = false)
        {
            var normalizer = new SyntaxNormalizer(trivia.FullSpan, GetDeclarationDepth(trivia.Token), indentWhitespace, eolWhitespace, useElasticTrivia);
            var result     = normalizer.RewriteTrivia(
                trivia,
                GetDeclarationDepth((SyntaxToken)trivia.ElementAt(0).Token),
                isTrailing: false,
                indentAfterLineBreak: false,
                mustHaveSeparator: false,
                lineBreaksAfter: 0);

            normalizer.Free();
            return(result);
        }
        /// <summary>
        /// Determines if the node should be json serialized based on the precedence of
        /// a //json single line comment
        /// </summary>
        public static bool ShouldBeConvertedToJson(this SyntaxNode node, SyntaxTriviaList leadingTrivia)
        {
            if (leadingTrivia == default)
            {
                return(false);
            }

            var singleLineCommentIndex = leadingTrivia.IndexOf(SyntaxKind.SingleLineCommentTrivia);

            if (singleLineCommentIndex == -1)
            {
                return(false);
            }

            // all trivia after the single line should be whitespace or end of line
            if (!leadingTrivia
                .SkipWhile((l, i) => i < singleLineCommentIndex)
                .Any(l => l.IsKind(SyntaxKind.EndOfLineTrivia) || l.IsKind(SyntaxKind.WhitespaceTrivia)))
            {
                return(false);
            }

            return(SingleLineJsonComment.IsMatch(leadingTrivia.ElementAt(singleLineCommentIndex).ToFullString()));
        }
        private static SyntaxTriviaList RemoveTriviaAtIndex(SyntaxTriviaList newTrivia, int index)
        {
            // Remove trivia
            newTrivia = newTrivia.RemoveAt(index);

            // Remove end of line after trivia
            if (index < newTrivia.Count && newTrivia.ElementAt(index).Kind() == SyntaxKind.EndOfLineTrivia)
            {
                newTrivia = newTrivia.RemoveAt(index);
            }

            return newTrivia;
        }