| 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 test.services.completion.util; | 5 library test.services.completion.util; |
| 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 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 | 444 |
| 445 // Update the index | 445 // Update the index |
| 446 result.changeNotices.forEach((ChangeNotice notice) { | 446 result.changeNotices.forEach((ChangeNotice notice) { |
| 447 CompilationUnit unit = notice.compilationUnit; | 447 CompilationUnit unit = notice.compilationUnit; |
| 448 if (unit != null) { | 448 if (unit != null) { |
| 449 index.indexUnit(context, unit); | 449 index.indexUnit(context, unit); |
| 450 } | 450 } |
| 451 }); | 451 }); |
| 452 | 452 |
| 453 // If the unit has been resolved, then finish the completion | 453 // If the unit has been resolved, then finish the completion |
| 454 LibraryElement library = context.getLibraryElement(testSource); | 454 List<Source> libSourceList = context.getLibrariesContaining(testSource); |
| 455 if (library != null) { | 455 if (libSourceList.length > 0) { |
| 456 CompilationUnit unit = | 456 LibraryElement library = context.getLibraryElement(libSourceList[0]); |
| 457 context.getResolvedCompilationUnit(testSource, library); | 457 if (library != null) { |
| 458 if (unit != null) { | 458 CompilationUnit unit = |
| 459 request.unit = unit; | 459 context.getResolvedCompilationUnit(testSource, library); |
| 460 request.node = | 460 if (unit != null) { |
| 461 new NodeLocator.con1(completionOffset).searchWithin(unit); | 461 request.unit = unit; |
| 462 if (request.node is SimpleIdentifier) { | 462 request.node = |
| 463 request.replacementOffset = request.node.offset; | 463 new NodeLocator.con1(completionOffset).searchWithin(unit); |
| 464 request.replacementLength = request.node.length; | 464 if (request.node is SimpleIdentifier) { |
| 465 } else { | 465 request.replacementOffset = request.node.offset; |
| 466 request.replacementOffset = request.offset; | 466 request.replacementLength = request.node.length; |
| 467 request.replacementLength = 0; | 467 } else { |
| 468 } | 468 request.replacementOffset = request.offset; |
| 469 if (request.replacementOffset == null) { | 469 request.replacementLength = 0; |
| 470 fail('expected non null'); | 470 } |
| 471 } | 471 if (request.replacementOffset == null) { |
| 472 resolved = true; | 472 fail('expected non null'); |
| 473 if (!fullAnalysis) { | 473 } |
| 474 break; | 474 resolved = true; |
| 475 if (!fullAnalysis) { |
| 476 break; |
| 477 } |
| 475 } | 478 } |
| 476 } | 479 } |
| 477 } | 480 } |
| 478 | 481 |
| 479 result = context.performAnalysisTask(); | 482 result = context.performAnalysisTask(); |
| 480 } | 483 } |
| 481 if (!resolved) { | 484 if (!resolved) { |
| 482 fail('expected unit to be resolved'); | 485 fail('expected unit to be resolved'); |
| 483 } | 486 } |
| 484 return computer.computeFull(request).then(assertFunction); | 487 return computer.computeFull(request).then(assertFunction); |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 756 CompletionSuggestion assertSuggestLocalTopLevelVar(String name, | 759 CompletionSuggestion assertSuggestLocalTopLevelVar(String name, |
| 757 String returnType, [CompletionRelevance relevance = | 760 String returnType, [CompletionRelevance relevance = |
| 758 CompletionRelevance.DEFAULT]) { | 761 CompletionRelevance.DEFAULT]) { |
| 759 if (computer is LocalComputer) { | 762 if (computer is LocalComputer) { |
| 760 return assertSuggestTopLevelVar(name, returnType, relevance); | 763 return assertSuggestTopLevelVar(name, returnType, relevance); |
| 761 } else { | 764 } else { |
| 762 return assertNotSuggested(name); | 765 return assertNotSuggested(name); |
| 763 } | 766 } |
| 764 } | 767 } |
| 765 | 768 |
| 769 CompletionSuggestion assertSuggestNonLocalClass(String name, |
| 770 [CompletionRelevance relevance = CompletionRelevance.DEFAULT, |
| 771 CompletionSuggestionKind kind = CompletionSuggestionKind.INVOCATION]) { |
| 772 return assertSuggestImportedClass(name, relevance, kind); |
| 773 } |
| 774 |
| 766 Future computeFull(assertFunction(bool result), {bool fullAnalysis: true}) { | 775 Future computeFull(assertFunction(bool result), {bool fullAnalysis: true}) { |
| 767 return super.computeFull( | 776 return super.computeFull( |
| 768 assertFunction, | 777 assertFunction, |
| 769 fullAnalysis: fullAnalysis).then(assertCachedCompute); | 778 fullAnalysis: fullAnalysis).then(assertCachedCompute); |
| 770 } | 779 } |
| 771 | 780 |
| 772 test_ArgumentList() { | 781 test_ArgumentList() { |
| 773 // ArgumentList MethodInvocation ExpressionStatement Block | 782 // ArgumentList MethodInvocation ExpressionStatement Block |
| 774 addSource('/libA.dart', ''' | 783 addSource('/libA.dart', ''' |
| 775 library A; | 784 library A; |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 // the user may be either (1) entering a type for the assignment | 951 // the user may be either (1) entering a type for the assignment |
| 943 // or (2) starting a new statement. | 952 // or (2) starting a new statement. |
| 944 // Consider suggesting only types | 953 // Consider suggesting only types |
| 945 // if only spaces separates the 1st and 2nd identifiers. | 954 // if only spaces separates the 1st and 2nd identifiers. |
| 946 //assertNotSuggested('a'); | 955 //assertNotSuggested('a'); |
| 947 //assertNotSuggested('main'); | 956 //assertNotSuggested('main'); |
| 948 //assertNotSuggested('identical'); | 957 //assertNotSuggested('identical'); |
| 949 }); | 958 }); |
| 950 } | 959 } |
| 951 | 960 |
| 961 test_AssignmentExpression_type_newline() { |
| 962 // SimpleIdentifier TypeName VariableDeclarationList |
| 963 // VariableDeclarationStatement Block |
| 964 addTestSource(''' |
| 965 class A {} main() { |
| 966 int a; |
| 967 ^ |
| 968 b = 1;}'''); |
| 969 computeFast(); |
| 970 return computeFull((bool result) { |
| 971 assertSuggestLocalClass('A'); |
| 972 assertSuggestImportedClass('int'); |
| 973 // Allow non-types preceding an identifier on LHS of assignment |
| 974 // if newline follows first identifier |
| 975 // because user is probably starting a new statement |
| 976 assertSuggestLocalVariable('a', 'int'); |
| 977 assertSuggestLocalFunction('main', null); |
| 978 assertSuggestImportedFunction('identical', 'bool'); |
| 979 }); |
| 980 } |
| 981 |
| 952 test_AssignmentExpression_type_partial() { | 982 test_AssignmentExpression_type_partial() { |
| 953 // SimpleIdentifier TypeName VariableDeclarationList | 983 // SimpleIdentifier TypeName VariableDeclarationList |
| 954 // VariableDeclarationStatement Block | 984 // VariableDeclarationStatement Block |
| 955 addTestSource(''' | 985 addTestSource(''' |
| 956 class A {} main() { | 986 class A {} main() { |
| 957 int a; | 987 int a; |
| 958 int^ b = 1;}'''); | 988 int^ b = 1;}'''); |
| 959 computeFast(); | 989 computeFast(); |
| 960 return computeFull((bool result) { | 990 return computeFull((bool result) { |
| 961 assertSuggestLocalClass('A'); | 991 assertSuggestLocalClass('A'); |
| 962 assertSuggestImportedClass('int'); | 992 assertSuggestImportedClass('int'); |
| 963 // TODO (danrubel) When entering 1st of 2 identifiers on assignment LHS | 993 // TODO (danrubel) When entering 1st of 2 identifiers on assignment LHS |
| 964 // the user may be either (1) entering a type for the assignment | 994 // the user may be either (1) entering a type for the assignment |
| 965 // or (2) starting a new statement. | 995 // or (2) starting a new statement. |
| 966 // Consider suggesting only types | 996 // Consider suggesting only types |
| 967 // if only spaces separates the 1st and 2nd identifiers. | 997 // if only spaces separates the 1st and 2nd identifiers. |
| 968 //assertNotSuggested('a'); | 998 //assertNotSuggested('a'); |
| 969 //assertNotSuggested('main'); | 999 //assertNotSuggested('main'); |
| 970 //assertNotSuggested('identical'); | 1000 //assertNotSuggested('identical'); |
| 971 }); | 1001 }); |
| 972 } | 1002 } |
| 973 | 1003 |
| 974 test_AssignmentExpression_type_newline() { | |
| 975 // SimpleIdentifier TypeName VariableDeclarationList | |
| 976 // VariableDeclarationStatement Block | |
| 977 addTestSource(''' | |
| 978 class A {} main() { | |
| 979 int a; | |
| 980 ^ | |
| 981 b = 1;}'''); | |
| 982 computeFast(); | |
| 983 return computeFull((bool result) { | |
| 984 assertSuggestLocalClass('A'); | |
| 985 assertSuggestImportedClass('int'); | |
| 986 // Allow non-types preceding an identifier on LHS of assignment | |
| 987 // if newline follows first identifier | |
| 988 // because user is probably starting a new statement | |
| 989 assertSuggestLocalVariable('a', 'int'); | |
| 990 assertSuggestLocalFunction('main', null); | |
| 991 assertSuggestImportedFunction('identical', 'bool'); | |
| 992 }); | |
| 993 } | |
| 994 | |
| 995 test_AssignmentExpression_type_partial_newline() { | 1004 test_AssignmentExpression_type_partial_newline() { |
| 996 // SimpleIdentifier TypeName VariableDeclarationList | 1005 // SimpleIdentifier TypeName VariableDeclarationList |
| 997 // VariableDeclarationStatement Block | 1006 // VariableDeclarationStatement Block |
| 998 addTestSource(''' | 1007 addTestSource(''' |
| 999 class A {} main() { | 1008 class A {} main() { |
| 1000 int a; | 1009 int a; |
| 1001 i^ | 1010 i^ |
| 1002 b = 1;}'''); | 1011 b = 1;}'''); |
| 1003 computeFast(); | 1012 computeFast(); |
| 1004 return computeFull((bool result) { | 1013 return computeFull((bool result) { |
| (...skipping 1032 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2037 assertNotSuggested('m'); | 2046 assertNotSuggested('m'); |
| 2038 assertNotSuggested('_n'); | 2047 assertNotSuggested('_n'); |
| 2039 assertNotSuggested('a'); | 2048 assertNotSuggested('a'); |
| 2040 assertNotSuggested('A'); | 2049 assertNotSuggested('A'); |
| 2041 assertNotSuggested('X'); | 2050 assertNotSuggested('X'); |
| 2042 assertNotSuggested('Object'); | 2051 assertNotSuggested('Object'); |
| 2043 assertNotSuggested('=='); | 2052 assertNotSuggested('=='); |
| 2044 }); | 2053 }); |
| 2045 } | 2054 } |
| 2046 | 2055 |
| 2056 test_partFile_TypeName() { |
| 2057 // SimpleIdentifier TypeName ConstructorName |
| 2058 addSource('/testB.dart', ''' |
| 2059 lib B; |
| 2060 int T1; |
| 2061 F1() { } |
| 2062 class X {X.c(); X._d(); z() {}}'''); |
| 2063 addSource('/testA.dart', ''' |
| 2064 library libA; |
| 2065 import "/testB.dart"; |
| 2066 part "$testFile"; |
| 2067 class A { } |
| 2068 var m;'''); |
| 2069 addTestSource(''' |
| 2070 part of libA; |
| 2071 class B { } |
| 2072 main() {new ^}'''); |
| 2073 computeFast(); |
| 2074 return computeFull((bool result) { |
| 2075 assertSuggestLocalClass('B'); |
| 2076 assertSuggestImportedClass('Object'); |
| 2077 assertSuggestImportedClass('X'); |
| 2078 assertSuggestNonLocalClass('A'); |
| 2079 assertNotSuggested('F1'); |
| 2080 assertNotSuggested('T1'); |
| 2081 assertNotSuggested('_d'); |
| 2082 assertNotSuggested('z'); |
| 2083 assertNotSuggested('m'); |
| 2084 }); |
| 2085 } |
| 2086 |
| 2087 test_partFile_TypeName2() { |
| 2088 // SimpleIdentifier TypeName ConstructorName |
| 2089 addSource('/testB.dart', ''' |
| 2090 lib B; |
| 2091 int T1; |
| 2092 F1() { } |
| 2093 class X {X.c(); X._d(); z() {}}'''); |
| 2094 addSource('/testA.dart',''' |
| 2095 part of libA; |
| 2096 class B { }'''); |
| 2097 addTestSource( ''' |
| 2098 library libA; |
| 2099 import "/testB.dart"; |
| 2100 part "/testA.dart"; |
| 2101 class A { } |
| 2102 main() {new ^} |
| 2103 var m;'''); |
| 2104 computeFast(); |
| 2105 return computeFull((bool result) { |
| 2106 assertSuggestLocalClass('A'); |
| 2107 assertSuggestImportedClass('Object'); |
| 2108 assertSuggestImportedClass('X'); |
| 2109 assertSuggestNonLocalClass('B'); |
| 2110 assertNotSuggested('F1'); |
| 2111 assertNotSuggested('T1'); |
| 2112 assertNotSuggested('_d'); |
| 2113 assertNotSuggested('z'); |
| 2114 assertNotSuggested('m'); |
| 2115 }); |
| 2116 } |
| 2117 |
| 2047 test_PrefixedIdentifier_class_const() { | 2118 test_PrefixedIdentifier_class_const() { |
| 2048 // SimpleIdentifier PrefixedIdentifier ExpressionStatement Block | 2119 // SimpleIdentifier PrefixedIdentifier ExpressionStatement Block |
| 2049 addSource('/testB.dart', ''' | 2120 addSource('/testB.dart', ''' |
| 2050 lib B; | 2121 lib B; |
| 2051 class I { | 2122 class I { |
| 2052 static const scI = 'boo'; | 2123 static const scI = 'boo'; |
| 2053 X get f => new A(); | 2124 X get f => new A(); |
| 2054 get _g => new A();} | 2125 get _g => new A();} |
| 2055 class B implements I { | 2126 class B implements I { |
| 2056 static const int scB = 12; | 2127 static const int scB = 12; |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2222 test_PrefixedIdentifier_propertyAccess() { | 2293 test_PrefixedIdentifier_propertyAccess() { |
| 2223 // PrefixedIdentifier ExpressionStatement Block BlockFunctionBody | 2294 // PrefixedIdentifier ExpressionStatement Block BlockFunctionBody |
| 2224 addTestSource('class A {String x; int get foo {x.^}'); | 2295 addTestSource('class A {String x; int get foo {x.^}'); |
| 2225 computeFast(); | 2296 computeFast(); |
| 2226 return computeFull((bool result) { | 2297 return computeFull((bool result) { |
| 2227 assertSuggestInvocationGetter('isEmpty', 'bool'); | 2298 assertSuggestInvocationGetter('isEmpty', 'bool'); |
| 2228 assertSuggestInvocationMethod('compareTo', 'Comparable', 'int'); | 2299 assertSuggestInvocationMethod('compareTo', 'Comparable', 'int'); |
| 2229 }); | 2300 }); |
| 2230 } | 2301 } |
| 2231 | 2302 |
| 2303 test_PrefixedIdentifier_propertyAccess_newStmt() { |
| 2304 // PrefixedIdentifier ExpressionStatement Block BlockFunctionBody |
| 2305 addTestSource('class A {String x; int get foo {x.^ int y = 0;}'); |
| 2306 computeFast(); |
| 2307 return computeFull((bool result) { |
| 2308 assertSuggestInvocationGetter('isEmpty', 'bool'); |
| 2309 assertSuggestInvocationMethod('compareTo', 'Comparable', 'int'); |
| 2310 }); |
| 2311 } |
| 2312 |
| 2232 test_PropertyAccess_expression() { | 2313 test_PropertyAccess_expression() { |
| 2233 // SimpleIdentifier MethodInvocation PropertyAccess ExpressionStatement | 2314 // SimpleIdentifier MethodInvocation PropertyAccess ExpressionStatement |
| 2234 addTestSource('class A {a() {"hello".to^String().length}}'); | 2315 addTestSource('class A {a() {"hello".to^String().length}}'); |
| 2235 computeFast(); | 2316 computeFast(); |
| 2236 return computeFull((bool result) { | 2317 return computeFull((bool result) { |
| 2237 assertSuggestInvocationGetter('length', 'int'); | 2318 assertSuggestInvocationGetter('length', 'int'); |
| 2238 assertNotSuggested('A'); | 2319 assertNotSuggested('A'); |
| 2239 assertNotSuggested('a'); | 2320 assertNotSuggested('a'); |
| 2240 assertNotSuggested('Object'); | 2321 assertNotSuggested('Object'); |
| 2241 assertNotSuggested('=='); | 2322 assertNotSuggested('=='); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2411 assertNotSuggested('bar2'); | 2492 assertNotSuggested('bar2'); |
| 2412 assertNotSuggested('_B'); | 2493 assertNotSuggested('_B'); |
| 2413 assertSuggestLocalClass('Y'); | 2494 assertSuggestLocalClass('Y'); |
| 2414 assertSuggestLocalClass('C'); | 2495 assertSuggestLocalClass('C'); |
| 2415 assertSuggestLocalVariable('f', null); | 2496 assertSuggestLocalVariable('f', null); |
| 2416 assertNotSuggested('x'); | 2497 assertNotSuggested('x'); |
| 2417 assertNotSuggested('e'); | 2498 assertNotSuggested('e'); |
| 2418 }); | 2499 }); |
| 2419 } | 2500 } |
| 2420 } | 2501 } |
| OLD | NEW |