| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/analysis/results.dart'; | 7 import 'package:analyzer/dart/analysis/results.dart'; |
| 8 import 'package:analyzer/src/dart/analysis/driver.dart'; | 8 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 9 import 'package:analyzer_plugin/plugin/plugin.dart'; | 9 import 'package:analyzer_plugin/plugin/plugin.dart'; |
| 10 import 'package:analyzer_plugin/protocol/protocol.dart'; | 10 import 'package:analyzer_plugin/protocol/protocol.dart'; |
| 11 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; | 11 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; |
| 12 import 'package:analyzer_plugin/src/utilities/completion/completion_core.dart'; | 12 import 'package:analyzer_plugin/src/utilities/completion/completion_core.dart'; |
| 13 import 'package:analyzer_plugin/utilities/completion/completion_core.dart'; | 13 import 'package:analyzer_plugin/utilities/completion/completion_core.dart'; |
| 14 import 'package:analyzer_plugin/utilities/generator.dart'; | 14 import 'package:analyzer_plugin/utilities/generator.dart'; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * A mixin that can be used when creating a subclass of [ServerPlugin] to | 17 * A mixin that can be used when creating a subclass of [ServerPlugin] to |
| 18 * provide most of the implementation for handling code completion requests. | 18 * provide most of the implementation for handling code completion requests. |
| 19 * | 19 * |
| 20 * Clients may not extend or implement this class, but are allowed to use it as | 20 * Clients may not extend or implement this class, but are allowed to use it as |
| 21 * a mix-in when creating a subclass of [ServerPlugin]. | 21 * a mix-in when creating a subclass of [ServerPlugin]. |
| 22 */ | 22 */ |
| 23 abstract class CompletionMixin implements ServerPlugin { | 23 abstract class CompletionMixin implements ServerPlugin { |
| 24 /** | 24 /** |
| 25 * Return a list containing the completion contributors that should be used to | 25 * Return a list containing the completion contributors that should be used to |
| 26 * create completion suggestions when used in the context of the given | 26 * create completion suggestions for the file with the given [path]. |
| 27 * analysis [driver]. | |
| 28 */ | 27 */ |
| 29 List<CompletionContributor> getCompletionContributors( | 28 List<CompletionContributor> getCompletionContributors(String path); |
| 30 covariant AnalysisDriverGeneric driver); | |
| 31 | 29 |
| 32 /** | 30 /** |
| 33 * Return the completion request that should be passes to the contributors | 31 * Return the completion request that should be passes to the contributors |
| 34 * returned from [getCompletionContributors]. | 32 * returned from [getCompletionContributors]. |
| 35 */ | 33 */ |
| 36 Future<CompletionRequest> getCompletionRequest( | 34 Future<CompletionRequest> getCompletionRequest( |
| 37 CompletionGetSuggestionsParams parameters, | 35 CompletionGetSuggestionsParams parameters); |
| 38 covariant AnalysisDriverGeneric driver); | |
| 39 | 36 |
| 40 @override | 37 @override |
| 41 Future<CompletionGetSuggestionsResult> handleCompletionGetSuggestions( | 38 Future<CompletionGetSuggestionsResult> handleCompletionGetSuggestions( |
| 42 CompletionGetSuggestionsParams parameters) async { | 39 CompletionGetSuggestionsParams parameters) async { |
| 43 String path = parameters.file; | 40 String path = parameters.file; |
| 44 ContextRoot contextRoot = contextRootContaining(path); | 41 CompletionRequest request = await getCompletionRequest(parameters); |
| 45 if (contextRoot == null) { | |
| 46 // Return an error from the request. | |
| 47 throw new RequestFailure( | |
| 48 RequestErrorFactory.pluginError('Failed to analyze $path', null)); | |
| 49 } | |
| 50 AnalysisDriverGeneric driver = driverMap[contextRoot]; | |
| 51 CompletionRequest request = await getCompletionRequest(parameters, driver); | |
| 52 CompletionGenerator generator = | 42 CompletionGenerator generator = |
| 53 new CompletionGenerator(getCompletionContributors(driver)); | 43 new CompletionGenerator(getCompletionContributors(path)); |
| 54 GeneratorResult result = | 44 GeneratorResult result = |
| 55 await generator.generateCompletionResponse(request); | 45 await generator.generateCompletionResponse(request); |
| 56 result.sendNotifications(channel); | 46 result.sendNotifications(channel); |
| 57 return result.result; | 47 return result.result; |
| 58 } | 48 } |
| 59 } | 49 } |
| 60 | 50 |
| 61 /** | 51 /** |
| 62 * A mixin that can be used when creating a subclass of [ServerPlugin] and | 52 * A mixin that can be used when creating a subclass of [ServerPlugin] and |
| 63 * mixing in [CompletionMixin]. This implements the creation of the completion | 53 * mixing in [CompletionMixin]. This implements the creation of the completion |
| 64 * request based on the assumption that the driver being created is an | 54 * request based on the assumption that the driver being created is an |
| 65 * [AnalysisDriver]. | 55 * [AnalysisDriver]. |
| 66 * | 56 * |
| 67 * Clients may not extend or implement this class, but are allowed to use it as | 57 * Clients may not extend or implement this class, but are allowed to use it as |
| 68 * a mix-in when creating a subclass of [ServerPlugin] that also uses | 58 * a mix-in when creating a subclass of [ServerPlugin] that also uses |
| 69 * [CompletionMixin] as a mix-in. | 59 * [CompletionMixin] as a mix-in. |
| 70 */ | 60 */ |
| 71 abstract class DartCompletionMixin implements CompletionMixin { | 61 abstract class DartCompletionMixin implements CompletionMixin { |
| 72 @override | 62 @override |
| 73 Future<CompletionRequest> getCompletionRequest( | 63 Future<CompletionRequest> getCompletionRequest( |
| 74 CompletionGetSuggestionsParams parameters, | 64 CompletionGetSuggestionsParams parameters) async { |
| 75 covariant AnalysisDriver driver) async { | 65 String path = parameters.file; |
| 66 AnalysisDriver driver = driverForPath(path); |
| 67 if (driver == null) { |
| 68 // Return an error from the request. |
| 69 throw new RequestFailure( |
| 70 RequestErrorFactory.pluginError('Failed to analyze $path', null)); |
| 71 } |
| 76 ResolveResult result = await driver.getResult(parameters.file); | 72 ResolveResult result = await driver.getResult(parameters.file); |
| 77 return new DartCompletionRequestImpl( | 73 return new DartCompletionRequestImpl( |
| 78 resourceProvider, parameters.offset, result); | 74 resourceProvider, parameters.offset, result); |
| 79 } | 75 } |
| 80 } | 76 } |
| OLD | NEW |