| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library computer.outline; | 5 library computer.outline; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/protocol.dart'; | 7 import 'package:analysis_server/src/protocol.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart' as engine; | 9 import 'package:analyzer/src/generated/element.dart' as engine; |
| 10 import 'package:analyzer/src/generated/engine.dart'; | 10 import 'package:analyzer/src/generated/engine.dart'; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 for (ClassMember classMember in classDeclaration.members) { | 36 for (ClassMember classMember in classDeclaration.members) { |
| 37 if (classMember is ConstructorDeclaration) { | 37 if (classMember is ConstructorDeclaration) { |
| 38 ConstructorDeclaration constructorDeclaration = classMember; | 38 ConstructorDeclaration constructorDeclaration = classMember; |
| 39 classContents.add(_newConstructorOutline(constructorDeclaration)); | 39 classContents.add(_newConstructorOutline(constructorDeclaration)); |
| 40 } | 40 } |
| 41 if (classMember is FieldDeclaration) { | 41 if (classMember is FieldDeclaration) { |
| 42 FieldDeclaration fieldDeclaration = classMember; | 42 FieldDeclaration fieldDeclaration = classMember; |
| 43 VariableDeclarationList fields = fieldDeclaration.fields; | 43 VariableDeclarationList fields = fieldDeclaration.fields; |
| 44 if (fields != null) { | 44 if (fields != null) { |
| 45 TypeName fieldType = fields.type; | 45 TypeName fieldType = fields.type; |
| 46 String fieldTypeName = fieldType != null ? fieldType.toSource() : | 46 String fieldTypeName = |
| 47 ''; | 47 fieldType != null ? fieldType.toSource() : ''; |
| 48 for (VariableDeclaration field in fields.variables) { | 48 for (VariableDeclaration field in fields.variables) { |
| 49 classContents.add(_newVariableOutline(fieldTypeName, | 49 classContents.add( |
| 50 ElementKind.FIELD, field, fieldDeclaration.isStatic)); | 50 _newVariableOutline( |
| 51 fieldTypeName, |
| 52 ElementKind.FIELD, |
| 53 field, |
| 54 fieldDeclaration.isStatic)); |
| 51 } | 55 } |
| 52 } | 56 } |
| 53 } | 57 } |
| 54 if (classMember is MethodDeclaration) { | 58 if (classMember is MethodDeclaration) { |
| 55 MethodDeclaration methodDeclaration = classMember; | 59 MethodDeclaration methodDeclaration = classMember; |
| 56 classContents.add(_newMethodOutline(methodDeclaration)); | 60 classContents.add(_newMethodOutline(methodDeclaration)); |
| 57 } | 61 } |
| 58 } | 62 } |
| 59 unitContents.add(_newClassOutline(classDeclaration, classContents)); | 63 unitContents.add(_newClassOutline(classDeclaration, classContents)); |
| 60 } | 64 } |
| 61 if (unitMember is TopLevelVariableDeclaration) { | 65 if (unitMember is TopLevelVariableDeclaration) { |
| 62 TopLevelVariableDeclaration fieldDeclaration = unitMember; | 66 TopLevelVariableDeclaration fieldDeclaration = unitMember; |
| 63 VariableDeclarationList fields = fieldDeclaration.variables; | 67 VariableDeclarationList fields = fieldDeclaration.variables; |
| 64 if (fields != null) { | 68 if (fields != null) { |
| 65 TypeName fieldType = fields.type; | 69 TypeName fieldType = fields.type; |
| 66 String fieldTypeName = fieldType != null ? fieldType.toSource() : ''; | 70 String fieldTypeName = fieldType != null ? fieldType.toSource() : ''; |
| 67 for (VariableDeclaration field in fields.variables) { | 71 for (VariableDeclaration field in fields.variables) { |
| 68 unitContents.add(_newVariableOutline(fieldTypeName, | 72 unitContents.add( |
| 69 ElementKind.TOP_LEVEL_VARIABLE, field, false)); | 73 _newVariableOutline( |
| 74 fieldTypeName, |
| 75 ElementKind.TOP_LEVEL_VARIABLE, |
| 76 field, |
| 77 false)); |
| 70 } | 78 } |
| 71 } | 79 } |
| 72 } | 80 } |
| 73 if (unitMember is FunctionDeclaration) { | 81 if (unitMember is FunctionDeclaration) { |
| 74 FunctionDeclaration functionDeclaration = unitMember; | 82 FunctionDeclaration functionDeclaration = unitMember; |
| 75 unitContents.add(_newFunctionOutline(functionDeclaration, true)); | 83 unitContents.add(_newFunctionOutline(functionDeclaration, true)); |
| 76 } | 84 } |
| 77 if (unitMember is ClassTypeAlias) { | 85 if (unitMember is ClassTypeAlias) { |
| 78 ClassTypeAlias alias = unitMember; | 86 ClassTypeAlias alias = unitMember; |
| 79 unitContents.add(_newClassTypeAlias(alias)); | 87 unitContents.add(_newClassTypeAlias(alias)); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 // not first child: [endOfPreviousSibling, endOfNode] | 159 // not first child: [endOfPreviousSibling, endOfNode] |
| 152 int prevSiblingEnd = siblings[index - 1].end; | 160 int prevSiblingEnd = siblings[index - 1].end; |
| 153 return new _SourceRegion(prevSiblingEnd, endOffset - prevSiblingEnd); | 161 return new _SourceRegion(prevSiblingEnd, endOffset - prevSiblingEnd); |
| 154 } | 162 } |
| 155 | 163 |
| 156 Outline _newClassOutline(ClassDeclaration classDeclaration, | 164 Outline _newClassOutline(ClassDeclaration classDeclaration, |
| 157 List<Outline> classContents) { | 165 List<Outline> classContents) { |
| 158 SimpleIdentifier nameNode = classDeclaration.name; | 166 SimpleIdentifier nameNode = classDeclaration.name; |
| 159 String name = nameNode.name; | 167 String name = nameNode.name; |
| 160 _SourceRegion sourceRegion = _getSourceRegion(classDeclaration); | 168 _SourceRegion sourceRegion = _getSourceRegion(classDeclaration); |
| 161 Element element = new Element(ElementKind.CLASS, name, | 169 Element element = new Element( |
| 162 Element.makeFlags(isPrivate: Identifier.isPrivateName(name), | 170 ElementKind.CLASS, |
| 171 name, |
| 172 Element.makeFlags( |
| 173 isPrivate: Identifier.isPrivateName(name), |
| 163 isDeprecated: _isDeprecated(classDeclaration), | 174 isDeprecated: _isDeprecated(classDeclaration), |
| 164 isAbstract: classDeclaration.isAbstract), | 175 isAbstract: classDeclaration.isAbstract), |
| 165 location: _getLocationNode(nameNode)); | 176 location: _getLocationNode(nameNode)); |
| 166 return new Outline(element, sourceRegion.offset, | 177 return new Outline( |
| 167 sourceRegion.length, children: classContents); | 178 element, |
| 179 sourceRegion.offset, |
| 180 sourceRegion.length, |
| 181 children: classContents.isEmpty ? null : classContents); |
| 168 } | 182 } |
| 169 | 183 |
| 170 Outline _newClassTypeAlias(ClassTypeAlias alias) { | 184 Outline _newClassTypeAlias(ClassTypeAlias alias) { |
| 171 SimpleIdentifier nameNode = alias.name; | 185 SimpleIdentifier nameNode = alias.name; |
| 172 String name = nameNode.name; | 186 String name = nameNode.name; |
| 173 _SourceRegion sourceRegion = _getSourceRegion(alias); | 187 _SourceRegion sourceRegion = _getSourceRegion(alias); |
| 174 Element element = new Element(ElementKind.CLASS_TYPE_ALIAS, name, | 188 Element element = new Element( |
| 175 Element.makeFlags(isPrivate: Identifier.isPrivateName(name), | 189 ElementKind.CLASS_TYPE_ALIAS, |
| 176 isDeprecated: _isDeprecated(alias), isAbstract: alias.isAbstract), | 190 name, |
| 191 Element.makeFlags( |
| 192 isPrivate: Identifier.isPrivateName(name), |
| 193 isDeprecated: _isDeprecated(alias), |
| 194 isAbstract: alias.isAbstract), |
| 177 location: _getLocationNode(nameNode)); | 195 location: _getLocationNode(nameNode)); |
| 178 return new Outline(element, sourceRegion.offset, | 196 return new Outline(element, sourceRegion.offset, sourceRegion.length); |
| 179 sourceRegion.length); | |
| 180 } | 197 } |
| 181 | 198 |
| 182 Outline _newConstructorOutline(ConstructorDeclaration constructor) { | 199 Outline _newConstructorOutline(ConstructorDeclaration constructor) { |
| 183 Identifier returnType = constructor.returnType; | 200 Identifier returnType = constructor.returnType; |
| 184 String name = returnType.name; | 201 String name = returnType.name; |
| 185 int offset = returnType.offset; | 202 int offset = returnType.offset; |
| 186 int length = returnType.length; | 203 int length = returnType.length; |
| 187 SimpleIdentifier constructorNameNode = constructor.name; | 204 SimpleIdentifier constructorNameNode = constructor.name; |
| 188 bool isPrivate = false; | 205 bool isPrivate = false; |
| 189 if (constructorNameNode != null) { | 206 if (constructorNameNode != null) { |
| 190 String constructorName = constructorNameNode.name; | 207 String constructorName = constructorNameNode.name; |
| 191 isPrivate = Identifier.isPrivateName(constructorName); | 208 isPrivate = Identifier.isPrivateName(constructorName); |
| 192 name += '.${constructorName}'; | 209 name += '.${constructorName}'; |
| 193 offset = constructorNameNode.offset; | 210 offset = constructorNameNode.offset; |
| 194 length = constructorNameNode.length; | 211 length = constructorNameNode.length; |
| 195 } | 212 } |
| 196 _SourceRegion sourceRegion = _getSourceRegion(constructor); | 213 _SourceRegion sourceRegion = _getSourceRegion(constructor); |
| 197 FormalParameterList parameters = constructor.parameters; | 214 FormalParameterList parameters = constructor.parameters; |
| 198 String parametersStr = parameters != null ? parameters.toSource() : ''; | 215 String parametersStr = parameters != null ? parameters.toSource() : ''; |
| 199 Element element = new Element(ElementKind.CONSTRUCTOR, name, | 216 Element element = new Element( |
| 200 Element.makeFlags(isPrivate: isPrivate, | 217 ElementKind.CONSTRUCTOR, |
| 218 name, |
| 219 Element.makeFlags( |
| 220 isPrivate: isPrivate, |
| 201 isDeprecated: _isDeprecated(constructor)), | 221 isDeprecated: _isDeprecated(constructor)), |
| 202 location: _getLocationOffsetLength(offset, length), | 222 location: _getLocationOffsetLength(offset, length), |
| 203 parameters: parametersStr); | 223 parameters: parametersStr); |
| 204 List<Outline> contents = _addLocalFunctionOutlines(constructor.body); | 224 List<Outline> contents = _addLocalFunctionOutlines(constructor.body); |
| 205 Outline outline = new Outline(element, sourceRegion.offset, | 225 Outline outline = new Outline( |
| 206 sourceRegion.length, children: contents); | 226 element, |
| 227 sourceRegion.offset, |
| 228 sourceRegion.length, |
| 229 children: contents.isEmpty ? null : contents); |
| 207 return outline; | 230 return outline; |
| 208 } | 231 } |
| 209 | 232 |
| 210 Outline _newFunctionOutline(FunctionDeclaration function, bool isStatic) { | 233 Outline _newFunctionOutline(FunctionDeclaration function, bool isStatic) { |
| 211 TypeName returnType = function.returnType; | 234 TypeName returnType = function.returnType; |
| 212 SimpleIdentifier nameNode = function.name; | 235 SimpleIdentifier nameNode = function.name; |
| 213 String name = nameNode.name; | 236 String name = nameNode.name; |
| 214 FunctionExpression functionExpression = function.functionExpression; | 237 FunctionExpression functionExpression = function.functionExpression; |
| 215 FormalParameterList parameters = functionExpression.parameters; | 238 FormalParameterList parameters = functionExpression.parameters; |
| 216 ElementKind kind; | 239 ElementKind kind; |
| 217 if (function.isGetter) { | 240 if (function.isGetter) { |
| 218 kind = ElementKind.GETTER; | 241 kind = ElementKind.GETTER; |
| 219 } else if (function.isSetter) { | 242 } else if (function.isSetter) { |
| 220 kind = ElementKind.SETTER; | 243 kind = ElementKind.SETTER; |
| 221 } else { | 244 } else { |
| 222 kind = ElementKind.FUNCTION; | 245 kind = ElementKind.FUNCTION; |
| 223 } | 246 } |
| 224 _SourceRegion sourceRegion = _getSourceRegion(function); | 247 _SourceRegion sourceRegion = _getSourceRegion(function); |
| 225 String parametersStr = parameters != null ? parameters.toSource() : ''; | 248 String parametersStr = parameters != null ? parameters.toSource() : ''; |
| 226 String returnTypeStr = returnType != null ? returnType.toSource() : ''; | 249 String returnTypeStr = returnType != null ? returnType.toSource() : ''; |
| 227 Element element = new Element(kind, name, | 250 Element element = new Element( |
| 228 Element.makeFlags(isPrivate: Identifier.isPrivateName(name), | 251 kind, |
| 229 isDeprecated: _isDeprecated(function), isStatic: isStatic), | 252 name, |
| 230 location: _getLocationNode(nameNode), parameters: parametersStr, | 253 Element.makeFlags( |
| 254 isPrivate: Identifier.isPrivateName(name), |
| 255 isDeprecated: _isDeprecated(function), |
| 256 isStatic: isStatic), |
| 257 location: _getLocationNode(nameNode), |
| 258 parameters: parametersStr, |
| 231 returnType: returnTypeStr); | 259 returnType: returnTypeStr); |
| 232 List<Outline> contents = _addLocalFunctionOutlines(functionExpression.body); | 260 List<Outline> contents = _addLocalFunctionOutlines(functionExpression.body); |
| 233 Outline outline = new Outline(element, sourceRegion.offset, | 261 Outline outline = new Outline( |
| 234 sourceRegion.length, children: contents); | 262 element, |
| 263 sourceRegion.offset, |
| 264 sourceRegion.length, |
| 265 children: contents.isEmpty ? null : contents); |
| 235 return outline; | 266 return outline; |
| 236 } | 267 } |
| 237 | 268 |
| 238 Outline _newFunctionTypeAliasOutline(FunctionTypeAlias alias) { | 269 Outline _newFunctionTypeAliasOutline(FunctionTypeAlias alias) { |
| 239 TypeName returnType = alias.returnType; | 270 TypeName returnType = alias.returnType; |
| 240 SimpleIdentifier nameNode = alias.name; | 271 SimpleIdentifier nameNode = alias.name; |
| 241 String name = nameNode.name; | 272 String name = nameNode.name; |
| 242 _SourceRegion sourceRegion = _getSourceRegion(alias); | 273 _SourceRegion sourceRegion = _getSourceRegion(alias); |
| 243 FormalParameterList parameters = alias.parameters; | 274 FormalParameterList parameters = alias.parameters; |
| 244 String parametersStr = parameters != null ? parameters.toSource() : ''; | 275 String parametersStr = parameters != null ? parameters.toSource() : ''; |
| 245 String returnTypeStr = returnType != null ? returnType.toSource() : ''; | 276 String returnTypeStr = returnType != null ? returnType.toSource() : ''; |
| 246 Element element = new Element(ElementKind.FUNCTION_TYPE_ALIAS, name, | 277 Element element = new Element( |
| 247 Element.makeFlags(isPrivate: Identifier.isPrivateName(name), | 278 ElementKind.FUNCTION_TYPE_ALIAS, |
| 279 name, |
| 280 Element.makeFlags( |
| 281 isPrivate: Identifier.isPrivateName(name), |
| 248 isDeprecated: _isDeprecated(alias)), | 282 isDeprecated: _isDeprecated(alias)), |
| 249 location: _getLocationNode(nameNode), parameters: parametersStr, | 283 location: _getLocationNode(nameNode), |
| 284 parameters: parametersStr, |
| 250 returnType: returnTypeStr); | 285 returnType: returnTypeStr); |
| 251 return new Outline(element, sourceRegion.offset, | 286 return new Outline(element, sourceRegion.offset, sourceRegion.length); |
| 252 sourceRegion.length); | |
| 253 } | 287 } |
| 254 | 288 |
| 255 Outline _newMethodOutline(MethodDeclaration method) { | 289 Outline _newMethodOutline(MethodDeclaration method) { |
| 256 TypeName returnType = method.returnType; | 290 TypeName returnType = method.returnType; |
| 257 SimpleIdentifier nameNode = method.name; | 291 SimpleIdentifier nameNode = method.name; |
| 258 String name = nameNode.name; | 292 String name = nameNode.name; |
| 259 FormalParameterList parameters = method.parameters; | 293 FormalParameterList parameters = method.parameters; |
| 260 ElementKind kind; | 294 ElementKind kind; |
| 261 if (method.isGetter) { | 295 if (method.isGetter) { |
| 262 kind = ElementKind.GETTER; | 296 kind = ElementKind.GETTER; |
| 263 } else if (method.isSetter) { | 297 } else if (method.isSetter) { |
| 264 kind = ElementKind.SETTER; | 298 kind = ElementKind.SETTER; |
| 265 } else { | 299 } else { |
| 266 kind = ElementKind.METHOD; | 300 kind = ElementKind.METHOD; |
| 267 } | 301 } |
| 268 _SourceRegion sourceRegion = _getSourceRegion(method); | 302 _SourceRegion sourceRegion = _getSourceRegion(method); |
| 269 String parametersStr = parameters != null ? parameters.toSource() : ''; | 303 String parametersStr = parameters != null ? parameters.toSource() : ''; |
| 270 String returnTypeStr = returnType != null ? returnType.toSource() : ''; | 304 String returnTypeStr = returnType != null ? returnType.toSource() : ''; |
| 271 Element element = new Element(kind, name, | 305 Element element = new Element( |
| 272 Element.makeFlags(isPrivate: Identifier.isPrivateName(name), | 306 kind, |
| 273 isDeprecated: _isDeprecated(method), isAbstract: method.isAbstract, | 307 name, |
| 274 isStatic: method.isStatic), location: _getLocationNode(nameNode), | 308 Element.makeFlags( |
| 275 parameters: parametersStr, returnType: returnTypeStr); | 309 isPrivate: Identifier.isPrivateName(name), |
| 310 isDeprecated: _isDeprecated(method), |
| 311 isAbstract: method.isAbstract, |
| 312 isStatic: method.isStatic), |
| 313 location: _getLocationNode(nameNode), |
| 314 parameters: parametersStr, |
| 315 returnType: returnTypeStr); |
| 276 List<Outline> contents = _addLocalFunctionOutlines(method.body); | 316 List<Outline> contents = _addLocalFunctionOutlines(method.body); |
| 277 Outline outline = new Outline(element, sourceRegion.offset, | 317 Outline outline = new Outline( |
| 278 sourceRegion.length, children: contents); | 318 element, |
| 319 sourceRegion.offset, |
| 320 sourceRegion.length, |
| 321 children: contents.isEmpty ? null : contents); |
| 279 return outline; | 322 return outline; |
| 280 } | 323 } |
| 281 | 324 |
| 282 Outline _newUnitOutline(List<Outline> unitContents) { | 325 Outline _newUnitOutline(List<Outline> unitContents) { |
| 283 Element element = new Element(ElementKind.COMPILATION_UNIT, '<unit>', | 326 Element element = new Element( |
| 284 Element.makeFlags(), location: _getLocationNode(_unit)); | 327 ElementKind.COMPILATION_UNIT, |
| 285 return new Outline(element, _unit.offset, _unit.length, | 328 '<unit>', |
| 286 children: unitContents); | 329 Element.makeFlags(), |
| 330 location: _getLocationNode(_unit)); |
| 331 return new Outline( |
| 332 element, |
| 333 _unit.offset, |
| 334 _unit.length, |
| 335 children: unitContents.isEmpty ? null : unitContents); |
| 287 } | 336 } |
| 288 | 337 |
| 289 Outline _newVariableOutline(String typeName, ElementKind kind, | 338 Outline _newVariableOutline(String typeName, ElementKind kind, |
| 290 VariableDeclaration variable, bool isStatic) { | 339 VariableDeclaration variable, bool isStatic) { |
| 291 SimpleIdentifier nameNode = variable.name; | 340 SimpleIdentifier nameNode = variable.name; |
| 292 String name = nameNode.name; | 341 String name = nameNode.name; |
| 293 _SourceRegion sourceRegion = _getSourceRegion(variable); | 342 _SourceRegion sourceRegion = _getSourceRegion(variable); |
| 294 Element element = new Element(kind, name, | 343 Element element = new Element( |
| 295 Element.makeFlags(isPrivate: Identifier.isPrivateName(name), | 344 kind, |
| 345 name, |
| 346 Element.makeFlags( |
| 347 isPrivate: Identifier.isPrivateName(name), |
| 296 isDeprecated: _isDeprecated(variable), | 348 isDeprecated: _isDeprecated(variable), |
| 297 isStatic: isStatic, isConst: variable.isConst, | 349 isStatic: isStatic, |
| 298 isFinal: variable.isFinal), location: _getLocationNode(nameNode), | 350 isConst: variable.isConst, |
| 351 isFinal: variable.isFinal), |
| 352 location: _getLocationNode(nameNode), |
| 299 returnType: typeName); | 353 returnType: typeName); |
| 300 Outline outline = new Outline(element, sourceRegion.offset, | 354 Outline outline = |
| 301 sourceRegion.length); | 355 new Outline(element, sourceRegion.offset, sourceRegion.length); |
| 302 return outline; | 356 return outline; |
| 303 } | 357 } |
| 304 | 358 |
| 305 /** | 359 /** |
| 306 * Returns `true` if the given [element] is not `null` and deprecated. | 360 * Returns `true` if the given [element] is not `null` and deprecated. |
| 307 */ | 361 */ |
| 308 static bool _isDeprecated(Declaration declaration) { | 362 static bool _isDeprecated(Declaration declaration) { |
| 309 engine.Element element = declaration.element; | 363 engine.Element element = declaration.element; |
| 310 return element != null && element.isDeprecated; | 364 return element != null && element.isDeprecated; |
| 311 } | 365 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 329 | 383 |
| 330 | 384 |
| 331 /** | 385 /** |
| 332 * A range of characters. | 386 * A range of characters. |
| 333 */ | 387 */ |
| 334 class _SourceRegion { | 388 class _SourceRegion { |
| 335 final int length; | 389 final int length; |
| 336 final int offset; | 390 final int offset; |
| 337 _SourceRegion(this.offset, this.length); | 391 _SourceRegion(this.offset, this.length); |
| 338 } | 392 } |
| OLD | NEW |