public static bool ContainsGlobalStatement(this ISyntaxFacts syntaxFacts, SyntaxNode node) => node.ChildNodes().Any(c => c.RawKind == syntaxFacts.SyntaxKinds.GlobalStatement);
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)); }
private static bool IsWordOrNumber(this ISyntaxFacts syntaxFacts, SyntaxToken token) => syntaxFacts.IsWord(token) || syntaxFacts.IsNumericLiteral(token);
protected abstract bool IsViableExtensionMethod(IMethodSymbol method, SyntaxNode expression, SemanticModel semanticModel, ISyntaxFacts syntaxFacts, CancellationToken cancellationToken);
protected abstract bool TryAnalyzePatternCondition( ISyntaxFacts syntaxFacts, SyntaxNode conditionNode, [NotNullWhen(true)] out SyntaxNode?conditionPartToCheck, out bool isEquals);
protected AbstractSyntaxTriviaService(ISyntaxFacts syntaxFacts, int endOfLineKind) { _syntaxFacts = syntaxFacts; _endOfLineKind = endOfLineKind; }
protected abstract bool CanAddImportForMethod(string diagnosticId, ISyntaxFacts syntaxFacts, SyntaxNode node, out TSimpleNameSyntax nameNode);
public static bool IsGlobalAttribute(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => syntaxFacts.IsGlobalAssemblyAttribute(node) || syntaxFacts.IsGlobalModuleAttribute(node);
public static bool IsReservedOrContextualKeyword(this ISyntaxFacts syntaxFacts, SyntaxToken token) => syntaxFacts.IsReservedKeyword(token) || syntaxFacts.IsContextualKeyword(token);
public static bool IsUsingStatement(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode node) => node?.RawKind == syntaxFacts.SyntaxKinds.UsingStatement;
public static bool IsAttribute(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => node?.RawKind == syntaxFacts.SyntaxKinds.Attribute;
public static bool IsLocalDeclarationStatement(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => node?.RawKind == syntaxFacts.SyntaxKinds.LocalDeclarationStatement;
public static bool IsForEachStatement(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => node?.RawKind == syntaxFacts.SyntaxKinds.ForEachStatement;
public static bool IsExpressionStatement(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => node?.RawKind == syntaxFacts.SyntaxKinds.ExpressionStatement;
public static void GetPartsOfAssignmentStatement( this ISyntaxFacts syntaxFacts, SyntaxNode statement, out SyntaxNode left, out SyntaxNode right) { syntaxFacts.GetPartsOfAssignmentStatement(statement, out left, out _, out right); }
public static bool IsParameter(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => node?.RawKind == syntaxFacts.SyntaxKinds.Parameter;
public static SyntaxNode GetExpressionOfInvocationExpression( this ISyntaxFacts syntaxFacts, SyntaxNode node) { syntaxFacts.GetPartsOfInvocationExpression(node, out var expression, out _); return(expression); }
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); } } } }
public static bool IsVariableDeclarator(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => node?.RawKind == syntaxFacts.SyntaxKinds.VariableDeclarator;
protected abstract bool CanAddImportForGetAsyncEnumerator(string diagnosticId, ISyntaxFacts syntaxFactsService, SyntaxNode node);
public static bool IsTypeArgumentList(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => node?.RawKind == syntaxFacts.SyntaxKinds.TypeArgumentList;
protected AbstractUseCompoundAssignmentDiagnosticAnalyzer( ISyntaxFacts syntaxFacts, ImmutableArray <(TSyntaxKind exprKind, TSyntaxKind assignmentKind, TSyntaxKind tokenKind)> kinds)
public static bool IsWord(this ISyntaxFacts syntaxFacts, SyntaxToken token) { return(syntaxFacts.IsIdentifier(token) || syntaxFacts.IsReservedOrContextualKeyword(token) || syntaxFacts.IsPreprocessorKeyword(token)); }
protected AbstractAddExplicitCastCodeFixProvider(ISyntaxFacts syntaxFacts) => SyntaxFacts = syntaxFacts;
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; }
public static bool IsRegularOrDocumentationComment(this ISyntaxFacts syntaxFacts, SyntaxTrivia trivia) => syntaxFacts.IsRegularComment(trivia) || syntaxFacts.IsDocumentationComment(trivia);
public static bool SpansPreprocessorDirective(this ISyntaxFacts service, SyntaxNode node) => service.SpansPreprocessorDirective(SpecializedCollections.SingletonEnumerable(node));
public static bool IsTupleExpression(this ISyntaxFacts syntaxFacts, [NotNullWhen(true)] SyntaxNode?node) => node?.RawKind == syntaxFacts.SyntaxKinds.TupleExpression;