OLD | NEW |
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 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
7 import 'package:analyzer/src/generated/source.dart'; | 7 import 'package:analyzer/src/generated/source.dart'; |
8 import 'package:analyzer_plugin/protocol/protocol_common.dart'; | 8 import 'package:analyzer_plugin/protocol/protocol_common.dart'; |
9 import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dar
t'; | 9 import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dar
t'; |
10 | 10 |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 EditBuilderImpl builder = createEditBuilder(range.offset, range.length); | 222 EditBuilderImpl builder = createEditBuilder(range.offset, range.length); |
223 fileEdit.add(builder.sourceEdit); | 223 fileEdit.add(builder.sourceEdit); |
224 } | 224 } |
225 | 225 |
226 @override | 226 @override |
227 void addInsertion(int offset, void buildEdit(EditBuilder builder)) { | 227 void addInsertion(int offset, void buildEdit(EditBuilder builder)) { |
228 EditBuilderImpl builder = createEditBuilder(offset, 0); | 228 EditBuilderImpl builder = createEditBuilder(offset, 0); |
229 try { | 229 try { |
230 buildEdit(builder); | 230 buildEdit(builder); |
231 } finally { | 231 } finally { |
232 fileEdit.add(builder.sourceEdit); | 232 SourceEdit edit = builder.sourceEdit; |
233 _captureSelection(builder); | 233 fileEdit.add(edit); |
| 234 _captureSelection(builder, edit); |
234 } | 235 } |
235 } | 236 } |
236 | 237 |
237 @override | 238 @override |
238 void addLinkedPosition(SourceRange range, String groupName) { | 239 void addLinkedPosition(SourceRange range, String groupName) { |
239 LinkedEditGroup group = changeBuilder.getLinkedEditGroup(groupName); | 240 LinkedEditGroup group = changeBuilder.getLinkedEditGroup(groupName); |
240 Position position = new Position( | 241 Position position = new Position( |
241 fileEdit.file, range.offset + _deltaToOffset(range.offset)); | 242 fileEdit.file, range.offset + _deltaToOffset(range.offset)); |
242 group.addPosition(position, range.length); | 243 group.addPosition(position, range.length); |
243 } | 244 } |
244 | 245 |
245 @override | 246 @override |
246 void addReplacement(SourceRange range, void buildEdit(EditBuilder builder)) { | 247 void addReplacement(SourceRange range, void buildEdit(EditBuilder builder)) { |
247 EditBuilderImpl builder = createEditBuilder(range.offset, range.length); | 248 EditBuilderImpl builder = createEditBuilder(range.offset, range.length); |
248 try { | 249 try { |
249 buildEdit(builder); | 250 buildEdit(builder); |
250 } finally { | 251 } finally { |
251 fileEdit.add(builder.sourceEdit); | 252 SourceEdit edit = builder.sourceEdit; |
252 _captureSelection(builder); | 253 fileEdit.add(edit); |
| 254 _captureSelection(builder, edit); |
253 } | 255 } |
254 } | 256 } |
255 | 257 |
256 @override | 258 @override |
257 void addSimpleInsertion(int offset, String text) { | 259 void addSimpleInsertion(int offset, String text) { |
258 EditBuilderImpl builder = createEditBuilder(offset, 0); | 260 EditBuilderImpl builder = createEditBuilder(offset, 0); |
259 try { | 261 try { |
260 builder.write(text); | 262 builder.write(text); |
261 } finally { | 263 } finally { |
262 fileEdit.add(builder.sourceEdit); | 264 SourceEdit edit = builder.sourceEdit; |
263 _captureSelection(builder); | 265 fileEdit.add(edit); |
| 266 _captureSelection(builder, edit); |
264 } | 267 } |
265 } | 268 } |
266 | 269 |
267 @override | 270 @override |
268 void addSimpleReplacement(SourceRange range, String text) { | 271 void addSimpleReplacement(SourceRange range, String text) { |
269 EditBuilderImpl builder = createEditBuilder(range.offset, range.length); | 272 EditBuilderImpl builder = createEditBuilder(range.offset, range.length); |
270 try { | 273 try { |
271 builder.write(text); | 274 builder.write(text); |
272 } finally { | 275 } finally { |
273 fileEdit.add(builder.sourceEdit); | 276 SourceEdit edit = builder.sourceEdit; |
274 _captureSelection(builder); | 277 fileEdit.add(edit); |
| 278 _captureSelection(builder, edit); |
275 } | 279 } |
276 } | 280 } |
277 | 281 |
278 EditBuilderImpl createEditBuilder(int offset, int length) { | 282 EditBuilderImpl createEditBuilder(int offset, int length) { |
279 return new EditBuilderImpl(this, offset, length); | 283 return new EditBuilderImpl(this, offset, length); |
280 } | 284 } |
281 | 285 |
282 /** | 286 /** |
283 * Finalize the source file edit that is being built. | 287 * Finalize the source file edit that is being built. |
284 */ | 288 */ |
285 void finalize() { | 289 void finalize() { |
286 // Nothing to do. | 290 // Nothing to do. |
287 } | 291 } |
288 | 292 |
289 /** | 293 /** |
290 * Capture the selection offset if one was set. | 294 * Capture the selection offset if one was set. |
291 */ | 295 */ |
292 void _captureSelection(EditBuilderImpl builder) { | 296 void _captureSelection(EditBuilderImpl builder, SourceEdit edit) { |
293 int offset = builder._selectionOffset; | 297 int offset = builder._selectionOffset; |
294 if (offset >= 0) { | 298 if (offset >= 0) { |
295 Position position = | 299 Position position = |
296 new Position(fileEdit.file, offset + _deltaToOffset(offset)); | 300 new Position(fileEdit.file, offset + _deltaToEdit(edit)); |
297 changeBuilder.setSelection(position); | 301 changeBuilder.setSelection(position); |
298 } | 302 } |
299 } | 303 } |
300 | 304 |
301 /** | 305 /** |
302 * Return the current delta caused by edits that will be applied before the | 306 * Return the current delta caused by edits that will be applied before the |
| 307 * [targetEdit]. In other words, if all of the edits that occur before the |
| 308 * target edit were to be applied, then the text at the offset of the target |
| 309 * edit before the applied edits will be at `offset + _deltaToOffset(offset)` |
| 310 * after the edits. |
| 311 */ |
| 312 int _deltaToEdit(SourceEdit targetEdit) { |
| 313 int delta = 0; |
| 314 for (SourceEdit edit in fileEdit.edits) { |
| 315 if (edit.offset < targetEdit.offset) { |
| 316 delta += edit.replacement.length - edit.length; |
| 317 } |
| 318 } |
| 319 return delta; |
| 320 } |
| 321 |
| 322 /** |
| 323 * Return the current delta caused by edits that will be applied before the |
303 * given [offset]. In other words, if all of the edits that have so far been | 324 * given [offset]. In other words, if all of the edits that have so far been |
304 * added were to be applied, then the text at the given `offset` before the | 325 * added were to be applied, then the text at the given `offset` before the |
305 * edits will be at `offset + deltaToOffset(offset)` after the edits. | 326 * applied edits will be at `offset + _deltaToOffset(offset)` after the edits. |
306 */ | 327 */ |
307 int _deltaToOffset(int targetOffset) { | 328 int _deltaToOffset(int offset) { |
308 int offset = 0; | 329 int delta = 0; |
309 for (var edit in fileEdit.edits) { | 330 for (SourceEdit edit in fileEdit.edits) { |
310 if (edit.offset <= targetOffset) { | 331 if (edit.offset <= offset) { |
311 offset += edit.replacement.length - edit.length; | 332 delta += edit.replacement.length - edit.length; |
312 } | 333 } |
313 } | 334 } |
314 return offset; | 335 return delta; |
315 } | 336 } |
316 } | 337 } |
317 | 338 |
318 /** | 339 /** |
319 * A builder used to build a [LinkedEdit] region within an edit. | 340 * A builder used to build a [LinkedEdit] region within an edit. |
320 */ | 341 */ |
321 class LinkedEditBuilderImpl implements LinkedEditBuilder { | 342 class LinkedEditBuilderImpl implements LinkedEditBuilder { |
322 final EditBuilderImpl editBuilder; | 343 final EditBuilderImpl editBuilder; |
323 | 344 |
324 final List<LinkedEditSuggestion> suggestions = <LinkedEditSuggestion>[]; | 345 final List<LinkedEditSuggestion> suggestions = <LinkedEditSuggestion>[]; |
(...skipping 13 matching lines...) Expand all Loading... |
338 @override | 359 @override |
339 void write(String string) { | 360 void write(String string) { |
340 editBuilder.write(string); | 361 editBuilder.write(string); |
341 } | 362 } |
342 | 363 |
343 @override | 364 @override |
344 void writeln([String string]) { | 365 void writeln([String string]) { |
345 editBuilder.writeln(string); | 366 editBuilder.writeln(string); |
346 } | 367 } |
347 } | 368 } |
OLD | NEW |