Ejemplo n.º 1
0
    def find_parent_scope(block):
        """
        Find parent scope, if the block is not a fold trigger.

        :param block: block from which the research will start
        """
        # if we moved up for more than n lines, just give up otherwise this
        # would take too much time.
        limit = 5000
        counter = 0
        original = block
        if not TextBlockHelper.is_fold_trigger(block):
            # search level of next non blank line
            while block.text().strip() == '' and block.isValid():
                block = block.next()
            ref_lvl = TextBlockHelper.get_fold_lvl(block) - 1
            block = original
            while (block.blockNumber() and counter < limit and
                   (not TextBlockHelper.is_fold_trigger(block) or
                    TextBlockHelper.get_fold_lvl(block) > ref_lvl)):
                counter += 1
                block = block.previous()
        if counter < limit:
            return block
        return None
Ejemplo n.º 2
0
    def find_parent_scope(block):
        """
        Find parent scope, if the block is not a fold trigger.

        :param block: block from which the research will start
        """
        # if we moved up for more than n lines, just give up otherwise this
        # would take too much time.
        limit = 5000
        counter = 0
        original = block
        if not TextBlockHelper.is_fold_trigger(block):
            # search level of next non blank line
            while block.text().strip() == '' and block.isValid():
                block = block.next()
            ref_lvl = TextBlockHelper.get_fold_lvl(block) - 1
            block = original
            while (block.blockNumber() and counter < limit
                   and (not TextBlockHelper.is_fold_trigger(block)
                        or TextBlockHelper.get_fold_lvl(block) > ref_lvl)):
                counter += 1
                block = block.previous()
        if counter < limit:
            return block
        return None
Ejemplo n.º 3
0
 def find_parent_scope(block):
     """Find parent scope, if the block is not a fold trigger."""
     original = block
     if not TextBlockHelper.is_fold_trigger(block):
         # search level of next non blank line
         while block.text().strip() == '' and block.isValid():
             block = block.next()
         ref_lvl = TextBlockHelper.get_fold_lvl(block) - 1
         block = original
         while (block.blockNumber()
                and (not TextBlockHelper.is_fold_trigger(block)
                     or TextBlockHelper.get_fold_lvl(block) > ref_lvl)):
             block = block.previous()
     return block
Ejemplo n.º 4
0
 def collapse_all(self):
     """
     Collapses all triggers and makes all blocks with fold level > 0
     invisible.
     """
     self._clear_block_deco()
     block = self.editor.document().firstBlock()
     last = self.editor.document().lastBlock()
     while block.isValid():
         lvl = TextBlockHelper.get_fold_lvl(block)
         trigger = TextBlockHelper.is_fold_trigger(block)
         if trigger:
             if lvl == 0:
                 self._show_previous_blank_lines(block)
             TextBlockHelper.set_collapsed(block, True)
         block.setVisible(lvl == 0)
         if block == last and block.text().strip() == '':
             block.setVisible(True)
             self._show_previous_blank_lines(block)
         block = block.next()
     self._refresh_editor_and_scrollbars()
     tc = self.editor.textCursor()
     tc.movePosition(tc.Start)
     self.editor.setTextCursor(tc)
     self.collapse_all_triggered.emit()
Ejemplo n.º 5
0
def _get_fold_levels(editor):
    """
    Return a list of all the class/function definition ranges.

    Parameters
    ----------
    editor : :class:`spyder.plugins.editor.widgets.codeeditor.CodeEditor`

    Returns
    -------
    folds : list of :class:`FoldScopeHelper`
        A list of all the class or function defintion fold points.
    """

    folds = []
    parents = []
    prev = None

    for oedata in editor.outlineexplorer_data_list():
        if TextBlockHelper.is_fold_trigger(oedata.block):
            try:
                if oedata.def_type in (OED.CLASS, OED.FUNCTION):
                    fsh = FoldScopeHelper(FoldScope(oedata.block), oedata)

                    # Determine the parents of the item using a stack.
                    _adjust_parent_stack(fsh, prev, parents)

                    # Update the parents of this FoldScopeHelper item
                    fsh.parents = copy.copy(parents)
                    folds.append(fsh)
                    prev = fsh
            except KeyError:
                pass
    return folds
Ejemplo n.º 6
0
 def _on_key_pressed(self, event):
     """
     Override key press to select the current scope if the user wants
     to deleted a folded scope (without selecting it).
     """
     delete_request = event.key() in [Qt.Key_Backspace, Qt.Key_Delete]
     if event.text() or delete_request:
         cursor = self.editor.textCursor()
         if cursor.hasSelection():
             # change selection to encompass the whole scope.
             positions_to_check = cursor.selectionStart(
             ), cursor.selectionEnd()
         else:
             positions_to_check = (cursor.position(), )
         for pos in positions_to_check:
             block = self.editor.document().findBlock(pos)
             th = TextBlockHelper()
             if th.is_fold_trigger(block) and th.is_collapsed(block):
                 self.toggle_fold_trigger(block)
                 if delete_request and cursor.hasSelection():
                     scope = FoldScope(self.find_parent_scope(block))
                     tc = TextHelper(
                         self.editor).select_lines(*scope.get_range())
                     if tc.selectionStart() > cursor.selectionStart():
                         start = cursor.selectionStart()
                     else:
                         start = tc.selectionStart()
                     if tc.selectionEnd() < cursor.selectionEnd():
                         end = cursor.selectionEnd()
                     else:
                         end = tc.selectionEnd()
                     tc.setPosition(start)
                     tc.setPosition(end, tc.KeepAnchor)
                     self.editor.setTextCursor(tc)
Ejemplo n.º 7
0
 def unfold(self):
     """Unfolds the region."""
     # set all direct child blocks which are not triggers to be visible
     self._trigger.setVisible(True)
     TextBlockHelper.set_collapsed(self._trigger, False)
     for block in self.blocks(ignore_blank_lines=False):
         block.setVisible(True)
         if TextBlockHelper.is_fold_trigger(block):
             TextBlockHelper.set_collapsed(block, False)
Ejemplo n.º 8
0
 def unfold(self):
     """Unfolds the region."""
     # set all direct child blocks which are not triggers to be visible
     self._trigger.setVisible(True)
     TextBlockHelper.set_collapsed(self._trigger, False)
     for block in self.blocks(ignore_blank_lines=False):
         block.setVisible(True)
         if TextBlockHelper.is_fold_trigger(block):
             TextBlockHelper.set_collapsed(block, False)
Ejemplo n.º 9
0
    def process_block(self, current_block, previous_block, text):
        """
        Processes a block and setup its folding info.

        This method call ``detect_fold_level`` and handles most of the tricky
        corner cases so that all you have to do is focus on getting the proper
        fold level foreach meaningful block, skipping the blank ones.

        :param current_block: current block to process
        :param previous_block: previous block
        :param text: current block text
        """
        prev_fold_level = TextBlockHelper.get_fold_lvl(previous_block)
        if text.strip() == '' or self.editor.is_comment(current_block):
            # blank or comment line always have the same level
            # as the previous line
            fold_level = prev_fold_level
        else:
            fold_level = self.detect_fold_level(
                previous_block, current_block)
            if fold_level > self.limit:
                fold_level = self.limit

        prev_fold_level = TextBlockHelper.get_fold_lvl(previous_block)

        if fold_level > prev_fold_level:
            # apply on previous blank or comment lines
            block = current_block.previous()
            while block.isValid() and (block.text().strip() == ''
                                       or self.editor.is_comment(block)):
                TextBlockHelper.set_fold_lvl(block, fold_level)
                block = block.previous()
            TextBlockHelper.set_fold_trigger(
                block, True)

        # update block fold level
        if text.strip() and not self.editor.is_comment(previous_block):
            TextBlockHelper.set_fold_trigger(
                previous_block, fold_level > prev_fold_level)
        TextBlockHelper.set_fold_lvl(current_block, fold_level)

        # user pressed enter at the beginning of a fold trigger line
        # the previous blank or comment line will keep the trigger state
        # and the new line (which actually contains the trigger) must use
        # the prev state (and prev state must then be reset).
        prev = current_block.previous()  # real prev block (may be blank)
        if (prev and prev.isValid() and
            (prev.text().strip() == '' or self.editor.is_comment(prev)) and
                TextBlockHelper.is_fold_trigger(prev)):
            # prev line has the correct trigger fold state
            TextBlockHelper.set_collapsed(
                current_block, TextBlockHelper.is_collapsed(
                    prev))
            # make empty or comment line not a trigger
            TextBlockHelper.set_fold_trigger(prev, False)
            TextBlockHelper.set_collapsed(prev, False)
Ejemplo n.º 10
0
 def child_regions(self):
     """This generator generates the list of direct child regions."""
     start, end = self.get_range()
     block = self._trigger.next()
     ref_lvl = self.scope_level
     while block.blockNumber() <= end and block.isValid():
         lvl = TextBlockHelper.get_fold_lvl(block)
         trigger = TextBlockHelper.is_fold_trigger(block)
         if lvl == ref_lvl and trigger:
             yield FoldScope(block)
         block = block.next()
Ejemplo n.º 11
0
 def child_regions(self):
     """This generator generates the list of direct child regions."""
     start, end = self.get_range()
     block = self._trigger.next()
     ref_lvl = self.scope_level
     while block.blockNumber() <= end and block.isValid():
         lvl = TextBlockHelper.get_fold_lvl(block)
         trigger = TextBlockHelper.is_fold_trigger(block)
         if lvl == ref_lvl and trigger:
             yield FoldScope(block)
         block = block.next()
Ejemplo n.º 12
0
    def __init__(self, block):
        """
        Create a fold-able region from a fold trigger block.

        :param block: The block **must** be a fold trigger.
        :type block: QTextBlock

        :raise: `ValueError` if the text block is not a fold trigger.
        """
        if not TextBlockHelper.is_fold_trigger(block):
            raise ValueError('Not a fold trigger')
        self._trigger = block
Ejemplo n.º 13
0
    def __init__(self, block):
        """
        Create a fold-able region from a fold trigger block.

        :param block: The block **must** be a fold trigger.
        :type block: QTextBlock

        :raise: `ValueError` if the text block is not a fold trigger.
        """
        if not TextBlockHelper.is_fold_trigger(block):
            raise ValueError('Not a fold trigger')
        self._trigger = block
Ejemplo n.º 14
0
    def toggle_fold_trigger(self, block):
        """
        Toggle a fold trigger block (expand or collapse it).

        :param block: The QTextBlock to expand/collapse
        """
        if not TextBlockHelper.is_fold_trigger(block):
            return
        region = FoldScope(block)
        if region.collapsed:
            region.unfold()
            if self._mouse_over_line is not None:
                self._decorate_block(*region.get_range())
        else:
            region.fold()
            self._clear_scope_decos()
        self._refresh_editor_and_scrollbars()
        self.trigger_state_changed.emit(region._trigger, region.collapsed)
Ejemplo n.º 15
0
    def parent(self):
        """
        Return the parent scope.

        :return: FoldScope or None
        """
        if TextBlockHelper.get_fold_lvl(self._trigger) > 0 and \
                self._trigger.blockNumber():
            block = self._trigger.previous()
            ref_lvl = self.trigger_level - 1
            while (block.blockNumber() and
                    (not TextBlockHelper.is_fold_trigger(block) or
                     TextBlockHelper.get_fold_lvl(block) > ref_lvl)):
                block = block.previous()
            try:
                return FoldScope(block)
            except ValueError:
                return None
        return None
Ejemplo n.º 16
0
    def parent(self):
        """
        Return the parent scope.

        :return: FoldScope or None
        """
        if TextBlockHelper.get_fold_lvl(self._trigger) > 0 and \
                self._trigger.blockNumber():
            block = self._trigger.previous()
            ref_lvl = self.trigger_level - 1
            while (block.blockNumber()
                   and (not TextBlockHelper.is_fold_trigger(block)
                        or TextBlockHelper.get_fold_lvl(block) > ref_lvl)):
                block = block.previous()
            try:
                return FoldScope(block)
            except ValueError:
                return None
        return None
Ejemplo n.º 17
0
def _get_fold_levels(editor):
    """
    Return a list of all the class/function definition ranges.

    Parameters
    ----------
    editor : :class:`spyder.plugins.editor.widgets.codeeditor.CodeEditor`

    Returns
    -------
    folds : list of :class:`FoldScopeHelper`
        A list of all the class or function defintion fold points.
    """
    block = editor.document().firstBlock()
    oed = editor.get_outlineexplorer_data()

    folds = []
    parents = []
    prev = None

    while block.isValid():
        if TextBlockHelper.is_fold_trigger(block):
            try:
                data = oed[block.firstLineNumber()]

                if data.def_type in (OED.CLASS, OED.FUNCTION):
                    fsh = FoldScopeHelper(FoldScope(block), data)

                    # Determine the parents of the item using a stack.
                    _adjust_parent_stack(fsh, prev, parents)

                    # Update the parents of this FoldScopeHelper item
                    fsh.parents = copy.copy(parents)
                    folds.append(fsh)
                    prev = fsh
            except KeyError:
                pass

        block = block.next()

    return folds
Ejemplo n.º 18
0
 def paintEvent(self, event):
     # Paints the fold indicators and the possible fold region background
     # on the folding panel.
     super(FoldingPanel, self).paintEvent(event)
     painter = QPainter(self)
     # Draw background over the selected non collapsed fold region
     if self._mouse_over_line is not None:
         block = self.editor.document().findBlockByNumber(
             self._mouse_over_line)
         try:
             self._draw_fold_region_background(block, painter)
         except ValueError:
             pass
     # Draw fold triggers
     for top_position, line_number, block in self.editor.visible_blocks:
         if TextBlockHelper.is_fold_trigger(block):
             collapsed = TextBlockHelper.is_collapsed(block)
             mouse_over = self._mouse_over_line == line_number
             self._draw_fold_indicator(top_position, mouse_over, collapsed,
                                       painter)
             if collapsed:
                 # check if the block already has a decoration, it might
                 # have been folded by the parent editor/document in the
                 # case of cloned editor
                 for deco in self._block_decos:
                     if deco.block == block:
                         # no need to add a deco, just go to the next block
                         break
                 else:
                     self._add_fold_decoration(block, FoldScope(block))
             else:
                 for deco in self._block_decos:
                     # check if the block decoration has been removed, it
                     # might have been unfolded by the parent
                     # editor/document in the case of cloned editor
                     if deco.block == block:
                         # remove it and
                         self._block_decos.remove(deco)
                         self.editor.decorations.remove(deco)
                         del deco
                         break
Ejemplo n.º 19
0
def _get_fold_levels(editor):
    """
    Return a list of all the class/function definition ranges.

    Parameters
    ----------
    editor : :class:`spyder.plugins.editor.widgets.codeeditor.CodeEditor`

    Returns
    -------
    folds : list of :class:`FoldScopeHelper`
        A list of all the class or function defintion fold points.
    """
    block = editor.document().firstBlock()
    oed = editor.get_outlineexplorer_data()

    folds = []
    parents = []
    prev = None

    while block.isValid():
        if TextBlockHelper.is_fold_trigger(block):
            try:
                data = oed[block.firstLineNumber()]

                if data.def_type in (OED.CLASS, OED.FUNCTION):
                    fsh = FoldScopeHelper(FoldScope(block), data)

                    # Determine the parents of the item using a stack.
                    _adjust_parent_stack(fsh, prev, parents)

                    # Update the parents of this FoldScopeHelper item
                    fsh.parents = copy.copy(parents)
                    folds.append(fsh)
                    prev = fsh
            except KeyError:
                pass

        block = block.next()

    return folds
Ejemplo n.º 20
0
    def _highlight_caret_scope(self):
        """
        Highlight the scope of the current caret position.

        This get called only if :attr:`
        spyder.widgets.panels.FoldingPanel.highlight_care_scope` is True.
        """
        cursor = self.editor.textCursor()
        block_nbr = cursor.blockNumber()
        if self._block_nbr != block_nbr:
            block = FoldScope.find_parent_scope(
                self.editor.textCursor().block())
            try:
                s = FoldScope(block)
            except ValueError:
                self._clear_scope_decos()
            else:
                self._mouse_over_line = block.blockNumber()
                if TextBlockHelper.is_fold_trigger(block):
                    self._highlight_block(block)
        self._block_nbr = block_nbr
Ejemplo n.º 21
0
    def mouseMoveEvent(self, event):
        """
        Detect mouser over indicator and highlight the current scope in the
        editor (up and down decoration arround the foldable text when the mouse
        is over an indicator).

        :param event: event
        """
        super(FoldingPanel, self).mouseMoveEvent(event)
        th = TextHelper(self.editor)
        line = th.line_nbr_from_position(event.pos().y())
        if line >= 0:
            block = FoldScope.find_parent_scope(
                self.editor.document().findBlockByNumber(line - 1))
            if TextBlockHelper.is_fold_trigger(block):
                if self._mouse_over_line is None:
                    # mouse enter fold scope
                    QApplication.setOverrideCursor(
                        QCursor(Qt.PointingHandCursor))
                if self._mouse_over_line != block.blockNumber() and \
                        self._mouse_over_line is not None:
                    # fold scope changed, a previous block was highlighter so
                    # we quickly update our highlighting
                    self._mouse_over_line = block.blockNumber()
                    self._highlight_block(block)
                else:
                    # same fold scope, request highlight
                    self._mouse_over_line = block.blockNumber()
                    self._highlight_runner.request_job(self._highlight_block,
                                                       block)
                self._highight_block = block
            else:
                # no fold scope to highlight, cancel any pending requests
                self._highlight_runner.cancel_requests()
                self._mouse_over_line = None
                QApplication.restoreOverrideCursor()
            self.repaint()