Exemplo n.º 1
0
        def fset(self, text):
            self._current_text = TextBuffer(text)

            # All our childs are set to "" so they
            # do no longer disturb anything that mirrors it
            for c in self._childs:
                c.current_text = ""
            self._childs = []
            self._tabstops = {}
Exemplo n.º 2
0
    def __init__(self, parent, start, end, initial_text):
        self._start = start
        self._end = end

        self._parent = parent

        self._childs = []
        self._tabstops = {}

        if parent is not None:
            parent._add_child(self)

        self._current_text = TextBuffer(initial_text)

        self._cts = 0
Exemplo n.º 3
0
        def fset(self, text):
            self._current_text = TextBuffer(text)

            # All our childs are set to "" so they
            # do no longer disturb anything that mirrors it
            for c in self._childs:
                c.current_text = ""
            self._childs = []
            self._tabstops = {}
Exemplo n.º 4
0
    def __init__(self, parent, token, end = None, initial_text = ""):
        self._parent = parent

        if end is not None: # Took 4 arguments
            self._start = token
            self._end = end
            self._current_text = TextBuffer(initial_text)
        else: # Initialize from token
            self._start = token.start
            self._end = token.end
            self._current_text = TextBuffer(token.initial_text)

        self._childs = []
        self._tabstops = {}

        if parent is not None:
            parent._add_child(self)

        self._cts = 0
Exemplo n.º 5
0
    def __init__(self, parent, token, end=None, initial_text=""):
        self._parent = parent

        if end is not None:  # Took 4 arguments
            self._start = token
            self._end = end
            self._current_text = TextBuffer(initial_text)
        else:  # Initialize from token
            self._start = token.start
            self._end = token.end
            self._current_text = TextBuffer(token.initial_text)

        self._childs = []
        self._tabstops = {}

        if parent is not None:
            parent._add_child(self)

        self._cts = 0
Exemplo n.º 6
0
    def __init__(self, parent, start, end, initial_text):
        self._start = start
        self._end = end

        self._parent = parent

        self._childs = []
        self._tabstops = {}

        if parent is not None:
            parent._add_child(self)

        self._current_text = TextBuffer(initial_text)

        self._cts = 0
Exemplo n.º 7
0
class TextObject(CheapTotalOrdering):
    """
    This base class represents any object in the text
    that has a span in any ways
    """
    def __init__(self, parent, token, end = None, initial_text = ""):
        self._parent = parent

        if end is not None: # Took 4 arguments
            self._start = token
            self._end = end
            self._current_text = TextBuffer(initial_text)
        else: # Initialize from token
            self._start = token.start
            self._end = token.end
            self._current_text = TextBuffer(token.initial_text)

        self._childs = []
        self._tabstops = {}

        if parent is not None:
            parent._add_child(self)

        self._cts = 0

    def __cmp__(self, other):
        return self._start.__cmp__(other._start)

    ##############
    # PROPERTIES #
    ##############
    def current_text():
        def fget(self):
            return str(self._current_text)
        def fset(self, text):
            self._current_text = TextBuffer(text)

            # All our childs are set to "" so they
            # do no longer disturb anything that mirrors it
            for c in self._childs:
                c.current_text = ""
            self._childs = []
            self._tabstops = {}
        return locals()

    current_text = property(**current_text())
    def abs_start(self):
        if self._parent:
            ps = self._parent.abs_start
            if self._start.line == 0:
                return ps + self._start
            else:
                return Position(ps.line + self._start.line, self._start.col)
        return self._start
    abs_start = property(abs_start)

    def abs_end(self):
        if self._parent:
            ps = self._parent.abs_start
            if self._end.line == 0:
                return ps + self._end
            else:
                return Position(ps.line + self._end.line, self._end.col)

        return self._end
    abs_end = property(abs_end)

    def span(self):
        return Span(self._start, self._end)
    span = property(span)

    def start(self):
        return self._start
    start = property(start)

    def end(self):
        return self._end
    end = property(end)

    def abs_span(self):
        return Span(self.abs_start, self.abs_end)
    abs_span = property(abs_span)

    ####################
    # Public functions #
    ####################
    def update(self):
        def _update_childs(childs):
            for idx,c in childs:
                oldend = Position(c.end.line, c.end.col)

                new_end = c.update()

                moved_lines = new_end.line - oldend.line
                moved_cols = new_end.col - oldend.col

                self._current_text.replace_text(c.start, oldend, c._current_text)

                self._move_textobjects_behind(c.start, oldend, moved_lines,
                            moved_cols, idx)

        _update_childs((idx, c) for idx, c in enumerate(self._childs) if isinstance(c, TabStop))
        _update_childs((idx, c) for idx, c in enumerate(self._childs) if not isinstance(c, TabStop))

        self._do_update()

        new_end = self._current_text.calc_end(self._start)

        self._end = new_end

        return new_end

    def _get_next_tab(self, no):
        if not len(self._tabstops.keys()):
            return
        tno_max = max(self._tabstops.keys())

        posible_sol = []
        i = no + 1
        while i <= tno_max:
            if i in self._tabstops:
                posible_sol.append( (i, self._tabstops[i]) )
                break
            i += 1

        c = [ c._get_next_tab(no) for c in self._childs ]
        c = filter(lambda i: i, c)

        posible_sol += c

        if not len(posible_sol):
            return None

        return min(posible_sol)


    def _get_prev_tab(self, no):
        if not len(self._tabstops.keys()):
            return
        tno_min = min(self._tabstops.keys())

        posible_sol = []
        i = no - 1
        while i >= tno_min and i > 0:
            if i in self._tabstops:
                posible_sol.append( (i, self._tabstops[i]) )
                break
            i -= 1

        c = [ c._get_prev_tab(no) for c in self._childs ]
        c = filter(lambda i: i, c)

        posible_sol += c

        if not len(posible_sol):
            return None

        return max(posible_sol)

    ###############################
    # Private/Protected functions #
    ###############################
    def _do_update(self):
        pass

    def _move_textobjects_behind(self, start, end, lines, cols, obj_idx):
        if lines == 0 and cols == 0:
            return

        for idx,m in enumerate(self._childs[obj_idx+1:]):
            delta_lines = 0
            delta_cols_begin = 0
            delta_cols_end = 0

            if m.start.line > end.line:
                delta_lines = lines
            elif m.start.line == end.line:
                if m.start.col >= end.col:
                    if lines:
                        delta_lines = lines
                    delta_cols_begin = cols
                    if m.start.line == m.end.line:
                        delta_cols_end = cols
            m.start.line += delta_lines
            m.end.line += delta_lines
            m.start.col += delta_cols_begin
            m.end.col += delta_cols_end

    def _get_tabstop(self, requester, no):
        if no in self._tabstops:
            return self._tabstops[no]
        for c in self._childs:
            if c is requester:
                continue

            rv = c._get_tabstop(self, no)
            if rv is not None:
                return rv
        if self._parent and requester is not self._parent:
            return self._parent._get_tabstop(self, no)

    def _add_child(self,c):
        self._childs.append(c)
        self._childs.sort()

    def _add_tabstop(self, no, ts):
        self._tabstops[no] = ts
Exemplo n.º 8
0
class TextObject(CheapTotalOrdering):
    """
    This base class represents any object in the text
    that has a span in any ways
    """
    def __init__(self, parent, token, end=None, initial_text=""):
        self._parent = parent

        if end is not None:  # Took 4 arguments
            self._start = token
            self._end = end
            self._current_text = TextBuffer(initial_text)
        else:  # Initialize from token
            self._start = token.start
            self._end = token.end
            self._current_text = TextBuffer(token.initial_text)

        self._childs = []
        self._tabstops = {}

        if parent is not None:
            parent._add_child(self)

        self._cts = 0

    def __cmp__(self, other):
        return self._start.__cmp__(other._start)

    ##############
    # PROPERTIES #
    ##############
    def current_text():
        def fget(self):
            return str(self._current_text)

        def fset(self, text):
            self._current_text = TextBuffer(text)

            # All our childs are set to "" so they
            # do no longer disturb anything that mirrors it
            for c in self._childs:
                c.current_text = ""
            self._childs = []
            self._tabstops = {}

        return locals()

    current_text = property(**current_text())

    def abs_start(self):
        if self._parent:
            ps = self._parent.abs_start
            if self._start.line == 0:
                return ps + self._start
            else:
                return Position(ps.line + self._start.line, self._start.col)
        return self._start

    abs_start = property(abs_start)

    def abs_end(self):
        if self._parent:
            ps = self._parent.abs_start
            if self._end.line == 0:
                return ps + self._end
            else:
                return Position(ps.line + self._end.line, self._end.col)

        return self._end

    abs_end = property(abs_end)

    def span(self):
        return Span(self._start, self._end)

    span = property(span)

    def start(self):
        return self._start

    start = property(start)

    def end(self):
        return self._end

    end = property(end)

    def abs_span(self):
        return Span(self.abs_start, self.abs_end)

    abs_span = property(abs_span)

    ####################
    # Public functions #
    ####################
    def update(self):
        def _update_childs(childs):
            for idx, c in childs:
                oldend = Position(c.end.line, c.end.col)

                new_end = c.update()

                moved_lines = new_end.line - oldend.line
                moved_cols = new_end.col - oldend.col

                self._current_text.replace_text(c.start, oldend,
                                                c._current_text)

                self._move_textobjects_behind(c.start, oldend, moved_lines,
                                              moved_cols, idx)

        _update_childs((idx, c) for idx, c in enumerate(self._childs)
                       if isinstance(c, TabStop))
        _update_childs((idx, c) for idx, c in enumerate(self._childs)
                       if not isinstance(c, TabStop))

        self._do_update()

        new_end = self._current_text.calc_end(self._start)

        self._end = new_end

        return new_end

    def _get_next_tab(self, no):
        if not len(self._tabstops.keys()):
            return
        tno_max = max(self._tabstops.keys())

        posible_sol = []
        i = no + 1
        while i <= tno_max:
            if i in self._tabstops:
                posible_sol.append((i, self._tabstops[i]))
                break
            i += 1

        c = [c._get_next_tab(no) for c in self._childs]
        c = filter(lambda i: i, c)

        posible_sol += c

        if not len(posible_sol):
            return None

        return min(posible_sol)

    def _get_prev_tab(self, no):
        if not len(self._tabstops.keys()):
            return
        tno_min = min(self._tabstops.keys())

        posible_sol = []
        i = no - 1
        while i >= tno_min and i > 0:
            if i in self._tabstops:
                posible_sol.append((i, self._tabstops[i]))
                break
            i -= 1

        c = [c._get_prev_tab(no) for c in self._childs]
        c = filter(lambda i: i, c)

        posible_sol += c

        if not len(posible_sol):
            return None

        return max(posible_sol)

    ###############################
    # Private/Protected functions #
    ###############################
    def _do_update(self):
        pass

    def _move_textobjects_behind(self, start, end, lines, cols, obj_idx):
        if lines == 0 and cols == 0:
            return

        for idx, m in enumerate(self._childs[obj_idx + 1:]):
            delta_lines = 0
            delta_cols_begin = 0
            delta_cols_end = 0

            if m.start.line > end.line:
                delta_lines = lines
            elif m.start.line == end.line:
                if m.start.col >= end.col:
                    if lines:
                        delta_lines = lines
                    delta_cols_begin = cols
                    if m.start.line == m.end.line:
                        delta_cols_end = cols
            m.start.line += delta_lines
            m.end.line += delta_lines
            m.start.col += delta_cols_begin
            m.end.col += delta_cols_end

    def _get_tabstop(self, requester, no):
        if no in self._tabstops:
            return self._tabstops[no]
        for c in self._childs:
            if c is requester:
                continue

            rv = c._get_tabstop(self, no)
            if rv is not None:
                return rv
        if self._parent and requester is not self._parent:
            return self._parent._get_tabstop(self, no)

    def _add_child(self, c):
        self._childs.append(c)
        self._childs.sort()

    def _add_tabstop(self, no, ts):
        self._tabstops[no] = ts