Пример #1
0
    def createKeywords(self):
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(QColor(64, 155, 11))

        keywords = [
            '\\babstract\\b', '\\bas\\b', '\\bbase\\b', '\\bbool\\b',
            '\\bbreak\\b', '\\bbyte\\b', '\\bcase\\b', '\\bcatch\\b',
            '\\bchar\\b', '\\bchecked\\b', '\\bclass\\b', '\\bconst\\b',
            '\\bcontinue\\b', '\\bdecimal\\b', '\\bdefault\\b',
            '\\bdelegate\\b', '\\bif\\b', '\\bimplicit\\b', '\\bin\\b',
            '\\bdo\\b', '\\bdouble\\b', '\\bint\\b', '\\binterface\\b',
            '\\belse\\b', '\\benum\\b', '\\bevent\\b', '\\bexplicit\\b',
            '\\bextern\\b', '\\bfalse\\b', '\\btrue\\b', '\\binternal\\b',
            '\\bfinally\\b', '\\bfixed\\b', '\\bfloat\\b', '\\bfor\\b',
            '\\bforeach\\b', '\\bgoto\\b', '\\bis\\b', '\\block\\b',
            '\\blong\\b', '\\bnamepsace\\b', '\\bnew\\b', '\\bnull\\b',
            '\\bobject\\b', '\\boperator\\b', '\\bout\\b', '\\boverride\\b',
            '\\bparams\\b', '\\bprivate\\b', '\\bprotected\\b', '\\bpublic\\b',
            '\\breadonly\\b', '\\bref\\b', '\\breturn\\b', '\\bsbyte\\b',
            '\\bsealed\\b', '\\bshort\\b', '\\bsizeof\\b', '\\bstackalloc\\b',
            '\\bstatic\\b', '\\bstring\\b', '\\bstruct\\b', '\\bswitch\\b',
            '\\bthis\\b', '\\bthrow\\b', '\\btry\\b', '\\btypeof\\b',
            '\\buint\\b', '\\bulong\\b', '\\bunchecked\\b', '\\bunsafe\\b',
            '\\bushort\\b', '\\busing\\b', '\\bvirtual\\b', '\\bvoid\\b',
            '\\bvolatile\\b', '\\bwhile\\b', '\\bpackage\\b', '\\bimport\\b',
            '\\bthrows\\b'
        ]

        self.highlightingRules = [(QRegExp(pattern), keywordFormat)
                                  for pattern in keywords]
Пример #2
0
def update_char_format(baseformat,
                       color=None,
                       background=None,
                       weight=None,
                       italic=None,
                       underline=None,
                       font=None):
    """
    Return a copy of `baseformat` :class:`QTextCharFormat` with
    updated color, weight, background and font properties.

    """
    charformat = QTextCharFormat(baseformat)

    if color is not None:
        charformat.setForeground(color)

    if background is not None:
        charformat.setBackground(background)

    if font is not None:
        charformat.setFont(font)
    else:
        font = update_font(baseformat.font(), weight, italic, underline)
        charformat.setFont(font)

    return charformat
Пример #3
0
    def createOperators(self):
        operatorFormat = QTextCharFormat()
        operatorFormat.setForeground(Qt.blue)

        operators = ['\\+', '-', '/', '\\*', '(', ')', '[', ']', '{', '}']

        self.highlightingRules.extend([(QRegExp(pattern), operatorFormat)
                                       for pattern in operators])
Пример #4
0
    def createClassVariables(self):
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(QColor(223, 109, 209))

        keywords = ['self', '__init__']

        self.highlightingRules.extend([(QRegExp(pattern), keywordFormat)
                                       for pattern in keywords])
Пример #5
0
    def __init__(self, parent):
        QSyntaxHighlighter.__init__(self, parent)

        self._multiLineCommentFormat = QTextCharFormat()
        self._multiLineCommentFormat.setForeground(Qt.lightGray)

        self._commentStartExpression = QRegExp("/\\*")
        self._commentEndExpression = QRegExp("\\*/")

        self.createKeywords()
        self.createOperators()
        self.createStrings()
        self.createArrows()
        self.createComments()
Пример #6
0
 def setCurrentCharFormat(self, charformat):
     # type: (QTextCharFormat) -> None
     """Set the QTextCharFormat to be used when writing.
     """
     assert QThread.currentThread() is self.thread()
     if self.__currentCharFormat != charformat:
         self.__currentCharFormat = QTextCharFormat(charformat)
Пример #7
0
    def createKeywords(self):
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(QColor(64, 155, 11))

        keywords = [
            '\\bchar\\b', '\\bclass\\b', '\\bconst\\b', '\\bdouble\\b',
            '\\benum\\b', '\\bexplicit\\b', '\\bfriend\\b', '\\binline\\b',
            '\\bint\\b', '\\blong\\b', '\\bnamespace\\b', '\\boperator\\b',
            '\\bprivate\\b', '\\bprotected\\b', '\\bpublic\\b', '\\bshort\\b',
            '\\bfloat\\b', '\\bbool\\b', '\\bsignals\\b', '\\bsigned\\b',
            '\\bslots\\b', '\\bstatic\\b', '\\bstruct\\b', '\\btemplate\\b',
            '\\btypedef\\b', '\\btypename\\b', '\\bunion\\b', '\\bunsigned\\b',
            '\\bvirtual\\b', '\\bvoid\\b', '\\bvolatile\\b'
        ]

        self.highlightingRules = [(QRegExp(pattern), keywordFormat)
                                  for pattern in keywords]
Пример #8
0
 def __init__(self, parent=None, **kwargs):
     super().__init__(parent, **kwargs)
     self.setDocumentLayout(QPlainTextDocumentLayout(self))
     self.__currentCharFormat = QTextCharFormat()
     if 'defaultFont' not in kwargs:
         defaultFont = QFontDatabase.systemFont(QFontDatabase.FixedFont)
         self.setDefaultFont(defaultFont)
     self.__streams = []
Пример #9
0
 def test_clone(self):
     doc = TerminalTextDocument()
     writer = TextStream()
     doc.connectStream(writer)
     writer.write("A")
     doc_c = doc.clone()
     writer.write("B")
     self.assertEqual(doc.toPlainText(), "AB")
     self.assertEqual(doc_c.toPlainText(), "AB")
     writer_err = TextStream()
     cf = QTextCharFormat()
     cf.setForeground(QColor(Qt.red))
     doc_c.connectStream(writer_err, cf)
     writer_err.write("C")
     self.assertEqual(doc_c.toPlainText(), "ABC")
     self.assertEqual(doc.toPlainText(), "AB")
     doc_c.disconnectStream(writer_err)
     writer_err.write("D")
     self.assertEqual(doc_c.toPlainText(), "ABC")
Пример #10
0
def update_char_format(baseformat, color=None, background=None, weight=None,
                       italic=None, underline=None, font=None):
    """
    Return a copy of `baseformat` :class:`QTextCharFormat` with
    updated color, weight, background and font properties.

    """
    charformat = QTextCharFormat(baseformat)

    if color is not None:
        charformat.setForeground(color)

    if background is not None:
        charformat.setBackground(background)

    if font is not None:
        charformat.setFont(font)
    else:
        font = update_font(baseformat.font(), weight, italic, underline)
        charformat.setFont(font)

    return charformat
Пример #11
0
    def createOperators(self):
        operatorFormat = QTextCharFormat()
        operatorFormat.setForeground(QColor(148, 99, 233))
        operatorFormat.setFontWeight(QFont.Bold)

        operators = [
            '\\+', '-', '/', '\\*', '=', '==', '!=', '<=', '>=', '<', '>'
        ]

        self.highlightingRules.extend([(QRegExp(pattern), operatorFormat)
                                       for pattern in operators])
Пример #12
0
    def connectStream(self,
                      stream: 'TextStream',
                      charformat: Optional[QTextCharFormat] = None,
                      **kwargs) -> None:
        """
        Connect a :class:`TextStream` instance to this document.

        The `stream` connection will be 'inherited' by `clone()`
        """
        if kwargs and charformat is not None:
            raise TypeError("'charformat' and kwargs cannot be used together")
        if kwargs:
            charformat = update_char_format(QTextCharFormat(), **kwargs)
        writer: Optional[Formatter] = None
        if charformat is not None:
            writer = Formatter(self, charformat)
        self.__streams.append((stream, writer))
        if writer is not None:
            stream.stream.connect(writer.write)
        else:
            stream.stream.connect(self.write)
Пример #13
0
    def createKeywords(self):
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(Qt.darkBlue)
        keywordFormat.setFontWeight(QFont.Bold)

        keywords = [
            '\\band\\b', '\\bdel\\b', '\\bfor\\b', '\\bas\\b', '\\bassert\\b',
            '\\bbreak\\b', '\\bclass\\b', '\\bcontinue\\b', '\\bdef\\b',
            '\\belif\\b', '\\belse\\b', '\\bexcept\\b', '\\bexec\\b',
            '\\bFalse\\b', '\\bfinally\\b', '\\bfrom\\b', '\\bglobal\\b',
            '\\bif\\b', '\\bimport\\b', '\\bin\\b', '\\bis\\b', '\\blambda\\b',
            '\\bNone\\b', '\\bnonlocal\\b', '\\bnot\\b', '\bor\b',
            '\\bpass\\b', '\\bprint\\b', '\\braise\\b', '\\breturn\\b',
            '\\bTrue\\b', '\\btry\\b', '\\bwhile\\b', '\\bwith\\b',
            '\\byield\\b', '<', '<=', '>', '>=', '==', '!='
        ]

        self.highlightingRules = [(QRegExp(pattern), keywordFormat)
                                  for pattern in keywords]
Пример #14
0
 def createStrings(self):
     stringFormat = QTextCharFormat()
     stringFormat.setForeground(Qt.lightGray)
     self.highlightingRules.append((QRegExp('\".*\"'), stringFormat))
     self.highlightingRules.append((QRegExp('\'.*\''), stringFormat))
Пример #15
0
 def currentCharFormat(self) -> QTextCharFormat:
     """Return the current char format."""
     return QTextCharFormat(self.__currentCharFormat)
Пример #16
0
 def currentCharFormat(self):
     # type: () -> QTextCharFormat
     return QTextCharFormat(self.__currentCharFormat)
Пример #17
0
def update_char_format(
        baseformat: QTextCharFormat,
        color: Optional[_Color] = None,
        background: Optional[_Color] = None,
        weight: Optional[int] = None,
        italic: Optional[bool] = None,
        underline: Optional[bool] = None,
        font: Optional[QFont] = None
) -> QTextCharFormat:
    """
    Return a copy of `baseformat` :class:`QTextCharFormat` with
    updated color, weight, background and font properties.
    """
    charformat = QTextCharFormat(baseformat)
    if color is not None:
        charformat.setForeground(color)
    if background is not None:
        charformat.setBackground(background)
    if font is not None:
        assert weight is None and italic is None and underline is None
        charformat.setFont(font)
    else:
        if weight is not None:
            charformat.setFontWeight(weight)
        if italic is not None:
            charformat.setFontItalic(italic)
        if underline is not None:
            charformat.setFontUnderline(underline)
    return charformat
Пример #18
0
class CppHighlighter(QSyntaxHighlighter):
    def __init__(self, parent):
        QSyntaxHighlighter.__init__(self, parent)

        self._multiLineCommentFormat = QTextCharFormat()
        self._multiLineCommentFormat.setForeground(Qt.lightGray)

        self._commentStartExpression = QRegExp("/\\*")
        self._commentEndExpression = QRegExp("\\*/")

        self.createKeywords()
        self.createOperators()
        self.createStrings()
        self.createArrows()
        self.createComments()

    def createKeywords(self):
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(QColor(64, 155, 11))

        keywords = [
            '\\bchar\\b', '\\bclass\\b', '\\bconst\\b', '\\bdouble\\b',
            '\\benum\\b', '\\bexplicit\\b', '\\bfriend\\b', '\\binline\\b',
            '\\bint\\b', '\\blong\\b', '\\bnamespace\\b', '\\boperator\\b',
            '\\bprivate\\b', '\\bprotected\\b', '\\bpublic\\b', '\\bshort\\b',
            '\\bfloat\\b', '\\bbool\\b', '\\bsignals\\b', '\\bsigned\\b',
            '\\bslots\\b', '\\bstatic\\b', '\\bstruct\\b', '\\btemplate\\b',
            '\\btypedef\\b', '\\btypename\\b', '\\bunion\\b', '\\bunsigned\\b',
            '\\bvirtual\\b', '\\bvoid\\b', '\\bvolatile\\b'
        ]

        self.highlightingRules = [(QRegExp(pattern), keywordFormat)
                                  for pattern in keywords]

    def createOperators(self):
        operatorFormat = QTextCharFormat()
        operatorFormat.setForeground(QColor(148, 99, 233))
        operatorFormat.setFontWeight(QFont.Bold)

        operators = [
            '\\+', '-', '/', '\\*', '=', '==', '!=', '<=', '>=', '<', '>'
        ]

        self.highlightingRules.extend([(QRegExp(pattern), operatorFormat)
                                       for pattern in operators])

    def createStrings(self):
        stringFormat = QTextCharFormat()
        stringFormat.setForeground(Qt.lightGray)
        self.highlightingRules.append((QRegExp('\".*\"'), stringFormat))
        self.highlightingRules.append((QRegExp('\'.*\''), stringFormat))

    def createArrows(self):
        arrowFormat = QTextCharFormat()
        arrowFormat.setForeground(Qt.red)
        arrows = ['<-', '->']
        self.highlightingRules.extend([(QRegExp(pattern), arrowFormat)
                                       for pattern in arrows])

    def createComments(self):
        singleLineCommentFormat = QTextCharFormat()
        singleLineCommentFormat.setForeground(Qt.gray)
        self.highlightingRules.append(
            (QRegExp('//[^\n]*'), singleLineCommentFormat))

    def highlightBlock(self, text):
        for pattern, format in self.highlightingRules:
            expression = pattern
            index = expression.indexIn(text)

            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, format)
                index = expression.indexIn(text, index + length)

        self.setCurrentBlockState(0)

        startIndex = 0

        if self.previousBlockState() != 1:
            startIndex = self._commentStartExpression.indexIn(text)

        while startIndex >= 0:
            endIndex = self._commentEndExpression.indexIn(text, startIndex)

            if endIndex == -1:
                self.setCurrentBlockState(1)
                commentLength = len(text) - startIndex
            else:
                commentLength = endIndex - startIndex + \
                                self._commentEndExpression.matchedLength()

            self.setFormat(startIndex, commentLength,
                           self._multiLineCommentFormat)
            startIndex = self._commentStartExpression.indexIn(
                text, startIndex + commentLength)
Пример #19
0
 def createArrows(self):
     arrowFormat = QTextCharFormat()
     arrowFormat.setForeground(Qt.red)
     arrows = ['<-', '->']
     self.highlightingRules.extend([(QRegExp(pattern), arrowFormat)
                                    for pattern in arrows])
Пример #20
0
def text_format(foreground=Qt.black, weight=QFont.Normal):
    fmt = QTextCharFormat()
    fmt.setForeground(QBrush(foreground))
    fmt.setFontWeight(weight)
    return fmt
Пример #21
0
 def currentCharFormat(self):
     # type: () -> QTextCharFormat
     return QTextCharFormat(self.document().currentCharFormat())
Пример #22
0
 def createComments(self):
     singleLineCommentFormat = QTextCharFormat()
     singleLineCommentFormat.setForeground(Qt.gray)
     self.highlightingRules.append(
         (QRegExp('//[^\n]*'), singleLineCommentFormat))
Пример #23
0
 def createAnnotations(self):
     annotationFormat = QTextCharFormat()
     annotationFormat.setForeground(QColor(108, 204, 255))
     self.highlightingRules.append((QRegExp('@[^\n]*'), annotationFormat))
Пример #24
0
def text_format(foreground=Qt.black, weight=QFont.Normal):
    fmt = QTextCharFormat()
    fmt.setForeground(QBrush(foreground))
    fmt.setFontWeight(weight)
    return fmt
Пример #25
0
class CsHighlighter(QSyntaxHighlighter):
    def __init__(self, parent):
        QSyntaxHighlighter.__init__(self, parent)

        self._multiLineCommentFormat = QTextCharFormat()
        self._multiLineCommentFormat.setForeground(Qt.lightGray)

        self._commentStartExpression = QRegExp("/\\*")
        self._commentEndExpression = QRegExp("\\*/")

        self.createKeywords()
        self.createOperators()
        self.createStrings()
        self.createArrows()
        self.createComments()

    def createKeywords(self):
        keywordFormat = QTextCharFormat()
        keywordFormat.setForeground(QColor(64, 155, 11))

        keywords = [
            '\\babstract\\b', '\\bas\\b', '\\bbase\\b', '\\bbool\\b',
            '\\bbreak\\b', '\\bbyte\\b', '\\bcase\\b', '\\bcatch\\b',
            '\\bchar\\b', '\\bchecked\\b', '\\bclass\\b', '\\bconst\\b',
            '\\bcontinue\\b', '\\bdecimal\\b', '\\bdefault\\b',
            '\\bdelegate\\b', '\\bif\\b', '\\bimplicit\\b', '\\bin\\b',
            '\\bdo\\b', '\\bdouble\\b', '\\bint\\b', '\\binterface\\b',
            '\\belse\\b', '\\benum\\b', '\\bevent\\b', '\\bexplicit\\b',
            '\\bextern\\b', '\\bfalse\\b', '\\btrue\\b', '\\binternal\\b',
            '\\bfinally\\b', '\\bfixed\\b', '\\bfloat\\b', '\\bfor\\b',
            '\\bforeach\\b', '\\bgoto\\b', '\\bis\\b', '\\block\\b',
            '\\blong\\b', '\\bnamepsace\\b', '\\bnew\\b', '\\bnull\\b',
            '\\bobject\\b', '\\boperator\\b', '\\bout\\b', '\\boverride\\b',
            '\\bparams\\b', '\\bprivate\\b', '\\bprotected\\b', '\\bpublic\\b',
            '\\breadonly\\b', '\\bref\\b', '\\breturn\\b', '\\bsbyte\\b',
            '\\bsealed\\b', '\\bshort\\b', '\\bsizeof\\b', '\\bstackalloc\\b',
            '\\bstatic\\b', '\\bstring\\b', '\\bstruct\\b', '\\bswitch\\b',
            '\\bthis\\b', '\\bthrow\\b', '\\btry\\b', '\\btypeof\\b',
            '\\buint\\b', '\\bulong\\b', '\\bunchecked\\b', '\\bunsafe\\b',
            '\\bushort\\b', '\\busing\\b', '\\bvirtual\\b', '\\bvoid\\b',
            '\\bvolatile\\b', '\\bwhile\\b', '\\bpackage\\b', '\\bimport\\b',
            '\\bthrows\\b'
        ]

        self.highlightingRules = [(QRegExp(pattern), keywordFormat)
                                  for pattern in keywords]

    def createOperators(self):
        operatorFormat = QTextCharFormat()
        operatorFormat.setForeground(QColor(148, 99, 233))
        operatorFormat.setFontWeight(QFont.Bold)

        operators = [
            '\\+', '-', '/', '\\*', '=', '==', '!=', '<=', '>=', '<', '>'
        ]

        self.highlightingRules.extend([(QRegExp(pattern), operatorFormat)
                                       for pattern in operators])

    def createStrings(self):
        stringFormat = QTextCharFormat()
        stringFormat.setForeground(Qt.darkGray)
        self.highlightingRules.append((QRegExp('\".*\"'), stringFormat))
        self.highlightingRules.append((QRegExp('\'.*\''), stringFormat))

    def createArrows(self):
        arrowFormat = QTextCharFormat()
        arrowFormat.setForeground(Qt.red)
        arrows = ['<-', '->']
        self.highlightingRules.extend([(QRegExp(pattern), arrowFormat)
                                       for pattern in arrows])

    def createComments(self):
        singleLineCommentFormat = QTextCharFormat()
        singleLineCommentFormat.setForeground(Qt.gray)
        self.highlightingRules.append(
            (QRegExp('//[^\n]*'), singleLineCommentFormat))

    def highlightBlock(self, text):
        for pattern, format in self.highlightingRules:
            expression = pattern
            index = expression.indexIn(text)

            while index >= 0:
                length = expression.matchedLength()
                self.setFormat(index, length, format)
                index = expression.indexIn(text, index + length)

        self.setCurrentBlockState(0)

        startIndex = 0

        if self.previousBlockState() != 1:
            startIndex = self._commentStartExpression.indexIn(text)

        while startIndex >= 0:
            endIndex = self._commentEndExpression.indexIn(text, startIndex)

            if endIndex == -1:
                self.setCurrentBlockState(1)
                commentLength = len(text) - startIndex
            else:
                commentLength = endIndex - startIndex + \
                                self._commentEndExpression.matchedLength()

            self.setFormat(startIndex, commentLength,
                           self._multiLineCommentFormat)
            startIndex = self._commentStartExpression.indexIn(
                text, startIndex + commentLength)