| 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'; | 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/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] | 21 * Instances of the class [CompletionDomainHandler] implement a [RequestHandler] |
| 22 * that handles requests in the search domain. | 22 * that handles requests in the search domain. |
| 23 */ | 23 */ |
| 24 class CompletionDomainHandler implements RequestHandler { | 24 class CompletionDomainHandler implements RequestHandler { |
| 25 /** | 25 /** |
| 26 * The analysis server that is using this handler to process requests. | 26 * The analysis server that is using this handler to process requests. |
| 27 */ | 27 */ |
| 28 final AnalysisServer server; | 28 final AnalysisServer server; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * The [SearchEngine] for this server. |
| 32 */ |
| 33 SearchEngine searchEngine; |
| 34 |
| 35 /** |
| 31 * The next completion response id. | 36 * The next completion response id. |
| 32 */ | 37 */ |
| 33 int _nextCompletionId = 0; | 38 int _nextCompletionId = 0; |
| 34 | 39 |
| 35 /** | 40 /** |
| 36 * The completion manager for most recent [Source] and [AnalysisContext], | 41 * The completion manager for most recent [Source] and [AnalysisContext], |
| 37 * or `null` if none. | 42 * or `null` if none. |
| 38 */ | 43 */ |
| 39 CompletionManager _manager; | 44 CompletionManager _manager; |
| 40 | 45 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 64 * Performance for the last priority change event. | 69 * Performance for the last priority change event. |
| 65 */ | 70 */ |
| 66 CompletionPerformance computeCachePerformance; | 71 CompletionPerformance computeCachePerformance; |
| 67 | 72 |
| 68 /** | 73 /** |
| 69 * Initialize a new request handler for the given [server]. | 74 * Initialize a new request handler for the given [server]. |
| 70 */ | 75 */ |
| 71 CompletionDomainHandler(this.server) { | 76 CompletionDomainHandler(this.server) { |
| 72 server.onContextsChanged.listen(contextsChanged); | 77 server.onContextsChanged.listen(contextsChanged); |
| 73 server.onPriorityChange.listen(priorityChanged); | 78 server.onPriorityChange.listen(priorityChanged); |
| 79 searchEngine = server.searchEngine; |
| 74 } | 80 } |
| 75 | 81 |
| 76 /** | 82 /** |
| 77 * Return the completion manager for most recent [Source] and [AnalysisContext
], | 83 * Return the completion manager for most recent [Source] and [AnalysisContext
], |
| 78 * or `null` if none. | 84 * or `null` if none. |
| 79 */ | 85 */ |
| 80 CompletionManager get manager => _manager; | 86 CompletionManager get manager => _manager; |
| 81 | 87 |
| 82 /** | 88 /** |
| 83 * Return the [CompletionManager] for the given [context] and [source], | 89 * Return the [CompletionManager] for the given [context] and [source], |
| 84 * creating a new manager or returning an existing manager as necessary. | 90 * creating a new manager or returning an existing manager as necessary. |
| 85 */ | 91 */ |
| 86 CompletionManager completionManagerFor(AnalysisContext context, | 92 CompletionManager completionManagerFor(AnalysisContext context, |
| 87 Source source) { | 93 Source source) { |
| 88 if (_manager != null) { | 94 if (_manager != null) { |
| 89 if (_manager.context == context && _manager.source == source) { | 95 if (_manager.context == context && _manager.source == source) { |
| 90 return _manager; | 96 return _manager; |
| 91 } | 97 } |
| 92 _discardManager(); | 98 _discardManager(); |
| 93 } | 99 } |
| 94 _manager = createCompletionManager(context, source, server.searchEngine); | 100 _manager = createCompletionManager(context, source, searchEngine); |
| 95 if (context != null) { | 101 if (context != null) { |
| 96 _sourcesChangedSubscription = | 102 _sourcesChangedSubscription = |
| 97 context.onSourcesChanged.listen(sourcesChanged); | 103 context.onSourcesChanged.listen(sourcesChanged); |
| 98 } | 104 } |
| 99 return _manager; | 105 return _manager; |
| 100 } | 106 } |
| 101 | 107 |
| 102 /** | 108 /** |
| 103 * If the context associated with the cache has changed or been removed | 109 * If the context associated with the cache has changed or been removed |
| 104 * then discard the cache. | 110 * then discard the cache. |
| 105 */ | 111 */ |
| 106 void contextsChanged(ContextsChangedEvent event) { | 112 void contextsChanged(ContextsChangedEvent event) { |
| 107 if (_manager != null) { | 113 if (_manager != null) { |
| 108 AnalysisContext context = _manager.context; | 114 AnalysisContext context = _manager.context; |
| 109 if (event.changed.contains(context) || event.removed.contains(context)) { | 115 if (event.changed.contains(context) || event.removed.contains(context)) { |
| 110 _discardManager(); | 116 _discardManager(); |
| 111 } | 117 } |
| 112 } | 118 } |
| 113 } | 119 } |
| 114 | 120 |
| 115 CompletionManager createCompletionManager(AnalysisContext context, | 121 CompletionManager createCompletionManager(AnalysisContext context, |
| 116 Source source, SearchEngine searchEngine) { | 122 Source source, SearchEngine searchEngine) { |
| 117 return new CompletionManager.create(context, source, searchEngine); | 123 return new CompletionManager.create(context, source, searchEngine); |
| 118 } | 124 } |
| 119 | 125 |
| 120 @override | 126 @override |
| 121 Response handleRequest(Request request) { | 127 Response handleRequest(Request request) { |
| 128 if (searchEngine == null) { |
| 129 return new Response.noIndexGenerated(request); |
| 130 } |
| 122 try { | 131 try { |
| 123 String requestName = request.method; | 132 String requestName = request.method; |
| 124 if (requestName == COMPLETION_GET_SUGGESTIONS) { | 133 if (requestName == COMPLETION_GET_SUGGESTIONS) { |
| 125 return processRequest(request); | 134 return processRequest(request); |
| 126 } | 135 } |
| 127 } on RequestFailure catch (exception) { | 136 } on RequestFailure catch (exception) { |
| 128 return exception.response; | 137 return exception.response; |
| 129 } | 138 } |
| 130 return null; | 139 return null; |
| 131 } | 140 } |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 if (_sourcesChangedSubscription != null) { | 273 if (_sourcesChangedSubscription != null) { |
| 265 _sourcesChangedSubscription.cancel(); | 274 _sourcesChangedSubscription.cancel(); |
| 266 _sourcesChangedSubscription = null; | 275 _sourcesChangedSubscription = null; |
| 267 } | 276 } |
| 268 if (_manager != null) { | 277 if (_manager != null) { |
| 269 _manager.dispose(); | 278 _manager.dispose(); |
| 270 _manager = null; | 279 _manager = null; |
| 271 } | 280 } |
| 272 } | 281 } |
| 273 } | 282 } |
| OLD | NEW |