|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a syntax highlighter for unified and context diff outputs. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from E5Gui.E5GenericDiffHighlighter import TERMINAL, E5GenericDiffHighlighter |
|
13 |
|
14 |
|
15 class DiffHighlighter(E5GenericDiffHighlighter): |
|
16 """ |
|
17 Class implementing a diff highlighter for Git. |
|
18 """ |
|
19 def __init__(self, doc): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param doc reference to the text document (QTextDocument) |
|
24 """ |
|
25 super(DiffHighlighter, self).__init__(doc) |
|
26 |
|
27 def generateRules(self): |
|
28 """ |
|
29 Public method to generate the rule set. |
|
30 """ |
|
31 diffHeaderBold = self.makeFormat(fg=self.textColor, |
|
32 bg=self.headerColor, |
|
33 bold=True) |
|
34 diffContext = self.makeFormat(fg=self.textColor, |
|
35 bg=self.contextColor) |
|
36 |
|
37 diffAdded = self.makeFormat(fg=self.textColor, |
|
38 bg=self.addedColor) |
|
39 diffRemoved = self.makeFormat(fg=self.textColor, |
|
40 bg=self.removedColor) |
|
41 diffReplaced = self.makeFormat(fg=self.textColor, |
|
42 bg=self.replacedColor) |
|
43 |
|
44 diffBarRegex = TERMINAL(r'^\*+$') |
|
45 |
|
46 diffOldRegex = TERMINAL(r'^--- ') |
|
47 diffNewRegex = TERMINAL(r'^\+\+\+ |^\*\*\*') |
|
48 diffContextRegex = TERMINAL(r'^@@ ') |
|
49 |
|
50 diffAddedRegex = TERMINAL(r'^[+>]') |
|
51 diffRemovedRegex = TERMINAL(r'^[-<]') |
|
52 diffReplacedRegex = TERMINAL(r'^!') |
|
53 |
|
54 self.createRules((diffBarRegex, diffHeaderBold), |
|
55 (diffOldRegex, diffRemoved), |
|
56 (diffNewRegex, diffAdded), |
|
57 (diffContextRegex, diffContext), |
|
58 (diffAddedRegex, diffAdded), |
|
59 (diffRemovedRegex, diffRemoved), |
|
60 (diffReplacedRegex, diffReplaced), |
|
61 ) |