| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library services.completion.computer.dart.local.Declaration.visitor; |
| 6 |
| 7 import 'package:analysis_server/src/services/completion/suggestion_builder.dart'
; |
| 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/scanner.dart'; |
| 10 |
| 11 /** |
| 12 * `LocalDeclarationCollector` visits an [AstNode] and its parent recursively |
| 13 * along with any declarations in those nodes. Setting the [finished] flag |
| 14 * `true` will prevent further recursion. |
| 15 */ |
| 16 abstract class LocalDeclarationVisitor extends GeneralizingAstVisitor<bool> { |
| 17 |
| 18 static final TypeName STACKTRACE_TYPE = new TypeName( |
| 19 new SimpleIdentifier(new StringToken(TokenType.IDENTIFIER, 'StackTrace', 0
)), |
| 20 null); |
| 21 |
| 22 final int offset; |
| 23 bool finished = false; |
| 24 |
| 25 LocalDeclarationVisitor(this.offset); |
| 26 |
| 27 void declaredClass(ClassDeclaration declaration); |
| 28 |
| 29 void declaredClassTypeAlias(ClassTypeAlias declaration); |
| 30 |
| 31 void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl); |
| 32 |
| 33 void declaredFunction(FunctionDeclaration declaration); |
| 34 |
| 35 void declaredFunctionTypeAlias(FunctionTypeAlias declaration); |
| 36 |
| 37 void declaredLabel(Label label); |
| 38 |
| 39 void declaredLocalVar(SimpleIdentifier name, TypeName type); |
| 40 |
| 41 void declaredMethod(MethodDeclaration declaration); |
| 42 |
| 43 void declaredParam(SimpleIdentifier name, TypeName type); |
| 44 |
| 45 void declaredTopLevelVar(VariableDeclarationList varList, |
| 46 VariableDeclaration varDecl); |
| 47 |
| 48 @override |
| 49 bool visitBlock(Block node) { |
| 50 node.statements.forEach((Statement stmt) { |
| 51 if (stmt.offset < offset) { |
| 52 if (stmt is LabeledStatement) { |
| 53 stmt.labels.forEach((Label label) { |
| 54 declaredLabel(label); |
| 55 }); |
| 56 } else if (stmt is VariableDeclarationStatement) { |
| 57 VariableDeclarationList varList = stmt.variables; |
| 58 if (varList != null) { |
| 59 varList.variables.forEach((VariableDeclaration varDecl) { |
| 60 if (varDecl.end < offset) { |
| 61 declaredLocalVar(varDecl.name, varList.type); |
| 62 } |
| 63 }); |
| 64 } |
| 65 } |
| 66 } |
| 67 }); |
| 68 return visitNode(node); |
| 69 } |
| 70 |
| 71 @override |
| 72 bool visitCatchClause(CatchClause node) { |
| 73 SimpleIdentifier param = node.exceptionParameter; |
| 74 if (param != null) { |
| 75 declaredParam(param, node.exceptionType); |
| 76 } |
| 77 param = node.stackTraceParameter; |
| 78 if (param != null) { |
| 79 declaredParam(param, STACKTRACE_TYPE); |
| 80 } |
| 81 return visitNode(node); |
| 82 } |
| 83 |
| 84 @override |
| 85 bool visitClassDeclaration(ClassDeclaration node) { |
| 86 _visitClassDeclarationMembers(node); |
| 87 visitInheritedTypes(node, (ClassDeclaration classNode) { |
| 88 _visitClassDeclarationMembers(classNode); |
| 89 }, (String typeName) { |
| 90 // ignored |
| 91 }); |
| 92 return visitNode(node); |
| 93 } |
| 94 |
| 95 @override |
| 96 bool visitCompilationUnit(CompilationUnit node) { |
| 97 node.declarations.forEach((Declaration declaration) { |
| 98 if (declaration is ClassDeclaration) { |
| 99 declaredClass(declaration); |
| 100 } else if (declaration is EnumDeclaration) { |
| 101 // TODO (danrubel) enum support |
| 102 // declaredEnum(........) |
| 103 } else if (declaration is FunctionDeclaration) { |
| 104 declaredFunction(declaration); |
| 105 } else if (declaration is TopLevelVariableDeclaration) { |
| 106 var varList = declaration.variables; |
| 107 if (varList != null) { |
| 108 varList.variables.forEach((VariableDeclaration varDecl) { |
| 109 declaredTopLevelVar(varList, varDecl); |
| 110 }); |
| 111 } |
| 112 } else if (declaration is ClassTypeAlias) { |
| 113 declaredClassTypeAlias(declaration); |
| 114 } else if (declaration is FunctionTypeAlias) { |
| 115 declaredFunctionTypeAlias(declaration); |
| 116 } |
| 117 }); |
| 118 return finished; |
| 119 } |
| 120 |
| 121 @override |
| 122 bool visitExpressionStatement(ExpressionStatement node) { |
| 123 Expression expression = node.expression; |
| 124 if (expression is SimpleIdentifier) { |
| 125 if (expression.end < offset) { |
| 126 return finished; |
| 127 } |
| 128 } |
| 129 return visitNode(node); |
| 130 } |
| 131 |
| 132 @override |
| 133 bool visitForEachStatement(ForEachStatement node) { |
| 134 SimpleIdentifier id; |
| 135 TypeName type; |
| 136 DeclaredIdentifier loopVar = node.loopVariable; |
| 137 if (loopVar != null) { |
| 138 id = loopVar.identifier; |
| 139 type = loopVar.type; |
| 140 } else { |
| 141 id = node.identifier; |
| 142 type = null; |
| 143 } |
| 144 declaredLocalVar(id, type); |
| 145 return visitNode(node); |
| 146 } |
| 147 |
| 148 @override |
| 149 bool visitForStatement(ForStatement node) { |
| 150 VariableDeclarationList varList = node.variables; |
| 151 if (varList != null) { |
| 152 varList.variables.forEach((VariableDeclaration varDecl) { |
| 153 declaredLocalVar(varDecl.name, varList.type); |
| 154 }); |
| 155 } |
| 156 return visitNode(node); |
| 157 } |
| 158 |
| 159 @override |
| 160 bool visitFunctionDeclaration(FunctionDeclaration node) { |
| 161 // declaredFunction is called by the compilation unit containing it |
| 162 return visitNode(node); |
| 163 } |
| 164 |
| 165 @override |
| 166 bool visitFunctionExpression(FunctionExpression node) { |
| 167 _visitParamList(node.parameters); |
| 168 return visitNode(node); |
| 169 } |
| 170 |
| 171 @override |
| 172 bool visitInterpolationExpression(InterpolationExpression node) { |
| 173 return visitNode(node); |
| 174 } |
| 175 |
| 176 @override |
| 177 bool visitMethodDeclaration(MethodDeclaration node) { |
| 178 _visitParamList(node.parameters); |
| 179 return visitNode(node); |
| 180 } |
| 181 |
| 182 @override |
| 183 bool visitNode(AstNode node) { |
| 184 if (finished) { |
| 185 return true; |
| 186 } |
| 187 return node.parent.accept(this); |
| 188 } |
| 189 |
| 190 @override |
| 191 bool visitStringInterpolation(StringInterpolation node) { |
| 192 return visitNode(node); |
| 193 } |
| 194 |
| 195 void _visitClassDeclarationMembers(ClassDeclaration node) { |
| 196 node.members.forEach((ClassMember member) { |
| 197 if (member is FieldDeclaration) { |
| 198 member.fields.variables.forEach((VariableDeclaration varDecl) { |
| 199 declaredField(member, varDecl); |
| 200 }); |
| 201 } else if (member is MethodDeclaration) { |
| 202 declaredMethod(member); |
| 203 } |
| 204 }); |
| 205 } |
| 206 |
| 207 void _visitParamList(FormalParameterList paramList) { |
| 208 if (paramList != null) { |
| 209 paramList.parameters.forEach((FormalParameter param) { |
| 210 NormalFormalParameter normalParam; |
| 211 if (param is DefaultFormalParameter) { |
| 212 normalParam = param.parameter; |
| 213 } else if (param is NormalFormalParameter) { |
| 214 normalParam = param; |
| 215 } |
| 216 TypeName type = null; |
| 217 if (normalParam is FieldFormalParameter) { |
| 218 type = normalParam.type; |
| 219 } else if (normalParam is FunctionTypedFormalParameter) { |
| 220 type = normalParam.returnType; |
| 221 } else if (normalParam is SimpleFormalParameter) { |
| 222 type = normalParam.type; |
| 223 } |
| 224 SimpleIdentifier name = param.identifier; |
| 225 declaredParam(name, type); |
| 226 }); |
| 227 } |
| 228 } |
| 229 } |
| OLD | NEW |