Ali Afshar avatar Ali Afshar committed 58f874c

Start of commands and buffers

Comments (0)

Files changed (6)

+# -*- coding: utf-8 -*-
+"""
+    er.buffer
+    ~~~~~~~~~
+
+    Abstraction of a single buffer
+
+    :copyright: (c) 2010 by AUTHOR.
+    :license: GPL, see LICENSE_FILE for more details.
+"""
+
+import gtksourceview2
+
+
+class Buffer(object):
+
+    def __init__(self):
+        self._source_buf = gtksourceview2.Buffer()
+
+    @property
+    def _start_iter(self):
+        return self._source_buf.get_start_iter()
+
+    @property
+    def _end_iter(self):
+        return self._source_buf.get_end_iter()
+
+    @property
+    def _cursor_iter(self):
+        return self._source_buf.get_iter_at_mark(
+            self._source_buf.get_insert())
+
+    def set_text(self, s):
+        self._source_buf.set_text(s)
+
+    def get_text(self):
+        return self._source_buf.get_text(
+            self._start_iter, self._end_iter)
+
+    @property
+    def cursor_offset(self):
+        return self._cursor_iter.get_offset()
+
+    @property
+    def cursor_line(self):
+        return self._cursor_iter.get_line()
+
+
+# -*- coding: utf-8 -*-
+"""
+    er.cmds
+    ~~~~~~~
+
+    Er base commands.
+
+    :copyright: (c) 2010 by er authors.
+    :license: GPLv3, see LICENSE.txt for more details.
+"""
+
+_er_registry = {}
+
+
+
+class Command(object):
+    """
+    A command instance, to be used as a decorator.
+    """
+
+    def __init__(self, name=None):
+        self.name = name
+
+    def __call__(self, f):
+        self.f = f
+        if self.name is None:
+            self.name = f.func_name
+        _er_registry[self.name] = self
+        return f
+
+    def execute(self, ctx, repeat, range):
+        return self.f(ctx)
+
+
+# External Api
+
+cmd = Command
+
+
+def get(name):
+    return _er_registry.get(name)
+
+
+
+
+class Context(object):
+
+    def __init__(self, e, b, args, kw):
+        self.e = e
+        self.b = b
+

tests/test_buffer.py

+
+from pygtkhelpers.utils import refresh_gui
+
+from er.buffer import Buffer
+
+def test_buffer_set_text():
+    b = Buffer()
+    b.set_text(u'Hello')
+    refresh_gui()
+    assert (b._source_buf.get_text(b._source_buf.get_start_iter(),
+                                   b._source_buf.get_end_iter())
+            == u'Hello')
+
+
+def test_buffer_get_text():
+    b = Buffer()
+    b._source_buf.set_text(u'Hello')
+    refresh_gui()
+    assert b.get_text() == u'Hello'
+
+
+def test_cursor_offset_empty():
+    b = Buffer()
+    assert b.cursor_offset == 0
+
+
+def test_cursor_offset_text_add():
+    b = Buffer()
+    b.set_text(u'Hello')
+    assert b.cursor_offset == 5
+
+
+def test_cursor_line_empty():
+    b = Buffer()
+    assert b.cursor_line == 0
+
+
+def test_cursor_line():
+    b = Buffer()
+    b.set_text(u'Hello')
+    assert b.cursor_line == 0
+
+
+def test_cursor_lines():
+    b = Buffer()
+    b.set_text(u'Hello\nHello')
+    print b.cursor_line
+    assert b.cursor_line == 1
+

tests/test_cmds.py

+
+
+from er.cmds import cmd as er_command, _er_registry, get as get_command
+
+
+def pytest_funcarg__reg(request):
+    _er_registry.clear()
+    return _er_registry
+
+
+def test_get_command():
+    def my_command():
+        return
+    er_command()(my_command)
+    assert get_command('my_command')
+
+
+def test_add_command(reg):
+    def my_command():
+        return
+    er_command()(my_command)
+    assert 'my_command' in reg
+
+
+def test_add_command_deco(reg):
+    @er_command()
+    def my_command():
+        return
+    assert 'my_command' in reg
+
+
+def test_add_command_name(reg):
+    def my_command():
+        return
+    er_command('y_command')(my_command)
+    assert 'y_command' in reg
+    assert 'my_command' not in reg
+
+
+def test_add_command_name_deco(reg):
+    @er_command('y_command')
+    def my_command():
+        return
+    assert 'y_command' in reg
+    assert 'my_command' not in reg

tests/test_ctx.py

+# -*- coding: utf-8 -*-
+"""
+    er.ctx
+    ~~~~~~
+
+    The context that er commands are run in
+
+    :copyright: (c) 2010 by er authors.
+    :license: GPLv3, see LICENSE for more details.
+"""
+
+from er.ctx import Context
+
+
+def test_context():
+    ctx = Context(None, None, None, None)
+    assert 0
+
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.