Chromium Code Reviews| 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 searc response id. | |
|
scheglov
2014/07/17 20:40:00
The comment has couple nits: "searc" is an incompl
danrubel
2014/07/17 21:01:14
Done.
| |
| 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 // file |
| 41 RequestDatum fileDatum = request.getRequiredParameter(FILE); | 55 RequestDatum fileDatum = request.getRequiredParameter(FILE); |
|
scheglov
2014/07/17 20:40:00
You could include these Datum objects here and lat
danrubel
2014/07/17 21:01:14
Good point. Done.
| |
| 42 String file = fileDatum.asString(); | 56 String file = fileDatum.asString(); |
| 43 // offset | 57 // offset |
| 44 RequestDatum offsetDatum = request.getRequiredParameter(OFFSET); | 58 RequestDatum offsetDatum = request.getRequiredParameter(OFFSET); |
| 45 int offset = offsetDatum.asInt(); | 59 int offset = offsetDatum.asInt(); |
| 46 // TODO(brianwilkerson) implement | 60 // schedule completion analysis |
| 47 return null; | 61 String completionId = (_nextCompletionId++).toString(); |
| 62 var computer = new TopLevelSuggestionsComputer(server.searchEngine); | |
| 63 var future = computer.compute(); | |
| 64 future.then((List<CompletionSuggestion> results) { | |
| 65 _sendSearchNotification(completionId, true, results); | |
| 66 }); | |
| 67 // respond | |
| 68 return new Response(request.id)..setResult(ID, completionId); | |
| 69 } | |
| 70 | |
| 71 void _sendSearchNotification(String completionId, bool isLast, | |
|
scheglov
2014/07/17 20:39:59
_secondCompletionNotification?
danrubel
2014/07/17 21:01:14
Done.
| |
| 72 Iterable<CompletionSuggestion> results) { | |
| 73 Notification notification = new Notification(COMPLETION_RESULTS); | |
| 74 notification.setParameter(ID, completionId); | |
| 75 notification.setParameter(LAST, isLast); | |
| 76 notification.setParameter(RESULTS, results); | |
| 77 server.sendNotification(notification); | |
| 48 } | 78 } |
| 49 } | 79 } |
| 80 | |
| 81 /** | |
| 82 * A computer for `completion.getSuggestions` request results. | |
| 83 */ | |
| 84 class TopLevelSuggestionsComputer { | |
|
scheglov
2014/07/17 20:40:00
Make it private?
danrubel
2014/07/17 21:01:14
I didn't bother because I envision more of these a
| |
| 85 final SearchEngine searchEngine; | |
| 86 | |
| 87 TopLevelSuggestionsComputer(this.searchEngine); | |
| 88 | |
| 89 /** | |
| 90 * Computes [CompletionSuggestion]s for the specified position in the source. | |
| 91 */ | |
| 92 Future<List<CompletionSuggestion>> compute() { | |
| 93 var future = searchEngine.searchTopLevelDeclarations(''); | |
|
scheglov
2014/07/17 20:40:00
Should we use an existing prefix here?
danrubel
2014/07/17 21:01:14
I want to return the complete list and let the cli
scheglov
2014/07/17 21:09:32
OK, good point.
| |
| 94 return future.then((List<SearchMatch> matches) { | |
| 95 return matches.map((SearchMatch match) { | |
| 96 return new CompletionSuggestion(match.element.displayName); | |
| 97 }).toList(); | |
| 98 }); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 /** | |
| 103 * A single completion suggestion. | |
| 104 */ | |
| 105 class CompletionSuggestion implements HasToJson { | |
| 106 final String completion; | |
| 107 | |
| 108 CompletionSuggestion(this.completion); | |
| 109 | |
| 110 factory CompletionSuggestion.fromJson(Map<String, Object> json) { | |
| 111 return new CompletionSuggestion(json[COMPLETION]); | |
| 112 } | |
| 113 | |
| 114 @override | |
| 115 Map<String, Object> toJson() { | |
| 116 return { | |
| 117 COMPLETION: completion | |
| 118 }; | |
| 119 } | |
| 120 } | |
| OLD | NEW |