| 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 library services.src.correction.source_buffer; | |
| 6 | |
| 7 import 'package:analysis_services/correction/change.dart'; | |
| 8 import 'package:analyzer/src/generated/source.dart'; | |
| 9 | |
| 10 | |
| 11 /** | |
| 12 * Helper for building Dart source with linked positions. | |
| 13 */ | |
| 14 class SourceBuilder { | |
| 15 final String file; | |
| 16 final int offset; | |
| 17 final StringBuffer _buffer = new StringBuffer(); | |
| 18 | |
| 19 final List<LinkedEditGroup> linkedPositionGroups = <LinkedEditGroup>[]; | |
| 20 LinkedEditGroup _currentLinkedPositionGroup; | |
| 21 int _currentPositionStart; | |
| 22 int _exitOffset; | |
| 23 | |
| 24 SourceBuilder(this.file, this.offset); | |
| 25 | |
| 26 SourceBuilder.buffer() | |
| 27 : file = null, | |
| 28 offset = 0; | |
| 29 | |
| 30 /** | |
| 31 * Returns the exit offset, maybe `null` if not set. | |
| 32 */ | |
| 33 int get exitOffset { | |
| 34 if (_exitOffset == null) { | |
| 35 return null; | |
| 36 } | |
| 37 return offset + _exitOffset; | |
| 38 } | |
| 39 | |
| 40 int get length => _buffer.length; | |
| 41 | |
| 42 void addSuggestion(LinkedEditSuggestionKind kind, String value) { | |
| 43 var suggestion = new LinkedEditSuggestion(kind, value); | |
| 44 _currentLinkedPositionGroup.addSuggestion(suggestion); | |
| 45 } | |
| 46 | |
| 47 void addSuggestions(LinkedEditSuggestionKind kind, List<String> values) { | |
| 48 values.forEach((value) => addSuggestion(kind, value)); | |
| 49 } | |
| 50 | |
| 51 /** | |
| 52 * Appends [s] to the buffer. | |
| 53 */ | |
| 54 SourceBuilder append(String s) { | |
| 55 _buffer.write(s); | |
| 56 return this; | |
| 57 } | |
| 58 | |
| 59 /** | |
| 60 * Ends position started using [startPosition]. | |
| 61 */ | |
| 62 void endPosition() { | |
| 63 assert(_currentLinkedPositionGroup != null); | |
| 64 _addPosition(); | |
| 65 _currentLinkedPositionGroup = null; | |
| 66 } | |
| 67 | |
| 68 /** | |
| 69 * Marks the current offset as an "exit" one. | |
| 70 */ | |
| 71 void setExitOffset() { | |
| 72 _exitOffset = _buffer.length; | |
| 73 } | |
| 74 | |
| 75 /** | |
| 76 * Marks start of a new linked position for the group with the given ID. | |
| 77 */ | |
| 78 void startPosition(String groupId) { | |
| 79 assert(_currentLinkedPositionGroup == null); | |
| 80 for (LinkedEditGroup position in linkedPositionGroups) { | |
| 81 if (position.id == groupId) { | |
| 82 _currentLinkedPositionGroup = position; | |
| 83 break; | |
| 84 } | |
| 85 } | |
| 86 if (_currentLinkedPositionGroup == null) { | |
| 87 _currentLinkedPositionGroup = new LinkedEditGroup(groupId); | |
| 88 linkedPositionGroups.add(_currentLinkedPositionGroup); | |
| 89 } | |
| 90 _currentPositionStart = _buffer.length; | |
| 91 } | |
| 92 | |
| 93 @override | |
| 94 String toString() => _buffer.toString(); | |
| 95 | |
| 96 /** | |
| 97 * Adds position location [SourceRange] using current fields. | |
| 98 */ | |
| 99 void _addPosition() { | |
| 100 int start = offset + _currentPositionStart; | |
| 101 int end = offset + _buffer.length; | |
| 102 int length = end - start; | |
| 103 Position position = new Position(file, start); | |
| 104 _currentLinkedPositionGroup.addPosition(position, length); | |
| 105 } | |
| 106 } | |
| OLD | NEW |