| 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/error/error.dart'; | 8 import 'package:analyzer/error/error.dart'; |
| 9 import 'package:analyzer/src/dart/analysis/driver.dart'; | 9 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| 11 import 'package:analyzer_plugin/plugin/plugin.dart'; | 11 import 'package:analyzer_plugin/plugin/plugin.dart'; |
| 12 import 'package:analyzer_plugin/protocol/protocol.dart'; | 12 import 'package:analyzer_plugin/protocol/protocol.dart'; |
| 13 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; | 13 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; |
| 14 import 'package:analyzer_plugin/src/utilities/fixes/fixes.dart'; | 14 import 'package:analyzer_plugin/src/utilities/fixes/fixes.dart'; |
| 15 import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; | 15 import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; |
| 16 import 'package:analyzer_plugin/utilities/generator.dart'; | 16 import 'package:analyzer_plugin/utilities/generator.dart'; |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * A mixin that can be used when creating a subclass of [ServerPlugin] and | 19 * A mixin that can be used when creating a subclass of [ServerPlugin] and |
| 20 * mixing in [FixesMixin]. This implements the creation of the fixes request | 20 * mixing in [FixesMixin]. This implements the creation of the fixes request |
| 21 * based on the assumption that the driver being created is an [AnalysisDriver]. | 21 * based on the assumption that the driver being created is an [AnalysisDriver]. |
| 22 * | 22 * |
| 23 * Clients may not extend or implement this class, but are allowed to use it as | 23 * Clients may not extend or implement this class, but are allowed to use it as |
| 24 * a mix-in when creating a subclass of [ServerPlugin] that also uses | 24 * a mix-in when creating a subclass of [ServerPlugin] that also uses |
| 25 * [FixesMixin] as a mix-in. | 25 * [FixesMixin] as a mix-in. |
| 26 */ | 26 */ |
| 27 abstract class DartFixesMixin implements FixesMixin { | 27 abstract class DartFixesMixin implements FixesMixin { |
| 28 @override | 28 @override |
| 29 Future<FixesRequest> getFixesRequest( | 29 Future<FixesRequest> getFixesRequest(EditGetFixesParams parameters) async { |
| 30 EditGetFixesParams parameters, covariant AnalysisDriver driver) async { | 30 String path = parameters.file; |
| 31 int offset = parameters.offset; | 31 int offset = parameters.offset; |
| 32 ResolveResult result = await driver.getResult(parameters.file); | 32 AnalysisDriver driver = driverForPath(path); |
| 33 if (driver == null) { |
| 34 // Return an error from the request. |
| 35 throw new RequestFailure( |
| 36 RequestErrorFactory.pluginError('Failed to analyze $path', null)); |
| 37 } |
| 38 ResolveResult result = await driver.getResult(path); |
| 33 return new DartFixesRequestImpl( | 39 return new DartFixesRequestImpl( |
| 34 resourceProvider, offset, _getErrors(offset, result), result); | 40 resourceProvider, offset, _getErrors(offset, result), result); |
| 35 } | 41 } |
| 36 | 42 |
| 37 List<AnalysisError> _getErrors(int offset, ResolveResult result) { | 43 List<AnalysisError> _getErrors(int offset, ResolveResult result) { |
| 38 LineInfo lineInfo = result.lineInfo; | 44 LineInfo lineInfo = result.lineInfo; |
| 39 int offsetLine = lineInfo.getLocation(offset).lineNumber; | 45 int offsetLine = lineInfo.getLocation(offset).lineNumber; |
| 40 return result.errors.where((AnalysisError error) { | 46 return result.errors.where((AnalysisError error) { |
| 41 int errorLine = lineInfo.getLocation(error.offset).lineNumber; | 47 int errorLine = lineInfo.getLocation(error.offset).lineNumber; |
| 42 return errorLine == offsetLine; | 48 return errorLine == offsetLine; |
| 43 }).toList(); | 49 }).toList(); |
| 44 } | 50 } |
| 45 } | 51 } |
| 46 | 52 |
| 47 /** | 53 /** |
| 48 * A mixin that can be used when creating a subclass of [ServerPlugin] to | 54 * A mixin that can be used when creating a subclass of [ServerPlugin] to |
| 49 * provide most of the implementation for handling fix requests. | 55 * provide most of the implementation for handling fix requests. |
| 50 * | 56 * |
| 51 * 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 |
| 52 * a mix-in when creating a subclass of [ServerPlugin]. | 58 * a mix-in when creating a subclass of [ServerPlugin]. |
| 53 */ | 59 */ |
| 54 abstract class FixesMixin implements ServerPlugin { | 60 abstract class FixesMixin implements ServerPlugin { |
| 55 /** | 61 /** |
| 56 * Return a list containing the fix contributors that should be used to create | 62 * Return a list containing the fix contributors that should be used to create |
| 57 * fixes when used in the context of the given analysis [driver]. | 63 * fixes for the file with the given [path]. |
| 58 */ | 64 */ |
| 59 List<FixContributor> getFixContributors( | 65 List<FixContributor> getFixContributors(String path); |
| 60 covariant AnalysisDriverGeneric driver); | |
| 61 | 66 |
| 62 /** | 67 /** |
| 63 * Return the fixes request that should be passes to the contributors | 68 * Return the fixes request that should be passes to the contributors |
| 64 * returned from [getFixContributors]. | 69 * returned from [getFixContributors]. |
| 65 */ | 70 */ |
| 66 Future<FixesRequest> getFixesRequest( | 71 Future<FixesRequest> getFixesRequest(EditGetFixesParams parameters); |
| 67 EditGetFixesParams parameters, covariant AnalysisDriverGeneric driver); | |
| 68 | 72 |
| 69 @override | 73 @override |
| 70 Future<EditGetFixesResult> handleEditGetFixes( | 74 Future<EditGetFixesResult> handleEditGetFixes( |
| 71 EditGetFixesParams parameters) async { | 75 EditGetFixesParams parameters) async { |
| 72 String path = parameters.file; | 76 String path = parameters.file; |
| 73 ContextRoot contextRoot = contextRootContaining(path); | 77 FixesRequest request = await getFixesRequest(parameters); |
| 74 if (contextRoot == null) { | 78 FixGenerator generator = new FixGenerator(getFixContributors(path)); |
| 75 // Return an error from the request. | |
| 76 throw new RequestFailure( | |
| 77 RequestErrorFactory.pluginError('Failed to analyze $path', null)); | |
| 78 } | |
| 79 AnalysisDriverGeneric driver = driverMap[contextRoot]; | |
| 80 FixesRequest request = await getFixesRequest(parameters, driver); | |
| 81 FixGenerator generator = new FixGenerator(getFixContributors(driver)); | |
| 82 GeneratorResult result = await generator.generateFixesResponse(request); | 79 GeneratorResult result = await generator.generateFixesResponse(request); |
| 83 result.sendNotifications(channel); | 80 result.sendNotifications(channel); |
| 84 return result.result; | 81 return result.result; |
| 85 } | 82 } |
| 86 } | 83 } |
| OLD | NEW |