| 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 search.domain; | 5 library search.domain; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/analysis_server.dart'; | 7 import 'package:analysis_server/src/analysis_server.dart'; |
| 8 import 'package:analysis_server/src/constants.dart'; | 8 import 'package:analysis_server/src/constants.dart'; |
| 9 import 'package:analysis_server/src/protocol.dart' as protocol; | 9 import 'package:analysis_server/src/protocol_server.dart' as protocol; |
| 10 import 'package:analysis_server/src/search/element_references.dart'; | 10 import 'package:analysis_server/src/search/element_references.dart'; |
| 11 import 'package:analysis_server/src/search/type_hierarchy.dart'; | 11 import 'package:analysis_server/src/search/type_hierarchy.dart'; |
| 12 import 'package:analysis_server/src/services/search/search_engine.dart'; | 12 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 13 import 'package:analyzer/src/generated/element.dart'; | 13 import 'package:analyzer/src/generated/element.dart'; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Instances of the class [SearchDomainHandler] implement a [RequestHandler] | 16 * Instances of the class [SearchDomainHandler] implement a [RequestHandler] |
| 17 * that handles requests in the search domain. | 17 * that handles requests in the search domain. |
| 18 */ | 18 */ |
| 19 class SearchDomainHandler implements protocol.RequestHandler { | 19 class SearchDomainHandler implements protocol.RequestHandler { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 33 int _nextSearchId = 0; | 33 int _nextSearchId = 0; |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * Initialize a newly created handler to handle requests for the given [server
]. | 36 * Initialize a newly created handler to handle requests for the given [server
]. |
| 37 */ | 37 */ |
| 38 SearchDomainHandler(this.server) { | 38 SearchDomainHandler(this.server) { |
| 39 searchEngine = server.searchEngine; | 39 searchEngine = server.searchEngine; |
| 40 } | 40 } |
| 41 | 41 |
| 42 protocol.Response findElementReferences(protocol.Request request) { | 42 protocol.Response findElementReferences(protocol.Request request) { |
| 43 var params = new protocol.SearchFindElementReferencesParams.fromRequest(requ
est); | 43 var params = |
| 44 new protocol.SearchFindElementReferencesParams.fromRequest(request); |
| 44 // prepare elements | 45 // prepare elements |
| 45 List<Element> elements = server.getElementsAtOffset(params.file, | 46 List<Element> elements = |
| 46 params.offset); | 47 server.getElementsAtOffset(params.file, params.offset); |
| 47 elements = elements.map((Element element) { | 48 elements = elements.map((Element element) { |
| 48 if (element is ImportElement) { | 49 if (element is ImportElement) { |
| 49 return element.prefix; | 50 return element.prefix; |
| 50 } | 51 } |
| 51 if (element is FieldFormalParameterElement) { | 52 if (element is FieldFormalParameterElement) { |
| 52 return element.field; | 53 return element.field; |
| 53 } | 54 } |
| 54 if (element is PropertyAccessorElement) { | 55 if (element is PropertyAccessorElement) { |
| 55 return element.variable; | 56 return element.variable; |
| 56 } | 57 } |
| 57 return element; | 58 return element; |
| 58 }).toList(); | 59 }).toList(); |
| 59 // schedule search | 60 // schedule search |
| 60 String searchId = (_nextSearchId++).toString(); | 61 String searchId = (_nextSearchId++).toString(); |
| 61 elements.forEach((Element element) { | 62 elements.forEach((Element element) { |
| 62 var computer = new ElementReferencesComputer(searchEngine); | 63 var computer = new ElementReferencesComputer(searchEngine); |
| 63 var future = computer.compute(element, params.includePotential); | 64 var future = computer.compute(element, params.includePotential); |
| 64 future.then((List<protocol.SearchResult> results) { | 65 future.then((List<protocol.SearchResult> results) { |
| 65 bool isLast = identical(element, elements.last); | 66 bool isLast = identical(element, elements.last); |
| 66 _sendSearchNotification(searchId, isLast, results); | 67 _sendSearchNotification(searchId, isLast, results); |
| 67 }); | 68 }); |
| 68 }); | 69 }); |
| 69 // respond | 70 // respond |
| 70 var result = new protocol.SearchFindElementReferencesResult(); | 71 var result = new protocol.SearchFindElementReferencesResult(); |
| 71 if (elements.isNotEmpty) { | 72 if (elements.isNotEmpty) { |
| 72 result.id = searchId; | 73 result.id = searchId; |
| 73 result.element = new protocol.Element.fromEngine(elements[0]); | 74 result.element = protocol.newElement_fromEngine(elements[0]); |
| 74 } | 75 } |
| 75 return result.toResponse(request.id); | 76 return result.toResponse(request.id); |
| 76 } | 77 } |
| 77 | 78 |
| 78 protocol.Response findMemberDeclarations(protocol.Request request) { | 79 protocol.Response findMemberDeclarations(protocol.Request request) { |
| 79 var params = new protocol.SearchFindMemberDeclarationsParams.fromRequest(req
uest); | 80 var params = |
| 81 new protocol.SearchFindMemberDeclarationsParams.fromRequest(request); |
| 80 // schedule search | 82 // schedule search |
| 81 String searchId = (_nextSearchId++).toString(); | 83 String searchId = (_nextSearchId++).toString(); |
| 82 { | 84 { |
| 83 var matchesFuture = searchEngine.searchMemberDeclarations(params.name); | 85 var matchesFuture = searchEngine.searchMemberDeclarations(params.name); |
| 84 matchesFuture.then((List<SearchMatch> matches) { | 86 matchesFuture.then((List<SearchMatch> matches) { |
| 85 _sendSearchNotification(searchId, true, matches.map(toResult)); | 87 _sendSearchNotification(searchId, true, matches.map(toResult)); |
| 86 }); | 88 }); |
| 87 } | 89 } |
| 88 // respond | 90 // respond |
| 89 return new protocol.SearchFindMemberDeclarationsResult( | 91 return new protocol.SearchFindMemberDeclarationsResult( |
| 90 searchId).toResponse(request.id); | 92 searchId).toResponse(request.id); |
| 91 } | 93 } |
| 92 | 94 |
| 93 protocol.Response findMemberReferences(protocol.Request request) { | 95 protocol.Response findMemberReferences(protocol.Request request) { |
| 94 var params = new protocol.SearchFindMemberReferencesParams.fromRequest(reque
st); | 96 var params = |
| 97 new protocol.SearchFindMemberReferencesParams.fromRequest(request); |
| 95 // schedule search | 98 // schedule search |
| 96 String searchId = (_nextSearchId++).toString(); | 99 String searchId = (_nextSearchId++).toString(); |
| 97 { | 100 { |
| 98 var matchesFuture = searchEngine.searchMemberReferences(params.name); | 101 var matchesFuture = searchEngine.searchMemberReferences(params.name); |
| 99 matchesFuture.then((List<SearchMatch> matches) { | 102 matchesFuture.then((List<SearchMatch> matches) { |
| 100 _sendSearchNotification(searchId, true, matches.map(toResult)); | 103 _sendSearchNotification(searchId, true, matches.map(toResult)); |
| 101 }); | 104 }); |
| 102 } | 105 } |
| 103 // respond | 106 // respond |
| 104 return new protocol.SearchFindMemberReferencesResult(searchId).toResponse( | 107 return new protocol.SearchFindMemberReferencesResult( |
| 105 request.id); | 108 searchId).toResponse(request.id); |
| 106 } | 109 } |
| 107 | 110 |
| 108 protocol.Response findTopLevelDeclarations(protocol.Request request) { | 111 protocol.Response findTopLevelDeclarations(protocol.Request request) { |
| 109 var params = new protocol.SearchFindTopLevelDeclarationsParams.fromRequest(r
equest); | 112 var params = |
| 113 new protocol.SearchFindTopLevelDeclarationsParams.fromRequest(request); |
| 110 // schedule search | 114 // schedule search |
| 111 String searchId = (_nextSearchId++).toString(); | 115 String searchId = (_nextSearchId++).toString(); |
| 112 { | 116 { |
| 113 var matchesFuture = searchEngine.searchTopLevelDeclarations(params.pattern
); | 117 var matchesFuture = |
| 118 searchEngine.searchTopLevelDeclarations(params.pattern); |
| 114 matchesFuture.then((List<SearchMatch> matches) { | 119 matchesFuture.then((List<SearchMatch> matches) { |
| 115 _sendSearchNotification(searchId, true, matches.map(toResult)); | 120 _sendSearchNotification(searchId, true, matches.map(toResult)); |
| 116 }); | 121 }); |
| 117 } | 122 } |
| 118 // respond | 123 // respond |
| 119 return new protocol.SearchFindTopLevelDeclarationsResult( | 124 return new protocol.SearchFindTopLevelDeclarationsResult( |
| 120 searchId).toResponse(request.id); | 125 searchId).toResponse(request.id); |
| 121 } | 126 } |
| 122 | 127 |
| 123 /** | 128 /** |
| 124 * Implement the `search.getTypeHierarchy` request. | 129 * Implement the `search.getTypeHierarchy` request. |
| 125 */ | 130 */ |
| 126 protocol.Response getTypeHierarchy(protocol.Request request) { | 131 protocol.Response getTypeHierarchy(protocol.Request request) { |
| 127 var params = new protocol.SearchGetTypeHierarchyParams.fromRequest(request); | 132 var params = new protocol.SearchGetTypeHierarchyParams.fromRequest(request); |
| 128 // prepare parameters | 133 // prepare parameters |
| 129 // prepare Element | 134 // prepare Element |
| 130 List<Element> elements = server.getElementsAtOffset(params.file, | 135 List<Element> elements = |
| 131 params.offset); | 136 server.getElementsAtOffset(params.file, params.offset); |
| 132 if (elements.isEmpty) { | 137 if (elements.isEmpty) { |
| 133 protocol.Response response = | 138 protocol.Response response = |
| 134 new protocol.SearchGetTypeHierarchyResult().toResponse(request.id); | 139 new protocol.SearchGetTypeHierarchyResult().toResponse(request.id); |
| 135 return response; | 140 return response; |
| 136 } | 141 } |
| 137 Element element = elements.first; | 142 Element element = elements.first; |
| 138 // prepare type hierarchy | 143 // prepare type hierarchy |
| 139 TypeHierarchyComputer computer = new TypeHierarchyComputer(searchEngine); | 144 TypeHierarchyComputer computer = new TypeHierarchyComputer(searchEngine); |
| 140 computer.compute(element).then((List<protocol.TypeHierarchyItem> items) { | 145 computer.compute(element).then((List<protocol.TypeHierarchyItem> items) { |
| 141 protocol.Response response = new protocol.SearchGetTypeHierarchyResult( | 146 protocol.Response response = new protocol.SearchGetTypeHierarchyResult( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 162 return getTypeHierarchy(request); | 167 return getTypeHierarchy(request); |
| 163 } | 168 } |
| 164 } on protocol.RequestFailure catch (exception) { | 169 } on protocol.RequestFailure catch (exception) { |
| 165 return exception.response; | 170 return exception.response; |
| 166 } | 171 } |
| 167 return null; | 172 return null; |
| 168 } | 173 } |
| 169 | 174 |
| 170 void _sendSearchNotification(String searchId, bool isLast, | 175 void _sendSearchNotification(String searchId, bool isLast, |
| 171 Iterable<protocol.SearchResult> results) { | 176 Iterable<protocol.SearchResult> results) { |
| 172 server.sendNotification(new protocol.SearchResultsParams(searchId, | 177 server.sendNotification( |
| 173 results.toList(), isLast).toNotification()); | 178 new protocol.SearchResultsParams( |
| 179 searchId, |
| 180 results.toList(), |
| 181 isLast).toNotification()); |
| 174 } | 182 } |
| 175 | 183 |
| 176 static protocol.SearchResult toResult(SearchMatch match) { | 184 static protocol.SearchResult toResult(SearchMatch match) { |
| 177 return new protocol.SearchResult.fromMatch(match); | 185 return protocol.newSearchResult_fromMatch(match); |
| 178 } | 186 } |
| 179 } | 187 } |
| OLD | NEW |