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 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
def createOperators(self): operatorFormat = QTextCharFormat() operatorFormat.setForeground(Qt.blue) operators = ['\\+', '-', '/', '\\*', '(', ')', '[', ']', '{', '}'] self.highlightingRules.extend([(QRegExp(pattern), operatorFormat) for pattern in operators])
def createClassVariables(self): keywordFormat = QTextCharFormat() keywordFormat.setForeground(QColor(223, 109, 209)) keywords = ['self', '__init__'] self.highlightingRules.extend([(QRegExp(pattern), keywordFormat) for pattern in keywords])
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 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)
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 __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 = []
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")
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 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)
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]
def createStrings(self): stringFormat = QTextCharFormat() stringFormat.setForeground(Qt.lightGray) self.highlightingRules.append((QRegExp('\".*\"'), stringFormat)) self.highlightingRules.append((QRegExp('\'.*\''), stringFormat))
def currentCharFormat(self) -> QTextCharFormat: """Return the current char format.""" return QTextCharFormat(self.__currentCharFormat)
def currentCharFormat(self): # type: () -> QTextCharFormat return QTextCharFormat(self.__currentCharFormat)
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
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)
def createArrows(self): arrowFormat = QTextCharFormat() arrowFormat.setForeground(Qt.red) arrows = ['<-', '->'] self.highlightingRules.extend([(QRegExp(pattern), arrowFormat) for pattern in arrows])
def text_format(foreground=Qt.black, weight=QFont.Normal): fmt = QTextCharFormat() fmt.setForeground(QBrush(foreground)) fmt.setFontWeight(weight) return fmt
def currentCharFormat(self): # type: () -> QTextCharFormat return QTextCharFormat(self.document().currentCharFormat())
def createComments(self): singleLineCommentFormat = QTextCharFormat() singleLineCommentFormat.setForeground(Qt.gray) self.highlightingRules.append( (QRegExp('//[^\n]*'), singleLineCommentFormat))
def createAnnotations(self): annotationFormat = QTextCharFormat() annotationFormat.setForeground(QColor(108, 204, 255)) self.highlightingRules.append((QRegExp('@[^\n]*'), annotationFormat))
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)