예제 #1
0
 public static bool ContainsGlobalStatement(this ISyntaxFacts syntaxFacts, SyntaxNode node)
 => node.ChildNodes().Any(c => c.RawKind == syntaxFacts.SyntaxKinds.GlobalStatement);
예제 #2
0
        private static SyntaxNode ReplaceHeader(ISyntaxFacts syntaxFacts, AbstractFileHeaderHelper fileHeaderHelper, SyntaxTrivia newLineTrivia, SyntaxNode root, string expectedFileHeader)
        {
            // Skip single line comments, whitespace, and end of line trivia until a blank line is encountered.
            var triviaList = root.GetLeadingTrivia();

            // True if the current line is blank so far (empty or whitespace); otherwise, false. The first line is
            // assumed to not be blank, which allows the analysis to detect a file header which follows a blank line at
            // the top of the file.
            var onBlankLine = false;

            // The set of indexes to remove from 'triviaList'. After removing these indexes, the remaining trivia (if
            // any) will be preserved in the document along with the replacement header.
            var removalList = new List <int>();

            // The number of spaces to indent the new header. This is expected to match the indentation of the header
            // which is being replaced.
            var leadingSpaces = string.Empty;

            // The number of spaces found so far on the current line. This will become 'leadingSpaces' if the spaces are
            // followed by a comment which is considered a header comment.
            var possibleLeadingSpaces = string.Empty;

            // Need to do this with index so we get the line endings correct.
            for (var i = 0; i < triviaList.Count; i++)
            {
                var triviaLine = triviaList[i];
                if (triviaLine.RawKind == syntaxFacts.SyntaxKinds.SingleLineCommentTrivia)
                {
                    if (possibleLeadingSpaces != string.Empty)
                    {
                        // One or more spaces precedes the comment. Keep track of these spaces so we can indent the new
                        // header by the same amount.
                        leadingSpaces = possibleLeadingSpaces;
                    }

                    removalList.Add(i);
                    onBlankLine = false;
                }
                else if (triviaLine.RawKind == syntaxFacts.SyntaxKinds.WhitespaceTrivia)
                {
                    if (leadingSpaces == string.Empty)
                    {
                        possibleLeadingSpaces = triviaLine.ToFullString();
                    }

                    removalList.Add(i);
                }
                else if (triviaLine.RawKind == syntaxFacts.SyntaxKinds.EndOfLineTrivia)
                {
                    possibleLeadingSpaces = string.Empty;
                    removalList.Add(i);

                    if (onBlankLine)
                    {
                        break;
                    }
                    else
                    {
                        onBlankLine = true;
                    }
                }
                else
                {
                    break;
                }
            }

            // Remove copyright lines in reverse order.
            for (var i = removalList.Count - 1; i >= 0; i--)
            {
                triviaList = triviaList.RemoveAt(removalList[i]);
            }

            var newHeaderTrivia = CreateNewHeader(syntaxFacts, leadingSpaces + fileHeaderHelper.CommentPrefix, expectedFileHeader, newLineTrivia.ToFullString());

            // Add a blank line and any remaining preserved trivia after the header.
            newHeaderTrivia = newHeaderTrivia.Add(newLineTrivia).Add(newLineTrivia).AddRange(triviaList);

            // Insert header at top of the file.
            return(root.WithLeadingTrivia(newHeaderTrivia));
        }
예제 #3
0
 private static bool IsWordOrNumber(this ISyntaxFacts syntaxFacts, SyntaxToken token)
 => syntaxFacts.IsWord(token) || syntaxFacts.IsNumericLiteral(token);
예제 #4
0
 protected abstract bool IsViableExtensionMethod(IMethodSymbol method, SyntaxNode expression, SemanticModel semanticModel, ISyntaxFacts syntaxFacts, CancellationToken cancellationToken);
예제 #5
0
 protected abstract bool TryAnalyzePatternCondition(
     ISyntaxFacts syntaxFacts, SyntaxNode conditionNode,
     [NotNullWhen(true)] out SyntaxNode?conditionPartToCheck, out bool isEquals);
예제 #6
0
 protected AbstractSyntaxTriviaService(ISyntaxFacts syntaxFacts, int endOfLineKind)
 {
     _syntaxFacts   = syntaxFacts;
     _endOfLineKind = endOfLineKind;
 }
예제 #7
0
 protected abstract bool CanAddImportForMethod(string diagnosticId, ISyntaxFacts syntaxFacts, SyntaxNode node, out TSimpleNameSyntax nameNode);
예제 #8
0
 public static bool IsGlobalAttribute(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => syntaxFacts.IsGlobalAssemblyAttribute(node) || syntaxFacts.IsGlobalModuleAttribute(node);
예제 #9
0
 public static bool IsReservedOrContextualKeyword(this ISyntaxFacts syntaxFacts, SyntaxToken token)
 => syntaxFacts.IsReservedKeyword(token) || syntaxFacts.IsContextualKeyword(token);
예제 #10
0
 public static bool IsUsingStatement(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.UsingStatement;
예제 #11
0
 public static bool IsAttribute(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.Attribute;
예제 #12
0
 public static bool IsLocalDeclarationStatement(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.LocalDeclarationStatement;
예제 #13
0
 public static bool IsForEachStatement(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.ForEachStatement;
예제 #14
0
 public static bool IsExpressionStatement(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.ExpressionStatement;
예제 #15
0
 public static void GetPartsOfAssignmentStatement(
     this ISyntaxFacts syntaxFacts, SyntaxNode statement,
     out SyntaxNode left, out SyntaxNode right)
 {
     syntaxFacts.GetPartsOfAssignmentStatement(statement, out left, out _, out right);
 }
예제 #16
0
 public static bool IsParameter(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.Parameter;
예제 #17
0
 public static SyntaxNode GetExpressionOfInvocationExpression(
     this ISyntaxFacts syntaxFacts, SyntaxNode node)
 {
     syntaxFacts.GetPartsOfInvocationExpression(node, out var expression, out _);
     return(expression);
 }
예제 #18
0
 public static bool IsTypeConstraint(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.TypeConstraint;
        protected override IEnumerable <SyntaxNode> ExtractNodesSimple(SyntaxNode?node, ISyntaxFacts syntaxFacts)
        {
            if (node == null)
            {
                yield break;
            }

            foreach (var extractedNode in base.ExtractNodesSimple(node, syntaxFacts))
            {
                yield return(extractedNode);
            }

            // `var a = b;`
            // -> `var a = b`;
            if (node is LocalDeclarationStatementSyntax localDeclaration)
            {
                yield return(localDeclaration.Declaration);
            }

            // var `a = b`;
            if (node is VariableDeclaratorSyntax declarator)
            {
                var declaration = declarator.Parent;
                if (declaration?.Parent is LocalDeclarationStatementSyntax localDeclarationStatement)
                {
                    var variables = syntaxFacts.GetVariablesOfLocalDeclarationStatement(localDeclarationStatement);
                    if (variables.Count == 1)
                    {
                        // -> `var a = b`;
                        yield return(declaration);

                        // -> `var a = b;`
                        yield return(localDeclarationStatement);
                    }
                }
            }
        }
예제 #20
0
 public static bool IsVariableDeclarator(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.VariableDeclarator;
예제 #21
0
 protected abstract bool CanAddImportForGetAsyncEnumerator(string diagnosticId, ISyntaxFacts syntaxFactsService, SyntaxNode node);
예제 #22
0
 public static bool IsTypeArgumentList(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.TypeArgumentList;
예제 #23
0
 protected AbstractUseCompoundAssignmentDiagnosticAnalyzer(
     ISyntaxFacts syntaxFacts,
     ImmutableArray <(TSyntaxKind exprKind, TSyntaxKind assignmentKind, TSyntaxKind tokenKind)> kinds)
예제 #24
0
 public static bool IsWord(this ISyntaxFacts syntaxFacts, SyntaxToken token)
 {
     return(syntaxFacts.IsIdentifier(token) ||
            syntaxFacts.IsReservedOrContextualKeyword(token) ||
            syntaxFacts.IsPreprocessorKeyword(token));
 }
예제 #25
0
 protected AbstractAddExplicitCastCodeFixProvider(ISyntaxFacts syntaxFacts)
 => SyntaxFacts = syntaxFacts;
예제 #26
0
 public static bool IsAnyMemberAccessExpression(
     this ISyntaxFacts syntaxFacts, SyntaxNode node)
 {
     return(syntaxFacts.IsSimpleMemberAccessExpression(node) || syntaxFacts.IsPointerMemberAccessExpression(node));
 }
 protected Analyzer(ISyntaxFacts syntaxFacts, Feature features)
 {
     _syntaxFacts = syntaxFacts;
     Features     = features;
 }
예제 #28
0
 public static bool IsRegularOrDocumentationComment(this ISyntaxFacts syntaxFacts, SyntaxTrivia trivia)
 => syntaxFacts.IsRegularComment(trivia) || syntaxFacts.IsDocumentationComment(trivia);
예제 #29
0
 public static bool SpansPreprocessorDirective(this ISyntaxFacts service, SyntaxNode node)
 => service.SpansPreprocessorDirective(SpecializedCollections.SingletonEnumerable(node));
예제 #30
0
 public static bool IsTupleExpression(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node)
 => node?.RawKind == syntaxFacts.SyntaxKinds.TupleExpression;