def highlightBlock(self, text): for pattern, format in self.rules: exp = QRegExp(pattern) index = exp.indexIn(text) while index >= 0: length = exp.matchedLength() if exp.numCaptures() > 0: self.setFormat(exp.pos(1), len(str(exp.cap(1))), format) else: self.setFormat(exp.pos(0), len(str(exp.cap(0))), format) index = exp.indexIn(text, index + length) # Multi line strings start = self.multilineStart end = self.multilineEnd self.setCurrentBlockState(0) startIndex, skip = 0, 0 if self.previousBlockState() != 1: startIndex, skip = start.indexIn(text), 3 while startIndex >= 0: endIndex = end.indexIn(text, startIndex + skip) if endIndex == -1: self.setCurrentBlockState(1) commentLen = len(text) - startIndex else: commentLen = endIndex - startIndex + 3 self.setFormat(startIndex, commentLen, self.stringFormat) startIndex, skip = (start.indexIn(text, startIndex + commentLen + 3), 3)
def handle_stdout(self): """ Private slot to handle the readyReadStdout signal of the pylint process. """ result_list = [] #regex = QRegExp('(\w)\S*:\S*(\d*):.*: (.*)') regex = QRegExp('(\w)\s*:\s*(\d*):(.*)') regex_score = \ QRegExp('.*at.(\d.\d*)/10.*') while self.pylint_pross and self.pylint_pross.canReadLine(): result = unicode(self.pylint_pross.readLine()) if result != None: pos = 0 while True: pos = regex.indexIn(result, pos) if pos < 0: if regex_score.indexIn(result, 0) >= 0: self.win.setWindowTitle( \ "PyLint Results :" \ + str(regex_score.cap(1)) \ + ':' + os.path.basename(str(self.parent.editor.filename))) break result_list.append( (regex.cap(1), regex.cap(2), regex.cap(3))) #print 'Append : ',(regex.cap(1), regex.cap(2), regex.cap(3)) pos = pos + regex.matchedLength() if len(result_list) > 0: self.win.append_results(result_list)
def highlightBlock(self, text): #for every pattern for pattern, format in self.highlightingRules: #Create a regular expression from the retrieved pattern expression = QRegExp(pattern) #Check what index that expression occurs at with the ENTIRE text index = expression.indexIn(text) #While the index is greater than 0 while index >= 0: #Get the length of how long the expression is true, set the format from the start to the length with the text format length = expression.matchedLength() self.setFormat(index, length, format) #Set index to where the expression ends in the text index = expression.indexIn(text, index + length) #HANDLE QUOTATION MARKS NOW.. WE WANT TO START WITH " AND END WITH ".. A THIRD " SHOULD NOT CAUSE THE WORDS INBETWEEN SECOND AND THIRD TO BE COLORED self.setCurrentBlockState(0) startIndex = 0 if self.previousBlockState() != 1: startIndex = self.valueStartExpression.indexIn(text) while startIndex >= 0: endIndex = self.valueEndExpression.indexIn(text, startIndex) if endIndex == -1: self.setCurrentBlockState(1) commentLength = len(text) - startIndex else: commentLength = endIndex - startIndex + self.valueEndExpression.matchedLength( ) self.setFormat(startIndex, commentLength, self.valueFormat) startIndex = self.valueStartExpression.indexIn( text, startIndex + commentLength)
class RegexHighlighter(QSyntaxHighlighter): def __init__(self, widget): QSyntaxHighlighter.__init__(self, widget) self.regex = None # create format type self.odd_format = QTextCharFormat() self.odd_format.setFontWeight(QFont.Bold) self.odd_format.setForeground(Qt.darkBlue) self.even_format = QTextCharFormat() self.even_format.setFontWeight(QFont.Bold) self.even_format.setForeground(Qt.darkMagenta) def set_regex(self, exp): self.regex = QRegExp(exp) def highlightBlock(self, text): if not self.regex: return matches = 0 pos = self.regex.indexIn(text, 0) while (pos >= 0): length = self.regex.matchedLength() matches += 1 if matches % 2 == 0: self.setFormat(pos, length, self.even_format) else: self.setFormat(pos, length, self.odd_format) # handle wildcard (*) if length == 0: pos += 1 pos = self.regex.indexIn(text, pos + length)
def handle_stdout(self): """ Private slot to handle the readyReadStdout signal of the pylint process. """ result_list = [] #regex = QRegExp('(\w)\S*:\S*(\d*):.*: (.*)') regex = QRegExp('(\w)\s*:\s*(\d*):(.*)') regex_score = \ QRegExp('.*at.(\d.\d*)/10.*') while self.pylint_pross and self.pylint_pross.canReadLine(): result = unicode(self.pylint_pross.readLine()) if result != None: pos = 0 while True: pos = regex.indexIn(result, pos) if pos < 0: if regex_score.indexIn(result, 0) >= 0: self.win.setWindowTitle( \ "PyLint Results :" \ + str(regex_score.cap(1)) \ + ':' + os.path.basename(str(self.parent.editor.filename))) break result_list.append((regex.cap(1), regex.cap(2), regex.cap(3))) #print 'Append : ',(regex.cap(1), regex.cap(2), regex.cap(3)) pos = pos + regex.matchedLength() if len(result_list)>0: self.win.append_results(result_list)
def highlightEntity(self,text,entity): if entity.style == self.Default: return expression=QRegExp(entity.pattern) index=expression.indexIn(text) while index>=0: length =expression.matchedLength() self.setFormat(index,length,self.style(entity.style)) index=expression.indexIn(text,index+length)
def highlightBlock(self, text): for rule in self.highlightingRules: 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 highlightRegion(self,text,block): if block.style == self.Default: return startId=0 self.setCurrentBlockState(0) start=QRegExp(block.start) end=QRegExp(block.end) if self.previousBlockState() != 1: startId= start.indexIn(text) while startId >= 0: endId=end.indexIn(text,startId) if endId == -1: self.setCurrentBlockState(1) length=text.length()-startId else: length=endId - startId +end.matchedLength() self.setFormat(startId,length,self.style(block.style)) startId=start.indexIn(text,startId+length)
def highlightBlock(self, text): # for every pattern for pattern, char_format in self.highlightingRules: # Create a regular expression from the retrieved pattern expression = QRegExp(pattern) # Check what index that expression occurs at with the ENTIRE text index = expression.indexIn(text) # While the index is greater than 0 while index >= 0: # Get the length of how long the expression is true, # set the format from the start to the length with # the text format length = expression.matchedLength() self.setFormat(index, length, char_format) # Set index to where the expression ends in the text index = expression.indexIn(text, index + length) self.setCurrentBlockState(0) start_index = 0 if self.previousBlockState() != 1: start_index = self.value_start_expression.indexIn(text) while start_index >= 0: end_index = self.value_end_expression.indexIn(text, start_index) if end_index == -1: self.setCurrentBlockState(1) comment_length = len(text) - start_index else: comment_length = \ end_index - start_index + \ self.value_end_expression.matchedLength() self.setFormat(start_index, comment_length, self.value_format) start_index = self.value_start_expression.indexIn( text, start_index + comment_length)
def highlightBlock(self, text): """ Reimplementation """ block_data = TextBlockData() # Paren index = self.paren.indexIn(text, 0) while index >= 0: matched_paren = str(self.paren.capturedTexts()[0]) info = ParenInfo(matched_paren, index) block_data.insert_paren_info(info) index = self.paren.indexIn(text, index + 1) self.setCurrentBlockUserData(block_data) for pattern, _format in self._rules: 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)
class XMLHighlighter(QSyntaxHighlighter): ''' Class for highlighting xml text inherited from QSyntaxHighlighter reference: http://www.yasinuludag.com/blog/?p=49 ''' def __init__(self, parent=None): super(XMLHighlighter, self).__init__(parent) self.highlightingRules = [] xmlElementFormat = QTextCharFormat() xmlElementFormat.setForeground(QColor("#000070")) #blue self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_]+(?=[\s/>])"), xmlElementFormat)) xmlAttributeFormat = QTextCharFormat() xmlAttributeFormat.setFontItalic(True) xmlAttributeFormat.setForeground(QColor("#177317")) #green self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_]+(?=\\=)"), xmlAttributeFormat)) self.highlightingRules.append((QRegExp("="), xmlAttributeFormat)) self.valueFormat = QTextCharFormat() self.valueFormat.setForeground(QColor("#e35e00")) #orange self.valueStartExpression = QRegExp("\"") self.valueEndExpression = QRegExp("\"(?=[\s></])") singleLineCommentFormat = QTextCharFormat() singleLineCommentFormat.setForeground(QColor("#a0a0a4")) #grey self.highlightingRules.append( (QRegExp("<!--[^\n]*-->"), singleLineCommentFormat)) textFormat = QTextCharFormat() textFormat.setForeground(QColor("#000000")) #black # (?<=...) - lookbehind is not supported self.highlightingRules.append((QRegExp(">(.+)(?=</)"), textFormat)) keywordFormat = QTextCharFormat() keywordFormat.setForeground(QColor("#000070")) #blue keywordFormat.setFontWeight(QFont.Bold) keywordPatterns = ["\\b?xml\\b", "/>", ">", "<", "</"] self.highlightingRules += [(QRegExp(pattern), keywordFormat) for pattern in keywordPatterns] #VIRTUAL FUNCTION WE OVERRIDE THAT DOES ALL THE COLLORING def highlightBlock(self, text): #for every pattern for pattern, format in self.highlightingRules: #Create a regular expression from the retrieved pattern expression = QRegExp(pattern) #Check what index that expression occurs at with the ENTIRE text index = expression.indexIn(text) #While the index is greater than 0 while index >= 0: #Get the length of how long the expression is true, set the format from the start to the length with the text format length = expression.matchedLength() self.setFormat(index, length, format) #Set index to where the expression ends in the text index = expression.indexIn(text, index + length) #HANDLE QUOTATION MARKS NOW.. WE WANT TO START WITH " AND END WITH ".. A THIRD " SHOULD NOT CAUSE THE WORDS INBETWEEN SECOND AND THIRD TO BE COLORED self.setCurrentBlockState(0) startIndex = 0 if self.previousBlockState() != 1: startIndex = self.valueStartExpression.indexIn(text) while startIndex >= 0: endIndex = self.valueEndExpression.indexIn(text, startIndex) if endIndex == -1: self.setCurrentBlockState(1) commentLength = len(text) - startIndex else: commentLength = endIndex - startIndex + self.valueEndExpression.matchedLength( ) self.setFormat(startIndex, commentLength, self.valueFormat) startIndex = self.valueStartExpression.indexIn( text, startIndex + commentLength)
class XMLHighlighter(QSyntaxHighlighter): def __init__(self, parent=None): super(XMLHighlighter, self).__init__(parent) keyword_format = QTextCharFormat() keyword_format.setForeground(Qt.darkMagenta) keyword_patterns = ["\\b?xml\\b", "/>", ">", "<"] self.highlightingRules = [(QRegExp(pattern), keyword_format) for pattern in keyword_patterns] xml_element_format = QTextCharFormat() xml_element_format.setForeground(QColor("#117700")) self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_\-]+(?=[\s/>])"), xml_element_format)) nominatim_area_format = QTextCharFormat() nominatim_area_format.setFontItalic(True) nominatim_area_format.setFontWeight(QFont.Bold) nominatim_area_format.setForeground(QColor("#FF7C00")) self.highlightingRules.append( (QRegExp("\{\{[A-Za-z0-9:, ]*\}\}"), nominatim_area_format)) xml_attribute_format = QTextCharFormat() xml_attribute_format.setFontItalic(True) xml_attribute_format.setForeground(QColor("#2020D2")) self.highlightingRules.append( (QRegExp("\\b[A-Za-z0-9_]+(?=\\=)"), xml_attribute_format)) self.value_format = QTextCharFormat() self.value_format.setForeground(Qt.red) self.value_start_expression = QRegExp("\"") self.value_end_expression = QRegExp("\"(?=[\s></])") single_line_comment_format = QTextCharFormat() single_line_comment_format.setForeground(Qt.gray) self.highlightingRules.append( (QRegExp("<!--[^\n]*-->"), single_line_comment_format)) def highlightBlock(self, text): # for every pattern for pattern, char_format in self.highlightingRules: # Create a regular expression from the retrieved pattern expression = QRegExp(pattern) # Check what index that expression occurs at with the ENTIRE text index = expression.indexIn(text) # While the index is greater than 0 while index >= 0: # Get the length of how long the expression is true, # set the format from the start to the length with # the text format length = expression.matchedLength() self.setFormat(index, length, char_format) # Set index to where the expression ends in the text index = expression.indexIn(text, index + length) self.setCurrentBlockState(0) start_index = 0 if self.previousBlockState() != 1: start_index = self.value_start_expression.indexIn(text) while start_index >= 0: end_index = self.value_end_expression.indexIn(text, start_index) if end_index == -1: self.setCurrentBlockState(1) comment_length = len(text) - start_index else: comment_length = \ end_index - start_index + \ self.value_end_expression.matchedLength() self.setFormat(start_index, comment_length, self.value_format) start_index = self.value_start_expression.indexIn( text, start_index + comment_length)