Ronny Pfannschmidt avatar Ronny Pfannschmidt committed 7d1ce3a

more slicing tests + support

Comments (0)

Files changed (2)

tests/test_engine.py

     assert len(buffer) == 1
 
 
-def test_buffer_slice(engine):
+def test_buffer_setitem_and_getitem(engine):
     buffer = engine.create_buffer()
     buffer.text = "Test text\n\nline2"
     assert len(buffer) == 3
 
     assert buffer[1] == "Test\n"
     assert buffer[2] == "line2"
+
+
+def test_buffer_get_slice_and_set_slice(engine):
+    buffer = engine.create_buffer()
+    buffer.text = "Test\nslicing\nwell"
+    assert buffer[:2] == ["Test\n", "slicing\n"]
+    assert buffer[2:] == ['well']
+
+
+    buffer[:2] = ['more\n', 'slicing\n', 'helps\n']
+    assert buffer[2] == "helps\n"
+
+
         return self.text_buffer.get_line_count()
 
     def __getitem__(self, item):
-        start = self.text_buffer.get_iter_at_line(item)
-        # the last line doesnt end with \n
-        if item+1 < len(self):
+        if isinstance(item, int):
+            start = self.text_buffer.get_iter_at_line(item)
+            # the last line doesnt end with \n
+            if item+1 < len(self):
+                end = self.text_buffer.get_iter_at_line(item +1)
+            else:
+                end = self.text_buffer.get_end_iter()
+            return self.text_buffer.get_slice(start, end)
+        else:
+            if item.start is None:
+                start = self.text_buffer.get_start_iter()
+            else:
+                start = self.text_buffer.get_iter_at_line(item.start)
+
+            if item.stop is None or item.stop >= len(self):
+                end = self.text_buffer.get_end_iter()
+            else:
+                end = self.text_buffer.get_iter_at_line(item.stop)
+
+            slice = self.text_buffer.get_slice(start, end)
+            return slice.splitlines(True)
+
+
+    def __setitem__(self, item, value):
+        if isinstance(value, list):
+            #XXX: smarter?
+            value = ''.join(value)
+
+
+        if isinstance(item, int):
+            start = self.text_buffer.get_iter_at_line(item)
             end = self.text_buffer.get_iter_at_line(item +1)
         else:
-            end = self.text_buffer.get_end_iter()
-        return self.text_buffer.get_slice(start, end)
+            if item.start is None:
+                start = self.text_buffer.get_start_iter()
 
-    def __setitem__(self, item, value):
-        start = self.text_buffer.get_iter_at_line(item)
-        end = self.text_buffer.get_iter_at_line(item +1)
+            end = self.text_buffer.get_iter_at_line(item.stop)
 
         self.text_buffer.select_range(start, end)
         self.text_buffer.delete_selection(False, False)
 
-        start = self.text_buffer.get_iter_at_line(item)
+        if isinstance(item, int):
+            start = self.text_buffer.get_iter_at_line(item)
+        elif item.start is None:
+            start = self.text_buffer.get_start_iter()
+        else:
+            start = self.text_buffer.get_iter_at_line(item.start)
         self.text_buffer.insert(start, value)
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.