OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'package:analyzer/dart/analysis/results.dart'; |
| 6 import 'package:analyzer/error/error.dart'; |
| 7 import 'package:analyzer/file_system/file_system.dart'; |
| 8 import 'package:analyzer/src/generated/source.dart'; |
| 9 import 'package:analyzer_plugin/protocol/protocol.dart'; |
| 10 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; |
| 11 import 'package:analyzer_plugin/src/utilities/fixes/fixes.dart'; |
| 12 import 'package:analyzer_plugin/utilities/generator.dart'; |
| 13 |
| 14 /** |
| 15 * An object that [FixContributor]s use to record fixes. |
| 16 * |
| 17 * Clients may not extend, implement or mix-in this class. |
| 18 */ |
| 19 abstract class FixCollector { |
| 20 /** |
| 21 * Record a new [change] (fix) associated with the given [error]. |
| 22 */ |
| 23 void addFix(AnalysisError error, PrioritizedSourceChange change); |
| 24 } |
| 25 |
| 26 /** |
| 27 * An object used to produce fixes. |
| 28 * |
| 29 * Clients may implement this class when implementing plugins. |
| 30 */ |
| 31 abstract class FixContributor { |
| 32 /** |
| 33 * Contribute fixes for the location in the file specified by the given |
| 34 * [request] into the given [collector]. |
| 35 */ |
| 36 void computeFixes(FixesRequest request, FixCollector collector); |
| 37 } |
| 38 |
| 39 /** |
| 40 * The information about a requested set of fixes. |
| 41 * |
| 42 * Clients may not extend, implement or mix-in this class. |
| 43 */ |
| 44 abstract class FixesRequest { |
| 45 /** |
| 46 * The analysis error to be fixed, or `null` if the error has not been |
| 47 * determined. |
| 48 */ |
| 49 AnalysisError get error; |
| 50 |
| 51 /** |
| 52 * Return the offset within the source for which fixes are being requested. |
| 53 */ |
| 54 int get offset; |
| 55 |
| 56 /** |
| 57 * Return the resource provider associated with this request. |
| 58 */ |
| 59 ResourceProvider get resourceProvider; |
| 60 |
| 61 /** |
| 62 * The analysis result for the file in which the fixes are being requested. |
| 63 */ |
| 64 ResolveResult get result; |
| 65 } |
| 66 |
| 67 /** |
| 68 * A generator that will generate an 'edit.getFixes' response. |
| 69 * |
| 70 * Clients may not extend, implement or mix-in this class. |
| 71 */ |
| 72 class FixGenerator { |
| 73 /** |
| 74 * The contributors to be used to generate the fixes. |
| 75 */ |
| 76 final List<FixContributor> contributors; |
| 77 |
| 78 /** |
| 79 * Initialize a newly created fix generator to use the given [contributors]. |
| 80 */ |
| 81 FixGenerator(this.contributors); |
| 82 |
| 83 /** |
| 84 * Create an 'edit.getFixes' response for the location in the file specified |
| 85 * by the given [request]. If any of the contributors throws an exception, |
| 86 * also create a non-fatal 'plugin.error' notification. |
| 87 */ |
| 88 GeneratorResult generateFixesResponse(FixesRequest request) { |
| 89 List<Notification> notifications = <Notification>[]; |
| 90 FixCollectorImpl collector = new FixCollectorImpl(); |
| 91 Iterable<AnalysisError> errors = _getErrors(request); |
| 92 FixesRequestImpl requestImpl = request; |
| 93 for (FixContributor contributor in contributors) { |
| 94 try { |
| 95 for (AnalysisError error in errors) { |
| 96 requestImpl.error = error; |
| 97 contributor.computeFixes(request, collector); |
| 98 } |
| 99 } catch (exception, stackTrace) { |
| 100 notifications.add(new PluginErrorParams( |
| 101 false, exception.toString(), stackTrace.toString()) |
| 102 .toNotification()); |
| 103 } finally { |
| 104 requestImpl.error = null; |
| 105 } |
| 106 } |
| 107 EditGetFixesResult result = new EditGetFixesResult(collector.fixes); |
| 108 return new GeneratorResult(result, notifications); |
| 109 } |
| 110 |
| 111 Iterable<AnalysisError> _getErrors(FixesRequest request) { |
| 112 int offset = request.offset; |
| 113 LineInfo lineInfo = request.result.lineInfo; |
| 114 int offsetLine = lineInfo.getLocation(offset).lineNumber; |
| 115 return request.result.errors.where((AnalysisError error) { |
| 116 int errorLine = lineInfo.getLocation(error.offset).lineNumber; |
| 117 return errorLine == offsetLine; |
| 118 }); |
| 119 } |
| 120 } |
OLD | NEW |