public static string GetCommentText(this SyntaxTrivia trivia)
        {
            var commentText = trivia.ToString();

            if (trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia)
            {
                if (commentText.StartsWith("//"))
                {
                    commentText = commentText.Substring(2);
                }

                return(commentText.TrimStart());
            }
            else if (trivia.CSharpKind() == SyntaxKind.MultiLineCommentTrivia)
            {
                var textBuilder = new StringBuilder();

                if (commentText.EndsWith("*/"))
                {
                    commentText = commentText.Substring(0, commentText.Length - 2);
                }

                if (commentText.StartsWith("/*"))
                {
                    commentText = commentText.Substring(2);
                }

                commentText = commentText.Trim();

                var newLine = Environment.NewLine;
                var lines   = commentText.Split(new[] { newLine }, StringSplitOptions.None);
                foreach (var line in lines)
                {
                    var trimmedLine = line.Trim();

                    // Note: we trim leading '*' characters in multi-line comments.
                    // If the '*' was intentional, sorry, it's gone.
                    if (trimmedLine.StartsWith("*"))
                    {
                        trimmedLine = trimmedLine.TrimStart('*');
                        trimmedLine = trimmedLine.TrimStart();
                    }

                    textBuilder.AppendLine(trimmedLine);
                }

                // remove last line break
                textBuilder.Remove(textBuilder.Length - newLine.Length, newLine.Length);

                return(textBuilder.ToString());
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #2
0
            private bool OnSkippedTokensOrText(SyntaxTrivia trivia)
            {
                if (trivia.CSharpKind() != SyntaxKind.SkippedTokensTrivia &&
                    trivia.CSharpKind() != SyntaxKind.PreprocessingMessageTrivia)
                {
                    return(false);
                }

                return(Contract.FailWithReturn <bool>("This can't happen"));
            }
Beispiel #3
0
 public override IDom CreateFrom(SyntaxTrivia trivia, IDom parent, OutputContext context)
 {
     if (trivia.CSharpKind() == SyntaxKind.RegionDirectiveTrivia)
     {
         return(CreateStartRegion(trivia, parent, context));
     }
     else if (trivia.CSharpKind() == SyntaxKind.EndRegionDirectiveTrivia)
     {
         return(CreateEndRegion(trivia, parent, context));
     }
     return(CreateComment(trivia, parent, context));
 }
Beispiel #4
0
        private static bool NeedsSeparatorBetween(SyntaxTrivia trivia)
        {
            switch (trivia.CSharpKind())
            {
            case SyntaxKind.None:
            case SyntaxKind.WhitespaceTrivia:
            case SyntaxKind.DocumentationCommentExteriorTrivia:
                return(false);

            default:
                return(!SyntaxFacts.IsPreprocessorDirective(trivia.CSharpKind()));
            }
        }
Beispiel #5
0
            private bool OnTouchedNoisyCharacter(SyntaxTrivia trivia)
            {
                if (trivia.IsElastic() ||
                    trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia ||
                    trivia.CSharpKind() == SyntaxKind.EndOfLineTrivia)
                {
                    return(false);
                }

                this.touchedNoisyCharacterOnCurrentLine = true;
                this.hasTrailingSpace = false;

                return(false);
            }
Beispiel #6
0
        private static bool EndsInLineBreak(SyntaxTrivia trivia)
        {
            if (trivia.CSharpKind() == SyntaxKind.EndOfLineTrivia)
            {
                return(true);
            }

            if (trivia.CSharpKind() == SyntaxKind.PreprocessingMessageTrivia || trivia.CSharpKind() == SyntaxKind.DisabledTextTrivia)
            {
                var text = trivia.ToFullString();
                return(text.Length > 0 && SyntaxFacts.IsNewLine(text.Last()));
            }

            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// Finds a token according to the following rules:
        /// 1) If position matches the End of the node/s FullSpan and the node is CompilationUnit,
        ///    then EoF is returned.
        ///
        ///  2) If node.FullSpan.Contains(position) then the token that contains given position is
        ///     returned.
        ///
        ///  3) Otherwise an ArgumentOutOfRangeException is thrown
        /// </summary>
        internal SyntaxToken FindTokenIncludingCrefAndNameAttributes(int position)
        {
            SyntaxToken nonTriviaToken = this.FindToken(position, findInsideTrivia: false);

            SyntaxTrivia trivia = GetTriviaFromSyntaxToken(position, nonTriviaToken);

            if (!SyntaxFacts.IsDocumentationCommentTrivia(trivia.CSharpKind()))
            {
                return(nonTriviaToken);
            }

            Debug.Assert(trivia.HasStructure);
            SyntaxToken triviaToken = ((CSharpSyntaxNode)trivia.GetStructure()).FindTokenInternal(position);

            // CONSIDER: We might want to use the trivia token anywhere within a doc comment.
            // Otherwise, we'll fall back on the enclosing scope outside of name and cref
            // attribute values.
            CSharpSyntaxNode curr = (CSharpSyntaxNode)triviaToken.Parent;

            while (curr != null)
            {
                // Don't return a trivia token unless we're in the scope of a cref or name attribute.
                if (curr.Kind == SyntaxKind.XmlCrefAttribute || curr.Kind == SyntaxKind.XmlNameAttribute)
                {
                    return(LookupPosition.IsInXmlAttributeValue(position, (XmlAttributeSyntax)curr)
                        ? triviaToken
                        : nonTriviaToken);
                }

                curr = curr.Parent;
            }

            return(nonTriviaToken);
        }
Beispiel #8
0
            private bool OnWhitespace(SyntaxTrivia trivia)
            {
                if (trivia.CSharpKind() != SyntaxKind.WhitespaceTrivia)
                {
                    return(false);
                }

                // there was noisy char after end of line trivia
                if (!this.UseIndentation || this.touchedNoisyCharacterOnCurrentLine)
                {
                    this.hasTrailingSpace = true;
                    return(false);
                }

                // right after end of line trivia. calculate indentation for current line
                Debug.Assert(trivia.ToString() == trivia.ToFullString());
                var text = trivia.ToString();

                // if text contains tab, we will give up perf optimization and use more expensive one to see whether we need to replace this trivia
                if (text.IndexOf('\t') >= 0)
                {
                    return(true);
                }

                this.indentation += text.ConvertTabToSpace(this.optionSet.GetOption(FormattingOptions.TabSize, LanguageNames.CSharp), this.indentation, text.Length);

                return(false);
            }
Beispiel #9
0
            public override void VisitTrivia(SyntaxTrivia trivia)
            {
                if (trivia.HasStructure)
                {
                    Visit(trivia.GetStructure());
                }

                switch (trivia.CSharpKind())
                {
                case SyntaxKind.MultiLineCommentTrivia:
                case SyntaxKind.SingleLineCommentTrivia:
                case SyntaxKind.SingleLineDocumentationCommentTrivia:
                case SyntaxKind.MultiLineDocumentationCommentTrivia:
                case SyntaxKind.DocumentationCommentExteriorTrivia:
                    Mark(trivia, "comment"); break;

                case SyntaxKind.EndOfLineTrivia:
                case SyntaxKind.WhitespaceTrivia:
                    Mark(trivia, "whitespace"); break;

                case SyntaxKind.RegionDirectiveTrivia:
                    Mark(trivia, "begin-region"); break;

                case SyntaxKind.EndRegionDirectiveTrivia:
                    Mark(trivia, "end-region"); break;

                case SyntaxKind.DisabledTextTrivia:
                    Mark(trivia, "disabled-text"); break;
                }
            }
            public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
            {
                if (trivia.CSharpKind() == SyntaxKind.DocumentationCommentExteriorTrivia)
                {
                    if (IsBeginningOrEndOfDocumentComment(trivia))
                    {
                        return(base.VisitTrivia(trivia));
                    }
                    else
                    {
                        var triviaText = trivia.ToFullString();

                        var newTriviaText = triviaText.AdjustIndentForXmlDocExteriorTrivia(
                            forceIndentation,
                            indentation,
                            indentationDelta,
                            this.optionSet.GetOption(FormattingOptions.UseTabs, LanguageNames.CSharp),
                            this.optionSet.GetOption(FormattingOptions.TabSize, LanguageNames.CSharp));

                        if (triviaText == newTriviaText)
                        {
                            return(base.VisitTrivia(trivia));
                        }

                        var parsedNewTrivia = SyntaxFactory.DocumentationCommentExterior(newTriviaText);

                        return(parsedNewTrivia);
                    }
                }

                return(base.VisitTrivia(trivia));
            }
Beispiel #11
0
 private bool RealWhitespace(SyntaxTrivia trivia)
 {
     if (trivia.CSharpKind() != SyntaxKind.WhitespaceTrivia)
     {
         return(false);
     }
     return(!string.IsNullOrEmpty(trivia.ToFullString()));
 }
Beispiel #12
0
            public override void VisitTrivia(SyntaxTrivia trivia)
            {
                if (trivia.CSharpKind() != SyntaxKind.None)
                {
                    this.Trivia.Add(trivia);
                }

                base.VisitTrivia(trivia);
            }
Beispiel #13
0
        private static int GetDeclarationDepth(SyntaxTrivia trivia)
        {
            if (SyntaxFacts.IsPreprocessorDirective(trivia.CSharpKind()))
            {
                return(0);
            }

            return(GetDeclarationDepth((SyntaxToken)trivia.Token));
        }
        private static bool EndsInLineBreak(SyntaxTrivia trivia)
        {
            if (trivia.CSharpKind() == SyntaxKind.EndOfLineTrivia)
            {
                return(true);
            }

            return(false);
        }
Beispiel #15
0
        private Whitespace2 GetWhitespace(SyntaxTrivia trivia)
        {
            var str = "";

            if (trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia)
            {
                str = trivia.ToFullString();
            }
            return(new Whitespace2(LanguagePart.Parent, LanguageElement.Detail, str, "", ""));
        }
Beispiel #16
0
 private void ClassifyPreprocessorTrivia(SyntaxTrivia trivia, bool allowComments)
 {
     if (allowComments && trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia)
     {
         AddClassification(trivia, ClassificationTypeNames.Comment);
     }
     else
     {
         AddClassification(trivia, ClassificationTypeNames.PreprocessorText);
     }
 }
Beispiel #17
0
        private ApiCall QuoteTrivia(SyntaxTrivia syntaxTrivia)
        {
            string factoryMethodName = "SyntaxFactory.Trivia";
            string text = syntaxTrivia.ToString();

            if (syntaxTrivia.FullSpan.Length == 0 || (syntaxTrivia.IsKind(SyntaxKind.WhitespaceTrivia) && UseDefaultFormatting))
            {
                return(null);
            }

            FieldInfo field = null;

            if (triviaFactoryFields.TryGetValue(syntaxTrivia.ToString( ), out field) &&
                ((SyntaxTrivia)field.GetValue(null)).CSharpKind( ) == syntaxTrivia.CSharpKind( ))
            {
                return(UseDefaultFormatting ? null : new ApiCall(null, "SyntaxFactory." + field.Name));
            }

            if (!string.IsNullOrEmpty(text) && string.IsNullOrWhiteSpace(text) && syntaxTrivia.IsKind(SyntaxKind.WhitespaceTrivia))
            {
                if (UseDefaultFormatting)
                {
                    return(null);
                }
                factoryMethodName = "SyntaxFactory.Whitespace";
            }

            if (syntaxTrivia.IsTriviaAny(SyntaxKind.SingleLineCommentTrivia,
                                         SyntaxKind.MultiLineCommentTrivia))
            {
                factoryMethodName = "SyntaxFactory.Comment";
            }
            if (syntaxTrivia.IsKind(SyntaxKind.PreprocessingMessageTrivia))
            {
                factoryMethodName = "SyntaxFactory.PreprocessingMessage";
            }
            if (syntaxTrivia.IsKind(SyntaxKind.DisabledTextTrivia))
            {
                factoryMethodName = "SyntaxFactory.DisabledText";
            }
            if (syntaxTrivia.IsKind(SyntaxKind.DocumentationCommentExteriorTrivia))
            {
                factoryMethodName = "SyntaxFactory.DocumentationCommentExterior";
            }

            object argument = "@\"" + Escape(syntaxTrivia.ToString()) + "\"";

            if (syntaxTrivia.HasStructure)
            {
                argument = QuoteNode(syntaxTrivia.GetStructure( ), "Structure");
            }

            return(new ApiCall(null, factoryMethodName, ArgList.Create(argument)));
        }
Beispiel #18
0
        private static bool NeedsLineBreakBefore(SyntaxTrivia trivia)
        {
            switch (trivia.CSharpKind())
            {
            case SyntaxKind.DocumentationCommentExteriorTrivia:
                return(true);

            default:
                return(false);
            }
        }
Beispiel #19
0
        private IDom CreateComment(SyntaxTrivia trivia, IDom parent, OutputContext context)
        {
            var isMultiline     = (trivia.CSharpKind() == SyntaxKind.MultiLineCommentTrivia);
            var precedingTrivia = trivia.Token.LeadingTrivia.PreviousSiblings(trivia);
            var commentText     = trivia.ToString();
            var tuple           = context.Corporation.CreateFromWorker.ExtractComment(trivia.ToString());
            var commentString   = tuple.Item2;
            var newComment      = new RDomComment(parent, trivia, tuple.Item2, isMultiline);

            triviaManager.StoreWhitespaceForComment(newComment, precedingTrivia, tuple.Item1, tuple.Item3);
            return(newComment);
        }
Beispiel #20
0
            private bool OnRegion(SyntaxTrivia trivia, int currentIndex)
            {
                if (trivia.CSharpKind() != SyntaxKind.RegionDirectiveTrivia &&
                    trivia.CSharpKind() != SyntaxKind.EndRegionDirectiveTrivia)
                {
                    return(false);
                }

                if (!this.UseIndentation)
                {
                    return(true);
                }

                if (indentation != this.context.GetBaseIndentation(trivia.SpanStart))
                {
                    return(true);
                }

                ResetStateAfterNewLine(currentIndex);
                return(false);
            }
Beispiel #21
0
 private void ClassifyTrivia(SyntaxTrivia trivia)
 {
     if (trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia || trivia.CSharpKind() == SyntaxKind.MultiLineCommentTrivia)
     {
         AddClassification(trivia, ClassificationTypeNames.Comment);
     }
     else if (trivia.CSharpKind() == SyntaxKind.DisabledTextTrivia)
     {
         AddClassification(trivia, ClassificationTypeNames.ExcludedCode);
     }
     else if (trivia.CSharpKind() == SyntaxKind.SkippedTokensTrivia)
     {
         ClassifySkippedTokens((SkippedTokensTriviaSyntax)trivia.GetStructure());
     }
     else if (trivia.IsDocComment())
     {
         ClassifyDocumentationComment((DocumentationCommentTriviaSyntax)trivia.GetStructure());
     }
     else if (trivia.CSharpKind() == SyntaxKind.DocumentationCommentExteriorTrivia)
     {
         AddClassification(trivia, ClassificationTypeNames.XmlDocCommentDelimiter);
     }
     else if (SyntaxFacts.IsPreprocessorDirective(trivia.CSharpKind()))
     {
         ClassifyPreprocessorDirective((DirectiveTriviaSyntax)trivia.GetStructure());
     }
 }
        public static bool IsCompleteMultiLineComment(this SyntaxTrivia trivia)
        {
            if (trivia.CSharpKind() != SyntaxKind.MultiLineCommentTrivia)
            {
                return(false);
            }

            var text = trivia.ToFullString();

            return(text.Length >= 4 &&
                   text[text.Length - 1] == '/' &&
                   text[text.Length - 2] == '*');
        }
        private static bool NeedsSeparatorBetween(SyntaxTrivia trivia)
        {
            switch (trivia.CSharpKind())
            {
            case SyntaxKind.None:
            case SyntaxKind.WhitespaceTrivia:
            case SyntaxKind.DocumentationCommentExteriorTrivia:
                return(false);

            default:
                return(true);
            }
        }
Beispiel #24
0
            public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
            {
                if (trivia.CSharpKind() == SyntaxKind.None)
                {
                    return(trivia);
                }

                if (trivia.HasStructure)
                {
                    return(base.VisitTrivia(trivia));
                }

                return(base.VisitTrivia(trivia).WithAdditionalAnnotations(annotation));
            }
Beispiel #25
0
        private static bool NeedsLineBreakAfter(SyntaxTrivia trivia, bool isTrailingTrivia)
        {
            switch (trivia.CSharpKind())
            {
            case SyntaxKind.SingleLineCommentTrivia:
                return(true);

            case SyntaxKind.MultiLineCommentTrivia:
                return(!isTrailingTrivia);

            default:
                return(false);
            }
        }
Beispiel #26
0
 private bool IsComment(SyntaxTrivia trivia)
 {
     return(trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia ||
            trivia.CSharpKind() == SyntaxKind.SingleLineDocumentationCommentTrivia ||
            trivia.CSharpKind() == SyntaxKind.DocumentationCommentExteriorTrivia ||
            trivia.CSharpKind() == SyntaxKind.MultiLineCommentTrivia ||
            trivia.CSharpKind() == SyntaxKind.MultiLineDocumentationCommentTrivia ||
            trivia.CSharpKind() == SyntaxKind.XmlComment);
 }
Beispiel #27
0
            public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
            {
                if (trivia.CSharpKind() == SyntaxKind.None)
                {
                    return(trivia);
                }

                // annotation will be set by actual structure trivia
                if (trivia.HasStructure)
                {
                    return(base.VisitTrivia(trivia));
                }

                return(this.triviaMap[trivia].CopyAnnotationsTo(base.VisitTrivia(trivia)));
            }
        public static string GetKind(this SyntaxTrivia trivia)
        {
            var kind = string.Empty;

            if (trivia.Language == LanguageNames.CSharp)
            {
                kind = trivia.CSharpKind().ToString();
            }
            else
            {
                kind = trivia.VBKind().ToString();
            }

            return(kind);
        }
Beispiel #29
0
        public static string GetKind(this SyntaxTrivia trivia)
        {
            var kind = string.Empty;

            if (trivia.Language == LanguageNames.CSharp)
            {
                kind = trivia.CSharpKind().ToString();
            }
            //else
            //{
            //	kind = trivia.VisualBasicKind().ToString();
            //}

            return(kind);
        }
Beispiel #30
0
        private static bool NeedsIndentAfterLineBreak(SyntaxTrivia trivia)
        {
            switch (trivia.CSharpKind())
            {
            case SyntaxKind.SingleLineCommentTrivia:
            case SyntaxKind.MultiLineCommentTrivia:
            case SyntaxKind.DocumentationCommentExteriorTrivia:
            case SyntaxKind.SingleLineDocumentationCommentTrivia:
            case SyntaxKind.MultiLineDocumentationCommentTrivia:
                return(true);

            default:
                return(false);
            }
        }
Beispiel #31
0
 private void ClassifyPreprocessorTrivia(SyntaxTrivia trivia, bool allowComments)
 {
     if (allowComments && trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia)
     {
         AddClassification(trivia, ClassificationTypeNames.Comment);
     }
     else
     {
         AddClassification(trivia, ClassificationTypeNames.PreprocessorText);
     }
 }
            private static void AnalyzeWhitespacesInTrivia(SyntaxTrivia trivia, ref AnalysisResult result)
            {
                // trivia already has text. getting text should be noop
                Debug.Assert(trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia);
                Debug.Assert(trivia.Width() == trivia.FullWidth());

                int space = 0;
                int tab = 0;
                int unknownWhitespace = 0;

                var text = trivia.ToString();
                for (int i = 0; i < trivia.Width(); i++)
                {
                    if (text[i] == ' ')
                    {
                        space++;
                    }
                    else if (text[i] == '\t')
                    {
                        if (result.Space > 0)
                        {
                            result.HasTabAfterSpace = true;
                        }

                        tab++;
                    }
                    else
                    {
                        unknownWhitespace++;
                    }
                }

                // set result
                result.Space += space;
                result.Tab += tab;
                result.HasUnknownWhitespace |= unknownWhitespace > 0;
                result.TreatAsElastic |= trivia.IsElastic();
            }
Beispiel #33
0
		public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
		{
			var comment = trivia.ToString();
			if (trivia.CSharpKind() == SyntaxKind.MultiLineCommentTrivia)
			{
				if (!comment.StartsWith("/*uncomment"))
				{
					bool shouldCreateTextBlock = trivia.GetParents().Count(p => IsNestingParent(p, trivia)) <= 1;
					if (shouldCreateTextBlock)
						Blocks.Add(ExtractMarkDownFromComment(trivia));
				}
			}
			else if (trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia)
			{
				if (comment.StartsWith("//#"))
				{
					var parts = comment.Split(new[] { ' ' }, 2);
					if (parts[0] == "//#video") EmbedVideo(parts[1]);
                    if (parts[0] == "//#include") EmbedCode(parts[1]);
                    if (parts[0] == "//#para") EmbedPara(parts[1]);
                    if (parts[0] == "//#gallery") EmbedGallery(parts[1]);
				}
			}
			return base.VisitTrivia(trivia);
		}
Beispiel #34
0
            public override SyntaxTrivia VisitTrivia(SyntaxTrivia trivia)
            {
                SyntaxTrivia updatedTrivia = base.VisitTrivia(trivia);
                if (trivia.CSharpKind() == SyntaxKind.RegionDirectiveTrivia ||
                    trivia.CSharpKind() == SyntaxKind.EndRegionDirectiveTrivia)
                {
                    // Remove the trivia entirely by returning default(SyntaxTrivia).
                    updatedTrivia = default(SyntaxTrivia);
                }

                return updatedTrivia;
            }
Beispiel #35
0
            public override void VisitTrivia(SyntaxTrivia trivia)
            {
                bool isDocComment = SyntaxFacts.IsDocumentationCommentTrivia(trivia.CSharpKind());
                if (isDocComment ||
                    trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia ||
                    trivia.CSharpKind() == SyntaxKind.MultiLineCommentTrivia)
                {
                    Results.AppendLine();
                    Results.Append(trivia.ToFullString().Trim());
                    Results.Append(" (Parent Token: ");
                    Results.Append(trivia.Token.CSharpKind());
                    Results.Append(")");
                    if (isDocComment)
                    {
                        // Trivia for xml documentation comments have addditional 'structure'
                        // available under a child DocumentationCommentSyntax.
                        Assert.IsTrue(trivia.HasStructure);
                        var documentationComment =
                            (DocumentationCommentTriviaSyntax)trivia.GetStructure();
                        Assert.IsTrue(documentationComment.ParentTrivia == trivia);
                        Results.Append(" (Structured)");
                    }
                }

                base.VisitTrivia(trivia);
            }
Beispiel #36
0
 private void ClassifyTrivia(SyntaxTrivia trivia)
 {
     if (trivia.CSharpKind() == SyntaxKind.SingleLineCommentTrivia || trivia.CSharpKind() == SyntaxKind.MultiLineCommentTrivia)
     {
         AddClassification(trivia, ClassificationTypeNames.Comment);
     }
     else if (trivia.CSharpKind() == SyntaxKind.DisabledTextTrivia)
     {
         AddClassification(trivia, ClassificationTypeNames.ExcludedCode);
     }
     else if (trivia.CSharpKind() == SyntaxKind.SkippedTokensTrivia)
     {
         ClassifySkippedTokens((SkippedTokensTriviaSyntax)trivia.GetStructure());
     }
     else if (trivia.IsDocComment())
     {
         ClassifyDocumentationComment((DocumentationCommentTriviaSyntax)trivia.GetStructure());
     }
     else if (trivia.CSharpKind() == SyntaxKind.DocumentationCommentExteriorTrivia)
     {
         AddClassification(trivia, ClassificationTypeNames.XmlDocCommentDelimiter);
     }
     else if (SyntaxFacts.IsPreprocessorDirective(trivia.CSharpKind()))
     {
         ClassifyPreprocessorDirective((DirectiveTriviaSyntax)trivia.GetStructure());
     }
 }
        public override void VisitTrivia(SyntaxTrivia trivia)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (IsSyntacticallyFilteredOut(trivia.FullSpan))
            {
                return;
            }

            if (!IsValidLocation && SyntaxFacts.IsDocumentationCommentTrivia(trivia.CSharpKind()))
            {
                int start = trivia.Position; // FullSpan start to include /** or ///
                const int length = 1; //Match dev11: span is just one character
                diagnostics.Add(ErrorCode.WRN_UnprocessedXMLComment, new SourceLocation(trivia.SyntaxTree, new TextSpan(start, length)));
            }
            base.VisitTrivia(trivia);
        }
 private bool IsWhitespace(SyntaxTrivia trivia)
 {
     return trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia
         || trivia.CSharpKind() == SyntaxKind.EndOfLineTrivia;
 }
            private bool OnWhitespace(SyntaxTrivia trivia)
            {
                if (trivia.CSharpKind() != SyntaxKind.WhitespaceTrivia)
                {
                    return false;
                }

                // there was noisy char after end of line trivia
                if (!this.UseIndentation || this.touchedNoisyCharacterOnCurrentLine)
                {
                    this.hasTrailingSpace = true;
                    return false;
                }

                // right after end of line trivia. calculate indentation for current line
                Debug.Assert(trivia.ToString() == trivia.ToFullString());
                var text = trivia.ToString();

                // if text contains tab, we will give up perf optimization and use more expensive one to see whether we need to replace this trivia
                if (text.IndexOf('\t') >= 0)
                {
                    return true;
                }

                this.indentation += text.ConvertTabToSpace(this.optionSet.GetOption(FormattingOptions.TabSize, LanguageNames.CSharp), this.indentation, text.Length);

                return false;
            }
Beispiel #40
0
            public override void VisitTrivia(SyntaxTrivia trivia)
            {
                if (trivia.HasStructure)
                    Visit(trivia.GetStructure());

                switch (trivia.CSharpKind())
                {
                    case SyntaxKind.MultiLineCommentTrivia:
                    case SyntaxKind.SingleLineCommentTrivia:
                    case SyntaxKind.SingleLineDocumentationCommentTrivia:
                    case SyntaxKind.MultiLineDocumentationCommentTrivia:
                    case SyntaxKind.DocumentationCommentExteriorTrivia:
                        Mark(trivia, "comment"); break;

                    case SyntaxKind.EndOfLineTrivia:
                    case SyntaxKind.WhitespaceTrivia:
                        Mark(trivia, "whitespace"); break;

                    case SyntaxKind.RegionDirectiveTrivia:
                        Mark(trivia, "begin-region"); break;

                    case SyntaxKind.EndRegionDirectiveTrivia:
                        Mark(trivia, "end-region"); break;

                    case SyntaxKind.DisabledTextTrivia:
                        Mark(trivia, "disabled-text"); break;
                }
            }
            private bool OnTouchedNoisyCharacter(SyntaxTrivia trivia)
            {
                if (trivia.IsElastic() ||
                    trivia.CSharpKind() == SyntaxKind.WhitespaceTrivia ||
                    trivia.CSharpKind() == SyntaxKind.EndOfLineTrivia)
                {
                    return false;
                }

                this.touchedNoisyCharacterOnCurrentLine = true;
                this.hasTrailingSpace = false;

                return false;
            }
            private bool OnPreprocessor(SyntaxTrivia trivia, int currentIndex)
            {
                if (!SyntaxFacts.IsPreprocessorDirective(trivia.CSharpKind()))
                {
                    return false;
                }

                if (!this.UseIndentation)
                {
                    return true;
                }

                // preprocessor must be at from column 0
                if (this.indentation != 0)
                {
                    return true;
                }

                ResetStateAfterNewLine(currentIndex);
                return false;
            }
            private bool OnRegion(SyntaxTrivia trivia, int currentIndex)
            {
                if (trivia.CSharpKind() != SyntaxKind.RegionDirectiveTrivia &&
                    trivia.CSharpKind() != SyntaxKind.EndRegionDirectiveTrivia)
                {
                    return false;
                }

                if (!this.UseIndentation)
                {
                    return true;
                }

                if (indentation != this.context.GetBaseIndentation(trivia.SpanStart))
                {
                    return true;
                }

                ResetStateAfterNewLine(currentIndex);
                return false;
            }
            private bool OnSkippedTokensOrText(SyntaxTrivia trivia)
            {
                if (trivia.CSharpKind() != SyntaxKind.SkippedTokensTrivia &&
                    trivia.CSharpKind() != SyntaxKind.PreprocessingMessageTrivia)
                {
                    return false;
                }

                return Contract.FailWithReturn<bool>("This can't happen");
            }
            private bool OnEndOfLine(SyntaxTrivia trivia, int currentIndex)
            {
                if (trivia.CSharpKind() != SyntaxKind.EndOfLineTrivia)
                {
                    return false;
                }

                // end of line trivia right after whitespace trivia
                if (this.hasTrailingSpace)
                {
                    // has trailing whitespace
                    return true;
                }

                // empty line with spaces. remove it.
                if (this.indentation > 0 && !this.touchedNoisyCharacterOnCurrentLine)
                {
                    return true;
                }

                ResetStateAfterNewLine(currentIndex);
                return false;
            }