def highlightBlock(self, text): for h in self.singleline: expression = QRegExp(h.expression) index = expression.indexIn(text) while index >= 0: length = expression.matchedLength() self.setFormat(index, length, h.get_format()) index = expression.indexIn(text, index + length) for h in self.multiline: startIndex = 0 self.setCurrentBlockState(0) expression = QRegExp(h.expression) expression_end = QRegExp(h.expression_end) if (self.previousBlockState() != 1): startIndex = expression.indexIn(text) while startIndex >= 0: endIndex = expression_end.indexIn(text, startIndex + 1) if endIndex == -1: self.setCurrentBlockState(1) commentLength = len(text) - startIndex else: commentLength = endIndex - startIndex + \ expression_end.matchedLength() self.setFormat(startIndex, commentLength, h.get_format()) startIndex = expression.indexIn(text, startIndex + commentLength)
def highlightBlock(self, text): """@reimp @protected""" for pattern, format in self.highlightingRules: expression = QRegExp(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 +1) 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 highlightBlock(self, text): for rule in self.highlighting_rules: expression = QRegExp(rule.pattern) index = expression.indexIn(text) while index >= 0: length = expression.matchedLength() self.setFormat(index, length, rule.format) index = expression.indexIn(text, index+length) self.setCurrentBlockState(0)
def variationFromString(self, string, caseSense=True): rx = QRegExp(self.variationRegExpPattern) rx.setMinimal(False) rx.setCaseSensitivity(Qt.CaseInsensitive) if caseSense: rx.setCaseSensitivity(Qt.CaseSensitive) index = rx.indexIn(string) length = rx.matchedLength() match = string[index:index + length] return match
def highlightBlock(self, text): # Derived from Qt function, used to apply color-syntaxing to text # :param text: text input rules = [(QColor(100, 165, 225), r"^(//|#).+$"), #blue 100, 165, 225 (QColor(205, 200, 120), r"^(//|#) Warning.+$"), #yellow 205, 200, 120 (QColor(165, 75, 75), r"^(//|#).+Error.+$"), #red 165, 75, 75 (QColor(115, 215, 150), r"^(//|#).+Result.+$")] #green 115, 215, 150 # loop through rules for color, pattern in rules: keyword = QTextCharFormat() keyword.setForeground(color) # get regexp pattern expression = QRegExp(pattern) index = expression.indexIn(text) # loop until all matches are done while index >= 0: length = expression.matchedLength() # format text with current formatting self.setFormat(index, length, keyword) index = expression.indexIn(text, index + length) self.setCurrentBlockState(0)
class StringParser(object): def __init__(self): super(StringParser, self).__init__() self._string = "" self._regExp = QRegExp("LOD[0-9]*[0-9]", Qt.CaseSensitive) self._regExpLOD = QRegExp("LOD[0-9]*[0-9]", Qt.CaseSensitive) self._regExpVariation = QRegExp("VAR[0-9]*[0-9]", Qt.CaseSensitive) self._resolutionsMap = { "1K": 1024, "2K": 2048, "3K": 3072, "4K": 4096, "5K": 5120, "6K": 6144, "7K": 7168, "8K": 8196 } @property def string(self): return self._string @property def regExpLOD(self): return self._regExpLOD @property def regExpVariation(self): return self._regExpVariation @property def regExp(self): return self._regExpVariation @string.setter def string(self, string): if not isinstance(string, str): raise TypeError("Expected < str >") self._string = string def clear(self): self._string = "" def resolutionFromString(self): for resolution in self._resolutionsMap: if self._string.find(resolution) != -1: res = self._resolutionsMap[resolution] return res, res def variation(self): index = self._regExpVariation.indexIn(self._string) length = self._regExpVariation.matchedLength() match = self._string[index:index + length] return match def LOD(self): index = self._regExpLOD.indexIn(self._string) length = self._regExpLOD.matchedLength() match = self._string[index:index + length] return match def find(self): """ Find by pattern and return first """ index = self._regExp.indexIn(self._string) length = self._regExp.matchedLength() match = self._string[index:index + length] return match def findAll(self): """ Find by pattern and return all """ index = 0 matches = [] while index >= 0: index = self._regExp.indexIn(self._string, index) length = self._regExp.matchedLength() print length match = self._string[index:index + length] index += length if match: matches.append(match) return matches
def keyPressEvent(self, event): self.dirty = True customKey = False #AutoTab if (event.key() == Qt.Key_Enter or event.key() == 16777220): customKey = True numTab = 0 #new line newBlock = self.textCursor().block() currLine = newBlock.text() tabRE = QRegExp("^[\t]*") tabRE.indexIn(currLine) numTab = tabRE.matchedLength() if (currLine != "" and currLine.strip()[-1] == "{"): numTab += 1 QPlainTextEdit.keyPressEvent(self, event) if (numTab > 0): tCursor = self.textCursor() for _ in range(0, numTab): tCursor.insertText("\t") #automatic close brace if currLine != "" and currLine.strip()[-1] == "{": tCursor.insertText("\n") for _ in range(0, numTab - 1): tCursor.insertText("\t") tCursor.insertText("}") tCursor.movePosition(QTextCursor.PreviousBlock) tCursor.movePosition(QTextCursor.EndOfLine) self.setTextCursor(tCursor) if event.key() == Qt.Key_Tab and self.textCursor().hasSelection(): customKey = True selStart = self.textCursor().selectionStart() selEnd = self.textCursor().selectionEnd() cur = self.textCursor() endBlock = self.document().findBlock(selEnd) currBlock = self.document().findBlock(selStart) while currBlock.position() <= endBlock.position(): cur.setPosition(currBlock.position()) cur.insertText("\t") currBlock = currBlock.next() if event.key() == Qt.Key_Backtab and self.textCursor().hasSelection(): customKey = True selStart = self.textCursor().selectionStart() selEnd = self.textCursor().selectionEnd() cur = self.textCursor() endBlock = self.document().findBlock(selEnd) currBlock = self.document().findBlock(selStart) while currBlock.position() <= endBlock.position(): cur.setPosition(currBlock.position()) if currBlock.text().left(1) == "\t": cur.deleteChar() currBlock = currBlock.next() # Allow commenting and uncommenting of blocks of code if event.key() == Qt.Key_Slash and event.modifiers( ) == Qt.ControlModifier: customKey = True selStart = self.textCursor().selectionStart() selEnd = self.textCursor().selectionEnd() cur = self.textCursor() endBlock = self.document().findBlock(selEnd) currBlock = self.document().findBlock(selStart) while currBlock.position() <= endBlock.position(): cur.setPosition(currBlock.position()) if currBlock.text()[0] == "#": cur.deleteChar() # Make sure we remove extra spaces while currBlock.text()[0] == " ": cur.deleteChar() else: cur.insertText("# ") currBlock = currBlock.next() # Open the text finder if event.key() == Qt.Key_F and event.modifiers() == Qt.ControlModifier: customKey = True print("Opening finder...") if not customKey: QPlainTextEdit.keyPressEvent(self, event)
class PyHighlighter(QSyntaxHighlighter): KEYWORD_PATTERNS = ( r"\bbreak\b", r"\bclass\b", r"\bcontinue\b", r"\bdef\b", r"\bdo\b", r"\belif\b", r"\belse\b", r"\bfor\b", r"\bfunction\b", r"\bid\b", r"\bif\b", r"\bimport\b", r"\bin\b", r"\bprint\b", r"\breturn\b", r"\btype\b", r"\bwhile\b", ) TYPE_PATTERNS = ( r"\bbool\b", r"\bdouble\b", r"\bint\b", r"\blong\b", r"\bprint\b", r"\bstr\b", r"\bunicode\b", ) CONSTANT_PATTERNS = ( r"\bTrue\b", r"\bFalse\b", ) def __init__(self, parent=None): """ @param parent QObject or QTextDocument or QTextEdit or None """ super(PyHighlighter, self).__init__(parent) keywordFormat = QTextCharFormat() keywordFormat.setForeground(HLS_KEYWORD_COLOR) keywordFormat.setFontWeight(QFont.Bold) self.highlightingRules = [(QRegExp(pattern), keywordFormat) for pattern in self.KEYWORD_PATTERNS] typeFormat = QTextCharFormat() typeFormat.setForeground(HLS_TYPE_COLOR) typeFormat.setFontWeight(QFont.Bold) self.highlightingRules.extend([(QRegExp(pattern), typeFormat) for pattern in self.TYPE_PATTERNS]) constantFormat = QTextCharFormat() constantFormat.setForeground(HLS_LITERAL_COLOR) self.highlightingRules.extend([(QRegExp(pattern), constantFormat) for pattern in self.CONSTANT_PATTERNS]) classFormat = QTextCharFormat() classFormat.setFontWeight(QFont.Bold) classFormat.setForeground(HLS_CLASS_COLOR) self.highlightingRules.append((QRegExp(r"\bQ[A-Za-z]+\b"), classFormat)) functionFormat = QTextCharFormat() functionFormat.setFontItalic(True) functionFormat.setForeground(HLS_FUNCTION_COLOR) self.highlightingRules.append((QRegExp(r"\b[A-Za-z0-9_]+(?=\()"), functionFormat)) quotationFormat = QTextCharFormat() quotationFormat.setForeground(HLS_LITERAL_COLOR) self.highlightingRules.append((QRegExp(r'"[^"]*"'), quotationFormat)) self.highlightingRules.append((QRegExp(r'u"[^"]*"'), quotationFormat)) self.highlightingRules.append((QRegExp(r"'[^']*'"), quotationFormat)) self.highlightingRules.append((QRegExp(r"u'[^']*'"), quotationFormat)) singleLineCommentFormat = QTextCharFormat() singleLineCommentFormat.setForeground(HLS_COMMENT_COLOR) self.highlightingRules.append((QRegExp("#[^\n]*"), singleLineCommentFormat)) self.multiLineCommentFormat = QTextCharFormat() self.multiLineCommentFormat.setForeground(HLS_COMMENT_COLOR) self.commentStartExpression = QRegExp(r'"""') self.commentEndExpression = QRegExp(r'"""') todoFormat = QTextCharFormat() todoFormat.setBackground(HLS_TODO_COLOR) self.postHighlightingRules = [(QRegExp(pattern), todoFormat) for pattern in HLS_TODO_PATTERNS] doxyFormat = QTextCharFormat() doxyFormat.setForeground(HLS_COMMENT_COLOR) doxyFormat.setFontWeight(QFont.Bold) self.postHighlightingRules.extend([(QRegExp(pattern), doxyFormat) for pattern in HLS_DOXY_PATTERNS]) def highlightBlock(self, text): """@reimp @protected""" for pattern, format in self.highlightingRules: expression = QRegExp(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 +1) 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); for pattern, format in self.postHighlightingRules: expression = QRegExp(pattern) index = expression.indexIn(text) while index >= 0: length = expression.matchedLength() self.setFormat(index, length, format) index = expression.indexIn(text, index + length)
class CppHighlighter(QSyntaxHighlighter): KEYWORD_PATTERNS = ( r"\bcase\b", r"\bclass\b", r"\bdefault\b", r"\benum\b", r"\bexplicit\b", r"\bfriend\b", r"\bfor\b", r"\belse\b", r"\bif\b", r"\binline\b", r"\bnamespace\b", r"\boperator\b", r"\bprivate\b", r"\bprotected\b", r"\bpublic\b", r"\breturn\b", r"\bswitch\b", r"\bstruct\b", r"\btemplate\b", r"\btypedef\b", r"\btypename\b", r"\bunion\b", r"\bvirtual\b", r"\bvolatile\b", ) TYPE_PATTERNS = ( r"\bchar\b", r"\bconst\b", r"\bdouble\b", r"\bexplicit\b", r"\bint\b", r"\blong\b", r"\bshort\b", r"\bfloat\b", r"\bdouble\b", r"\bsignals\b", r"\bsigned\b", r"\bslots\b", r"\bstatic\b", r"\bstruct\b", r"\bunsigned\b", ) CONSTANT_PATTERNS = ( r"\btrue\b", r"\bfalse\b", ) def __init__(self, parent=None): """ @param parent QObject or QTextDocument or QTextEdit or None """ super(CppHighlighter, self).__init__(parent) keywordFormat = QTextCharFormat() keywordFormat.setForeground(HLS_KEYWORD_COLOR) keywordFormat.setFontWeight(QFont.Bold) self.highlightingRules = [(QRegExp(pattern), keywordFormat) for pattern in self.KEYWORD_PATTERNS] typeFormat = QTextCharFormat() typeFormat.setForeground(HLS_TYPE_COLOR) typeFormat.setFontWeight(QFont.Bold) self.highlightingRules.extend([(QRegExp(pattern), typeFormat) for pattern in self.TYPE_PATTERNS]) constantFormat = QTextCharFormat() constantFormat.setForeground(HLS_LITERAL_COLOR) self.highlightingRules.extend([(QRegExp(pattern), constantFormat) for pattern in self.CONSTANT_PATTERNS]) classFormat = QTextCharFormat() classFormat.setFontWeight(QFont.Bold) classFormat.setForeground(HLS_CLASS_COLOR) self.highlightingRules.append((QRegExp(r"\bQ[A-Za-z]+\b"), classFormat)) functionFormat = QTextCharFormat() functionFormat.setFontItalic(True) functionFormat.setForeground(HLS_FUNCTION_COLOR) self.highlightingRules.append((QRegExp(r"\b[A-Za-z0-9_]+(?=\()"), functionFormat)) quotationFormat = QTextCharFormat() quotationFormat.setForeground(HLS_LITERAL_COLOR) self.highlightingRules.append((QRegExp(r'"[^"]*"'), quotationFormat)) # This must comes before the line comments since they conficts pragmaFormat = QTextCharFormat() pragmaFormat.setForeground(HLS_PRAGMA_COLOR) self.highlightingRules.append((QRegExp(r"#[^\n]*"), pragmaFormat)) self.multiLineCommentFormat = QTextCharFormat() self.multiLineCommentFormat.setForeground(HLS_COMMENT_COLOR) singleLineCommentFormat = QTextCharFormat() singleLineCommentFormat.setForeground(HLS_COMMENT_COLOR) self.highlightingRules.append((QRegExp("//[^\n]*"), singleLineCommentFormat)) self.commentStartExpression = QRegExp(r"/\*") self.commentEndExpression = QRegExp(r"\*/") def highlightBlock(self, text): """@reimp @protected""" for pattern, format in self.highlightingRules: expression = QRegExp(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 +1) 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);