def test_000(): ns = init_testing(False) frac = Fraction(T(u'Zähler'), T(u'Nenner')) factory = Builder(TextModel()) # not very nice box = factory.Fraction_handler(frac, 0, length(frac))[0] assert len(box) == length(frac) assert check_box(box, frac) assert check_box(box.nominator) assert check_box(box.denominator) model = ns['model'] #model.insert_text(0, "1234") model.insert_fraction(len(model)) #model.texel.dump() model.insert_text(0, "x") model.remove(0, 1) #model.texel.dump() model.insert_text(1, "x") model.remove(1, 2) #model.texel.dump() layout = ns['view'].layout assert check_box(layout)
def Fraction_handler(self, texel, i1, i2): denominator = texel.childs[1] denomboxes = self.create_boxes(denominator, 0, length(denominator)) nominator = texel.childs[3] nomboxes = self.create_boxes(nominator, 0, length(nominator)) r = FractionBox(denomboxes, nomboxes, style=get_style(texel, 0), device=self.device) return [r]
def test_00(): "cell" ns = init_testing(False) cell = Cell(Text(u'1234567890'), Text(u'abcdefghij')) assert length(cell.input) == 10 assert length(cell.output) == 10 assert length(cell) == 23 texel = grouped(insert(cell, 1, [Text(u'x')])) assert texeltree.get_text(texel)[1:2] == u'x'
def execute(self): i0, cell = self.find_cell() n = length(cell) new = cell.execute() assert i0 >= 0 assert i0 + n <= len(self.model) infos = [] infos.append(self._remove(i0, i0 + n)) self.model.insert(i0, mk_textmodel(new)) infos.append((self._remove, i0, i0 + length(new))) self.add_undo(infos) self.adjust_viewport()
def test_03(): "find_cell" tmp1 = TextModel(u'for a in range(3):\n print a') tmp2 = TextModel(u'for a in range(10):\n print a') cell1 = Cell(tmp1.texel, Text(u'')) cell2 = Cell(tmp2.texel, Text(u'')) model = TextModel('') model.insert(len(model), mk_textmodel(cell1)) model.insert(len(model), mk_textmodel(cell2)) assert find_cell(model.texel, 1) == (0, cell1) assert find_cell(model.texel, length(cell1) - 1) == (0, cell1) assert find_cell(model.texel, length(cell1)) == (length(cell1), cell2) assert find_cell(model.texel, length(cell1) + 5) == (length(cell1), cell2)
def Cell_handler(self, texel, i1, i2): assert i2 <= length(texel) #print "Cell handler: (i1, i2)=", (i1, i2) #dump_range(texel, 0, len(texel)) tmp, (j1, j2, inp), tmp, (k1, k2, outp), tmp = iter_childs(texel) if i1 < j2 and j1 < i2: if self._has_temp: t1, t2 = self._temp_range i0, tmp = find_cell(self.model.texel, t1) assert tmp is texel from textmodel.texeltree import insert, grouped _rest, _temp = texel.takeout(t1 - i0, t2 - i0) cell = grouped(_rest).colorize() cell = grouped(insert(cell, t1 - i0, _temp)) else: cell = texel.colorize() inbox = self.create_parstack(cell.input, add_newline=True) if i1 < k2 and k1 < i2: outbox = self.create_parstack(texel.output, add_newline=True) if j1 <= i1 <= i2 <= k1: #print "in inbox: (i1,i2)=", (i1, i2), "(j1, j2)=", (j1, j2) assert j1 == i1 and i2 == j2 + 1 return [inbox] if k1 <= i1 <= i2 <= k2 + 1: assert k1 == i1 and i2 == k2 + 1 return [outbox] assert i1 == 0 and i2 == k2 + 1 cell = CellBox(inbox, outbox, number=texel.number, device=self.device) return [cell]
def insert(self, i, textmodel): needscell = True try: i0, cell = self.find_cell() if not (i == i0 or i == i0 + length(cell)): needscell = False except NotFound: pass try: find_cell(textmodel.texel, 0) hascell = True except NotFound: hascell = False if needscell and not hascell: cell = Cell(NULL_TEXEL, NULL_TEXEL) self.model.insert(i, mk_textmodel(cell)) info = self._remove, i, i + length(cell) self.add_undo(info) i = i + 1 _WXTextView.insert(self, i, textmodel)
class Cell(Container): def __init__(self, input, output, number=0, **kwargs): assert isinstance(input, Texel) assert isinstance(output, Texel) self.input = input self.output = output self.number = number self.childs = [NL, input, NL, output, NL] self.compute_weights() def get_kwds(self): kwds = Container.get_kwds(self) kwds['number'] = self.number return kwds def execute(self): buf = Buffer() code = texeltree.get_text(self.input) INTERPRETER.execute(code, buf.output) number = INTERPRETER.counter return self.__class__(self.input, buf.model.texel, number) def colorize(self): if 1: return self # colorize text = texeltree.get_text(self.input) from textmodel.textmodel import pycolorize try: colorized = pycolorize(text).texel except Exception, e: return self assert length(colorized) == length(self.input) r = grouped(self.replace_child(1, len(self.input) + 1, [colorized])) assert length(r) == length(self) return r
def test_10(): "Factory" ns = init_testing(False) cell = Cell(Text(u'a'), Text(u'b')) factory = Builder(TextModel('')) boxes = factory.create_all(cell) assert len(boxes) == 1 cellbox = boxes[0] assert len(cellbox) == 5 assert length(cell) == 5 check_box(cellbox) check_box(cellbox.input) check_box(cellbox.output) return ns
def test_11(): ns = init_testing(redirect=True) model = ns['model'] model.remove(0, len(model)) tmp = TextModel(u'for a in range(16):\n print a') cell = Cell(tmp.texel, Text(u'')) model.insert(len(model), mk_textmodel(cell)) assert model.index2position(0) == (0, 0) assert model.index2position(1) == (1, 0) cell = model.texel assert find_cell(model.texel, 1) == (0, cell) view = ns['view'] view.index = 1 #print model.texel view.execute() check_box(view.builder._layout, model.texel) #view.layout.dump_boxes(0, 0, 0) #assert inside_cell(view.layout, 68, 68) model.insert_text(68, u'x') assert model.get_text()[65:71] == '14\nx15' #view.layout.dump_boxes(0, 0, 0) model.remove(68, 69) #view.layout.dump_boxes(0, 0, 0) assert model.get_text()[65:70] == '14\n15' model.insert(0, mk_textmodel(cell)) model.remove(0, length(cell)) # insert new cell, insert input view.insert(0, TextModel("a=1")) # insert output view.insert(5, TextModel("x")) # remove the complete input-part model.remove(1, 4) # remove the complete output-part model.remove(2, 3) return ns
def handle_action(self, action, shift=False): if action != 'complete': self.clear_temp() return _WXTextView.handle_action(self, action, shift) try: i0, cell = self.find_cell() except NotFound: return index = self.index if index <= i0 or index >= i0 + length(cell): return if self.has_temp(): self.clear_temp() maxoptions = 2000 else: maxoptions = 200 word = self.get_word(index) completer = rlcompleter.Completer(INTERPRETER.namespace) options = set() i = 0 while True: option = completer.complete(word, i) i += 1 if option is None or len(options) == maxoptions: break options.add(option) if not options: self.print_temp("\n[No completion]\n") else: completion = reduce(common, options)[len(word):] if completion and len(options) != maxoptions: self.model.insert_text(index, completion) info = self._remove, index, index + len(completion) self.add_undo(info) index += len(completion) else: options = list(sorted(options)) s = ', '.join(options) s = s.replace('(', '') # I don't like the bracket if len(options) == maxoptions: s += ' ... ' self.print_temp('\n' + s + '\n') self.index = index
def create_parstack(self, texel, add_newline=False): l = self.create_paragraphs(texel, 0, length(texel), add_newline=add_newline) return ParagraphStack(l, device=self.device)
def Root_handler(self, texel, i1, i2): content = texel.childs[1] boxes = self.create_boxes(content, 0, length(content)) return [RootBox(boxes, device=self.device)]