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 | 10 |
11 | 11 |
12 /** | 12 /** |
13 * A description of a single change to one or more files. | 13 * A description of a single change to one or more files. |
14 */ | 14 */ |
15 class Change implements HasToJson { | 15 class Change implements HasToJson { |
16 /** | 16 /** |
17 * A textual description of the change to be applied. | 17 * A textual description of the change to be applied. |
18 */ | 18 */ |
19 final String message; | 19 final String message; |
20 | 20 |
21 /** | 21 /** |
22 * A list of the [FileEdit]s used to effect the change. | 22 * A list of the [FileEdit]s used to effect the change. |
23 */ | 23 */ |
24 final List<FileEdit> fileEdits = <FileEdit>[]; | 24 final List<SourceFileEdit> fileEdits = <SourceFileEdit>[]; |
25 | 25 |
26 /** | 26 /** |
27 * A list of the [LinkedEditGroup]s in the change. | 27 * A list of the [LinkedEditGroup]s in the change. |
28 */ | 28 */ |
29 final List<LinkedEditGroup> linkedEditGroups = <LinkedEditGroup>[]; | 29 final List<LinkedEditGroup> linkedEditGroups = <LinkedEditGroup>[]; |
30 | 30 |
31 /** | 31 /** |
32 * The position that should be selected after the edits have been applied. | 32 * The position that should be selected after the edits have been applied. |
33 */ | 33 */ |
34 Position selection; | 34 Position selection; |
35 | 35 |
36 Change(this.message); | 36 Change(this.message); |
37 | 37 |
38 /** | 38 /** |
39 * Adds [edit] to the [FileEdit] for the given [file]. | 39 * Adds [edit] to the [FileEdit] for the given [file]. |
40 */ | 40 */ |
41 void addEdit(String file, SourceEdit edit) { | 41 void addEdit(String file, SourceEdit edit) { |
42 FileEdit fileEdit = getFileEdit(file); | 42 SourceFileEdit fileEdit = getFileEdit(file); |
43 if (fileEdit == null) { | 43 if (fileEdit == null) { |
44 fileEdit = new FileEdit(file); | 44 fileEdit = new SourceFileEdit(file, <SourceEdit>[]); |
scheglov
2014/08/21 18:03:22
Can we get rid of the second parameter in the cons
Paul Berry
2014/08/21 20:00:44
I think so. I'll work on this in a separate CL.
| |
45 addFileEdit(fileEdit); | 45 addFileEdit(fileEdit); |
46 } | 46 } |
47 fileEdit.add(edit); | 47 fileEdit.add(edit); |
48 } | 48 } |
49 | 49 |
50 /** | 50 /** |
51 * Adds the given [FileEdit]. | 51 * Adds the given [FileEdit]. |
52 */ | 52 */ |
53 void addFileEdit(FileEdit edit) { | 53 void addFileEdit(SourceFileEdit edit) { |
54 fileEdits.add(edit); | 54 fileEdits.add(edit); |
55 } | 55 } |
56 | 56 |
57 /** | 57 /** |
58 * Adds the given [LinkedEditGroup]. | 58 * Adds the given [LinkedEditGroup]. |
59 */ | 59 */ |
60 void addLinkedEditGroup(LinkedEditGroup linkedEditGroup) { | 60 void addLinkedEditGroup(LinkedEditGroup linkedEditGroup) { |
61 linkedEditGroups.add(linkedEditGroup); | 61 linkedEditGroups.add(linkedEditGroup); |
62 } | 62 } |
63 | 63 |
64 /** | 64 /** |
65 * Returns the [FileEdit] for the given [file], maybe `null`. | 65 * Returns the [FileEdit] for the given [file], maybe `null`. |
66 */ | 66 */ |
67 FileEdit getFileEdit(String file) { | 67 SourceFileEdit getFileEdit(String file) { |
68 for (FileEdit fileEdit in fileEdits) { | 68 for (SourceFileEdit fileEdit in fileEdits) { |
69 if (fileEdit.file == file) { | 69 if (fileEdit.file == file) { |
70 return fileEdit; | 70 return fileEdit; |
71 } | 71 } |
72 } | 72 } |
73 return null; | 73 return null; |
74 } | 74 } |
75 | 75 |
76 @override | 76 @override |
77 Map<String, Object> toJson() { | 77 Map<String, Object> toJson() { |
78 Map<String, Object> json = { | 78 Map<String, Object> json = { |
79 MESSAGE: message, | 79 MESSAGE: message, |
80 EDITS: objectToJson(fileEdits), | 80 EDITS: objectToJson(fileEdits), |
81 LINKED_EDIT_GROUPS: objectToJson(linkedEditGroups) | 81 LINKED_EDIT_GROUPS: objectToJson(linkedEditGroups) |
82 }; | 82 }; |
83 if (selection != null) { | 83 if (selection != null) { |
84 json[SELECTION] = selection.toJson(); | 84 json[SELECTION] = selection.toJson(); |
85 } | 85 } |
86 return json; | 86 return json; |
87 } | 87 } |
88 | 88 |
89 @override | 89 @override |
90 String toString() => | 90 String toString() => |
91 'Change(message=$message, edits=$fileEdits, ' | 91 'Change(message=$message, edits=$fileEdits, ' |
92 'linkedEditGroups=$linkedEditGroups, selection=$selection)'; | 92 'linkedEditGroups=$linkedEditGroups, selection=$selection)'; |
93 } | 93 } |
94 | 94 |
95 | 95 |
96 /** | 96 /** |
97 * A description of a set of changes to a single file. | |
98 * | |
99 * [Edit]s are added in the order of decreasing offset, so they are easy to | |
100 * apply to the original file content without correcting offsets. | |
101 */ | |
102 class FileEdit implements HasToJson { | |
103 /** | |
104 * The file to be modified. | |
105 */ | |
106 final String file; | |
107 | |
108 /** | |
109 * A list of the [Edit]s used to effect the change. | |
110 */ | |
111 final List<SourceEdit> edits = <SourceEdit>[]; | |
112 | |
113 FileEdit(this.file); | |
114 | |
115 /** | |
116 * Adds the given [Edit] to the list. | |
117 */ | |
118 void add(SourceEdit edit) { | |
119 int index = 0; | |
120 while (index < edits.length && edits[index].offset > edit.offset) { | |
121 index++; | |
122 } | |
123 edits.insert(index, edit); | |
124 } | |
125 | |
126 /** | |
127 * Adds the given [Edit]s. | |
128 */ | |
129 void addAll(Iterable<SourceEdit> edits) { | |
130 edits.forEach(add); | |
131 } | |
132 | |
133 @override | |
134 Map<String, Object> toJson() { | |
135 return { | |
136 FILE: file, | |
137 EDITS: objectToJson(edits) | |
138 }; | |
139 } | |
140 | |
141 @override | |
142 String toString() => "FileEdit(file=$file, edits=$edits)"; | |
143 } | |
144 | |
145 | |
146 /** | |
147 * A group of linked [Position]s in multiple files that are simultaneously | 97 * A group of linked [Position]s in multiple files that are simultaneously |
148 * modified - if one gets edited, all other positions in a group are edited the | 98 * modified - if one gets edited, all other positions in a group are edited the |
149 * same way. All linked positions in a group have the same content. | 99 * same way. All linked positions in a group have the same content. |
150 */ | 100 */ |
151 class LinkedEditGroup implements HasToJson { | 101 class LinkedEditGroup implements HasToJson { |
152 int length; | 102 int length; |
153 final List<Position> positions = <Position>[]; | 103 final List<Position> positions = <Position>[]; |
154 final List<LinkedEditSuggestion> suggestions = <LinkedEditSuggestion>[]; | 104 final List<LinkedEditSuggestion> suggestions = <LinkedEditSuggestion>[]; |
155 | 105 |
156 void addPosition(Position position, int length) { | 106 void addPosition(Position position, int length) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 static const PARAMETER = const LinkedEditSuggestionKind('PARAMETER'); | 165 static const PARAMETER = const LinkedEditSuggestionKind('PARAMETER'); |
216 static const TYPE = const LinkedEditSuggestionKind('TYPE'); | 166 static const TYPE = const LinkedEditSuggestionKind('TYPE'); |
217 static const VARIABLE = const LinkedEditSuggestionKind('VARIABLE'); | 167 static const VARIABLE = const LinkedEditSuggestionKind('VARIABLE'); |
218 final String name; | 168 final String name; |
219 | 169 |
220 const LinkedEditSuggestionKind(this.name); | 170 const LinkedEditSuggestionKind(this.name); |
221 | 171 |
222 @override | 172 @override |
223 String toString() => name; | 173 String toString() => name; |
224 } | 174 } |
225 | |
226 | |
227 /** | |
228 * A position in a file. | |
229 */ | |
230 class Position implements HasToJson { | |
231 final String file; | |
232 final int offset; | |
233 | |
234 Position(this.file, this.offset); | |
235 | |
236 int get hashCode { | |
237 int hash = file.hashCode; | |
238 hash = hash * 31 + offset; | |
239 return hash; | |
240 } | |
241 | |
242 bool operator ==(other) { | |
243 if (other is Position) { | |
244 return other.file == file && other.offset == offset; | |
245 } | |
246 return false; | |
247 } | |
248 | |
249 @override | |
250 Map<String, Object> toJson() { | |
251 return { | |
252 FILE: file, | |
253 OFFSET: offset | |
254 }; | |
255 } | |
256 | |
257 @override | |
258 String toString() => 'Position(file=$file, offset=$offset)'; | |
259 } | |
OLD | NEW |