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

Side by Side Diff: pkg/analysis_services/lib/correction/status.dart

Issue 458063002: Initial work on refactorings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2014, 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 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information.
7
8 library services.status;
9
10 import 'package:analysis_services/search/search_engine.dart';
11 import 'package:analysis_services/src/correction/source_range.dart';
12 import 'package:analyzer/src/generated/ast.dart';
13 import 'package:analyzer/src/generated/element.dart';
14 import 'package:analyzer/src/generated/engine.dart';
15 import 'package:analyzer/src/generated/java_core.dart';
16 import 'package:analyzer/src/generated/source.dart';
17
18
19 /**
20 * Creates a new [RefactoringStatus] with [RefactoringStatusSeverity.ERROR].
21 */
22 RefactoringStatus createErrorStatus(String msg,
Brian Wilkerson 2014/08/11 16:27:47 I would have made these named constructors: Ref
scheglov 2014/08/11 18:06:24 Thank you! Done.
23 [RefactoringStatusContext context]) {
24 RefactoringStatus status = new RefactoringStatus();
25 status.addError(msg, context);
26 return status;
27 }
28
29
30 /**
31 * Creates a new [RefactoringStatus] with [RefactoringStatusSeverity.FATAL].
32 */
33 RefactoringStatus createFatalErrorStatus(String msg,
34 [RefactoringStatusContext context]) {
35 RefactoringStatus status = new RefactoringStatus();
36 status.addFatalError(msg, context);
37 return status;
38 }
39
40
41 /**
42 * Creates a new [RefactoringStatus] with [RefactoringStatusSeverity.WARNING].
43 */
44 RefactoringStatus createWarningStatus(String msg,
45 [RefactoringStatusContext context]) {
46 RefactoringStatus status = new RefactoringStatus();
47 status.addWarning(msg, context);
48 return status;
49 }
50
51
52 /**
53 * An outcome of a condition checking operation.
54 */
55 class RefactoringStatus {
56 RefactoringStatusSeverity _severity = RefactoringStatusSeverity.OK;
Brian Wilkerson 2014/08/11 16:27:46 Please document this field.
scheglov 2014/08/11 18:06:24 Done.
57
58 /**
59 * A list of [RefactoringStatusEntry]s.
60 */
61 final List<RefactoringStatusEntry> entries = [];
62
63 /**
64 * Returns the [RefactoringStatusEntry] with the highest severity, or `null`
65 * if no entries.
66 */
67 RefactoringStatusEntry get entryWithHighestSeverity {
68 if (entries.isEmpty) {
69 return null;
70 }
71 RefactoringStatusEntry result = entries[0];
72 for (RefactoringStatusEntry entry in entries) {
73 if (result.severity.ordinal < entry.severity.ordinal) {
Brian Wilkerson 2014/08/11 16:27:47 Given that _severity is the maximum severity, coul
scheglov 2014/08/11 18:06:24 Done.
74 result = entry;
75 }
76 }
77 return result;
78 }
79
80 /**
81 * Returns `true` if the severity is FATAL or ERROR.
82 */
83 bool get hasError =>
84 _severity == RefactoringStatusSeverity.FATAL ||
85 _severity == RefactoringStatusSeverity.ERROR;
86
87 /**
88 * Returns `true` if the severity is FATAL.
89 */
90 bool get hasFatalError => _severity == RefactoringStatusSeverity.FATAL;
91
92 /**
93 * Returns `true` if the severity is WARNING.
94 */
95 bool get hasWarning => _severity == RefactoringStatusSeverity.WARNING;
96
97 /**
98 * Return `true` if the severity is `OK`.
99 */
100 bool get isOK => _severity == RefactoringStatusSeverity.OK;
101
102 /**
103 * Returns the message of the [RefactoringStatusEntry] with highest severity;
104 * may be `null` if no entries.
105 */
106 String get message {
107 RefactoringStatusEntry entry = entryWithHighestSeverity;
108 if (entry == null) {
109 return null;
110 }
111 return entry.message;
112 }
113
114 /**
115 * Returns the current severity of this [RefactoringStatus].
116 */
117 RefactoringStatusSeverity get severity => _severity;
118
119 /**
120 * Adds an ERROR entry with the given message and status.
121 */
122 void addError(String msg, [RefactoringStatusContext context]) {
123 _addEntry(
124 new RefactoringStatusEntry(RefactoringStatusSeverity.ERROR, msg, context ));
125 }
126
127 /**
128 * Adds a FATAL entry with the given message and status.
129 */
130 void addFatalError(String msg, [RefactoringStatusContext context]) {
131 _addEntry(
132 new RefactoringStatusEntry(RefactoringStatusSeverity.FATAL, msg, context ));
133 }
134
135 /**
136 * Adds a WARNING entry with the given message and status.
137 */
138 void addWarning(String msg, [RefactoringStatusContext context]) {
139 _addEntry(
140 new RefactoringStatusEntry(RefactoringStatusSeverity.WARNING, msg, conte xt));
141 }
142
143 /**
144 * Returns a copy of this [RefactoringStatus] with ERROR replaced with FATAL.
145 */
146 RefactoringStatus escalateErrorToFatal() {
147 RefactoringStatus result = new RefactoringStatus();
148 for (RefactoringStatusEntry entry in entries) {
149 RefactoringStatusSeverity severity = entry.severity;
150 if (severity == RefactoringStatusSeverity.ERROR) {
151 severity = RefactoringStatusSeverity.FATAL;
152 }
153 result._addEntry(
154 new RefactoringStatusEntry(severity, entry.message, entry.context));
Brian Wilkerson 2014/08/11 16:27:47 Could we share entries when the severity has not c
scheglov 2014/08/11 18:06:24 Done.
155 }
156 return result;
157 }
158
159 /**
160 * Merges the receiver and the parameter statuses.
161 *
162 * The resulting list of entries in the receiver will contain entries from
163 * both.
164 *
165 * The resulting severity in the receiver will be the more severe of its
166 * current severityand the parameter's severity.
Brian Wilkerson 2014/08/11 16:27:46 "severityand" --> "severity and"
scheglov 2014/08/11 18:06:24 Done.
167 *
168 * Merging with `null` is allowed - it has no effect.
169 */
170 void merge(RefactoringStatus other) {
Brian Wilkerson 2014/08/11 16:27:47 When I saw this method being used I had to come ba
scheglov 2014/08/11 18:06:25 Done.
171 if (other == null) {
172 return;
173 }
174 entries.addAll(other.entries);
175 _severity = _max(_severity, other.severity);
176 }
177
178 @override
179 String toString() {
180 StringBuffer sb = new StringBuffer();
181 sb.write("<");
182 sb.write(_severity.name);
183 if (!isOK) {
184 sb.write("\n");
185 for (RefactoringStatusEntry entry in entries) {
186 sb.write("\t");
187 sb.write(entry);
188 sb.write("\n");
189 }
190 }
191 sb.write(">");
192 return sb.toString();
193 }
194
195 /**
196 * Adds the given [RefactoringStatusEntry] and updates [severity].
197 */
198 void _addEntry(RefactoringStatusEntry entry) {
199 entries.add(entry);
200 _severity = _max(_severity, entry.severity);
201 }
202
203 /**
204 * Returns the [RefactoringStatusSeverity] with the maximal ordinal.
205 */
206 static RefactoringStatusSeverity _max(RefactoringStatusSeverity a,
Brian Wilkerson 2014/08/11 16:27:46 This should be defined on RefactoringStatusSeverit
scheglov 2014/08/11 18:06:24 Done.
207 RefactoringStatusSeverity b) {
208 if (b.ordinal > a.ordinal) {
209 return b;
210 }
211 return a;
212 }
213 }
214
215
216 /**
217 * [RefactoringStatusContext] can be used to annotate [RefactoringStatusEntry]s
218 * with additional information typically presented in the user interface.
219 */
220 class RefactoringStatusContext {
221 /**
222 * The [AnalysisContext] in which this status occurs.
223 */
224 final AnalysisContext context;
225
226 /**
227 * The [Source] in which this status occurs.
228 */
229 final Source source;
230
231 /**
232 * The [SourceRange] with specific location where this status occurs.
233 */
234 final SourceRange range;
235
236 /**
237 * Creates a new [RefactoringStatusContext].
238 */
239 RefactoringStatusContext(this.context, this.source, this.range);
240
241 /**
242 * Creates a new [RefactoringStatusContext] for the given [Element].
243 */
244 factory RefactoringStatusContext.forElement(Element element) {
245 AnalysisContext context = element.context;
246 Source source = element.source;
247 SourceRange range = rangeElementName(element);
248 return new RefactoringStatusContext(context, source, range);
249 }
250
251 /**
252 * Creates a new [RefactoringStatusContext] for the given [SearchMatch].
253 */
254 factory RefactoringStatusContext.forMatch(SearchMatch match) {
255 Element enclosingElement = match.element;
256 return new RefactoringStatusContext(
257 enclosingElement.context,
258 enclosingElement.source,
259 match.sourceRange);
260 }
261
262 /**
263 * Creates a new [RefactoringStatusContext] for the given [AstNode].
264 */
265 factory RefactoringStatusContext.forNode(AstNode node) {
266 CompilationUnit unit = node.getAncestor((node) => node is CompilationUnit);
267 CompilationUnitElement unitElement = unit.element;
268 AnalysisContext context = unitElement.context;
269 Source source = unitElement.source;
270 SourceRange range = rangeNode(node);
271 return new RefactoringStatusContext(context, source, range);
272 }
273
274 /**
275 * Creates a new [RefactoringStatusContext] for the given [CompilationUnit].
276 */
277 factory RefactoringStatusContext.forUnit(CompilationUnit unit,
278 SourceRange range) {
279 CompilationUnitElement unitElement = unit.element;
280 AnalysisContext context = unitElement.context;
281 Source source = unitElement.source;
282 return new RefactoringStatusContext(context, source, range);
283 }
284
285 @override
286 String toString() {
287 JavaStringBuilder builder = new JavaStringBuilder();
288 builder.append("[source=");
289 builder.append(source);
290 builder.append(", range=");
291 builder.append(range);
292 builder.append("]");
293 return builder.toString();
294 }
295 }
296
297
298 /**
299 * An immutable object representing an entry in a [RefactoringStatus].
300 *
301 * A [RefactoringStatusEntry] consists of a severity, a message and a context.
302 */
303 class RefactoringStatusEntry {
304 /**
305 * The severity level.
306 */
307 final RefactoringStatusSeverity severity;
308
309 /**
310 * The message of the status entry.
311 */
312 final String message;
313
314 /**
315 * The [RefactoringStatusContext] which can be used to show more detailed
316 * information regarding this status entry in the UI.
317 *
318 * May be `null` indicating that no context is available.
319 */
320 final RefactoringStatusContext context;
321
322 RefactoringStatusEntry(this.severity, this.message, [this.context]);
323
324 /**
325 * Returns whether the entry represents an error or not.
326 */
327 bool get isError => severity == RefactoringStatusSeverity.ERROR;
328
329 /**
330 * Returns whether the entry represents a fatal error or not.
331 */
332 bool get isFatalError => severity == RefactoringStatusSeverity.FATAL;
333
334 /**
335 * Returns whether the entry represents a warning or not.
336 */
337 bool get isWarning => severity == RefactoringStatusSeverity.WARNING;
338
339 @override
340 String toString() {
341 if (context != null) {
342 return "${severity}: ${message}; Context: ${context}";
343 } else {
344 return "${severity}: ${message}";
345 }
346 }
347 }
348
349
350 /**
351 * Severity of [RefactoringStatus].
352 */
353 class RefactoringStatusSeverity {
Brian Wilkerson 2014/08/11 16:27:47 Should this be a subclass of Enum?
scheglov 2014/08/11 18:06:24 I try to avoid using Java artefacts.
354 static const OK = const RefactoringStatusSeverity('OK', 0);
355 static const WARNING = const RefactoringStatusSeverity('WARNING', 2);
356 static const ERROR = const RefactoringStatusSeverity('ERROR', 3);
357 static const FATAL = const RefactoringStatusSeverity('FATAL', 4);
Brian Wilkerson 2014/08/11 16:27:47 It would be good to document these severities, esp
scheglov 2014/08/11 18:06:24 Done.
358
359 final String name;
360 final int ordinal;
361
362 const RefactoringStatusSeverity(this.name, this.ordinal);
363
364 @override
365 String toString() => name;
366 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698