Skip to content

PythonHighlighter

Qt Base Class: QSyntaxHighlighter

Signature: QSyntaxHighlighter(self, parent: PySide6.QtCore.QObject) -> None QSyntaxHighlighter(self, parent: PySide6.QtGui.QTextDocument) -> None

Base classes

Name Children Inherits
BaseRuleSyntaxHighlighter
prettyqt.syntaxhighlighters.baserulesyntaxhighlighter

⋔ Inheritance diagram

graph TD
  1473296312496["syntaxhighlighters.PythonHighlighter"]
  1473296248080["syntaxhighlighters.BaseRuleSyntaxHighlighter"]
  1473245668528["gui.SyntaxHighlighter"]
  1473245664624["gui.SyntaxHighlighterMixin"]
  1473299815024["core.ObjectMixin"]
  140713234304496["builtins.object"]
  1473289231040["QtGui.QSyntaxHighlighter"]
  1473288842240["QtCore.QObject"]
  1473291690208["Shiboken.Object"]
  1473296248080 --> 1473296312496
  1473245668528 --> 1473296248080
  1473245664624 --> 1473245668528
  1473299815024 --> 1473245664624
  140713234304496 --> 1473299815024
  1473289231040 --> 1473245668528
  1473288842240 --> 1473289231040
  1473291690208 --> 1473288842240
  140713234304496 --> 1473291690208

🛈 DocStrings

Bases: BaseRuleSyntaxHighlighter

Syntax highlighter for the Python language.

Source code in prettyqt\syntaxhighlighters\pythonhighlighter.py
class PythonHighlighter(syntaxhighlighters.BaseRuleSyntaxHighlighter):
    """Syntax highlighter for the Python language."""

    RULES = Rule.__subclasses__()

    def highlightBlock(self, text: str):
        """Apply syntax highlighting to the given block of text."""
        # Do other syntax formatting
        super().highlightBlock(text)
        self.setCurrentBlockState(0)
        # Do multi-line strings
        if not self.match_multiline(text, *TRI_SINGLE):
            self.match_multiline(text, *TRI_DOUBLE)

    def match_multiline(
        self,
        text: str,
        delimiter: core.RegularExpression,
        in_state: int,
        style: gui.TextCharFormat,
    ):
        """Do highlighting of multi-line strings.

        ``delimiter`` should be a
        ``core.RegExp`` for triple-single-quotes or triple-double-quotes, and
        ``in_state`` should be a unique integer to represent the corresponding
        state changes when inside those strings. Returns True if we're still
        inside a multi-line string when this function is finished.
        """
        # If inside triple-single quotes, start at 0
        if self.previousBlockState() == in_state:
            start = 0
            add = 0
        # Otherwise, look for the delimiter on this line
        else:
            match = delimiter.match(text)
            if not match.hasMatch():
                return
            start = match.capturedStart()
            add = match.capturedLength()

        # As long as there's a delimiter match on this line...
        while start >= 0:
            # Look for the ending delimiter
            match = delimiter.match(text, start + add)
            end = match.capturedStart()
            # Ending delimiter on this line?
            if end >= add:
                length = end - start + add + match.capturedLength()
                self.setCurrentBlockState(0)
            # No; multi-line string
            else:
                self.setCurrentBlockState(in_state)
                length = len(text) - start + add
            # Apply formatting
            self.setFormat(start, length, style)
            # Look for the next match
            start = delimiter.match(text, start + length).capturedStart()

        # Return True if still inside a multi-line string, False otherwise
        return self.currentBlockState() == in_state

highlightBlock(text: str)

Apply syntax highlighting to the given block of text.

Source code in prettyqt\syntaxhighlighters\pythonhighlighter.py
def highlightBlock(self, text: str):
    """Apply syntax highlighting to the given block of text."""
    # Do other syntax formatting
    super().highlightBlock(text)
    self.setCurrentBlockState(0)
    # Do multi-line strings
    if not self.match_multiline(text, *TRI_SINGLE):
        self.match_multiline(text, *TRI_DOUBLE)

match_multiline(text: str, delimiter: core.RegularExpression, in_state: int, style: gui.TextCharFormat)

Do highlighting of multi-line strings.

delimiter should be a core.RegExp for triple-single-quotes or triple-double-quotes, and in_state should be a unique integer to represent the corresponding state changes when inside those strings. Returns True if we're still inside a multi-line string when this function is finished.

Source code in prettyqt\syntaxhighlighters\pythonhighlighter.py
def match_multiline(
    self,
    text: str,
    delimiter: core.RegularExpression,
    in_state: int,
    style: gui.TextCharFormat,
):
    """Do highlighting of multi-line strings.

    ``delimiter`` should be a
    ``core.RegExp`` for triple-single-quotes or triple-double-quotes, and
    ``in_state`` should be a unique integer to represent the corresponding
    state changes when inside those strings. Returns True if we're still
    inside a multi-line string when this function is finished.
    """
    # If inside triple-single quotes, start at 0
    if self.previousBlockState() == in_state:
        start = 0
        add = 0
    # Otherwise, look for the delimiter on this line
    else:
        match = delimiter.match(text)
        if not match.hasMatch():
            return
        start = match.capturedStart()
        add = match.capturedLength()

    # As long as there's a delimiter match on this line...
    while start >= 0:
        # Look for the ending delimiter
        match = delimiter.match(text, start + add)
        end = match.capturedStart()
        # Ending delimiter on this line?
        if end >= add:
            length = end - start + add + match.capturedLength()
            self.setCurrentBlockState(0)
        # No; multi-line string
        else:
            self.setCurrentBlockState(in_state)
            length = len(text) - start + add
        # Apply formatting
        self.setFormat(start, length, style)
        # Look for the next match
        start = delimiter.match(text, start + length).capturedStart()

    # Return True if still inside a multi-line string, False otherwise
    return self.currentBlockState() == in_state

⌗ Property table

Qt Property Type Doc
objectName QString