コード例 #1
0
 public static bool IsCommentOrDirectiveTrivia(this SyntaxTrivia t)
 {
     if (t.IsSingleLineComment())
     {
         return(true);
     }
     if (t.IsMultiLineComment())
     {
         return(true);
     }
     if (t.IsDirective)
     {
         return(true);
     }
     return(false);
 }
コード例 #2
0
        private static string GetCommentBannerText(SyntaxTrivia comment)
        {
            Contract.ThrowIfFalse(comment.IsSingleLineComment() || comment.IsMultiLineComment());

            if (comment.IsSingleLineComment())
            {
                return CreateCommentBannerTextWithPrefix(comment.ToString(), "//");
            }
            else if (comment.IsMultiLineComment())
            {
                int lineBreakStart = comment.ToString().IndexOfAny(new char[] { '\r', '\n' });

                var text = comment.ToString();
                if (lineBreakStart >= 0)
                {
                    text = text.Substring(0, lineBreakStart);
                }
                else
                {
                    text = text.EndsWith(MultiLineCommentSuffix) ? text.Substring(0, text.Length - MultiLineCommentSuffix.Length) : text;
                }

                return CreateCommentBannerTextWithPrefix(text, "/*");
            }
            else
            {
                return string.Empty;
            }
        }
コード例 #3
0
        protected override LineColumnRule GetLineColumnRuleBetween(SyntaxTrivia trivia1, LineColumnDelta existingWhitespaceBetween, bool implicitLineBreak, SyntaxTrivia trivia2)
        {
            if (IsStartOrEndOfFile(trivia1, trivia2))
            {
                return(LineColumnRule.PreserveLinesWithAbsoluteIndentation(lines: 0, indentation: 0));
            }

            // [trivia] [whitespace] [token] case
            if (trivia2.IsKind(SyntaxKind.None))
            {
                var insertNewLine = this.FormattingRules.GetAdjustNewLinesOperation(this.Token1, this.Token2) != null;

                if (IsMultilineComment(trivia1))
                {
                    return(LineColumnRule.PreserveLinesWithGivenIndentation(lines: insertNewLine ? 1 : 0));
                }

                if (insertNewLine)
                {
                    return(LineColumnRule.PreserveLinesWithDefaultIndentation(lines: 0));
                }

                if (existingWhitespaceBetween.Lines > 0 && existingWhitespaceBetween.Spaces != this.Spaces)
                {
                    return(LineColumnRule.PreserveWithGivenSpaces(spaces: this.Spaces));
                }

                return(LineColumnRule.Preserve);
            }

            // preprocessor case
            if (SyntaxFacts.IsPreprocessorDirective(trivia2.Kind()))
            {
                // Check for immovable preprocessor directives, which are bad directive trivia
                // without a preceding line break
                if (trivia2.IsKind(SyntaxKind.BadDirectiveTrivia) && existingWhitespaceBetween.Lines == 0 && !implicitLineBreak)
                {
                    _succeeded = false;
                    return(LineColumnRule.Preserve);
                }

                // if current line is the first line of the file, don't put extra line 1
                var lines = (trivia1.IsKind(SyntaxKind.None) && this.Token1.IsKind(SyntaxKind.None)) ? 0 : 1;

                if (trivia2.IsKind(SyntaxKind.RegionDirectiveTrivia) || trivia2.IsKind(SyntaxKind.EndRegionDirectiveTrivia))
                {
                    return(LineColumnRule.PreserveLinesWithDefaultIndentation(lines));
                }

                return(LineColumnRule.PreserveLinesWithAbsoluteIndentation(lines, indentation: 0));
            }

            // comments case
            if (trivia2.IsRegularOrDocComment())
            {
                // Start of new comments group.
                //
                // 1. Comment groups must contain the same kind of comments
                // 2. Every block comment is a group of its own
                if (!trivia1.IsKind(trivia2.Kind()) || trivia2.IsMultiLineComment() || trivia2.IsMultiLineDocComment() || existingWhitespaceBetween.Lines > 1)
                {
                    if (this.FormattingRules.GetAdjustNewLinesOperation(this.Token1, this.Token2) != null)
                    {
                        return(LineColumnRule.PreserveLinesWithDefaultIndentation(lines: 0));
                    }

                    return(LineColumnRule.PreserveLinesWithGivenIndentation(lines: 0));
                }

                // comments after existing comment
                if (existingWhitespaceBetween.Lines == 0)
                {
                    return(LineColumnRule.PreserveLinesWithGivenIndentation(lines: 0));
                }

                return(LineColumnRule.PreserveLinesWithFollowingPrecedingIndentation);
            }

            if (trivia2.IsKind(SyntaxKind.SkippedTokensTrivia))
            {
                // if there is any skipped tokens, it is not possible to format this trivia range.
                _succeeded = false;
            }

            return(LineColumnRule.Preserve);
        }
コード例 #4
0
 private static bool IsMultilineComment(SyntaxTrivia trivia1)
 => trivia1.IsMultiLineComment() || trivia1.IsMultiLineDocComment();
コード例 #5
0
 public static bool IsRegularOrDocComment(this SyntaxTrivia trivia)
 {
     return(trivia.IsSingleLineComment() || trivia.IsMultiLineComment() || trivia.IsDocComment());
 }
 protected override bool IsMultilineComment(SyntaxTrivia trivia)
 {
     return trivia.IsMultiLineComment() || trivia.IsMultiLineDocComment();
 }
コード例 #7
0
 protected override bool IsMultilineComment(SyntaxTrivia trivia)
 {
     return(trivia.IsMultiLineComment() || trivia.IsMultiLineDocComment());
 }
コード例 #8
0
 private static bool IsMultilineComment(SyntaxTrivia trivia1)
 {
     return(trivia1.IsMultiLineComment() || trivia1.IsMultiLineDocComment());
 }
コード例 #9
0
 public static bool IsRegularComment(this SyntaxTrivia trivia)
 {
     return(trivia.IsSingleLineComment() || trivia.IsMultiLineComment() || trivia.IsShebangDirective());
 }
コード例 #10
0
 private static bool IsMultilineComment(SyntaxTrivia trivia1)
 {
     return trivia1.IsMultiLineComment() || trivia1.IsMultiLineDocComment();
 }
 protected override bool IsMultilineComment(SyntaxTrivia trivia)
 => trivia.IsMultiLineComment() || trivia.IsMultiLineDocComment();