|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +namespace OmniSharp.Roslyn.CSharp.Services.Signatures |
| 8 | +{ |
| 9 | + static internal class CheckForStatic |
| 10 | + { |
| 11 | + public static bool IsInStaticContext(this SyntaxNode node) |
| 12 | + { |
| 13 | + // this/base calls are always static. |
| 14 | + if (node.FirstAncestorOrSelf<ConstructorInitializerSyntax>() != null) |
| 15 | + { |
| 16 | + return true; |
| 17 | + } |
| 18 | + |
| 19 | + var memberDeclaration = node.FirstAncestorOrSelf<MemberDeclarationSyntax>(); |
| 20 | + if (memberDeclaration == null) |
| 21 | + { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + switch (memberDeclaration.Kind()) |
| 26 | + { |
| 27 | + case SyntaxKind.MethodDeclaration: |
| 28 | + case SyntaxKind.ConstructorDeclaration: |
| 29 | + case SyntaxKind.EventDeclaration: |
| 30 | + case SyntaxKind.IndexerDeclaration: |
| 31 | + return GetModifiers(memberDeclaration).Any(SyntaxKind.StaticKeyword); |
| 32 | + |
| 33 | + case SyntaxKind.PropertyDeclaration: |
| 34 | + return GetModifiers(memberDeclaration).Any(SyntaxKind.StaticKeyword) || |
| 35 | + node.IsFoundUnder((PropertyDeclarationSyntax p) => p.Initializer); |
| 36 | + |
| 37 | + case SyntaxKind.FieldDeclaration: |
| 38 | + case SyntaxKind.EventFieldDeclaration: |
| 39 | + // Inside a field one can only access static members of a type (unless it's top-level). |
| 40 | + return !memberDeclaration.Parent.IsKind(SyntaxKind.CompilationUnit); |
| 41 | + |
| 42 | + case SyntaxKind.DestructorDeclaration: |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + // Global statements are not a static context. |
| 47 | + if (node.FirstAncestorOrSelf<GlobalStatementSyntax>() != null) |
| 48 | + { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + // any other location is considered static |
| 53 | + return true; |
| 54 | + } |
| 55 | + public static SyntaxTokenList GetModifiers(SyntaxNode member) |
| 56 | + { |
| 57 | + if (member != null) |
| 58 | + { |
| 59 | + switch (member.Kind()) |
| 60 | + { |
| 61 | + case SyntaxKind.EnumDeclaration: |
| 62 | + return ((EnumDeclarationSyntax)member).Modifiers; |
| 63 | + case SyntaxKind.ClassDeclaration: |
| 64 | + case SyntaxKind.InterfaceDeclaration: |
| 65 | + case SyntaxKind.StructDeclaration: |
| 66 | + return ((TypeDeclarationSyntax)member).Modifiers; |
| 67 | + case SyntaxKind.DelegateDeclaration: |
| 68 | + return ((DelegateDeclarationSyntax)member).Modifiers; |
| 69 | + case SyntaxKind.FieldDeclaration: |
| 70 | + return ((FieldDeclarationSyntax)member).Modifiers; |
| 71 | + case SyntaxKind.EventFieldDeclaration: |
| 72 | + return ((EventFieldDeclarationSyntax)member).Modifiers; |
| 73 | + case SyntaxKind.ConstructorDeclaration: |
| 74 | + return ((ConstructorDeclarationSyntax)member).Modifiers; |
| 75 | + case SyntaxKind.DestructorDeclaration: |
| 76 | + return ((DestructorDeclarationSyntax)member).Modifiers; |
| 77 | + case SyntaxKind.PropertyDeclaration: |
| 78 | + return ((PropertyDeclarationSyntax)member).Modifiers; |
| 79 | + case SyntaxKind.EventDeclaration: |
| 80 | + return ((EventDeclarationSyntax)member).Modifiers; |
| 81 | + case SyntaxKind.IndexerDeclaration: |
| 82 | + return ((IndexerDeclarationSyntax)member).Modifiers; |
| 83 | + case SyntaxKind.OperatorDeclaration: |
| 84 | + return ((OperatorDeclarationSyntax)member).Modifiers; |
| 85 | + case SyntaxKind.ConversionOperatorDeclaration: |
| 86 | + return ((ConversionOperatorDeclarationSyntax)member).Modifiers; |
| 87 | + case SyntaxKind.MethodDeclaration: |
| 88 | + return ((MethodDeclarationSyntax)member).Modifiers; |
| 89 | + case SyntaxKind.GetAccessorDeclaration: |
| 90 | + case SyntaxKind.SetAccessorDeclaration: |
| 91 | + case SyntaxKind.AddAccessorDeclaration: |
| 92 | + case SyntaxKind.RemoveAccessorDeclaration: |
| 93 | + return ((AccessorDeclarationSyntax)member).Modifiers; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return default; |
| 98 | + } |
| 99 | + public static bool IsFoundUnder<TParent>(this SyntaxNode node, Func<TParent, SyntaxNode> childGetter) |
| 100 | + where TParent : SyntaxNode |
| 101 | + { |
| 102 | + var ancestor = node.GetAncestor<TParent>(); |
| 103 | + if (ancestor == null) |
| 104 | + { |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + var child = childGetter(ancestor); |
| 109 | + |
| 110 | + // See if node passes through child on the way up to ancestor. |
| 111 | + return node.GetAncestorsOrThis<SyntaxNode>().Contains(child); |
| 112 | + } |
| 113 | + public static TNode GetAncestor<TNode>(this SyntaxNode node) |
| 114 | + where TNode : SyntaxNode |
| 115 | + { |
| 116 | + var current = node.Parent; |
| 117 | + while (current != null) |
| 118 | + { |
| 119 | + if (current is TNode tNode) |
| 120 | + { |
| 121 | + return tNode; |
| 122 | + } |
| 123 | + |
| 124 | + current = current.GetParent(); |
| 125 | + } |
| 126 | + |
| 127 | + return null; |
| 128 | + } |
| 129 | + private static SyntaxNode GetParent(this SyntaxNode node) |
| 130 | + { |
| 131 | + return node is IStructuredTriviaSyntax trivia ? trivia.ParentTrivia.Token.Parent : node.Parent; |
| 132 | + } |
| 133 | + |
| 134 | + public static TNode FirstAncestorOrSelfUntil<TNode>(this SyntaxNode node, Func<SyntaxNode, bool> predicate) |
| 135 | + where TNode : SyntaxNode |
| 136 | + { |
| 137 | + for (var current = node; current != null; current = current.GetParent()) |
| 138 | + { |
| 139 | + if (current is TNode tnode) |
| 140 | + { |
| 141 | + return tnode; |
| 142 | + } |
| 143 | + |
| 144 | + if (predicate(current)) |
| 145 | + { |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return default; |
| 151 | + } |
| 152 | + |
| 153 | + public static TNode GetAncestorOrThis<TNode>(this SyntaxNode node) |
| 154 | + where TNode : SyntaxNode |
| 155 | + { |
| 156 | + return node?.GetAncestorsOrThis<TNode>().FirstOrDefault(); |
| 157 | + } |
| 158 | + |
| 159 | + public static IEnumerable<TNode> GetAncestorsOrThis<TNode>(this SyntaxNode node) |
| 160 | + where TNode : SyntaxNode |
| 161 | + { |
| 162 | + var current = node; |
| 163 | + while (current != null) |
| 164 | + { |
| 165 | + if (current is TNode tNode) |
| 166 | + { |
| 167 | + yield return tNode; |
| 168 | + } |
| 169 | + |
| 170 | + current = current.GetParent(); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | +} |
| 175 | + |
0 commit comments