Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 * An outcome of a condition checking operation. | |
| 21 */ | |
| 22 class RefactoringStatus { | |
| 23 /** | |
| 24 * The current severity of this [RefactoringStatus] - the maximum of the | |
| 25 * severities of its [entries]. | |
| 26 */ | |
| 27 RefactoringStatusSeverity _severity = RefactoringStatusSeverity.OK; | |
| 28 | |
| 29 /** | |
| 30 * A list of [RefactoringStatusEntry]s. | |
| 31 */ | |
| 32 final List<RefactoringStatusEntry> entries = []; | |
| 33 | |
| 34 /** | |
| 35 * Creates a new OK [RefactoringStatus]. | |
| 36 */ | |
| 37 RefactoringStatus(); | |
| 38 | |
| 39 /** | |
| 40 * Creates a new [RefactoringStatus] with the ERROR severity. | |
| 41 */ | |
| 42 factory RefactoringStatus.error(String msg, | |
| 43 [RefactoringStatusContext context]) { | |
| 44 RefactoringStatus status = new RefactoringStatus(); | |
| 45 status.addError(msg, context); | |
| 46 return status; | |
| 47 } | |
| 48 | |
| 49 /** | |
| 50 * Creates a new [RefactoringStatus] with the FATAL severity. | |
| 51 */ | |
| 52 factory RefactoringStatus.fatal(String msg, | |
| 53 [RefactoringStatusContext context]) { | |
| 54 RefactoringStatus status = new RefactoringStatus(); | |
| 55 status.addFatalError(msg, context); | |
| 56 return status; | |
| 57 } | |
| 58 | |
| 59 /** | |
| 60 * Creates a new [RefactoringStatus] with the WARNING severity. | |
| 61 */ | |
| 62 factory RefactoringStatus.warning(String msg, | |
| 63 [RefactoringStatusContext context]) { | |
| 64 RefactoringStatus status = new RefactoringStatus(); | |
| 65 status.addWarning(msg, context); | |
| 66 return status; | |
| 67 } | |
| 68 | |
| 69 /** | |
| 70 * Returns the [RefactoringStatusEntry] with the highest severity, or `null` | |
|
Brian Wilkerson
2014/08/11 18:45:29
"the [" --> "the first ["? (There could be more th
scheglov
2014/08/11 18:58:20
Done.
| |
| 71 * if no entries. | |
| 72 */ | |
| 73 RefactoringStatusEntry get entryWithHighestSeverity { | |
| 74 for (RefactoringStatusEntry entry in entries) { | |
| 75 if (entry.severity == _severity) { | |
| 76 return entry; | |
| 77 } | |
| 78 } | |
| 79 return null; | |
| 80 } | |
| 81 | |
| 82 /** | |
| 83 * Returns `true` if the severity is FATAL or ERROR. | |
| 84 */ | |
| 85 bool get hasError => | |
| 86 _severity == RefactoringStatusSeverity.FATAL || | |
| 87 _severity == RefactoringStatusSeverity.ERROR; | |
| 88 | |
| 89 /** | |
| 90 * Returns `true` if the severity is FATAL. | |
| 91 */ | |
| 92 bool get hasFatalError => _severity == RefactoringStatusSeverity.FATAL; | |
| 93 | |
| 94 /** | |
| 95 * Returns `true` if the severity is WARNING. | |
| 96 */ | |
| 97 bool get hasWarning => _severity == RefactoringStatusSeverity.WARNING; | |
| 98 | |
| 99 /** | |
| 100 * Return `true` if the severity is `OK`. | |
| 101 */ | |
| 102 bool get isOK => _severity == RefactoringStatusSeverity.OK; | |
| 103 | |
| 104 /** | |
| 105 * Returns the message of the [RefactoringStatusEntry] with highest severity; | |
| 106 * may be `null` if no entries. | |
| 107 */ | |
| 108 String get message { | |
| 109 RefactoringStatusEntry entry = entryWithHighestSeverity; | |
| 110 if (entry == null) { | |
| 111 return null; | |
| 112 } | |
| 113 return entry.message; | |
| 114 } | |
| 115 | |
| 116 /** | |
| 117 * Returns the current severity of this [RefactoringStatus]. | |
| 118 */ | |
| 119 RefactoringStatusSeverity get severity => _severity; | |
| 120 | |
| 121 /** | |
| 122 * Adds an ERROR entry with the given message and status. | |
| 123 */ | |
| 124 void addError(String msg, [RefactoringStatusContext context]) { | |
| 125 _addEntry( | |
| 126 new RefactoringStatusEntry(RefactoringStatusSeverity.ERROR, msg, context )); | |
| 127 } | |
| 128 | |
| 129 /** | |
| 130 * Adds a FATAL entry with the given message and status. | |
| 131 */ | |
| 132 void addFatalError(String msg, [RefactoringStatusContext context]) { | |
| 133 _addEntry( | |
| 134 new RefactoringStatusEntry(RefactoringStatusSeverity.FATAL, msg, context )); | |
| 135 } | |
| 136 | |
| 137 /** | |
| 138 * Merges [other] into this [RefactoringStatus]. | |
| 139 * | |
| 140 * The [other]'s entries are added to this. | |
| 141 * | |
| 142 * The resulting severity is the more severe of this and [other] severities. | |
| 143 * | |
| 144 * Merging with `null` is allowed - it has no effect. | |
| 145 */ | |
| 146 void addStatus(RefactoringStatus other) { | |
| 147 if (other == null) { | |
| 148 return; | |
| 149 } | |
| 150 entries.addAll(other.entries); | |
| 151 _severity = RefactoringStatusSeverity._max(_severity, other.severity); | |
| 152 } | |
| 153 | |
| 154 /** | |
| 155 * Adds a WARNING entry with the given message and status. | |
| 156 */ | |
| 157 void addWarning(String msg, [RefactoringStatusContext context]) { | |
| 158 _addEntry( | |
| 159 new RefactoringStatusEntry(RefactoringStatusSeverity.WARNING, msg, conte xt)); | |
| 160 } | |
| 161 | |
| 162 /** | |
| 163 * Returns a copy of this [RefactoringStatus] with ERROR replaced with FATAL. | |
| 164 */ | |
| 165 RefactoringStatus escalateErrorToFatal() { | |
| 166 RefactoringStatus result = new RefactoringStatus(); | |
| 167 for (RefactoringStatusEntry entry in entries) { | |
| 168 if (entry.severity == RefactoringStatusSeverity.ERROR) { | |
| 169 entry = new RefactoringStatusEntry( | |
| 170 RefactoringStatusSeverity.FATAL, | |
| 171 entry.message, | |
| 172 entry.context); | |
| 173 } | |
| 174 result._addEntry(entry); | |
| 175 } | |
| 176 return result; | |
| 177 } | |
| 178 | |
| 179 @override | |
| 180 String toString() { | |
| 181 StringBuffer sb = new StringBuffer(); | |
| 182 sb.write("<"); | |
| 183 sb.write(_severity.name); | |
| 184 if (!isOK) { | |
| 185 sb.write("\n"); | |
| 186 for (RefactoringStatusEntry entry in entries) { | |
| 187 sb.write("\t"); | |
| 188 sb.write(entry); | |
| 189 sb.write("\n"); | |
| 190 } | |
| 191 } | |
| 192 sb.write(">"); | |
| 193 return sb.toString(); | |
| 194 } | |
| 195 | |
| 196 /** | |
| 197 * Adds the given [RefactoringStatusEntry] and updates [severity]. | |
| 198 */ | |
| 199 void _addEntry(RefactoringStatusEntry entry) { | |
| 200 entries.add(entry); | |
| 201 _severity = RefactoringStatusSeverity._max(_severity, entry.severity); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 | |
| 206 /** | |
| 207 * [RefactoringStatusContext] can be used to annotate [RefactoringStatusEntry]s | |
| 208 * with additional information typically presented in the user interface. | |
| 209 */ | |
| 210 class RefactoringStatusContext { | |
| 211 /** | |
| 212 * The [AnalysisContext] in which this status occurs. | |
| 213 */ | |
| 214 final AnalysisContext context; | |
| 215 | |
| 216 /** | |
| 217 * The [Source] in which this status occurs. | |
| 218 */ | |
| 219 final Source source; | |
| 220 | |
| 221 /** | |
| 222 * The [SourceRange] with specific location where this status occurs. | |
| 223 */ | |
| 224 final SourceRange range; | |
| 225 | |
| 226 /** | |
| 227 * Creates a new [RefactoringStatusContext]. | |
| 228 */ | |
| 229 RefactoringStatusContext(this.context, this.source, this.range); | |
| 230 | |
| 231 /** | |
| 232 * Creates a new [RefactoringStatusContext] for the given [Element]. | |
| 233 */ | |
| 234 factory RefactoringStatusContext.forElement(Element element) { | |
| 235 AnalysisContext context = element.context; | |
| 236 Source source = element.source; | |
| 237 SourceRange range = rangeElementName(element); | |
| 238 return new RefactoringStatusContext(context, source, range); | |
| 239 } | |
| 240 | |
| 241 /** | |
| 242 * Creates a new [RefactoringStatusContext] for the given [SearchMatch]. | |
| 243 */ | |
| 244 factory RefactoringStatusContext.forMatch(SearchMatch match) { | |
| 245 Element enclosingElement = match.element; | |
| 246 return new RefactoringStatusContext( | |
| 247 enclosingElement.context, | |
| 248 enclosingElement.source, | |
| 249 match.sourceRange); | |
| 250 } | |
| 251 | |
| 252 /** | |
| 253 * Creates a new [RefactoringStatusContext] for the given [AstNode]. | |
| 254 */ | |
| 255 factory RefactoringStatusContext.forNode(AstNode node) { | |
| 256 CompilationUnit unit = node.getAncestor((node) => node is CompilationUnit); | |
| 257 CompilationUnitElement unitElement = unit.element; | |
| 258 AnalysisContext context = unitElement.context; | |
| 259 Source source = unitElement.source; | |
| 260 SourceRange range = rangeNode(node); | |
| 261 return new RefactoringStatusContext(context, source, range); | |
| 262 } | |
| 263 | |
| 264 /** | |
| 265 * Creates a new [RefactoringStatusContext] for the given [CompilationUnit]. | |
| 266 */ | |
| 267 factory RefactoringStatusContext.forUnit(CompilationUnit unit, | |
| 268 SourceRange range) { | |
| 269 CompilationUnitElement unitElement = unit.element; | |
| 270 AnalysisContext context = unitElement.context; | |
| 271 Source source = unitElement.source; | |
| 272 return new RefactoringStatusContext(context, source, range); | |
| 273 } | |
| 274 | |
| 275 @override | |
| 276 String toString() { | |
| 277 JavaStringBuilder builder = new JavaStringBuilder(); | |
| 278 builder.append("[source="); | |
| 279 builder.append(source); | |
| 280 builder.append(", range="); | |
| 281 builder.append(range); | |
| 282 builder.append("]"); | |
| 283 return builder.toString(); | |
| 284 } | |
| 285 } | |
| 286 | |
| 287 | |
| 288 /** | |
| 289 * An immutable object representing an entry in a [RefactoringStatus]. | |
| 290 * | |
| 291 * A [RefactoringStatusEntry] consists of a severity, a message and a context. | |
| 292 */ | |
| 293 class RefactoringStatusEntry { | |
| 294 /** | |
| 295 * The severity level. | |
| 296 */ | |
| 297 final RefactoringStatusSeverity severity; | |
| 298 | |
| 299 /** | |
| 300 * The message of the status entry. | |
| 301 */ | |
| 302 final String message; | |
| 303 | |
| 304 /** | |
| 305 * The [RefactoringStatusContext] which can be used to show more detailed | |
| 306 * information regarding this status entry in the UI. | |
| 307 * | |
| 308 * May be `null` indicating that no context is available. | |
| 309 */ | |
| 310 final RefactoringStatusContext context; | |
| 311 | |
| 312 RefactoringStatusEntry(this.severity, this.message, [this.context]); | |
| 313 | |
| 314 /** | |
| 315 * Returns whether the entry represents an error or not. | |
| 316 */ | |
| 317 bool get isError => severity == RefactoringStatusSeverity.ERROR; | |
| 318 | |
| 319 /** | |
| 320 * Returns whether the entry represents a fatal error or not. | |
| 321 */ | |
| 322 bool get isFatalError => severity == RefactoringStatusSeverity.FATAL; | |
| 323 | |
| 324 /** | |
| 325 * Returns whether the entry represents a warning or not. | |
| 326 */ | |
| 327 bool get isWarning => severity == RefactoringStatusSeverity.WARNING; | |
| 328 | |
| 329 @override | |
| 330 String toString() { | |
| 331 if (context != null) { | |
| 332 return "${severity}: ${message}; Context: ${context}"; | |
| 333 } else { | |
| 334 return "${severity}: ${message}"; | |
| 335 } | |
| 336 } | |
| 337 } | |
| 338 | |
| 339 | |
| 340 /** | |
| 341 * Severity of [RefactoringStatus]. | |
| 342 */ | |
| 343 class RefactoringStatusSeverity { | |
| 344 /** | |
| 345 * The severity indicating the nominal case. | |
| 346 */ | |
| 347 static const OK = const RefactoringStatusSeverity('OK', 0); | |
| 348 | |
| 349 /** | |
| 350 * The severity indicating a warning. | |
| 351 * | |
| 352 * Use this severity if the refactoring can be performed, but you assume that | |
| 353 * the user could not be aware of problems or confusions resulting from the | |
| 354 * execution. | |
| 355 */ | |
| 356 static const WARNING = const RefactoringStatusSeverity('WARNING', 2); | |
| 357 | |
| 358 /** | |
| 359 * The severity indicating an error. | |
| 360 * | |
| 361 * Use this severity if the refactoring can be performed, but the refactoring | |
| 362 * will not be behavior preserving and/or the partial execution will lead to | |
| 363 * an inconsistent state (e.g. compile errors). | |
| 364 */ | |
| 365 static const ERROR = const RefactoringStatusSeverity('ERROR', 3); | |
| 366 | |
| 367 /** | |
| 368 * The severity indicating a fatal error. | |
| 369 * | |
| 370 * Use this severity if the refactoring cannot be performed, and execution | |
| 371 * would lead to major problems. Note that this completely blocks the user | |
| 372 * from performing this refactoring. | |
| 373 * | |
| 374 * It is often preferable to use an [ERROR] status and allow a partial | |
| 375 * execution (e.g. if just one reference to a refactored element cannot be | |
| 376 * updated). | |
| 377 */ | |
| 378 static const FATAL = const RefactoringStatusSeverity('FATAL', 4); | |
| 379 | |
| 380 final String name; | |
| 381 final int ordinal; | |
| 382 | |
| 383 const RefactoringStatusSeverity(this.name, this.ordinal); | |
| 384 | |
| 385 @override | |
| 386 String toString() => name; | |
| 387 | |
| 388 /** | |
| 389 * Returns the most severe [RefactoringStatusSeverity]. | |
| 390 */ | |
| 391 static RefactoringStatusSeverity _max(RefactoringStatusSeverity a, | |
| 392 RefactoringStatusSeverity b) { | |
| 393 if (b.ordinal > a.ordinal) { | |
| 394 return b; | |
| 395 } | |
| 396 return a; | |
| 397 } | |
| 398 } | |
| OLD | NEW |