Apply syntax highlighting to the given block of text.
516389
def highlightBlock(self, text):
        
        # Do other syntax formatting
        for expression, nth, format in self.rules:
            index = expression.indexIn(text, 0)

            while index >= 0:
                # We actually want the index of the nth match
                index = expression.pos(nth)
                length = len(expression.cap(nth))
                self.setFormat(index, length, format)
                index = expression.indexIn(text, index + length)

        self.setCurrentBlockState(0)

        # Do multi-line strings
        in_multiline = self.match_multiline(text, *self.tri_single)
        if not in_multiline:
            in_multiline = self.match_multiline(text, *self.tri_double)
458403
def blocks_by_text(cls, text):
        
        text = kurt.BlockType._strip_text(text)
        matches = []
        for block in cls.blocks:
            for pbt in block.conversions:
                if pbt.stripped_text == text:
                    matches.append(block)
                    break
        return matches
217153
def highlight_spaces(self, text, offset=0):
        
        flags_text = self.document().defaultTextOption().flags()
        show_blanks =  flags_text & QTextOption.ShowTabsAndSpaces
        if show_blanks:
            format_leading = self.formats.get("leading", None)
            format_trailing = self.formats.get("trailing", None)
            match = self.BLANKPROG.search(text, offset)
            while match:
                start, end = match.span()
                start = max([0, start+offset])
                end = max([0, end+offset])
                # Format trailing spaces at the end of the line. 
                if end == len(text) and format_trailing is not None:
                    self.setFormat(start, end, format_trailing)
                # Format leading spaces, e.g. indentation.
                if start == 0 and format_leading is not None:
                    self.setFormat(start, end, format_leading)
                format = self.format(start)
                color_foreground = format.foreground().color()
                alpha_new = self.BLANK_ALPHA_FACTOR * color_foreground.alphaF()
                color_foreground.setAlphaF(alpha_new)
                self.setFormat(start, end-start, color_foreground)
                match = self.BLANKPROG.search(text, match.end())
1079711
def _highlight_surrounding_scopes(self, block):
        
        scope = FoldScope(block)
        if (self._current_scope is None or
                self._current_scope.get_range() != scope.get_range()):
            self._current_scope = scope
            self._clear_scope_decos()
            # highlight surrounding parent scopes with a darker color
            start, end = scope.get_range()
            if not TextBlockHelper.is_collapsed(block):
                self._add_scope_decorations(block, start, end)
513992
def highlightBlock(self, text):
        
        # Note that an undefined blockstate is equal to -1, so the first block
        # will have the correct behaviour of starting at 0.
        if self._allow_highlight:
            start = self.previousBlockState() + 1
            end = start + len(text)
            for i, (fmt, letter) in enumerate(self._charlist[start:end]):
                self.setFormat(i, 1, fmt)
            self.setCurrentBlockState(end)
            self.highlight_spaces(text)
410642
def underline(self, msg):
        """"""
        return click.style(msg, underline=True) if self.colorize else msg
933308
def color_text(text, color):
    
    """
    import utool as ut
    if color is None or not ENABLE_COLORS:
        return text
    elif color == 'python':
        return highlight_text(text, color)
    elif color == 'sql':
        return highlight_text(text, 'sql')
    try:
        import pygments
        import pygments.console
        # if color == 'guess':
        #     import linguist  # NOQA
        #     pygments.lexers.guess_lexer(text)
        #     return highlight_text(text, color)
        ansi_text = pygments.console.colorize(color, text)
        if ut.WIN32:
            import colorama
            ansi_reset = (colorama.Style.RESET_ALL)
        else:
            ansi_reset = pygments.console.colorize('reset', '')
        ansi_text = ansi_text + ansi_reset
        return ansi_text
    except ImportError:
        return text
270350
def _highlight_block(self, block):
        
        scope = FoldScope(block)
        if (self._current_scope is None or
                self._current_scope.get_range() != scope.get_range()):
            self._current_scope = scope
            self._clear_scope_decos()
            # highlight current scope with darker or lighter color
            start, end = scope.get_range()
            if not TextBlockHelper.is_collapsed(block):
                self._decorate_block(start, end)
103259
def _do_code_blocks(self, text):
        """"""
        code_block_re = re.compile(r'''
            (?:\n\n|\A\n?)
            (               # $1 = the code block -- one or more lines, starting with a space/tab
              (?:
                (?:[ ]{%d} | \t)  # Lines must start with a tab or a tab-width of spaces
                .*\n+
              )+
            )
            ((?=^[ ]{0,%d}\S)|\Z)   # Lookahead for non-space at line-start, or end of doc
            ''' % (self.tab_width, self.tab_width),
            re.M | re.X)
        return code_block_re.sub(self._code_block_sub, text)
820968
def highlightBlock(self, text):
        

        # I need to know where in the document we are,
        # because our formatting info is global to
        # the document
        cb = self.currentBlock()
        p = cb.position()

        # The \n is not really needed, but sometimes
        # you are in an empty last block, so your position is
        # **after** the end of the document.
        text = str(self.document().toPlainText()) + '\n'

        # Yes, re-highlight the whole document.
        # There **must** be some optimizacion possibilities
        # but it seems fast enough.
        highlight(text, self.lexer, self.formatter)

        # Just apply the formatting to this block.
        # For titles, it may be necessary to backtrack
        # and format a couple of blocks **earlier**.
        for i in range(len(str(text))):
            try:
                self.setFormat(i, 1, self.formatter.data[p + i])
            except IndexError:
                pass

        # I may need to do something about this being called
        # too quickly.
        self.tstamp = time.time()
347715
def highlight_syntax(src, lang, linenums=False):
    
    src = src.strip('\n')
    if not lang:
        lexer = TextLexer()
    else:
        try:
            lexer = get_lexer_by_name(lang, stripall=True)
        except ValueError:
            lexer = TextLexer()
    formatter = HtmlFormatter(linenos=linenums, tab_length=TAB_LENGTH)
    html = highlight(src, lexer, formatter)

    if lang:
        open_code = OPEN_CODE % (LANG_TAG % (lang, ), )
    else:
        open_code = OPEN_CODE % u''
    html = html.replace('<div class="highlight"><pre>', open_code, 1)
    html = html.replace('</pre></div>', CLOSE_CODE)
    return html
400761
def wrap_toks(self, block, lineno, indent):
        """"""
        tokens = tokenize.generate_tokens(self.gen_lines(block, indent).next)
        for type, value, (line0, col0), (line1, col1), line_text in tokens:
            line0 += lineno - 1
            line1 += lineno - 1
            # Don't bother updating the columns; this is too complicated
            # since line_text would also have to be updated and it would
            # still break for tokens spanning lines.  Let the user guess
            # that the column numbers for doctests are relative to the
            # end of the prompt string (PS1 or PS2).
            yield type, value, (line0, col0), (line1, col1), line_text
693935
private void highlightText(int start) {
        editor.setCaretPosition(editor.getText().length());
        editor.moveCaretPosition(start);
    }
512831
def selector(C, style):
        """"""
        clas = C.classname(style.name)
        if style.type == 'paragraph':
            # heading outline levels are 0..7 internally, indicating h1..h8
            outlineLvl = int((style.properties.get('outlineLvl') or {}).get('val') or 8) + 1
            if outlineLvl < 9:
                tag = 'h%d' % outlineLvl
            else:
                tag = 'p'
        elif style.type == 'character':
            tag = 'span'
        elif style.type == 'table':
            tag = 'table'
        elif style.type == 'numbering':
            tag = 'ol'
        return "%s.%s" % (tag, clas)
782279
def insert_text(self, text):
        """"""
        # Eventually this maybe should wrap to insert_text_to if
        # backspace-handling is required
        self.textCursor().insertText(text, self.default_style.format)
53864
def _fill(self, text = ""):
        ""
        if self._do_indent:
            self._write("\n"+"    "*self._indent + text)
        else:
            self._write(text)
691702
function highlightMatches(chunks) {
    return $.map(chunks, function (chunk) {
        var text = utils.escapeHtml(chunk.text);

        if (text && chunk.matched) {
            text = '<strong>' + text + '</strong>';
        }
        return text;
    }).join('');
}