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)
def highlight_block(self, text, block): prev_block = block.previous() prev_state = TextBlockHelper.get_state(prev_block) if prev_state == self.INSIDE_DQ3STRING: offset = -4 text = r'""" ' + text elif prev_state == self.INSIDE_SQ3STRING: offset = -4 text = r"''' " + text elif prev_state == self.INSIDE_DQSTRING: offset = -2 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)
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)