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 |
22 @return marked pattern (string) |
22 @type str |
|
23 @return marked pattern |
|
24 @rtype str |
23 """ |
25 """ |
24 return "__TERMINAL__:{0}".format(pattern) |
26 return "__TERMINAL__:{0}".format(pattern) |
25 |
27 |
26 |
28 |
27 # Cache the results of re.compile for performance reasons |
29 # Cache the results of re.compile for performance reasons |
58 def createRules(self, *rules): |
61 def createRules(self, *rules): |
59 """ |
62 """ |
60 Public method to create the highlighting rules. |
63 Public method to create the highlighting rules. |
61 |
64 |
62 @param rules set of highlighting rules (list of tuples of rule |
65 @param rules set of highlighting rules (list of tuples of rule |
63 pattern (string) and highlighting format (QTextCharFormat)) |
66 pattern and highlighting format) |
|
67 @type set of tuple of (str, QTextCharFormat) |
64 """ |
68 """ |
65 for _idx, ruleFormat in enumerate(rules): |
69 for _idx, ruleFormat in enumerate(rules): |
66 rule, formats = ruleFormat |
70 rule, formats = ruleFormat |
67 terminal = rule.startswith(TERMINAL("")) |
71 terminal = rule.startswith(TERMINAL("")) |
68 if terminal: |
72 if terminal: |
76 def formats(self, line): |
80 def formats(self, line): |
77 """ |
81 """ |
78 Public method to determine the highlighting formats for a line of |
82 Public method to determine the highlighting formats for a line of |
79 text. |
83 text. |
80 |
84 |
81 @param line text line to be highlighted (string) |
85 @param line text line to be highlighted |
|
86 @type str |
82 @return list of matched highlighting rules (list of tuples of match |
87 @return list of matched highlighting rules (list of tuples of match |
83 object and format (QTextCharFormat)) |
88 object and format) |
|
89 @rtype list of tuple of (re.Match, QTextCharFormat) |
84 """ |
90 """ |
85 matched = [] |
91 matched = [] |
86 for rx, formats, terminal in self._rules: |
92 for rx, formats, terminal in self._rules: |
87 match = rx.match(line) |
93 match = rx.match(line) |
88 if not match: |
94 if not match: |
95 |
101 |
96 def makeFormat(self, fg=None, bg=None, bold=False): |
102 def makeFormat(self, fg=None, bg=None, bold=False): |
97 """ |
103 """ |
98 Public method to generate a format definition. |
104 Public method to generate a format definition. |
99 |
105 |
100 @param fg foreground color (QColor) |
106 @param fg foreground color |
101 @param bg background color (QColor) |
107 @type QColor |
102 @param bold flag indicating bold text (boolean) |
108 @param bg background color |
103 @return format definiton (QTextCharFormat) |
109 @type QColor |
|
110 @param bold flag indicating bold text |
|
111 @type bool |
|
112 @return format definiton |
|
113 @rtype QTextCharFormat |
104 """ |
114 """ |
105 font = Preferences.getEditorOtherFonts("MonospacedFont") |
115 font = Preferences.getEditorOtherFonts("MonospacedFont") |
106 charFormat = QTextCharFormat() |
116 charFormat = QTextCharFormat() |
107 charFormat.setFontFamilies([font.family()]) |
117 charFormat.setFontFamilies([font.family()]) |
108 charFormat.setFontPointSize(font.pointSize()) |
118 charFormat.setFontPointSize(font.pointSize()) |
120 |
130 |
121 def highlightBlock(self, text): |
131 def highlightBlock(self, text): |
122 """ |
132 """ |
123 Public method to highlight a block of text. |
133 Public method to highlight a block of text. |
124 |
134 |
125 @param text text to be highlighted (string) |
135 @param text text to be highlighted |
|
136 @type str |
126 """ |
137 """ |
127 formats = self.formats(text) |
138 formats = self.formats(text) |
128 if not formats: |
139 if not formats: |
129 # nothing matched |
140 # nothing matched |
130 self.setFormat(0, len(text), self.normalFormat) |
141 self.setFormat(0, len(text), self.normalFormat) |