src/eric7/EricGui/EricGenericDiffHighlighter.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9413
80c06d472826
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
15 15
16 16
17 def TERMINAL(pattern): 17 def TERMINAL(pattern):
18 """ 18 """
19 Function to mark a pattern as the final one to search for. 19 Function to mark a pattern as the final one to search for.
20 20
21 @param pattern pattern to be marked (string) 21 @param pattern pattern to be marked (string)
22 @return marked pattern (string) 22 @return marked pattern (string)
23 """ 23 """
24 return "__TERMINAL__:{0}".format(pattern) 24 return "__TERMINAL__:{0}".format(pattern)
25
25 26
26 # Cache the results of re.compile for performance reasons 27 # Cache the results of re.compile for performance reasons
27 _REGEX_CACHE = {} 28 _REGEX_CACHE = {}
28 29
29 30
30 class EricGenericDiffHighlighter(QSyntaxHighlighter): 31 class EricGenericDiffHighlighter(QSyntaxHighlighter):
31 """ 32 """
32 Class implementing a generic diff highlighter. 33 Class implementing a generic diff highlighter.
33 """ 34 """
35
34 def __init__(self, doc): 36 def __init__(self, doc):
35 """ 37 """
36 Constructor 38 Constructor
37 39
38 @param doc reference to the text document (QTextDocument) 40 @param doc reference to the text document (QTextDocument)
39 """ 41 """
40 super().__init__(doc) 42 super().__init__(doc)
41 43
42 self.regenerateRules() 44 self.regenerateRules()
43 45
44 def __initColours(self): 46 def __initColours(self):
45 """ 47 """
46 Private method to initialize the highlighter colours. 48 Private method to initialize the highlighter colours.
47 """ 49 """
48 self.textColor = Preferences.getDiffColour("TextColor") 50 self.textColor = Preferences.getDiffColour("TextColor")
50 self.removedColor = Preferences.getDiffColour("RemovedColor") 52 self.removedColor = Preferences.getDiffColour("RemovedColor")
51 self.replacedColor = Preferences.getDiffColour("ReplacedColor") 53 self.replacedColor = Preferences.getDiffColour("ReplacedColor")
52 self.contextColor = Preferences.getDiffColour("ContextColor") 54 self.contextColor = Preferences.getDiffColour("ContextColor")
53 self.headerColor = Preferences.getDiffColour("HeaderColor") 55 self.headerColor = Preferences.getDiffColour("HeaderColor")
54 self.whitespaceColor = Preferences.getDiffColour("BadWhitespaceColor") 56 self.whitespaceColor = Preferences.getDiffColour("BadWhitespaceColor")
55 57
56 def createRules(self, *rules): 58 def createRules(self, *rules):
57 """ 59 """
58 Public method to create the highlighting rules. 60 Public method to create the highlighting rules.
59 61
60 @param rules set of highlighting rules (list of tuples of rule 62 @param rules set of highlighting rules (list of tuples of rule
61 pattern (string) and highlighting format (QTextCharFormat)) 63 pattern (string) and highlighting format (QTextCharFormat))
62 """ 64 """
63 for _idx, ruleFormat in enumerate(rules): 65 for _idx, ruleFormat in enumerate(rules):
64 rule, formats = ruleFormat 66 rule, formats = ruleFormat
65 terminal = rule.startswith(TERMINAL('')) 67 terminal = rule.startswith(TERMINAL(""))
66 if terminal: 68 if terminal:
67 rule = rule[len(TERMINAL('')):] 69 rule = rule[len(TERMINAL("")) :]
68 try: 70 try:
69 regex = _REGEX_CACHE[rule] 71 regex = _REGEX_CACHE[rule]
70 except KeyError: 72 except KeyError:
71 regex = _REGEX_CACHE[rule] = re.compile(rule) 73 regex = _REGEX_CACHE[rule] = re.compile(rule)
72 self._rules.append((regex, formats, terminal)) 74 self._rules.append((regex, formats, terminal))
73 75
74 def formats(self, line): 76 def formats(self, line):
75 """ 77 """
76 Public method to determine the highlighting formats for a line of 78 Public method to determine the highlighting formats for a line of
77 text. 79 text.
78 80
79 @param line text line to be highlighted (string) 81 @param line text line to be highlighted (string)
80 @return list of matched highlighting rules (list of tuples of match 82 @return list of matched highlighting rules (list of tuples of match
81 object and format (QTextCharFormat)) 83 object and format (QTextCharFormat))
82 """ 84 """
83 matched = [] 85 matched = []
86 if not match: 88 if not match:
87 continue 89 continue
88 matched.append([match, formats]) 90 matched.append([match, formats])
89 if terminal: 91 if terminal:
90 return matched 92 return matched
91 93
92 return matched 94 return matched
93 95
94 def makeFormat(self, fg=None, bg=None, bold=False): 96 def makeFormat(self, fg=None, bg=None, bold=False):
95 """ 97 """
96 Public method to generate a format definition. 98 Public method to generate a format definition.
97 99
98 @param fg foreground color (QColor) 100 @param fg foreground color (QColor)
99 @param bg background color (QColor) 101 @param bg background color (QColor)
100 @param bold flag indicating bold text (boolean) 102 @param bold flag indicating bold text (boolean)
101 @return format definiton (QTextCharFormat) 103 @return format definiton (QTextCharFormat)
102 """ 104 """
103 font = Preferences.getEditorOtherFonts("MonospacedFont") 105 font = Preferences.getEditorOtherFonts("MonospacedFont")
104 charFormat = QTextCharFormat() 106 charFormat = QTextCharFormat()
105 charFormat.setFontFamilies([font.family()]) 107 charFormat.setFontFamilies([font.family()])
106 charFormat.setFontPointSize(font.pointSize()) 108 charFormat.setFontPointSize(font.pointSize())
107 109
108 if fg: 110 if fg:
109 charFormat.setForeground(fg) 111 charFormat.setForeground(fg)
110 112
111 if bg: 113 if bg:
112 charFormat.setBackground(bg) 114 charFormat.setBackground(bg)
113 115
114 if bold: 116 if bold:
115 charFormat.setFontWeight(QFont.Weight.Bold) 117 charFormat.setFontWeight(QFont.Weight.Bold)
116 118
117 return charFormat 119 return charFormat
118 120
119 def highlightBlock(self, text): 121 def highlightBlock(self, text):
120 """ 122 """
121 Public method to highlight a block of text. 123 Public method to highlight a block of text.
122 124
123 @param text text to be highlighted (string) 125 @param text text to be highlighted (string)
124 """ 126 """
125 formats = self.formats(text) 127 formats = self.formats(text)
126 if not formats: 128 if not formats:
127 # nothing matched 129 # nothing matched
128 self.setFormat(0, len(text), self.normalFormat) 130 self.setFormat(0, len(text), self.normalFormat)
129 return 131 return
130 132
131 for match, formatStr in formats: 133 for match, formatStr in formats:
132 start = match.start() 134 start = match.start()
133 groups = match.groups() 135 groups = match.groups()
134 136
135 # No groups in the regex, assume this is a single rule 137 # No groups in the regex, assume this is a single rule
141 # Groups exist, rule is a tuple corresponding to group 143 # Groups exist, rule is a tuple corresponding to group
142 for groupIndex, group in enumerate(groups): 144 for groupIndex, group in enumerate(groups):
143 if not group: 145 if not group:
144 # empty match 146 # empty match
145 continue 147 continue
146 148
147 # allow None as a no-op format 149 # allow None as a no-op format
148 length = len(group) 150 length = len(group)
149 if formatStr[groupIndex]: 151 if formatStr[groupIndex]:
150 self.setFormat(start, start + length, 152 self.setFormat(start, start + length, formatStr[groupIndex])
151 formatStr[groupIndex])
152 start += length 153 start += length
153 154
154 def regenerateRules(self): 155 def regenerateRules(self):
155 """ 156 """
156 Public method to initialize or regenerate the syntax highlighter rules. 157 Public method to initialize or regenerate the syntax highlighter rules.
157 """ 158 """
158 self.normalFormat = self.makeFormat() 159 self.normalFormat = self.makeFormat()
159 160
160 self.__initColours() 161 self.__initColours()
161 162
162 self._rules = [] 163 self._rules = []
163 self.generateRules() 164 self.generateRules()
164 165
165 def generateRules(self): 166 def generateRules(self):
166 """ 167 """
167 Public method to generate the rule set. 168 Public method to generate the rule set.
168 169
169 Note: This method must me implemented by derived syntax 170 Note: This method must me implemented by derived syntax
170 highlighters. 171 highlighters.
171 """ 172 """
172 pass 173 pass

eric ide

mercurial