Ver Fonte

split `splice` into `remove`, `insert` and `insertAll` for better readablity

xuty há 4 anos atrás
pai
commit
79e543fb9d
2 ficheiros alterados com 37 adições e 18 exclusões
  1. 3 3
      lib/buffer/buffer.dart
  2. 34 15
      lib/utli/circular_list.dart

+ 3 - 3
lib/buffer/buffer.dart

@@ -449,10 +449,10 @@ class Buffer {
     if (!isInScrollableRegion) {
       final index = convertViewLineToRawLine(_cursorX);
       final newLine = _newEmptyLine();
-      lines.splice(index, 0, [newLine]);
+      lines.insert(index, newLine);
     } else {
       final newLine = _newEmptyLine();
-      lines.splice(_cursorY, 0, [newLine]);
+      lines.insert(_cursorY, newLine);
     }
   }
 
@@ -475,7 +475,7 @@ class Buffer {
       return;
     }
 
-    lines.splice(index, 1, []);
+    lines.remove(index);
   }
 
   void resize(int newWidth, int newHeight) {

+ 34 - 15
lib/utli/circular_list.dart

@@ -97,25 +97,44 @@ class CircularList<T> {
     return _array[_getCyclicIndex(_length-- - 1)]!;
   }
 
-  /// Deletes and/or inserts items at a particular index (in that order).
-  void splice(int start, int deleteCount, List<T> items) {
-    // delete items
-    if (deleteCount > 0) {
-      for (int i = start; i < _length - deleteCount; i++)
-        _array[_getCyclicIndex(i)] = _array[_getCyclicIndex(i + deleteCount)];
-      length -= deleteCount;
+  /// Deletes item at [index].
+  void remove(int index, [int count = 1]) {
+    if (count > 0) {
+      for (var i = index; i < _length - count; i++) {
+        _array[_getCyclicIndex(i)] = _array[_getCyclicIndex(i + count)];
+      }
+      length -= count;
+    }
+  }
+
+  /// Inserts [item] at [index].
+  void insert(int index, T item) {
+    for (var i = _length - 1; i >= index; i--) {
+      _array[_getCyclicIndex(i + 1)] = _array[_getCyclicIndex(i)];
+    }
+
+    _array[_getCyclicIndex(index)] = item;
+
+    if (_length + 1 > _array.length) {
+      _startIndex += 1;
+      onTrimmed?.call(1);
+    } else {
+      _length++;
     }
-    if (items.length != 0) {
-      // add items
-      for (int i = _length - 1; i >= start; i--)
-        _array[_getCyclicIndex(i + items.length)] = _array[_getCyclicIndex(i)];
-      for (int i = 0; i < items.length; i++)
-        _array[_getCyclicIndex(start + i)] = items[i];
+  }
+
+  /// Inserts [items] at [index] in order.
+  void insertAll(int index, List<T> items) {
+    for (var i = _length - 1; i >= index; i--) {
+      _array[_getCyclicIndex(i + 1)] = _array[_getCyclicIndex(i)];
+    }
+
+    for (var i = 0; i < items.length; i++) {
+      _array[_getCyclicIndex(index + i)] = items[i];
     }
 
-    // Adjust length as needed
     if (_length + items.length > _array.length) {
-      int countToTrim = _length + items.length - _array.length;
+      final countToTrim = _length + items.length - _array.length;
       _startIndex += countToTrim;
       length = _array.length;
       onTrimmed?.call(countToTrim);