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