--- a/src/eric7/EricGui/EricGenericDiffHighlighter.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/EricGui/EricGenericDiffHighlighter.py Wed Jul 13 14:55:47 2022 +0200 @@ -17,12 +17,13 @@ def TERMINAL(pattern): """ Function to mark a pattern as the final one to search for. - + @param pattern pattern to be marked (string) @return marked pattern (string) """ return "__TERMINAL__:{0}".format(pattern) + # Cache the results of re.compile for performance reasons _REGEX_CACHE = {} @@ -31,16 +32,17 @@ """ Class implementing a generic diff highlighter. """ + def __init__(self, doc): """ Constructor - + @param doc reference to the text document (QTextDocument) """ super().__init__(doc) - + self.regenerateRules() - + def __initColours(self): """ Private method to initialize the highlighter colours. @@ -52,30 +54,30 @@ self.contextColor = Preferences.getDiffColour("ContextColor") self.headerColor = Preferences.getDiffColour("HeaderColor") self.whitespaceColor = Preferences.getDiffColour("BadWhitespaceColor") - + def createRules(self, *rules): """ Public method to create the highlighting rules. - + @param rules set of highlighting rules (list of tuples of rule pattern (string) and highlighting format (QTextCharFormat)) """ for _idx, ruleFormat in enumerate(rules): rule, formats = ruleFormat - terminal = rule.startswith(TERMINAL('')) + terminal = rule.startswith(TERMINAL("")) if terminal: - rule = rule[len(TERMINAL('')):] + rule = rule[len(TERMINAL("")) :] try: regex = _REGEX_CACHE[rule] except KeyError: regex = _REGEX_CACHE[rule] = re.compile(rule) self._rules.append((regex, formats, terminal)) - + def formats(self, line): """ Public method to determine the highlighting formats for a line of text. - + @param line text line to be highlighted (string) @return list of matched highlighting rules (list of tuples of match object and format (QTextCharFormat)) @@ -88,13 +90,13 @@ matched.append([match, formats]) if terminal: return matched - + return matched - + def makeFormat(self, fg=None, bg=None, bold=False): """ Public method to generate a format definition. - + @param fg foreground color (QColor) @param bg background color (QColor) @param bold flag indicating bold text (boolean) @@ -104,22 +106,22 @@ charFormat = QTextCharFormat() charFormat.setFontFamilies([font.family()]) charFormat.setFontPointSize(font.pointSize()) - + if fg: charFormat.setForeground(fg) - + if bg: charFormat.setBackground(bg) - + if bold: charFormat.setFontWeight(QFont.Weight.Bold) - + return charFormat - + def highlightBlock(self, text): """ Public method to highlight a block of text. - + @param text text to be highlighted (string) """ formats = self.formats(text) @@ -127,7 +129,7 @@ # nothing matched self.setFormat(0, len(text), self.normalFormat) return - + for match, formatStr in formats: start = match.start() groups = match.groups() @@ -143,29 +145,28 @@ if not group: # empty match continue - + # allow None as a no-op format length = len(group) if formatStr[groupIndex]: - self.setFormat(start, start + length, - formatStr[groupIndex]) + self.setFormat(start, start + length, formatStr[groupIndex]) start += length - + def regenerateRules(self): """ Public method to initialize or regenerate the syntax highlighter rules. """ self.normalFormat = self.makeFormat() - + self.__initColours() - + self._rules = [] self.generateRules() - + def generateRules(self): """ Public method to generate the rule set. - + Note: This method must me implemented by derived syntax highlighters. """