| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library services.correction.change; | 5 library services.correction.change; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/constants.dart'; | 7 import 'package:analysis_server/src/constants.dart'; |
| 8 import 'package:analysis_server/src/protocol2.dart'; | 8 import 'package:analysis_server/src/protocol2.dart'; |
| 9 import 'package:analysis_server/src/services/json.dart'; | 9 import 'package:analysis_server/src/services/json.dart'; |
| 10 import 'package:analyzer/src/generated/source.dart'; | |
| 11 | 10 |
| 12 | 11 |
| 13 /** | 12 /** |
| 14 * A description of a single change to one or more files. | 13 * A description of a single change to one or more files. |
| 15 */ | 14 */ |
| 16 class Change implements HasToJson { | 15 class Change implements HasToJson { |
| 17 /** | 16 /** |
| 18 * A textual description of the change to be applied. | 17 * A textual description of the change to be applied. |
| 19 */ | 18 */ |
| 20 final String message; | 19 final String message; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 87 } |
| 89 | 88 |
| 90 @override | 89 @override |
| 91 String toString() => | 90 String toString() => |
| 92 'Change(message=$message, edits=$fileEdits, ' | 91 'Change(message=$message, edits=$fileEdits, ' |
| 93 'linkedEditGroups=$linkedEditGroups, selection=$selection)'; | 92 'linkedEditGroups=$linkedEditGroups, selection=$selection)'; |
| 94 } | 93 } |
| 95 | 94 |
| 96 | 95 |
| 97 /** | 96 /** |
| 98 * Convert a SourceRange to a SourceEdit. | |
| 99 */ | |
| 100 SourceEdit editFromRange(SourceRange range, String replacement, {String id}) => | |
| 101 new SourceEdit(range.offset, range.length, replacement, id: id); | |
| 102 | |
| 103 /** | |
| 104 * Get the result of applying the edit to the given [code]. | |
| 105 */ | |
| 106 String applyEdit(String code, SourceEdit edit) { | |
| 107 return code.substring(0, edit.offset) + edit.replacement + | |
| 108 code.substring(edit.offset + edit.length); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Get the result of applying a set of [edits] to the given [code]. Edits | |
| 113 * are applied in the order they appear in [edits]. | |
| 114 */ | |
| 115 String applySequence(String code, Iterable<SourceEdit> edits) { | |
| 116 edits.forEach((SourceEdit edit) { | |
| 117 code = applyEdit(code, edit); | |
| 118 }); | |
| 119 return code; | |
| 120 } | |
| 121 | |
| 122 | |
| 123 /** | |
| 124 * A description of a set of changes to a single file. | 97 * A description of a set of changes to a single file. |
| 125 * | 98 * |
| 126 * [Edit]s are added in the order of decreasing offset, so they are easy to | 99 * [Edit]s are added in the order of decreasing offset, so they are easy to |
| 127 * apply to the original file content without correcting offsets. | 100 * apply to the original file content without correcting offsets. |
| 128 */ | 101 */ |
| 129 class FileEdit implements HasToJson { | 102 class FileEdit implements HasToJson { |
| 130 /** | 103 /** |
| 131 * The file to be modified. | 104 * The file to be modified. |
| 132 */ | 105 */ |
| 133 final String file; | 106 final String file; |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 Map<String, Object> toJson() { | 250 Map<String, Object> toJson() { |
| 278 return { | 251 return { |
| 279 FILE: file, | 252 FILE: file, |
| 280 OFFSET: offset | 253 OFFSET: offset |
| 281 }; | 254 }; |
| 282 } | 255 } |
| 283 | 256 |
| 284 @override | 257 @override |
| 285 String toString() => 'Position(file=$file, offset=$offset)'; | 258 String toString() => 'Position(file=$file, offset=$offset)'; |
| 286 } | 259 } |
| OLD | NEW |