|
| 1 | +// <auto-generated/> |
| 2 | +// https://raw.githubusercontent.com/dotnet/roslyn-analyzers/84fb81c27e0554eadf6b12f97eb52c7cd2803c7e/src/Utilities/Refactoring.CSharp/CSharpSyntaxFacts.cs |
| 3 | + |
| 4 | +#nullable enable |
| 5 | + |
| 6 | +// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. |
| 7 | + |
| 8 | +#nullable disable warnings |
| 9 | + |
| 10 | +using System.Collections.Immutable; |
| 11 | +using System.Diagnostics.CodeAnalysis; |
| 12 | +using System.Linq; |
| 13 | +using Analyzer.Utilities.Extensions; |
| 14 | +using Microsoft.CodeAnalysis; |
| 15 | +using Microsoft.CodeAnalysis.CSharp; |
| 16 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 17 | + |
| 18 | +namespace Analyzer.Utilities |
| 19 | +{ |
| 20 | + internal sealed class CSharpSyntaxFacts : AbstractSyntaxFacts, ISyntaxFacts |
| 21 | + { |
| 22 | + public static CSharpSyntaxFacts Instance { get; } = new CSharpSyntaxFacts(); |
| 23 | + |
| 24 | + private CSharpSyntaxFacts() |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + public override ISyntaxKinds SyntaxKinds => CSharpSyntaxKinds.Instance; |
| 29 | + |
| 30 | + public SyntaxNode GetExpressionOfExpressionStatement(SyntaxNode node) |
| 31 | + => ((ExpressionStatementSyntax)node).Expression; |
| 32 | + |
| 33 | + public bool IsSimpleAssignmentStatement(SyntaxNode statement) |
| 34 | + { |
| 35 | + return statement is ExpressionStatementSyntax exprStatement |
| 36 | + && exprStatement.Expression.IsKind(SyntaxKind.SimpleAssignmentExpression); |
| 37 | + } |
| 38 | + |
| 39 | + public void GetPartsOfAssignmentExpressionOrStatement(SyntaxNode statement, out SyntaxNode left, out SyntaxToken operatorToken, out SyntaxNode right) |
| 40 | + { |
| 41 | + var expression = statement; |
| 42 | + if (statement is ExpressionStatementSyntax expressionStatement) |
| 43 | + { |
| 44 | + expression = expressionStatement.Expression; |
| 45 | + } |
| 46 | + |
| 47 | + var assignment = (AssignmentExpressionSyntax)expression; |
| 48 | + left = assignment.Left; |
| 49 | + operatorToken = assignment.OperatorToken; |
| 50 | + right = assignment.Right; |
| 51 | + } |
| 52 | + |
| 53 | + public override SyntaxList<SyntaxNode> GetAttributeLists(SyntaxNode node) |
| 54 | + => node.GetAttributeLists(); |
| 55 | + |
| 56 | + public SeparatedSyntaxList<SyntaxNode> GetVariablesOfLocalDeclarationStatement(SyntaxNode node) |
| 57 | + => ((LocalDeclarationStatementSyntax)node).Declaration.Variables; |
| 58 | + |
| 59 | + public SyntaxNode GetInitializerOfVariableDeclarator(SyntaxNode node) |
| 60 | + => ((VariableDeclaratorSyntax)node).Initializer; |
| 61 | + |
| 62 | + public SyntaxNode? GetValueOfEqualsValueClause(SyntaxNode? node) |
| 63 | + => ((EqualsValueClauseSyntax?)node)?.Value; |
| 64 | + |
| 65 | + public bool IsOnTypeHeader(SyntaxNode root, int position, bool fullHeader, [NotNullWhen(true)] out SyntaxNode? typeDeclaration) |
| 66 | + { |
| 67 | + var node = TryGetAncestorForLocation<BaseTypeDeclarationSyntax>(root, position); |
| 68 | + if (node is null) |
| 69 | + { |
| 70 | + typeDeclaration = null; |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + typeDeclaration = node; |
| 75 | + var lastToken = (node as TypeDeclarationSyntax)?.TypeParameterList?.GetLastToken() ?? node.Identifier; |
| 76 | + if (fullHeader) |
| 77 | + lastToken = node.BaseList?.GetLastToken() ?? lastToken; |
| 78 | + |
| 79 | + return IsOnHeader(root, position, node, lastToken); |
| 80 | + } |
| 81 | + |
| 82 | + public bool IsOnPropertyDeclarationHeader(SyntaxNode root, int position, [NotNullWhen(true)] out SyntaxNode? propertyDeclaration) |
| 83 | + { |
| 84 | + var node = TryGetAncestorForLocation<PropertyDeclarationSyntax>(root, position); |
| 85 | + if (node is null) |
| 86 | + { |
| 87 | + propertyDeclaration = null; |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + propertyDeclaration = node; |
| 92 | + return IsOnHeader(root, position, node, node.Identifier); |
| 93 | + } |
| 94 | + |
| 95 | + public bool IsOnParameterHeader(SyntaxNode root, int position, [NotNullWhen(true)] out SyntaxNode? parameter) |
| 96 | + { |
| 97 | + var node = TryGetAncestorForLocation<ParameterSyntax>(root, position); |
| 98 | + if (node is null) |
| 99 | + { |
| 100 | + parameter = null; |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + parameter = node; |
| 105 | + return IsOnHeader(root, position, node, node); |
| 106 | + } |
| 107 | + |
| 108 | + public bool IsOnMethodHeader(SyntaxNode root, int position, [NotNullWhen(true)] out SyntaxNode? method) |
| 109 | + { |
| 110 | + var node = TryGetAncestorForLocation<MethodDeclarationSyntax>(root, position); |
| 111 | + if (node is null) |
| 112 | + { |
| 113 | + method = null; |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + method = node; |
| 118 | + return IsOnHeader(root, position, node, node.ParameterList); |
| 119 | + } |
| 120 | + |
| 121 | + public bool IsOnLocalFunctionHeader(SyntaxNode root, int position, [NotNullWhen(true)] out SyntaxNode? localFunction) |
| 122 | + { |
| 123 | + var node = TryGetAncestorForLocation<LocalFunctionStatementSyntax>(root, position); |
| 124 | + if (node is null) |
| 125 | + { |
| 126 | + localFunction = null; |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + localFunction = node; |
| 131 | + return IsOnHeader(root, position, node, node.ParameterList); |
| 132 | + } |
| 133 | + |
| 134 | + public bool IsOnLocalDeclarationHeader(SyntaxNode root, int position, [NotNullWhen(true)] out SyntaxNode? localDeclaration) |
| 135 | + { |
| 136 | + var node = TryGetAncestorForLocation<LocalDeclarationStatementSyntax>(root, position); |
| 137 | + if (node is null) |
| 138 | + { |
| 139 | + localDeclaration = null; |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + localDeclaration = node; |
| 144 | + var initializersExpressions = node.Declaration.Variables |
| 145 | + .Where(v => v.Initializer != null) |
| 146 | + .Select(initializedV => initializedV.Initializer.Value) |
| 147 | + .ToImmutableArray(); |
| 148 | + return IsOnHeader(root, position, node, node, holes: initializersExpressions); |
| 149 | + } |
| 150 | + |
| 151 | + public bool IsOnIfStatementHeader(SyntaxNode root, int position, [NotNullWhen(true)] out SyntaxNode? ifStatement) |
| 152 | + { |
| 153 | + var node = TryGetAncestorForLocation<IfStatementSyntax>(root, position); |
| 154 | + if (node is null) |
| 155 | + { |
| 156 | + ifStatement = null; |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + ifStatement = node; |
| 161 | + return IsOnHeader(root, position, node, node.CloseParenToken); |
| 162 | + } |
| 163 | + |
| 164 | + public bool IsOnForeachHeader(SyntaxNode root, int position, [NotNullWhen(true)] out SyntaxNode? foreachStatement) |
| 165 | + { |
| 166 | + var node = TryGetAncestorForLocation<ForEachStatementSyntax>(root, position); |
| 167 | + if (node is null) |
| 168 | + { |
| 169 | + foreachStatement = null; |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + foreachStatement = node; |
| 174 | + return IsOnHeader(root, position, node, node.CloseParenToken); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments