| 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 domain.completion; | 5 library domain.completion; |
| 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'; | 9 import 'package:analysis_server/src/protocol.dart'; |
| 10 import 'package:analysis_services/completion/completion_suggestion.dart'; | 10 import 'package:analysis_services/completion/completion_suggestion.dart'; |
| 11 import 'package:analysis_services/completion/top_level_computer.dart'; | 11 import 'package:analysis_services/completion/completion_computer.dart'; |
| 12 import 'package:analysis_services/constants.dart'; | 12 import 'package:analysis_services/constants.dart'; |
| 13 import 'package:analysis_services/search/search_engine.dart'; | 13 import 'package:analysis_services/search/search_engine.dart'; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] | 16 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] |
| 17 * that handles requests in the search domain. | 17 * that handles requests in the search domain. |
| 18 */ | 18 */ |
| 19 class CompletionDomainHandler implements RequestHandler { | 19 class CompletionDomainHandler implements RequestHandler { |
| 20 /** | 20 /** |
| 21 * The analysis server that is using this handler to process requests. | 21 * The analysis server that is using this handler to process requests. |
| 22 */ | 22 */ |
| 23 final AnalysisServer server; | 23 final AnalysisServer server; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * The [SearchEngine] for this server. | 26 * The [SearchEngine] for this server. |
| 27 */ | 27 */ |
| 28 SearchEngine searchEngine; | 28 SearchEngine searchEngine; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * The next completion response id. | 31 * The next completion response id. |
| 32 */ | 32 */ |
| 33 int _nextCompletionId = 0; | 33 int _nextCompletionId = 0; |
| 34 | 34 |
| 35 /** | 35 /** |
| 36 * Initialize a newly created handler to handle requests for the given [server
]. | 36 * Initialize a new request handler for the given [server]. |
| 37 */ | 37 */ |
| 38 CompletionDomainHandler(this.server); | 38 CompletionDomainHandler(this.server); |
| 39 | 39 |
| 40 @override | 40 @override |
| 41 Response handleRequest(Request request) { | 41 Response handleRequest(Request request) { |
| 42 try { | 42 try { |
| 43 String requestName = request.method; | 43 String requestName = request.method; |
| 44 if (requestName == COMPLETION_GET_SUGGESTIONS) { | 44 if (requestName == COMPLETION_GET_SUGGESTIONS) { |
| 45 return getSuggestions(request); | 45 return processRequest(request); |
| 46 } | 46 } |
| 47 } on RequestFailure catch (exception) { | 47 } on RequestFailure catch (exception) { |
| 48 return exception.response; | 48 return exception.response; |
| 49 } | 49 } |
| 50 return null; | 50 return null; |
| 51 } | 51 } |
| 52 | 52 |
| 53 Response getSuggestions(Request request) { | 53 /** |
| 54 * Process a `completion.getSuggestions` request. |
| 55 */ |
| 56 Response processRequest(Request request) { |
| 54 // extract param | 57 // extract param |
| 55 String file = request.getRequiredParameter(FILE).asString(); | 58 String file = request.getRequiredParameter(FILE).asString(); |
| 56 int offset = request.getRequiredParameter(OFFSET).asInt(); | 59 int offset = request.getRequiredParameter(OFFSET).asInt(); |
| 57 // schedule completion analysis | 60 // schedule completion analysis |
| 58 String completionId = (_nextCompletionId++).toString(); | 61 String completionId = (_nextCompletionId++).toString(); |
| 59 var computer = new TopLevelComputer(server.searchEngine); | 62 CompletionComputer.create(server.searchEngine).then((computers) { |
| 60 var future = computer.compute(); | 63 int count = computers.length; |
| 61 future.then((List<CompletionSuggestion> results) { | 64 List<CompletionSuggestion> results = new List<CompletionSuggestion>(); |
| 62 _sendCompletionNotification(completionId, true, results); | 65 computers.forEach((CompletionComputer c) { |
| 66 c.compute().then((List<CompletionSuggestion> partialResults) { |
| 67 // send aggregate results as we compute them |
| 68 results.addAll(partialResults); |
| 69 sendCompletionNotification(completionId, --count == 0, results); |
| 70 }); |
| 71 }); |
| 63 }); | 72 }); |
| 64 // respond | 73 // initial response without results |
| 65 return new Response(request.id)..setResult(ID, completionId); | 74 return new Response(request.id)..setResult(ID, completionId); |
| 66 } | 75 } |
| 67 | 76 |
| 68 void _sendCompletionNotification(String completionId, bool isLast, | 77 /** |
| 78 * Send completion notification results. |
| 79 */ |
| 80 void sendCompletionNotification(String completionId, bool isLast, |
| 69 Iterable<CompletionSuggestion> results) { | 81 Iterable<CompletionSuggestion> results) { |
| 70 Notification notification = new Notification(COMPLETION_RESULTS); | 82 Notification notification = new Notification(COMPLETION_RESULTS); |
| 71 notification.setParameter(ID, completionId); | 83 notification.setParameter(ID, completionId); |
| 72 notification.setParameter(LAST, isLast); | 84 notification.setParameter(LAST, isLast); |
| 73 notification.setParameter(RESULTS, results); | 85 notification.setParameter(RESULTS, results); |
| 74 server.sendNotification(notification); | 86 server.sendNotification(notification); |
| 75 } | 87 } |
| 76 } | 88 } |
| 77 | |
| OLD | NEW |