Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1166)

Side by Side Diff: pkg/analyzer_plugin/lib/utilities/fixes/fixes.dart

Issue 2953093002: Update the plugin API (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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'; 8 import 'package:analyzer/src/generated/source.dart';
9 import 'package:analyzer_plugin/protocol/protocol.dart'; 9 import 'package:analyzer_plugin/protocol/protocol.dart';
10 import 'package:analyzer_plugin/protocol/protocol_generated.dart'; 10 import 'package:analyzer_plugin/protocol/protocol_generated.dart';
11 import 'package:analyzer_plugin/src/utilities/fixes/fixes.dart'; 11 import 'package:analyzer_plugin/src/utilities/fixes/fixes.dart';
12 import 'package:analyzer_plugin/utilities/generator.dart'; 12 import 'package:analyzer_plugin/utilities/generator.dart';
13 13
14 /** 14 /**
15 * The information about a requested set of fixes when computing fixes in a
16 * `.dart` file.
17 *
18 * Clients may not extend, implement or mix-in this class.
19 */
20 abstract class DartFixesRequest implements FixesRequest {
21 /**
22 * The analysis result for the file in which the fixes are being requested.
23 */
24 ResolveResult get result;
25 }
26
27 /**
15 * An object that [FixContributor]s use to record fixes. 28 * An object that [FixContributor]s use to record fixes.
16 * 29 *
17 * Clients may not extend, implement or mix-in this class. 30 * Clients may not extend, implement or mix-in this class.
18 */ 31 */
19 abstract class FixCollector { 32 abstract class FixCollector {
20 /** 33 /**
21 * Record a new [change] (fix) associated with the given [error]. 34 * Record a new [change] (fix) associated with the given [error].
22 */ 35 */
23 void addFix(AnalysisError error, PrioritizedSourceChange change); 36 void addFix(AnalysisError error, PrioritizedSourceChange change);
24 } 37 }
25 38
26 /** 39 /**
27 * An object used to produce fixes. 40 * An object used to produce fixes.
28 * 41 *
29 * Clients may implement this class when implementing plugins. 42 * Clients may implement this class when implementing plugins.
30 */ 43 */
31 abstract class FixContributor { 44 abstract class FixContributor {
32 /** 45 /**
33 * Contribute fixes for the location in the file specified by the given 46 * Contribute fixes for the location in the file specified by the given
34 * [request] into the given [collector]. 47 * [request] into the given [collector].
35 */ 48 */
36 void computeFixes(FixesRequest request, FixCollector collector); 49 void computeFixes(covariant FixesRequest request, FixCollector collector);
37 } 50 }
38 51
39 /** 52 /**
40 * The information about a requested set of fixes. 53 * The information about a requested set of fixes.
41 * 54 *
42 * Clients may not extend, implement or mix-in this class. 55 * Clients may not extend, implement or mix-in this class.
43 */ 56 */
44 abstract class FixesRequest { 57 abstract class FixesRequest {
maxkim 2017/06/22 20:52:57 Can we also add 'String path' to this object?
45 /** 58 /**
46 * The analysis error to be fixed, or `null` if the error has not been 59 * The analysis error to be fixed, or `null` if the error has not been
47 * determined. 60 * determined.
48 */ 61 */
49 AnalysisError get error; 62 AnalysisError get error;
50 63
51 /** 64 /**
52 * Return the offset within the source for which fixes are being requested. 65 * Return the offset within the source for which fixes are being requested.
53 */ 66 */
54 int get offset; 67 int get offset;
55 68
56 /** 69 /**
57 * Return the resource provider associated with this request. 70 * Return the resource provider associated with this request.
58 */ 71 */
59 ResourceProvider get resourceProvider; 72 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 } 73 }
66 74
67 /** 75 /**
68 * A generator that will generate an 'edit.getFixes' response. 76 * A generator that will generate an 'edit.getFixes' response.
69 * 77 *
70 * Clients may not extend, implement or mix-in this class. 78 * Clients may not extend, implement or mix-in this class.
71 */ 79 */
72 class FixGenerator { 80 class FixGenerator {
73 /** 81 /**
74 * The contributors to be used to generate the fixes. 82 * The contributors to be used to generate the fixes.
75 */ 83 */
76 final List<FixContributor> contributors; 84 final List<FixContributor> contributors;
77 85
78 /** 86 /**
79 * Initialize a newly created fix generator to use the given [contributors]. 87 * Initialize a newly created fix generator to use the given [contributors].
80 */ 88 */
81 FixGenerator(this.contributors); 89 FixGenerator(this.contributors);
82 90
83 /** 91 /**
84 * Create an 'edit.getFixes' response for the location in the file specified 92 * 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, 93 * by the given [request]. If any of the contributors throws an exception,
86 * also create a non-fatal 'plugin.error' notification. 94 * also create a non-fatal 'plugin.error' notification.
87 */ 95 */
88 GeneratorResult generateFixesResponse(FixesRequest request) { 96 GeneratorResult generateFixesResponse(DartFixesRequest request) {
Brian Wilkerson 2017/06/22 17:12:21 I'm not entirely thrilled with this change. The al
maxkim 2017/06/22 20:52:56 Adding a 'List<AnalysisError> get errorsAtOffset'
Brian Wilkerson 2017/06/22 22:07:01 Yeah, I'm starting to think that you're right, and
maxkim 2017/06/22 23:18:19 Acknowledged.
89 List<Notification> notifications = <Notification>[]; 97 List<Notification> notifications = <Notification>[];
90 FixCollectorImpl collector = new FixCollectorImpl(); 98 FixCollectorImpl collector = new FixCollectorImpl();
91 Iterable<AnalysisError> errors = _getErrors(request); 99 Iterable<AnalysisError> errors = _getErrors(request);
92 FixesRequestImpl requestImpl = request; 100 FixesRequestImpl requestImpl = request;
93 for (FixContributor contributor in contributors) { 101 for (FixContributor contributor in contributors) {
94 try { 102 try {
95 for (AnalysisError error in errors) { 103 for (AnalysisError error in errors) {
96 requestImpl.error = error; 104 requestImpl.error = error;
97 contributor.computeFixes(request, collector); 105 contributor.computeFixes(request, collector);
98 } 106 }
99 } catch (exception, stackTrace) { 107 } catch (exception, stackTrace) {
100 notifications.add(new PluginErrorParams( 108 notifications.add(new PluginErrorParams(
101 false, exception.toString(), stackTrace.toString()) 109 false, exception.toString(), stackTrace.toString())
102 .toNotification()); 110 .toNotification());
103 } finally { 111 } finally {
104 requestImpl.error = null; 112 requestImpl.error = null;
105 } 113 }
106 } 114 }
107 EditGetFixesResult result = new EditGetFixesResult(collector.fixes); 115 EditGetFixesResult result = new EditGetFixesResult(collector.fixes);
108 return new GeneratorResult(result, notifications); 116 return new GeneratorResult(result, notifications);
109 } 117 }
110 118
111 Iterable<AnalysisError> _getErrors(FixesRequest request) { 119 Iterable<AnalysisError> _getErrors(DartFixesRequest request) {
maxkim 2017/06/22 20:52:57 If we put 'errorsAtOffset' within FixesRequest, th
112 int offset = request.offset; 120 int offset = request.offset;
113 LineInfo lineInfo = request.result.lineInfo; 121 LineInfo lineInfo = request.result.lineInfo;
114 int offsetLine = lineInfo.getLocation(offset).lineNumber; 122 int offsetLine = lineInfo.getLocation(offset).lineNumber;
115 return request.result.errors.where((AnalysisError error) { 123 return request.result.errors.where((AnalysisError error) {
116 int errorLine = lineInfo.getLocation(error.offset).lineNumber; 124 int errorLine = lineInfo.getLocation(error.offset).lineNumber;
117 return errorLine == offsetLine; 125 return errorLine == offsetLine;
118 }); 126 });
119 } 127 }
120 } 128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698