Commits

Ronny Pfannschmidt committed 0903409

some more fixes

  • Participants
  • Parent commits dbaebf6

Comments (0)

Files changed (2)

File tests/test_input.py

     def press_keys(self, input):
         for press in split_input(input):
             result = self.keypress(press)
-            if result is not None:
+            if result.next_tree is not None:
                 yield result
 
 def test_split_input():
     assert call.call
     final = machine.keypress("d")
     assert final.final
-    assert final.last_match is call
+    assert final.previous_actor is call
+
+    call, final = machine.press_keys("22d2d")
+    assert call.count == 22
+    assert final.count == 2
+    assert final.previous_actor is call
+

File vimish/input.py

 
 class Match(object):
-    def __init__(self, next_tree=None, call=None, reset=None, final=None, last_match=None):
+    __slots__ = "next_tree", "call", "reset", "final", "_last_match", "count"
+    def __init__(self,
+                 next_tree=None,
+                 call=None,
+                 reset=None,
+                 final=None,
+                 last_match=None,
+                 count=None):
         self.next_tree = next_tree
         self.call = call
         self.reset = reset
         self.final = final
-        self.last_match = last_match
+        self._last_match = last_match
+        self.count = count
+
+    def __repr__(self):
+        return "<Match %s>" %' '.join("%s=%r"%(x, getattr(self, x)) for x in self.__slots__ if x[0]!='_')
+
+    @property
+    def previous_actor(self):
+        def components(x):
+            while x is not None:
+                x = x._last_match
+                yield x
+
+        for c in components(self):
+            if c.next_tree is not None:
+                return c
 
 
 class numbered(object):
         self.args = kw
 
     def match(self, input, last_match):
-        print self
+        nondigits = input.lstrip("0123456789")
+        digits = input[:-len(nondigits)] if nondigits else input
+        number = int(digits) if digits else 1
+        if not nondigits:
+            return Match(count=number, last_match=last_match)
         for cmd, tree in self.commands:
-            print cmd, tree, input
-            if cmd == input:
-                print "hit"
-                #XXX: aliases
-                print self.args
-                print last_match.call
+            if cmd == nondigits:
                 return Match(
                         next_tree=tree,
                         final=tree.final,
-                        last_match = last_match,
+                        count=number,
+                        last_match=last_match,
                         call=(tree.args.get('call') or 
                               last_match.call))
     
         return Match(reset=any(
                         cmd[0].startswith(input)
-                        for cmd in self.commands))
+                        for cmd in self.commands),
+                     last_match=last_match)
 
     def __repr__(self):
         return "<Numbered %r (%s)>"%(
 eol = "eol"
 line = "line"
 
+
+
 normal = numbered(
     ('d', numbered(
         ('d', alias('Vl')),
         if match.next_tree is not None:
             self.current = match.next_tree
             self.input = ""
-        elif match.reset or match.final:
+
+        if match.reset or match.final:
             self.current = self.tree
             self.input = ""
             self.match = Match()