| 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/assist/assist.dart'; | 12 import 'package:analyzer_plugin/src/utilities/assist/assist.dart'; |
| 13 import 'package:analyzer_plugin/utilities/assist/assist.dart'; | 13 import 'package:analyzer_plugin/utilities/assist/assist.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 assist requests. | 18 * provide most of the implementation for handling assist 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 AssistsMixin implements ServerPlugin { | 23 abstract class AssistsMixin implements ServerPlugin { |
| 24 /** | 24 /** |
| 25 * Return a list containing the assist contributors that should be used to | 25 * Return a list containing the assist contributors that should be used to |
| 26 * create assists when used in the context of the given analysis [driver]. | 26 * create assists for the file with the given [path]. |
| 27 */ | 27 */ |
| 28 List<AssistContributor> getAssistContributors( | 28 List<AssistContributor> getAssistContributors(String path); |
| 29 covariant AnalysisDriverGeneric driver); | |
| 30 | 29 |
| 31 /** | 30 /** |
| 32 * Return the assist request that should be passes to the contributors | 31 * Return the assist request that should be passes to the contributors |
| 33 * returned from [getAssistContributors]. | 32 * returned from [getAssistContributors]. |
| 34 */ | 33 */ |
| 35 Future<AssistRequest> getAssistRequest( | 34 Future<AssistRequest> getAssistRequest(EditGetAssistsParams parameters); |
| 36 EditGetAssistsParams parameters, covariant AnalysisDriverGeneric driver); | |
| 37 | 35 |
| 38 @override | 36 @override |
| 39 Future<EditGetAssistsResult> handleEditGetAssists( | 37 Future<EditGetAssistsResult> handleEditGetAssists( |
| 40 EditGetAssistsParams parameters) async { | 38 EditGetAssistsParams parameters) async { |
| 41 String path = parameters.file; | 39 String path = parameters.file; |
| 42 ContextRoot contextRoot = contextRootContaining(path); | 40 AssistRequest request = await getAssistRequest(parameters); |
| 43 if (contextRoot == null) { | |
| 44 // Return an error from the request. | |
| 45 throw new RequestFailure( | |
| 46 RequestErrorFactory.pluginError('Failed to analyze $path', null)); | |
| 47 } | |
| 48 AnalysisDriverGeneric driver = driverMap[contextRoot]; | |
| 49 AssistRequest request = await getAssistRequest(parameters, driver); | |
| 50 AssistGenerator generator = | 41 AssistGenerator generator = |
| 51 new AssistGenerator(getAssistContributors(driver)); | 42 new AssistGenerator(getAssistContributors(path)); |
| 52 GeneratorResult result = await generator.generateAssistsResponse(request); | 43 GeneratorResult result = await generator.generateAssistsResponse(request); |
| 53 result.sendNotifications(channel); | 44 result.sendNotifications(channel); |
| 54 return result.result; | 45 return result.result; |
| 55 } | 46 } |
| 56 } | 47 } |
| 57 | 48 |
| 58 /** | 49 /** |
| 59 * A mixin that can be used when creating a subclass of [ServerPlugin] and | 50 * A mixin that can be used when creating a subclass of [ServerPlugin] and |
| 60 * mixing in [AssistsMixin]. This implements the creation of the assists request | 51 * mixing in [AssistsMixin]. This implements the creation of the assists request |
| 61 * based on the assumption that the driver being created is an [AnalysisDriver]. | 52 * based on the assumption that the driver being created is an [AnalysisDriver]. |
| 62 * | 53 * |
| 63 * Clients may not extend or implement this class, but are allowed to use it as | 54 * Clients may not extend or implement this class, but are allowed to use it as |
| 64 * a mix-in when creating a subclass of [ServerPlugin] that also uses | 55 * a mix-in when creating a subclass of [ServerPlugin] that also uses |
| 65 * [AssistsMixin] as a mix-in. | 56 * [AssistsMixin] as a mix-in. |
| 66 */ | 57 */ |
| 67 abstract class DartAssistsMixin implements AssistsMixin { | 58 abstract class DartAssistsMixin implements AssistsMixin { |
| 68 @override | 59 @override |
| 69 Future<AssistRequest> getAssistRequest( | 60 Future<AssistRequest> getAssistRequest( |
| 70 EditGetAssistsParams parameters, covariant AnalysisDriver driver) async { | 61 EditGetAssistsParams parameters) async { |
| 71 ResolveResult result = await driver.getResult(parameters.file); | 62 String path = parameters.file; |
| 63 AnalysisDriver driver = driverForPath(path); |
| 64 if (driver == null) { |
| 65 // Return an error from the request. |
| 66 throw new RequestFailure( |
| 67 RequestErrorFactory.pluginError('Failed to analyze $path', null)); |
| 68 } |
| 69 ResolveResult result = await driver.getResult(path); |
| 72 return new DartAssistRequestImpl( | 70 return new DartAssistRequestImpl( |
| 73 resourceProvider, parameters.offset, parameters.length, result); | 71 resourceProvider, parameters.offset, parameters.length, result); |
| 74 } | 72 } |
| 75 } | 73 } |
| OLD | NEW |