| 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_computer.dart'; |
| 10 import 'package:analysis_services/completion/completion_suggestion.dart'; | 11 import 'package:analysis_services/completion/completion_suggestion.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:analyzer/src/generated/java_io.dart'; |
| 14 import 'package:analyzer/src/generated/source_io.dart'; |
| 14 | 15 |
| 15 /** | 16 /** |
| 16 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] | 17 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] |
| 17 * that handles requests in the search domain. | 18 * that handles requests in the search domain. |
| 18 */ | 19 */ |
| 19 class CompletionDomainHandler implements RequestHandler { | 20 class CompletionDomainHandler implements RequestHandler { |
| 20 /** | 21 /** |
| 21 * The analysis server that is using this handler to process requests. | 22 * The analysis server that is using this handler to process requests. |
| 22 */ | 23 */ |
| 23 final AnalysisServer server; | 24 final AnalysisServer server; |
| 24 | 25 |
| 25 /** | 26 /** |
| 26 * The [SearchEngine] for this server. | |
| 27 */ | |
| 28 SearchEngine searchEngine; | |
| 29 | |
| 30 /** | |
| 31 * The next completion response id. | 27 * The next completion response id. |
| 32 */ | 28 */ |
| 33 int _nextCompletionId = 0; | 29 int _nextCompletionId = 0; |
| 34 | 30 |
| 35 /** | 31 /** |
| 36 * Initialize a new request handler for the given [server]. | 32 * Initialize a new request handler for the given [server]. |
| 37 */ | 33 */ |
| 38 CompletionDomainHandler(this.server); | 34 CompletionDomainHandler(this.server); |
| 39 | 35 |
| 40 @override | 36 @override |
| (...skipping 11 matching lines...) Expand all Loading... |
| 52 | 48 |
| 53 /** | 49 /** |
| 54 * Process a `completion.getSuggestions` request. | 50 * Process a `completion.getSuggestions` request. |
| 55 */ | 51 */ |
| 56 Response processRequest(Request request) { | 52 Response processRequest(Request request) { |
| 57 // extract param | 53 // extract param |
| 58 String file = request.getRequiredParameter(FILE).asString(); | 54 String file = request.getRequiredParameter(FILE).asString(); |
| 59 int offset = request.getRequiredParameter(OFFSET).asInt(); | 55 int offset = request.getRequiredParameter(OFFSET).asInt(); |
| 60 // schedule completion analysis | 56 // schedule completion analysis |
| 61 String completionId = (_nextCompletionId++).toString(); | 57 String completionId = (_nextCompletionId++).toString(); |
| 62 CompletionComputer.create(server.searchEngine).then((computers) { | 58 Source source = server.getSource(file); |
| 59 CompletionManager manager = |
| 60 CompletionManager.create(source, offset, server.searchEngine); |
| 61 manager.generate().then((List<CompletionComputer> computers) { |
| 63 int count = computers.length; | 62 int count = computers.length; |
| 64 List<CompletionSuggestion> results = new List<CompletionSuggestion>(); | 63 List<CompletionSuggestion> results = new List<CompletionSuggestion>(); |
| 65 computers.forEach((CompletionComputer c) { | 64 computers.forEach((CompletionComputer c) { |
| 66 c.compute().then((List<CompletionSuggestion> partialResults) { | 65 c.compute().then((List<CompletionSuggestion> partialResults) { |
| 67 // send aggregate results as we compute them | 66 // send aggregate results as we compute them |
| 68 results.addAll(partialResults); | 67 results.addAll(partialResults); |
| 69 sendCompletionNotification(completionId, --count == 0, results); | 68 sendCompletionNotification( |
| 69 manager.replacementOffset, |
| 70 manager.replacementLength, |
| 71 completionId, |
| 72 --count == 0, |
| 73 results); |
| 70 }); | 74 }); |
| 71 }); | 75 }); |
| 72 }); | 76 }); |
| 73 // initial response without results | 77 // initial response without results |
| 74 return new Response(request.id)..setResult(ID, completionId); | 78 return new Response(request.id)..setResult(ID, completionId); |
| 75 } | 79 } |
| 76 | 80 |
| 77 /** | 81 /** |
| 78 * Send completion notification results. | 82 * Send completion notification results. |
| 79 */ | 83 */ |
| 80 void sendCompletionNotification(String completionId, bool isLast, | 84 void sendCompletionNotification(int replacementOffset, int replacementLength, |
| 81 Iterable<CompletionSuggestion> results) { | 85 String completionId, bool isLast, Iterable<CompletionSuggestion> results)
{ |
| 82 Notification notification = new Notification(COMPLETION_RESULTS); | 86 Notification notification = new Notification(COMPLETION_RESULTS); |
| 83 notification.setParameter(ID, completionId); | 87 notification.setParameter(ID, completionId); |
| 88 notification.setParameter(REPLACEMENT_OFFSET, replacementOffset); |
| 89 notification.setParameter(REPLACEMENT_LENGTH, replacementLength); |
| 90 notification.setParameter(RESULTS, results); |
| 84 notification.setParameter(LAST, isLast); | 91 notification.setParameter(LAST, isLast); |
| 85 notification.setParameter(RESULTS, results); | |
| 86 server.sendNotification(notification); | 92 server.sendNotification(notification); |
| 87 } | 93 } |
| 88 } | 94 } |
| OLD | NEW |