| 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 'dart:async'; |
| 8 |
| 7 import 'package:analysis_server/src/analysis_server.dart'; | 9 import 'package:analysis_server/src/analysis_server.dart'; |
| 8 import 'package:analysis_server/src/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
| 9 import 'package:analysis_server/src/protocol.dart'; | 11 import 'package:analysis_server/src/protocol.dart'; |
| 12 import 'package:analysis_services/search/search_engine.dart'; |
| 13 import 'package:analysis_server/src/collections.dart'; |
| 10 | 14 |
| 11 /** | 15 /** |
| 12 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] | 16 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] |
| 13 * that handles requests in the search domain. | 17 * that handles requests in the search domain. |
| 14 */ | 18 */ |
| 15 class CompletionDomainHandler implements RequestHandler { | 19 class CompletionDomainHandler implements RequestHandler { |
| 16 /** | 20 /** |
| 17 * The analysis server that is using this handler to process requests. | 21 * The analysis server that is using this handler to process requests. |
| 18 */ | 22 */ |
| 19 final AnalysisServer server; | 23 final AnalysisServer server; |
| 20 | 24 |
| 21 /** | 25 /** |
| 26 * The [SearchEngine] for this server. |
| 27 */ |
| 28 SearchEngine searchEngine; |
| 29 |
| 30 /** |
| 31 * The next completion response id. |
| 32 */ |
| 33 int _nextCompletionId = 0; |
| 34 |
| 35 /** |
| 22 * 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
]. |
| 23 */ | 37 */ |
| 24 CompletionDomainHandler(this.server); | 38 CompletionDomainHandler(this.server); |
| 25 | 39 |
| 26 @override | 40 @override |
| 27 Response handleRequest(Request request) { | 41 Response handleRequest(Request request) { |
| 28 try { | 42 try { |
| 29 String requestName = request.method; | 43 String requestName = request.method; |
| 30 if (requestName == COMPLETION_GET_SUGGESTIONS) { | 44 if (requestName == COMPLETION_GET_SUGGESTIONS) { |
| 31 return getSuggestions(request); | 45 return getSuggestions(request); |
| 32 } | 46 } |
| 33 } on RequestFailure catch (exception) { | 47 } on RequestFailure catch (exception) { |
| 34 return exception.response; | 48 return exception.response; |
| 35 } | 49 } |
| 36 return null; | 50 return null; |
| 37 } | 51 } |
| 38 | 52 |
| 39 Response getSuggestions(Request request) { | 53 Response getSuggestions(Request request) { |
| 40 // file | 54 // extract param |
| 41 RequestDatum fileDatum = request.getRequiredParameter(FILE); | 55 String file = request.getRequiredParameter(FILE).asString(); |
| 42 String file = fileDatum.asString(); | 56 int offset = request.getRequiredParameter(OFFSET).asInt(); |
| 43 // offset | 57 // schedule completion analysis |
| 44 RequestDatum offsetDatum = request.getRequiredParameter(OFFSET); | 58 String completionId = (_nextCompletionId++).toString(); |
| 45 int offset = offsetDatum.asInt(); | 59 var computer = new TopLevelSuggestionsComputer(server.searchEngine); |
| 46 // TODO(brianwilkerson) implement | 60 var future = computer.compute(); |
| 47 return null; | 61 future.then((List<CompletionSuggestion> results) { |
| 62 _sendCompletionNotification(completionId, true, results); |
| 63 }); |
| 64 // respond |
| 65 return new Response(request.id)..setResult(ID, completionId); |
| 66 } |
| 67 |
| 68 void _sendCompletionNotification(String completionId, bool isLast, |
| 69 Iterable<CompletionSuggestion> results) { |
| 70 Notification notification = new Notification(COMPLETION_RESULTS); |
| 71 notification.setParameter(ID, completionId); |
| 72 notification.setParameter(LAST, isLast); |
| 73 notification.setParameter(RESULTS, results); |
| 74 server.sendNotification(notification); |
| 48 } | 75 } |
| 49 } | 76 } |
| 77 |
| 78 /** |
| 79 * A computer for `completion.getSuggestions` request results. |
| 80 */ |
| 81 class TopLevelSuggestionsComputer { |
| 82 final SearchEngine searchEngine; |
| 83 |
| 84 TopLevelSuggestionsComputer(this.searchEngine); |
| 85 |
| 86 /** |
| 87 * Computes [CompletionSuggestion]s for the specified position in the source. |
| 88 */ |
| 89 Future<List<CompletionSuggestion>> compute() { |
| 90 var future = searchEngine.searchTopLevelDeclarations(''); |
| 91 return future.then((List<SearchMatch> matches) { |
| 92 return matches.map((SearchMatch match) { |
| 93 return new CompletionSuggestion(match.element.displayName); |
| 94 }).toList(); |
| 95 }); |
| 96 } |
| 97 } |
| 98 |
| 99 /** |
| 100 * A single completion suggestion. |
| 101 */ |
| 102 class CompletionSuggestion implements HasToJson { |
| 103 final String completion; |
| 104 |
| 105 CompletionSuggestion(this.completion); |
| 106 |
| 107 factory CompletionSuggestion.fromJson(Map<String, Object> json) { |
| 108 return new CompletionSuggestion(json[COMPLETION]); |
| 109 } |
| 110 |
| 111 @override |
| 112 Map<String, Object> toJson() { |
| 113 return { |
| 114 COMPLETION: completion |
| 115 }; |
| 116 } |
| 117 } |
| OLD | NEW |