| 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 services.completion.computer.dart.local; | 5 library services.completion.computer.dart.local; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/protocol.dart' as protocol show Element, | 9 import 'package:analysis_server/src/protocol.dart' as protocol show Element, |
| 10 ElementKind; | 10 ElementKind; |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 final bool typesOnly; | 200 final bool typesOnly; |
| 201 final bool excludeVoidReturn; | 201 final bool excludeVoidReturn; |
| 202 | 202 |
| 203 _LocalVisitor(this.request, int offset, this.typesOnly, | 203 _LocalVisitor(this.request, int offset, this.typesOnly, |
| 204 this.excludeVoidReturn) | 204 this.excludeVoidReturn) |
| 205 : super(offset); | 205 : super(offset); |
| 206 | 206 |
| 207 @override | 207 @override |
| 208 void declaredClass(ClassDeclaration declaration) { | 208 void declaredClass(ClassDeclaration declaration) { |
| 209 bool isDeprecated = _isDeprecated(declaration); | 209 bool isDeprecated = _isDeprecated(declaration); |
| 210 CompletionSuggestion suggestion = | 210 CompletionSuggestion suggestion = _addSuggestion( |
| 211 _addSuggestion(declaration.name, NO_RETURN_TYPE, isDeprecated); | 211 declaration.name, |
| 212 NO_RETURN_TYPE, |
| 213 isDeprecated, |
| 214 COMPLETION_RELEVANCE_DEFAULT); |
| 212 if (suggestion != null) { | 215 if (suggestion != null) { |
| 213 suggestion.element = _createElement( | 216 suggestion.element = _createElement( |
| 214 protocol.ElementKind.CLASS, | 217 protocol.ElementKind.CLASS, |
| 215 declaration.name, | 218 declaration.name, |
| 216 returnType: NO_RETURN_TYPE, | 219 returnType: NO_RETURN_TYPE, |
| 217 isAbstract: declaration.isAbstract, | 220 isAbstract: declaration.isAbstract, |
| 218 isDeprecated: isDeprecated); | 221 isDeprecated: isDeprecated); |
| 219 } | 222 } |
| 220 } | 223 } |
| 221 | 224 |
| 222 @override | 225 @override |
| 223 void declaredClassTypeAlias(ClassTypeAlias declaration) { | 226 void declaredClassTypeAlias(ClassTypeAlias declaration) { |
| 224 bool isDeprecated = _isDeprecated(declaration); | 227 bool isDeprecated = _isDeprecated(declaration); |
| 225 CompletionSuggestion suggestion = | 228 CompletionSuggestion suggestion = _addSuggestion( |
| 226 _addSuggestion(declaration.name, NO_RETURN_TYPE, isDeprecated); | 229 declaration.name, |
| 230 NO_RETURN_TYPE, |
| 231 isDeprecated, |
| 232 COMPLETION_RELEVANCE_DEFAULT); |
| 227 if (suggestion != null) { | 233 if (suggestion != null) { |
| 228 suggestion.element = _createElement( | 234 suggestion.element = _createElement( |
| 229 protocol.ElementKind.CLASS_TYPE_ALIAS, | 235 protocol.ElementKind.CLASS_TYPE_ALIAS, |
| 230 declaration.name, | 236 declaration.name, |
| 231 returnType: NO_RETURN_TYPE, | 237 returnType: NO_RETURN_TYPE, |
| 232 isAbstract: true, | 238 isAbstract: true, |
| 233 isDeprecated: isDeprecated); | 239 isDeprecated: isDeprecated); |
| 234 } | 240 } |
| 235 } | 241 } |
| 236 | 242 |
| 237 @override | 243 @override |
| 238 void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) { | 244 void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) { |
| 239 if (typesOnly) { | 245 if (typesOnly) { |
| 240 return; | 246 return; |
| 241 } | 247 } |
| 242 bool isDeprecated = _isDeprecated(fieldDecl) || _isDeprecated(varDecl); | 248 bool isDeprecated = _isDeprecated(fieldDecl) || _isDeprecated(varDecl); |
| 243 TypeName type = fieldDecl.fields.type; | 249 TypeName type = fieldDecl.fields.type; |
| 244 CompletionSuggestion suggestion = | 250 CompletionSuggestion suggestion = _addSuggestion( |
| 245 _addSuggestion(varDecl.name, type, isDeprecated, classDecl: fieldDecl.pa
rent); | 251 varDecl.name, |
| 252 type, |
| 253 isDeprecated, |
| 254 DART_RELEVANCE_LOCAL_FIELD, |
| 255 classDecl: fieldDecl.parent); |
| 246 if (suggestion != null) { | 256 if (suggestion != null) { |
| 247 suggestion.element = _createElement( | 257 suggestion.element = _createElement( |
| 248 protocol.ElementKind.FIELD, | 258 protocol.ElementKind.FIELD, |
| 249 varDecl.name, | 259 varDecl.name, |
| 250 returnType: type, | 260 returnType: type, |
| 251 isDeprecated: isDeprecated); | 261 isDeprecated: isDeprecated); |
| 252 } | 262 } |
| 253 } | 263 } |
| 254 | 264 |
| 255 @override | 265 @override |
| 256 void declaredFunction(FunctionDeclaration declaration) { | 266 void declaredFunction(FunctionDeclaration declaration) { |
| 257 if (typesOnly) { | 267 if (typesOnly) { |
| 258 return; | 268 return; |
| 259 } | 269 } |
| 260 TypeName returnType = declaration.returnType; | 270 TypeName returnType = declaration.returnType; |
| 261 bool isDeprecated = _isDeprecated(declaration); | 271 bool isDeprecated = _isDeprecated(declaration); |
| 262 protocol.ElementKind kind; | 272 protocol.ElementKind kind; |
| 273 int defaultRelevance = COMPLETION_RELEVANCE_DEFAULT; |
| 263 if (declaration.isGetter) { | 274 if (declaration.isGetter) { |
| 264 kind = protocol.ElementKind.GETTER; | 275 kind = protocol.ElementKind.GETTER; |
| 276 defaultRelevance = DART_RELEVANCE_LOCAL_ACCESSOR; |
| 265 } else if (declaration.isSetter) { | 277 } else if (declaration.isSetter) { |
| 266 if (excludeVoidReturn) { | 278 if (excludeVoidReturn) { |
| 267 return; | 279 return; |
| 268 } | 280 } |
| 269 kind = protocol.ElementKind.SETTER; | 281 kind = protocol.ElementKind.SETTER; |
| 270 returnType = NO_RETURN_TYPE; | 282 returnType = NO_RETURN_TYPE; |
| 283 defaultRelevance = DART_RELEVANCE_LOCAL_ACCESSOR; |
| 271 } else { | 284 } else { |
| 272 if (excludeVoidReturn && _isVoid(returnType)) { | 285 if (excludeVoidReturn && _isVoid(returnType)) { |
| 273 return; | 286 return; |
| 274 } | 287 } |
| 275 kind = protocol.ElementKind.FUNCTION; | 288 kind = protocol.ElementKind.FUNCTION; |
| 289 defaultRelevance = DART_RELEVANCE_LOCAL_FUNCTION; |
| 276 } | 290 } |
| 277 CompletionSuggestion suggestion = | 291 CompletionSuggestion suggestion = _addSuggestion( |
| 278 _addSuggestion(declaration.name, returnType, isDeprecated); | 292 declaration.name, |
| 293 returnType, |
| 294 isDeprecated, |
| 295 defaultRelevance); |
| 279 if (suggestion != null) { | 296 if (suggestion != null) { |
| 280 FormalParameterList param = declaration.functionExpression.parameters; | 297 FormalParameterList param = declaration.functionExpression.parameters; |
| 281 suggestion.element = _createElement( | 298 suggestion.element = _createElement( |
| 282 kind, | 299 kind, |
| 283 declaration.name, | 300 declaration.name, |
| 284 parameters: param != null ? param.toSource() : null, | 301 parameters: param != null ? param.toSource() : null, |
| 285 returnType: returnType, | 302 returnType: returnType, |
| 286 isDeprecated: isDeprecated); | 303 isDeprecated: isDeprecated); |
| 287 if (kind == protocol.ElementKind.FUNCTION) { | 304 if (kind == protocol.ElementKind.FUNCTION) { |
| 288 _addParameterInfo( | 305 _addParameterInfo( |
| 289 suggestion, | 306 suggestion, |
| 290 declaration.functionExpression.parameters); | 307 declaration.functionExpression.parameters); |
| 291 } | 308 } |
| 292 } | 309 } |
| 293 } | 310 } |
| 294 | 311 |
| 295 @override | 312 @override |
| 296 void declaredFunctionTypeAlias(FunctionTypeAlias declaration) { | 313 void declaredFunctionTypeAlias(FunctionTypeAlias declaration) { |
| 297 bool isDeprecated = _isDeprecated(declaration); | 314 bool isDeprecated = _isDeprecated(declaration); |
| 298 TypeName returnType = declaration.returnType; | 315 TypeName returnType = declaration.returnType; |
| 299 CompletionSuggestion suggestion = | 316 CompletionSuggestion suggestion = _addSuggestion( |
| 300 _addSuggestion(declaration.name, returnType, isDeprecated); | 317 declaration.name, |
| 318 returnType, |
| 319 isDeprecated, |
| 320 COMPLETION_RELEVANCE_DEFAULT); |
| 301 if (suggestion != null) { | 321 if (suggestion != null) { |
| 302 // TODO (danrubel) determine parameters and return type | 322 // TODO (danrubel) determine parameters and return type |
| 303 suggestion.element = _createElement( | 323 suggestion.element = _createElement( |
| 304 protocol.ElementKind.FUNCTION_TYPE_ALIAS, | 324 protocol.ElementKind.FUNCTION_TYPE_ALIAS, |
| 305 declaration.name, | 325 declaration.name, |
| 306 returnType: returnType, | 326 returnType: returnType, |
| 307 isAbstract: true, | 327 isAbstract: true, |
| 308 isDeprecated: isDeprecated); | 328 isDeprecated: isDeprecated); |
| 309 } | 329 } |
| 310 } | 330 } |
| 311 | 331 |
| 312 @override | 332 @override |
| 313 void declaredLabel(Label label, bool isCaseLabel) { | 333 void declaredLabel(Label label, bool isCaseLabel) { |
| 314 // ignored | 334 // ignored |
| 315 } | 335 } |
| 316 | 336 |
| 317 @override | 337 @override |
| 318 void declaredLocalVar(SimpleIdentifier name, TypeName type) { | 338 void declaredLocalVar(SimpleIdentifier name, TypeName type) { |
| 319 if (typesOnly) { | 339 if (typesOnly) { |
| 320 return; | 340 return; |
| 321 } | 341 } |
| 322 CompletionSuggestion suggestion = _addSuggestion(name, type, false); | 342 CompletionSuggestion suggestion = |
| 343 _addSuggestion(name, type, false, DART_RELEVANCE_LOCAL_VARIABLE); |
| 323 if (suggestion != null) { | 344 if (suggestion != null) { |
| 324 suggestion.element = | 345 suggestion.element = |
| 325 _createElement(protocol.ElementKind.LOCAL_VARIABLE, name, returnType:
type); | 346 _createElement(protocol.ElementKind.LOCAL_VARIABLE, name, returnType:
type); |
| 326 } | 347 } |
| 327 } | 348 } |
| 328 | 349 |
| 329 @override | 350 @override |
| 330 void declaredMethod(MethodDeclaration declaration) { | 351 void declaredMethod(MethodDeclaration declaration) { |
| 331 if (typesOnly) { | 352 if (typesOnly) { |
| 332 return; | 353 return; |
| 333 } | 354 } |
| 334 protocol.ElementKind kind; | 355 protocol.ElementKind kind; |
| 335 String parameters; | 356 String parameters; |
| 336 TypeName returnType = declaration.returnType; | 357 TypeName returnType = declaration.returnType; |
| 358 int defaultRelevance = COMPLETION_RELEVANCE_DEFAULT; |
| 337 if (declaration.isGetter) { | 359 if (declaration.isGetter) { |
| 338 kind = protocol.ElementKind.GETTER; | 360 kind = protocol.ElementKind.GETTER; |
| 339 parameters = null; | 361 parameters = null; |
| 362 defaultRelevance = DART_RELEVANCE_LOCAL_ACCESSOR; |
| 340 } else if (declaration.isSetter) { | 363 } else if (declaration.isSetter) { |
| 341 if (excludeVoidReturn) { | 364 if (excludeVoidReturn) { |
| 342 return; | 365 return; |
| 343 } | 366 } |
| 344 kind = protocol.ElementKind.SETTER; | 367 kind = protocol.ElementKind.SETTER; |
| 345 returnType = NO_RETURN_TYPE; | 368 returnType = NO_RETURN_TYPE; |
| 369 defaultRelevance = DART_RELEVANCE_LOCAL_ACCESSOR; |
| 346 } else { | 370 } else { |
| 347 if (excludeVoidReturn && _isVoid(returnType)) { | 371 if (excludeVoidReturn && _isVoid(returnType)) { |
| 348 return; | 372 return; |
| 349 } | 373 } |
| 350 kind = protocol.ElementKind.METHOD; | 374 kind = protocol.ElementKind.METHOD; |
| 351 parameters = declaration.parameters.toSource(); | 375 parameters = declaration.parameters.toSource(); |
| 376 defaultRelevance = DART_RELEVANCE_LOCAL_METHOD; |
| 352 } | 377 } |
| 353 bool isDeprecated = _isDeprecated(declaration); | 378 bool isDeprecated = _isDeprecated(declaration); |
| 354 CompletionSuggestion suggestion = _addSuggestion( | 379 CompletionSuggestion suggestion = _addSuggestion( |
| 355 declaration.name, | 380 declaration.name, |
| 356 returnType, | 381 returnType, |
| 357 isDeprecated, | 382 isDeprecated, |
| 383 defaultRelevance, |
| 358 classDecl: declaration.parent); | 384 classDecl: declaration.parent); |
| 359 if (suggestion != null) { | 385 if (suggestion != null) { |
| 360 suggestion.element = _createElement( | 386 suggestion.element = _createElement( |
| 361 kind, | 387 kind, |
| 362 declaration.name, | 388 declaration.name, |
| 363 parameters: parameters, | 389 parameters: parameters, |
| 364 returnType: returnType, | 390 returnType: returnType, |
| 365 isAbstract: declaration.isAbstract, | 391 isAbstract: declaration.isAbstract, |
| 366 isDeprecated: isDeprecated); | 392 isDeprecated: isDeprecated); |
| 367 if (kind == protocol.ElementKind.METHOD) { | 393 if (kind == protocol.ElementKind.METHOD) { |
| 368 _addParameterInfo(suggestion, declaration.parameters); | 394 _addParameterInfo(suggestion, declaration.parameters); |
| 369 } | 395 } |
| 370 } | 396 } |
| 371 } | 397 } |
| 372 | 398 |
| 373 @override | 399 @override |
| 374 void declaredParam(SimpleIdentifier name, TypeName type) { | 400 void declaredParam(SimpleIdentifier name, TypeName type) { |
| 375 if (typesOnly) { | 401 if (typesOnly) { |
| 376 return; | 402 return; |
| 377 } | 403 } |
| 378 CompletionSuggestion suggestion = _addSuggestion(name, type, false); | 404 CompletionSuggestion suggestion = |
| 405 _addSuggestion(name, type, false, DART_RELEVANCE_PARAMETER); |
| 379 if (suggestion != null) { | 406 if (suggestion != null) { |
| 380 suggestion.element = | 407 suggestion.element = |
| 381 _createElement(protocol.ElementKind.PARAMETER, name, returnType: type)
; | 408 _createElement(protocol.ElementKind.PARAMETER, name, returnType: type)
; |
| 382 } | 409 } |
| 383 } | 410 } |
| 384 | 411 |
| 385 @override | 412 @override |
| 386 void declaredTopLevelVar(VariableDeclarationList varList, | 413 void declaredTopLevelVar(VariableDeclarationList varList, |
| 387 VariableDeclaration varDecl) { | 414 VariableDeclaration varDecl) { |
| 388 if (typesOnly) { | 415 if (typesOnly) { |
| 389 return; | 416 return; |
| 390 } | 417 } |
| 391 bool isDeprecated = _isDeprecated(varList) || _isDeprecated(varDecl); | 418 bool isDeprecated = _isDeprecated(varList) || _isDeprecated(varDecl); |
| 392 CompletionSuggestion suggestion = | 419 CompletionSuggestion suggestion = _addSuggestion( |
| 393 _addSuggestion(varDecl.name, varList.type, isDeprecated); | 420 varDecl.name, |
| 421 varList.type, |
| 422 isDeprecated, |
| 423 DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE); |
| 394 if (suggestion != null) { | 424 if (suggestion != null) { |
| 395 suggestion.element = _createElement( | 425 suggestion.element = _createElement( |
| 396 protocol.ElementKind.TOP_LEVEL_VARIABLE, | 426 protocol.ElementKind.TOP_LEVEL_VARIABLE, |
| 397 varDecl.name, | 427 varDecl.name, |
| 398 returnType: varList.type, | 428 returnType: varList.type, |
| 399 isDeprecated: isDeprecated); | 429 isDeprecated: isDeprecated); |
| 400 } | 430 } |
| 401 } | 431 } |
| 402 | 432 |
| 403 void _addParameterInfo(CompletionSuggestion suggestion, | 433 void _addParameterInfo(CompletionSuggestion suggestion, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 429 } | 459 } |
| 430 return typeId.name; | 460 return typeId.name; |
| 431 }).toList(); | 461 }).toList(); |
| 432 suggestion.requiredParameterCount = paramList.where( | 462 suggestion.requiredParameterCount = paramList.where( |
| 433 (FormalParameter param) => param is! DefaultFormalParameter).length; | 463 (FormalParameter param) => param is! DefaultFormalParameter).length; |
| 434 suggestion.hasNamedParameters = | 464 suggestion.hasNamedParameters = |
| 435 paramList.any((FormalParameter param) => param.kind == ParameterKind.NAM
ED); | 465 paramList.any((FormalParameter param) => param.kind == ParameterKind.NAM
ED); |
| 436 } | 466 } |
| 437 | 467 |
| 438 CompletionSuggestion _addSuggestion(SimpleIdentifier id, TypeName returnType, | 468 CompletionSuggestion _addSuggestion(SimpleIdentifier id, TypeName returnType, |
| 439 bool isDeprecated, {ClassDeclaration classDecl}) { | 469 bool isDeprecated, int defaultRelevance, {ClassDeclaration classDecl}) { |
| 440 if (id != null) { | 470 if (id != null) { |
| 441 String completion = id.name; | 471 String completion = id.name; |
| 442 if (completion != null && completion.length > 0 && completion != '_') { | 472 if (completion != null && completion.length > 0 && completion != '_') { |
| 443 CompletionSuggestion suggestion = new CompletionSuggestion( | 473 CompletionSuggestion suggestion = new CompletionSuggestion( |
| 444 CompletionSuggestionKind.INVOCATION, | 474 CompletionSuggestionKind.INVOCATION, |
| 445 isDeprecated ? COMPLETION_RELEVANCE_LOW : COMPLETION_RELEVANCE_DEFAU
LT, | 475 isDeprecated ? COMPLETION_RELEVANCE_LOW : defaultRelevance, |
| 446 completion, | 476 completion, |
| 447 completion.length, | 477 completion.length, |
| 448 0, | 478 0, |
| 449 isDeprecated, | 479 isDeprecated, |
| 450 false, | 480 false, |
| 451 returnType: _nameForType(returnType)); | 481 returnType: _nameForType(returnType)); |
| 452 if (classDecl != null) { | 482 if (classDecl != null) { |
| 453 SimpleIdentifier identifier = classDecl.name; | 483 SimpleIdentifier identifier = classDecl.name; |
| 454 if (identifier != null) { | 484 if (identifier != null) { |
| 455 String name = identifier.name; | 485 String name = identifier.name; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 if (name == null || name.length <= 0) { | 558 if (name == null || name.length <= 0) { |
| 529 return DYNAMIC; | 559 return DYNAMIC; |
| 530 } | 560 } |
| 531 TypeArgumentList typeArgs = type.typeArguments; | 561 TypeArgumentList typeArgs = type.typeArguments; |
| 532 if (typeArgs != null) { | 562 if (typeArgs != null) { |
| 533 //TODO (danrubel) include type arguments | 563 //TODO (danrubel) include type arguments |
| 534 } | 564 } |
| 535 return name; | 565 return name; |
| 536 } | 566 } |
| 537 } | 567 } |
| OLD | NEW |