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 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:analysis_server/src/analysis_server.dart'; | 9 import 'package:analysis_server/src/analysis_server.dart'; |
10 import 'package:analysis_server/src/computer/element.dart' as se; | |
11 import 'package:analysis_server/src/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
12 import 'package:analysis_server/src/protocol.dart'; | 11 import 'package:analysis_server/src/protocol.dart'; |
13 import 'package:analysis_server/src/protocol2.dart' show | 12 import 'package:analysis_server/src/protocol2.dart' as protocol; |
14 SearchFindElementReferencesParams, SearchFindMemberDeclarationsParams, | |
15 SearchFindMemberReferencesParams, SearchFindTopLevelDeclarationsParams, | |
16 SearchGetTypeHierarchyParams; | |
17 import 'package:analysis_server/src/search/element_references.dart'; | 13 import 'package:analysis_server/src/search/element_references.dart'; |
18 import 'package:analysis_server/src/search/search_result.dart'; | |
19 import 'package:analysis_server/src/search/type_hierarchy.dart'; | 14 import 'package:analysis_server/src/search/type_hierarchy.dart'; |
20 import 'package:analysis_server/src/services/json.dart'; | 15 import 'package:analysis_server/src/services/json.dart'; |
21 import 'package:analysis_server/src/services/search/search_engine.dart'; | 16 import 'package:analysis_server/src/services/search/search_engine.dart'; |
22 import 'package:analyzer/src/generated/element.dart'; | 17 import 'package:analyzer/src/generated/element.dart'; |
23 | 18 |
24 /** | 19 /** |
25 * Instances of the class [SearchDomainHandler] implement a [RequestHandler] | 20 * Instances of the class [SearchDomainHandler] implement a [RequestHandler] |
26 * that handles requests in the search domain. | 21 * that handles requests in the search domain. |
27 */ | 22 */ |
28 class SearchDomainHandler implements RequestHandler { | 23 class SearchDomainHandler implements RequestHandler { |
(...skipping 13 matching lines...) Expand all Loading... |
42 int _nextSearchId = 0; | 37 int _nextSearchId = 0; |
43 | 38 |
44 /** | 39 /** |
45 * Initialize a newly created handler to handle requests for the given [server
]. | 40 * Initialize a newly created handler to handle requests for the given [server
]. |
46 */ | 41 */ |
47 SearchDomainHandler(this.server) { | 42 SearchDomainHandler(this.server) { |
48 searchEngine = server.searchEngine; | 43 searchEngine = server.searchEngine; |
49 } | 44 } |
50 | 45 |
51 Response findElementReferences(Request request) { | 46 Response findElementReferences(Request request) { |
52 var params = new SearchFindElementReferencesParams.fromRequest(request); | 47 var params = new protocol.SearchFindElementReferencesParams.fromRequest(requ
est); |
53 // prepare elements | 48 // prepare elements |
54 List<Element> elements = server.getElementsAtOffset(params.file, | 49 List<Element> elements = server.getElementsAtOffset(params.file, |
55 params.offset); | 50 params.offset); |
56 elements = elements.map((Element element) { | 51 elements = elements.map((Element element) { |
57 if (element is FieldFormalParameterElement) { | 52 if (element is FieldFormalParameterElement) { |
58 return element.field; | 53 return element.field; |
59 } | 54 } |
60 if (element is PropertyAccessorElement) { | 55 if (element is PropertyAccessorElement) { |
61 return element.variable; | 56 return element.variable; |
62 } | 57 } |
63 return element; | 58 return element; |
64 }).toList(); | 59 }).toList(); |
65 // schedule search | 60 // schedule search |
66 String searchId = (_nextSearchId++).toString(); | 61 String searchId = (_nextSearchId++).toString(); |
67 elements.forEach((Element element) { | 62 elements.forEach((Element element) { |
68 var computer = new ElementReferencesComputer(searchEngine); | 63 var computer = new ElementReferencesComputer(searchEngine); |
69 var future = computer.compute(element, params.includePotential); | 64 var future = computer.compute(element, params.includePotential); |
70 future.then((List<SearchResult> results) { | 65 future.then((List<protocol.SearchResult> results) { |
71 bool isLast = identical(element, elements.last); | 66 bool isLast = identical(element, elements.last); |
72 _sendSearchNotification(searchId, isLast, results); | 67 _sendSearchNotification(searchId, isLast, results); |
73 }); | 68 }); |
74 }); | 69 }); |
75 if (elements.isEmpty) { | 70 if (elements.isEmpty) { |
76 new Future.microtask(() { | 71 new Future.microtask(() { |
77 _sendSearchNotification(searchId, true, []); | 72 _sendSearchNotification(searchId, true, []); |
78 }); | 73 }); |
79 } | 74 } |
80 // respond | 75 // respond |
81 Response response = new Response(request.id); | 76 protocol.Element element; |
82 response.setResult(ID, searchId); | |
83 if (elements.isNotEmpty) { | 77 if (elements.isNotEmpty) { |
84 var serverElement = new se.Element.fromEngine(elements[0]); | 78 element = new protocol.Element.fromEngine(elements[0]); |
85 response.setResult(ELEMENT, serverElement.toJson()); | |
86 } | 79 } |
87 return response; | 80 return new protocol.SearchFindElementReferencesResult(searchId, |
| 81 element: element).toResponse(request.id); |
88 } | 82 } |
89 | 83 |
90 Response findMemberDeclarations(Request request) { | 84 Response findMemberDeclarations(Request request) { |
91 var params = new SearchFindMemberDeclarationsParams.fromRequest(request); | 85 var params = new protocol.SearchFindMemberDeclarationsParams.fromRequest(req
uest); |
92 // schedule search | 86 // schedule search |
93 String searchId = (_nextSearchId++).toString(); | 87 String searchId = (_nextSearchId++).toString(); |
94 { | 88 { |
95 var matchesFuture = searchEngine.searchMemberDeclarations(params.name); | 89 var matchesFuture = searchEngine.searchMemberDeclarations(params.name); |
96 matchesFuture.then((List<SearchMatch> matches) { | 90 matchesFuture.then((List<SearchMatch> matches) { |
97 _sendSearchNotification(searchId, true, matches.map(toResult)); | 91 _sendSearchNotification(searchId, true, matches.map(toResult)); |
98 }); | 92 }); |
99 } | 93 } |
100 // respond | 94 // respond |
101 return new Response(request.id)..setResult(ID, searchId); | 95 return new protocol.SearchFindMemberDeclarationsResult( |
| 96 searchId).toResponse(request.id); |
102 } | 97 } |
103 | 98 |
104 Response findMemberReferences(Request request) { | 99 Response findMemberReferences(Request request) { |
105 var params = new SearchFindMemberReferencesParams.fromRequest(request); | 100 var params = new protocol.SearchFindMemberReferencesParams.fromRequest(reque
st); |
106 // schedule search | 101 // schedule search |
107 String searchId = (_nextSearchId++).toString(); | 102 String searchId = (_nextSearchId++).toString(); |
108 { | 103 { |
109 var matchesFuture = searchEngine.searchMemberReferences(params.name); | 104 var matchesFuture = searchEngine.searchMemberReferences(params.name); |
110 matchesFuture.then((List<SearchMatch> matches) { | 105 matchesFuture.then((List<SearchMatch> matches) { |
111 _sendSearchNotification(searchId, true, matches.map(toResult)); | 106 _sendSearchNotification(searchId, true, matches.map(toResult)); |
112 }); | 107 }); |
113 } | 108 } |
114 // respond | 109 // respond |
115 return new Response(request.id)..setResult(ID, searchId); | 110 return new protocol.SearchFindMemberReferencesResult(searchId).toResponse( |
| 111 request.id); |
116 } | 112 } |
117 | 113 |
118 Response findTopLevelDeclarations(Request request) { | 114 Response findTopLevelDeclarations(Request request) { |
119 var params = new SearchFindTopLevelDeclarationsParams.fromRequest(request); | 115 var params = new protocol.SearchFindTopLevelDeclarationsParams.fromRequest(r
equest); |
120 // schedule search | 116 // schedule search |
121 String searchId = (_nextSearchId++).toString(); | 117 String searchId = (_nextSearchId++).toString(); |
122 { | 118 { |
123 var matchesFuture = searchEngine.searchTopLevelDeclarations(params.pattern
); | 119 var matchesFuture = searchEngine.searchTopLevelDeclarations(params.pattern
); |
124 matchesFuture.then((List<SearchMatch> matches) { | 120 matchesFuture.then((List<SearchMatch> matches) { |
125 _sendSearchNotification(searchId, true, matches.map(toResult)); | 121 _sendSearchNotification(searchId, true, matches.map(toResult)); |
126 }); | 122 }); |
127 } | 123 } |
128 // respond | 124 // respond |
129 return new Response(request.id)..setResult(ID, searchId); | 125 return new protocol.SearchFindTopLevelDeclarationsResult( |
| 126 searchId).toResponse(request.id); |
130 } | 127 } |
131 | 128 |
132 /** | 129 /** |
133 * Implement the `search.getTypeHierarchy` request. | 130 * Implement the `search.getTypeHierarchy` request. |
134 */ | 131 */ |
135 Response getTypeHierarchy(Request request) { | 132 Response getTypeHierarchy(Request request) { |
136 var params = new SearchGetTypeHierarchyParams.fromRequest(request); | 133 var params = new protocol.SearchGetTypeHierarchyParams.fromRequest(request); |
137 // prepare parameters | 134 // prepare parameters |
138 // prepare Element | 135 // prepare Element |
139 List<Element> elements = server.getElementsAtOffset(params.file, | 136 List<Element> elements = server.getElementsAtOffset(params.file, |
140 params.offset); | 137 params.offset); |
141 Response response = new Response(request.id); | |
142 if (elements.isEmpty) { | 138 if (elements.isEmpty) { |
143 response.setEmptyResult(); | 139 Response response = new protocol.SearchGetTypeHierarchyResult().toResponse
(request.id); |
144 return response; | 140 return response; |
145 } | 141 } |
146 Element element = elements.first; | 142 Element element = elements.first; |
147 // prepare type hierarchy | 143 // prepare type hierarchy |
148 TypeHierarchyComputer computer = new TypeHierarchyComputer(searchEngine); | 144 TypeHierarchyComputer computer = new TypeHierarchyComputer(searchEngine); |
149 computer.compute(element).then((List<TypeHierarchyItem> items) { | 145 computer.compute(element).then((List<protocol.TypeHierarchyItem> items) { |
150 if (items != null) { | 146 Response response = new protocol.SearchGetTypeHierarchyResult( |
151 response.setResult(HIERARCHY_ITEMS, objectToJson(items)); | 147 hierarchyItems: items).toResponse(request.id); |
152 } else { | |
153 response.setEmptyResult(); | |
154 } | |
155 server.sendResponse(response); | 148 server.sendResponse(response); |
156 }); | 149 }); |
157 // delay response | 150 // delay response |
158 return Response.DELAYED_RESPONSE; | 151 return Response.DELAYED_RESPONSE; |
159 } | 152 } |
160 | 153 |
161 @override | 154 @override |
162 Response handleRequest(Request request) { | 155 Response handleRequest(Request request) { |
163 try { | 156 try { |
164 String requestName = request.method; | 157 String requestName = request.method; |
165 if (requestName == SEARCH_FIND_ELEMENT_REFERENCES) { | 158 if (requestName == SEARCH_FIND_ELEMENT_REFERENCES) { |
166 return findElementReferences(request); | 159 return findElementReferences(request); |
167 } else if (requestName == SEARCH_FIND_MEMBER_DECLARATIONS) { | 160 } else if (requestName == SEARCH_FIND_MEMBER_DECLARATIONS) { |
168 return findMemberDeclarations(request); | 161 return findMemberDeclarations(request); |
169 } else if (requestName == SEARCH_FIND_MEMBER_REFERENCES) { | 162 } else if (requestName == SEARCH_FIND_MEMBER_REFERENCES) { |
170 return findMemberReferences(request); | 163 return findMemberReferences(request); |
171 } else if (requestName == SEARCH_FIND_TOP_LEVEL_DECLARATIONS) { | 164 } else if (requestName == SEARCH_FIND_TOP_LEVEL_DECLARATIONS) { |
172 return findTopLevelDeclarations(request); | 165 return findTopLevelDeclarations(request); |
173 } else if (requestName == SEARCH_GET_TYPE_HIERARCHY) { | 166 } else if (requestName == SEARCH_GET_TYPE_HIERARCHY) { |
174 return getTypeHierarchy(request); | 167 return getTypeHierarchy(request); |
175 } | 168 } |
176 } on RequestFailure catch (exception) { | 169 } on RequestFailure catch (exception) { |
177 return exception.response; | 170 return exception.response; |
178 } | 171 } |
179 return null; | 172 return null; |
180 } | 173 } |
181 | 174 |
182 void _sendSearchNotification(String searchId, bool isLast, | 175 void _sendSearchNotification(String searchId, bool isLast, |
183 Iterable<SearchResult> results) { | 176 Iterable<protocol.SearchResult> results) { |
184 Notification notification = new Notification(SEARCH_RESULTS); | 177 Notification notification = new Notification(SEARCH_RESULTS); |
185 notification.setParameter(ID, searchId); | 178 notification.setParameter(ID, searchId); |
186 notification.setParameter(LAST, isLast); | 179 notification.setParameter(LAST, isLast); |
187 notification.setParameter(RESULTS, objectToJson(results)); | 180 notification.setParameter(RESULTS, objectToJson(results)); |
188 server.sendNotification(notification); | 181 server.sendNotification(notification); |
189 } | 182 } |
190 | 183 |
191 static SearchResult toResult(SearchMatch match) { | 184 static protocol.SearchResult toResult(SearchMatch match) { |
192 return new SearchResult.fromMatch(match); | 185 return new protocol.SearchResult.fromMatch(match); |
193 } | 186 } |
194 } | 187 } |
OLD | NEW |