当前位置: 首页>>代码示例>>Python>>正文


Python TextBlockHelper.set_state方法代码示例

本文整理汇总了Python中pyqode.core.api.TextBlockHelper.set_state方法的典型用法代码示例。如果您正苦于以下问题:Python TextBlockHelper.set_state方法的具体用法?Python TextBlockHelper.set_state怎么用?Python TextBlockHelper.set_state使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyqode.core.api.TextBlockHelper的用法示例。


在下文中一共展示了TextBlockHelper.set_state方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: highlight_block

# 需要导入模块: from pyqode.core.api import TextBlockHelper [as 别名]
# 或者: from pyqode.core.api.TextBlockHelper import set_state [as 别名]

#.........这里部分代码省略.........
            text = r'" ' + text
        elif prev_state == self.INSIDE_SQSTRING:
            offset = -2
            text = r"' " + text
        else:
            offset = 0

        import_stmt = None
        # set docstring dynamic attribute, used by the fold detector.
        block.docstring = False

        self.setFormat(0, len(text), self.formats["normal"])

        state = self.NORMAL
        match = self.PROG.search(text)
        while match:
            for key, value in list(match.groupdict().items()):
                if value:
                    start, end = match.span(key)
                    start = max([0, start + offset])
                    end = max([0, end + offset])
                    if key == "uf_sq3string":
                        self.setFormat(start, end - start,
                                       self.formats["docstring"])
                        block.docstring = True
                        state = self.INSIDE_SQ3STRING
                    elif key == "uf_dq3string":
                        self.setFormat(start, end - start,
                                       self.formats["docstring"])
                        block.docstring = True
                        state = self.INSIDE_DQ3STRING
                    elif key == "uf_sqstring":
                        self.setFormat(start, end - start,
                                       self.formats["string"])
                        state = self.INSIDE_SQSTRING
                    elif key == "uf_dqstring":
                        self.setFormat(start, end - start,
                                       self.formats["string"])
                        state = self.INSIDE_DQSTRING
                    elif key == 'builtin_fct':
                        # trick to highlight __init__, __add__ and so on with
                        # builtin color
                        self.setFormat(start, end - start,
                                       self.formats["constant"])
                    else:
                        if ('"""' in value or "'''" in value) and \
                                key != 'comment':
                            # highlight docstring with a different color
                            block.docstring = True
                            self.setFormat(start, end - start,
                                           self.formats["docstring"])
                        elif key == 'decorator':
                            # highlight decorators
                            self.setFormat(start, end - start,
                                           self.formats["decorator"])
                        elif value in ['self', 'cls']:
                            # highlight self attribute
                            self.setFormat(start, end - start,
                                           self.formats["self"])
                        else:
                            # highlight all other tokens
                            self.setFormat(start, end - start,
                                           self.formats[key])
                        if key == "keyword":
                            if value in ("def", "class"):
                                match1 = self.IDPROG.match(text, end)
                                if match1:
                                    start1, end1 = match1.span(1)
                                    fmt_key = ('definition' if value == 'class'
                                               else 'function')
                                    fmt = self.formats[fmt_key]
                                    self.setFormat(start1, end1 - start1, fmt)
                        if key == 'namespace':
                            import_stmt = text.strip()
                            # color all the "as" words on same line, except
                            # if in a comment; cheap approximation to the
                            # truth
                            if '#' in text:
                                endpos = text.index('#')
                            else:
                                endpos = len(text)
                            while True:
                                match1 = self.ASPROG.match(text, end,
                                                           endpos)
                                if not match1:
                                    break
                                start, end = match1.span(1)
                                self.setFormat(start, end - start,
                                               self.formats["namespace"])
            # next match
            match = self.PROG.search(text, match.end())
        TextBlockHelper.set_state(block, state)

        # update import zone
        if import_stmt is not None:
            block.import_stmt = import_stmt
            self.import_statements.append(block)
            block.import_stmt = True
        elif block.docstring:
            self.docstrings.append(block)
开发者ID:SirmoGames,项目名称:hackedit,代码行数:104,代码来源:sh.py

示例2: highlight_block

# 需要导入模块: from pyqode.core.api import TextBlockHelper [as 别名]
# 或者: from pyqode.core.api.TextBlockHelper import set_state [as 别名]
    def highlight_block(self, text, block):
        """
        Highlight a block of text.

        :param text: The text of the block to highlight
        :param block: The QTextBlock to highlight
        """
        self._check_formats()
        prev_block = block.previous()
        prev_state = TextBlockHelper.get_state(prev_block)
        self.setFormat(0, len(text), self.formats["normal"])
        no_formats = True
        match = self.PROG.search(text)
        state = self.NORMAL
        while match:
            for key, value in list(match.groupdict().items()):
                if value:
                    no_formats = False
                    start, end = match.span(key)
                    if key == 'tag' and len(set(text)) != 1:
                            # 2 different characters -> not a header,
                            # probably a table
                            continue
                    self.setFormat(start, end - start, self.formats[key])
                    if key == 'comment':
                        state = self.INSIDE_COMMENT
                    if key == 'string' and not match.group(0).endswith('`'):
                        state = self.INSIDE_STRING
                    # make sure to highlight previous block
                    if key == 'tag':
                        state = self.INSIDE_HEADER
                        pblock = block.previous()
                        if pblock.isValid() and pblock.text() and \
                                prev_state != self.INSIDE_HEADER:
                            self._block_to_rehighlight = pblock
                            QtCore.QTimer.singleShot(
                                1, self._rehighlight_block)

            match = self.PROG.search(text, match.end())

        if no_formats:
            nblock = block.next()
            indent = len(text) - len(text.lstrip())
            if nblock.isValid() and self.PROG_HEADER.match(nblock.text()) and \
                    len(set(nblock.text())) == 1:
                self.setFormat(0, len(text), self.formats["tag"])
                state = self.INSIDE_HEADER
            elif prev_state == self.INSIDE_COMMENT and (
                    indent > 0 or not len(text)):
                self.setFormat(0, len(text), self.formats["comment"])
                state = self.INSIDE_COMMENT
            elif prev_state == self.INSIDE_STRING:
                # check if end string found -> highlight match only otherwise
                # highlight whole line
                match = self.PROG_END_STRING.match(text)
                if match:
                    end = match.end()
                else:
                    state = self.INSIDE_STRING
                    end = len(text)
                self.setFormat(0, end, self.formats["string"])
        TextBlockHelper.set_state(block, state)
开发者ID:SirmoGames,项目名称:hackedit,代码行数:64,代码来源:sh.py


注:本文中的pyqode.core.api.TextBlockHelper.set_state方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。