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.contributor.dart.local; | 5 library services.completion.contributor.dart.local; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analysis_server/src/protocol.dart' as protocol | 9 import 'package:analysis_server/src/protocol.dart' as protocol |
10 show Element, ElementKind; | 10 show Element, ElementKind; |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 */ | 350 */ |
351 class _LocalVisitor extends LocalDeclarationVisitor { | 351 class _LocalVisitor extends LocalDeclarationVisitor { |
352 final DartCompletionRequest request; | 352 final DartCompletionRequest request; |
353 final OpType optype; | 353 final OpType optype; |
354 | 354 |
355 _LocalVisitor(this.request, int offset, this.optype) : super(offset); | 355 _LocalVisitor(this.request, int offset, this.optype) : super(offset); |
356 | 356 |
357 @override | 357 @override |
358 void declaredClass(ClassDeclaration declaration) { | 358 void declaredClass(ClassDeclaration declaration) { |
359 if (optype.includeTypeNameSuggestions) { | 359 if (optype.includeTypeNameSuggestions) { |
360 bool deprecated = isDeprecated(declaration); | 360 _addSuggestion( |
361 CompletionSuggestion suggestion = _addSuggestion( | 361 declaration.name, NO_RETURN_TYPE, protocol.ElementKind.CLASS, |
362 declaration.name, NO_RETURN_TYPE, deprecated, DART_RELEVANCE_DEFAULT); | 362 isAbstract: declaration.isAbstract, |
363 if (suggestion != null) { | 363 isDeprecated: isDeprecated(declaration)); |
364 suggestion.element = createElement( | |
365 protocol.ElementKind.CLASS, declaration.name, | |
366 returnType: NO_RETURN_TYPE, | |
367 isAbstract: declaration.isAbstract, | |
368 isDeprecated: deprecated); | |
369 } | |
370 } | 364 } |
371 } | 365 } |
372 | 366 |
373 @override | 367 @override |
374 void declaredClassTypeAlias(ClassTypeAlias declaration) { | 368 void declaredClassTypeAlias(ClassTypeAlias declaration) { |
375 if (optype.includeTypeNameSuggestions) { | 369 if (optype.includeTypeNameSuggestions) { |
376 bool deprecated = isDeprecated(declaration); | 370 _addSuggestion(declaration.name, NO_RETURN_TYPE, |
377 CompletionSuggestion suggestion = _addSuggestion( | 371 protocol.ElementKind.CLASS_TYPE_ALIAS, |
378 declaration.name, NO_RETURN_TYPE, deprecated, DART_RELEVANCE_DEFAULT); | 372 isAbstract: true, isDeprecated: isDeprecated(declaration)); |
379 if (suggestion != null) { | |
380 suggestion.element = createElement( | |
381 protocol.ElementKind.CLASS_TYPE_ALIAS, declaration.name, | |
382 returnType: NO_RETURN_TYPE, | |
383 isAbstract: true, | |
384 isDeprecated: deprecated); | |
385 } | |
386 } | 373 } |
387 } | 374 } |
388 | 375 |
389 @override | 376 @override |
390 void declaredEnum(EnumDeclaration declaration) { | 377 void declaredEnum(EnumDeclaration declaration) { |
391 SimpleIdentifier enumId = declaration.name; | 378 if (optype.includeTypeNameSuggestions) { |
392 if (enumId != null) { | 379 _addSuggestion( |
393 bool deprecated = isDeprecated(declaration); | 380 declaration.name, NO_RETURN_TYPE, protocol.ElementKind.ENUM, |
394 if (optype.includeTypeNameSuggestions) { | 381 isDeprecated: isDeprecated(declaration)); |
395 CompletionSuggestion suggestion = _addSuggestion( | |
396 enumId, NO_RETURN_TYPE, deprecated, DART_RELEVANCE_DEFAULT); | |
397 if (suggestion != null) { | |
398 suggestion.element = createElement(protocol.ElementKind.ENUM, enumId, | |
399 returnType: NO_RETURN_TYPE, isDeprecated: deprecated); | |
400 } | |
401 } | |
402 } | 382 } |
403 } | 383 } |
404 | 384 |
405 @override | 385 @override |
406 void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) { | 386 void declaredField(FieldDeclaration fieldDecl, VariableDeclaration varDecl) { |
407 if (optype.includeReturnValueSuggestions) { | 387 if (optype.includeReturnValueSuggestions) { |
408 CompletionSuggestion suggestion = | 388 CompletionSuggestion suggestion = |
409 createFieldSuggestion(fieldDecl, varDecl); | 389 createFieldSuggestion(fieldDecl, varDecl); |
410 if (suggestion != null) { | 390 if (suggestion != null) { |
411 request.addSuggestion(suggestion); | 391 request.addSuggestion(suggestion); |
412 } | 392 } |
413 } | 393 } |
414 } | 394 } |
415 | 395 |
416 @override | 396 @override |
417 void declaredFunction(FunctionDeclaration declaration) { | 397 void declaredFunction(FunctionDeclaration declaration) { |
418 if (optype.includeReturnValueSuggestions || | 398 if (optype.includeReturnValueSuggestions || |
419 optype.includeVoidReturnSuggestions) { | 399 optype.includeVoidReturnSuggestions) { |
420 TypeName returnType = declaration.returnType; | 400 TypeName typeName = declaration.returnType; |
421 bool deprecated = isDeprecated(declaration); | 401 protocol.ElementKind elemKind; |
422 protocol.ElementKind kind; | 402 int relevance = DART_RELEVANCE_DEFAULT; |
423 int defaultRelevance = DART_RELEVANCE_DEFAULT; | |
424 if (declaration.isGetter) { | 403 if (declaration.isGetter) { |
425 kind = protocol.ElementKind.GETTER; | 404 elemKind = protocol.ElementKind.GETTER; |
426 defaultRelevance = DART_RELEVANCE_LOCAL_ACCESSOR; | 405 relevance = DART_RELEVANCE_LOCAL_ACCESSOR; |
427 } else if (declaration.isSetter) { | 406 } else if (declaration.isSetter) { |
428 if (!optype.includeVoidReturnSuggestions) { | 407 if (!optype.includeVoidReturnSuggestions) { |
429 return; | 408 return; |
430 } | 409 } |
431 kind = protocol.ElementKind.SETTER; | 410 elemKind = protocol.ElementKind.SETTER; |
432 returnType = NO_RETURN_TYPE; | 411 typeName = NO_RETURN_TYPE; |
433 defaultRelevance = DART_RELEVANCE_LOCAL_ACCESSOR; | 412 relevance = DART_RELEVANCE_LOCAL_ACCESSOR; |
434 } else { | 413 } else { |
435 if (!optype.includeVoidReturnSuggestions && _isVoid(returnType)) { | 414 if (!optype.includeVoidReturnSuggestions && _isVoid(typeName)) { |
436 return; | 415 return; |
437 } | 416 } |
438 kind = protocol.ElementKind.FUNCTION; | 417 elemKind = protocol.ElementKind.FUNCTION; |
439 defaultRelevance = DART_RELEVANCE_LOCAL_FUNCTION; | 418 relevance = DART_RELEVANCE_LOCAL_FUNCTION; |
440 } | 419 } |
441 CompletionSuggestion suggestion = _addSuggestion( | 420 _addSuggestion(declaration.name, typeName, elemKind, |
442 declaration.name, returnType, deprecated, defaultRelevance); | 421 isDeprecated: isDeprecated(declaration), |
443 if (suggestion != null) { | 422 param: declaration.functionExpression.parameters, |
444 FormalParameterList param = declaration.functionExpression.parameters; | 423 relevance: relevance); |
445 suggestion.element = createElement(kind, declaration.name, | |
446 parameters: param != null ? param.toSource() : null, | |
447 returnType: returnType, | |
448 isDeprecated: deprecated); | |
449 if (kind == protocol.ElementKind.FUNCTION) { | |
450 _addParameterInfo( | |
451 suggestion, declaration.functionExpression.parameters); | |
452 } | |
453 } | |
454 } | 424 } |
455 } | 425 } |
456 | 426 |
457 @override | 427 @override |
458 void declaredFunctionTypeAlias(FunctionTypeAlias declaration) { | 428 void declaredFunctionTypeAlias(FunctionTypeAlias declaration) { |
459 if (optype.includeTypeNameSuggestions) { | 429 if (optype.includeTypeNameSuggestions) { |
460 bool deprecated = isDeprecated(declaration); | 430 // TODO (danrubel) determine parameters and return type |
461 TypeName returnType = declaration.returnType; | 431 _addSuggestion(declaration.name, declaration.returnType, |
462 CompletionSuggestion suggestion = _addSuggestion( | 432 protocol.ElementKind.FUNCTION_TYPE_ALIAS, |
463 declaration.name, returnType, deprecated, DART_RELEVANCE_DEFAULT); | 433 isAbstract: true, isDeprecated: isDeprecated(declaration)); |
464 if (suggestion != null) { | |
465 // TODO (danrubel) determine parameters and return type | |
466 suggestion.element = createElement( | |
467 protocol.ElementKind.FUNCTION_TYPE_ALIAS, declaration.name, | |
468 returnType: returnType, isAbstract: true, isDeprecated: deprecated); | |
469 } | |
470 } | 434 } |
471 } | 435 } |
472 | 436 |
473 @override | 437 @override |
474 void declaredLabel(Label label, bool isCaseLabel) { | 438 void declaredLabel(Label label, bool isCaseLabel) { |
475 // ignored | 439 // ignored |
476 } | 440 } |
477 | 441 |
478 @override | 442 @override |
479 void declaredLocalVar(SimpleIdentifier name, TypeName type) { | 443 void declaredLocalVar(SimpleIdentifier id, TypeName typeName) { |
480 if (optype.includeReturnValueSuggestions) { | 444 if (optype.includeReturnValueSuggestions) { |
481 CompletionSuggestion suggestion = | 445 _addSuggestion(id, typeName, protocol.ElementKind.LOCAL_VARIABLE, |
482 _addSuggestion(name, type, false, DART_RELEVANCE_LOCAL_VARIABLE); | 446 relevance: DART_RELEVANCE_LOCAL_VARIABLE); |
483 if (suggestion != null) { | |
484 suggestion.element = createElement( | |
485 protocol.ElementKind.LOCAL_VARIABLE, name, returnType: type); | |
486 } | |
487 } | 447 } |
488 } | 448 } |
489 | 449 |
490 @override | 450 @override |
491 void declaredMethod(MethodDeclaration declaration) { | 451 void declaredMethod(MethodDeclaration declaration) { |
492 if (optype.includeReturnValueSuggestions || | 452 if (optype.includeReturnValueSuggestions || |
493 optype.includeVoidReturnSuggestions) { | 453 optype.includeVoidReturnSuggestions) { |
494 protocol.ElementKind kind; | 454 protocol.ElementKind elemKind; |
495 String parameters; | 455 FormalParameterList param; |
496 TypeName returnType = declaration.returnType; | 456 TypeName typeName = declaration.returnType; |
497 int defaultRelevance = DART_RELEVANCE_DEFAULT; | 457 int relevance = DART_RELEVANCE_DEFAULT; |
498 if (declaration.isGetter) { | 458 if (declaration.isGetter) { |
499 kind = protocol.ElementKind.GETTER; | 459 elemKind = protocol.ElementKind.GETTER; |
500 parameters = null; | 460 param = null; |
501 defaultRelevance = DART_RELEVANCE_LOCAL_ACCESSOR; | 461 relevance = DART_RELEVANCE_LOCAL_ACCESSOR; |
502 } else if (declaration.isSetter) { | 462 } else if (declaration.isSetter) { |
503 if (!optype.includeVoidReturnSuggestions) { | 463 if (!optype.includeVoidReturnSuggestions) { |
504 return; | 464 return; |
505 } | 465 } |
506 kind = protocol.ElementKind.SETTER; | 466 elemKind = protocol.ElementKind.SETTER; |
507 returnType = NO_RETURN_TYPE; | 467 typeName = NO_RETURN_TYPE; |
508 defaultRelevance = DART_RELEVANCE_LOCAL_ACCESSOR; | 468 relevance = DART_RELEVANCE_LOCAL_ACCESSOR; |
509 } else { | 469 } else { |
510 if (!optype.includeVoidReturnSuggestions && _isVoid(returnType)) { | 470 if (!optype.includeVoidReturnSuggestions && _isVoid(typeName)) { |
511 return; | 471 return; |
512 } | 472 } |
513 kind = protocol.ElementKind.METHOD; | 473 elemKind = protocol.ElementKind.METHOD; |
514 parameters = declaration.parameters.toSource(); | 474 param = declaration.parameters; |
515 defaultRelevance = DART_RELEVANCE_LOCAL_METHOD; | 475 relevance = DART_RELEVANCE_LOCAL_METHOD; |
516 } | 476 } |
517 bool deprecated = isDeprecated(declaration); | 477 _addSuggestion(declaration.name, typeName, elemKind, |
518 CompletionSuggestion suggestion = _addSuggestion( | 478 isAbstract: declaration.isAbstract, |
519 declaration.name, returnType, deprecated, defaultRelevance, | 479 isDeprecated: isDeprecated(declaration), |
520 classDecl: declaration.parent); | 480 classDecl: declaration.parent, |
521 if (suggestion != null) { | 481 param: param, |
522 suggestion.element = createElement(kind, declaration.name, | 482 relevance: relevance); |
523 parameters: parameters, | |
524 returnType: returnType, | |
525 isAbstract: declaration.isAbstract, | |
526 isDeprecated: deprecated); | |
527 if (kind == protocol.ElementKind.METHOD) { | |
528 _addParameterInfo(suggestion, declaration.parameters); | |
529 } | |
530 } | |
531 } | 483 } |
532 } | 484 } |
533 | 485 |
534 @override | 486 @override |
535 void declaredParam(SimpleIdentifier name, TypeName type) { | 487 void declaredParam(SimpleIdentifier id, TypeName typeName) { |
536 if (optype.includeReturnValueSuggestions) { | 488 if (optype.includeReturnValueSuggestions) { |
537 CompletionSuggestion suggestion = | 489 _addSuggestion(id, typeName, protocol.ElementKind.PARAMETER, |
538 _addSuggestion(name, type, false, DART_RELEVANCE_PARAMETER); | 490 relevance: DART_RELEVANCE_PARAMETER); |
539 if (suggestion != null) { | |
540 suggestion.element = createElement(protocol.ElementKind.PARAMETER, name, | |
541 returnType: type); | |
542 } | |
543 } | 491 } |
544 } | 492 } |
545 | 493 |
546 @override | 494 @override |
547 void declaredTopLevelVar( | 495 void declaredTopLevelVar( |
548 VariableDeclarationList varList, VariableDeclaration varDecl) { | 496 VariableDeclarationList varList, VariableDeclaration varDecl) { |
549 if (optype.includeReturnValueSuggestions) { | 497 if (optype.includeReturnValueSuggestions) { |
550 bool deprecated = isDeprecated(varList) || isDeprecated(varDecl); | 498 _addSuggestion( |
551 CompletionSuggestion suggestion = _addSuggestion(varDecl.name, | 499 varDecl.name, varList.type, protocol.ElementKind.TOP_LEVEL_VARIABLE, |
552 varList.type, deprecated, DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE); | 500 isDeprecated: isDeprecated(varList) || isDeprecated(varDecl), |
553 if (suggestion != null) { | 501 relevance: DART_RELEVANCE_LOCAL_TOP_LEVEL_VARIABLE); |
554 suggestion.element = createElement( | |
555 protocol.ElementKind.TOP_LEVEL_VARIABLE, varDecl.name, | |
556 returnType: varList.type, isDeprecated: deprecated); | |
557 } | |
558 } | 502 } |
559 } | 503 } |
560 | 504 |
561 void _addParameterInfo( | 505 void _addParameterInfo( |
562 CompletionSuggestion suggestion, FormalParameterList parameters) { | 506 CompletionSuggestion suggestion, FormalParameterList parameters) { |
563 var paramList = parameters.parameters; | 507 var paramList = parameters.parameters; |
564 suggestion.parameterNames = paramList | 508 suggestion.parameterNames = paramList |
565 .map((FormalParameter param) => param.identifier.name) | 509 .map((FormalParameter param) => param.identifier.name) |
566 .toList(); | 510 .toList(); |
567 suggestion.parameterTypes = paramList.map((FormalParameter param) { | 511 suggestion.parameterTypes = paramList.map((FormalParameter param) { |
(...skipping 19 matching lines...) Expand all Loading... |
587 return 'dynamic'; | 531 return 'dynamic'; |
588 } | 532 } |
589 return typeId.name; | 533 return typeId.name; |
590 }).toList(); | 534 }).toList(); |
591 suggestion.requiredParameterCount = paramList.where( | 535 suggestion.requiredParameterCount = paramList.where( |
592 (FormalParameter param) => param is! DefaultFormalParameter).length; | 536 (FormalParameter param) => param is! DefaultFormalParameter).length; |
593 suggestion.hasNamedParameters = paramList | 537 suggestion.hasNamedParameters = paramList |
594 .any((FormalParameter param) => param.kind == ParameterKind.NAMED); | 538 .any((FormalParameter param) => param.kind == ParameterKind.NAMED); |
595 } | 539 } |
596 | 540 |
597 CompletionSuggestion _addSuggestion(SimpleIdentifier id, TypeName returnType, | 541 void _addSuggestion( |
598 bool isDeprecated, int defaultRelevance, {ClassDeclaration classDecl}) { | 542 SimpleIdentifier id, TypeName typeName, protocol.ElementKind elemKind, |
| 543 {bool isAbstract: false, bool isDeprecated: false, |
| 544 ClassDeclaration classDecl, FormalParameterList param, |
| 545 int relevance: DART_RELEVANCE_DEFAULT}) { |
599 CompletionSuggestion suggestion = createSuggestion( | 546 CompletionSuggestion suggestion = createSuggestion( |
600 id, isDeprecated, defaultRelevance, returnType, classDecl: classDecl); | 547 id, isDeprecated, relevance, typeName, classDecl: classDecl); |
601 if (suggestion != null) { | 548 if (suggestion != null) { |
602 request.addSuggestion(suggestion); | 549 request.addSuggestion(suggestion); |
| 550 suggestion.element = createElement(elemKind, id, |
| 551 isAbstract: isAbstract, |
| 552 isDeprecated: isDeprecated, |
| 553 parameters: param != null ? param.toSource() : null, |
| 554 returnType: typeName); |
| 555 if ((elemKind == protocol.ElementKind.METHOD || |
| 556 elemKind == protocol.ElementKind.FUNCTION) && |
| 557 param != null) { |
| 558 _addParameterInfo(suggestion, param); |
| 559 } |
603 } | 560 } |
604 return suggestion; | |
605 } | 561 } |
606 | 562 |
607 bool _isVoid(TypeName returnType) { | 563 bool _isVoid(TypeName returnType) { |
608 if (returnType != null) { | 564 if (returnType != null) { |
609 Identifier id = returnType.name; | 565 Identifier id = returnType.name; |
610 if (id != null && id.name == 'void') { | 566 if (id != null && id.name == 'void') { |
611 return true; | 567 return true; |
612 } | 568 } |
613 } | 569 } |
614 return false; | 570 return false; |
615 } | 571 } |
616 } | 572 } |
OLD | NEW |