Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/lexers/diff.py: 93%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

42 statements  

1""" 

2 pygments.lexers.diff 

3 ~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexers for diff/patch formats. 

6 

7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11import re 

12 

13from pygments.lexer import RegexLexer, include, bygroups 

14from pygments.token import Text, Comment, Operator, Keyword, Name, Generic, \ 

15 Literal, Whitespace 

16 

17__all__ = ['DiffLexer', 'DarcsPatchLexer', 'WDiffLexer'] 

18 

19 

20class DiffLexer(RegexLexer): 

21 """ 

22 Lexer for unified or context-style diffs or patches. 

23 """ 

24 

25 name = 'Diff' 

26 aliases = ['diff', 'udiff'] 

27 filenames = ['*.diff', '*.patch'] 

28 mimetypes = ['text/x-diff', 'text/x-patch'] 

29 url = 'https://en.wikipedia.org/wiki/Diff' 

30 version_added = '' 

31 

32 tokens = { 

33 'root': [ 

34 (r'( )(.*)(\n)', bygroups(Whitespace, Text, Whitespace)), 

35 (r'(!.*|---)(\n)', bygroups(Generic.Strong, Whitespace)), 

36 (r'((?:< |-).*)(\n)', bygroups(Generic.Deleted, Whitespace)), 

37 (r'((?:> |\+).*)(\n)', bygroups(Generic.Inserted, Whitespace)), 

38 ( 

39 r'(@.*|\d(?:,\d+)?(?:a|c|d)\d+(?:,\d+)?)(\n)', 

40 bygroups(Generic.Subheading, Whitespace), 

41 ), 

42 (r'((?:[Ii]ndex|diff).*)(\n)', bygroups(Generic.Heading, Whitespace)), 

43 (r'(=.*)(\n)', bygroups(Generic.Heading, Whitespace)), 

44 (r'(.*)(\n)', bygroups(Text, Whitespace)), 

45 ] 

46 } 

47 

48 def analyse_text(text): 

49 if text[:7] == 'Index: ': 

50 return True 

51 if text[:5] == 'diff ': 

52 return True 

53 if text[:4] == '--- ': 

54 return 0.9 

55 

56 

57class DarcsPatchLexer(RegexLexer): 

58 """ 

59 DarcsPatchLexer is a lexer for the various versions of the darcs patch 

60 format. Examples of this format are derived by commands such as 

61 ``darcs annotate --patch`` and ``darcs send``. 

62 """ 

63 

64 name = 'Darcs Patch' 

65 aliases = ['dpatch'] 

66 filenames = ['*.dpatch', '*.darcspatch'] 

67 url = 'https://darcs.net' 

68 version_added = '0.10' 

69 

70 DPATCH_KEYWORDS = ('hunk', 'addfile', 'adddir', 'rmfile', 'rmdir', 'move', 

71 'replace') 

72 

73 tokens = { 

74 'root': [ 

75 (r'<', Operator), 

76 (r'>', Operator), 

77 (r'\{', Operator), 

78 (r'\}', Operator), 

79 (r'(\[)((?:TAG )?)(.*)(\n)(.*)(\*\*)(\d+)(\s?)(\])', 

80 bygroups(Operator, Keyword, Name, Whitespace, Name, Operator, 

81 Literal.Date, Whitespace, Operator)), 

82 (r'(\[)((?:TAG )?)(.*)(\n)(.*)(\*\*)(\d+)(\s?)', 

83 bygroups(Operator, Keyword, Name, Whitespace, Name, Operator, 

84 Literal.Date, Whitespace), 'comment'), 

85 (r'New patches:', Generic.Heading), 

86 (r'Context:', Generic.Heading), 

87 (r'Patch bundle hash:', Generic.Heading), 

88 (r'(\s*)({})(.*)(\n)'.format('|'.join(DPATCH_KEYWORDS)), 

89 bygroups(Whitespace, Keyword, Text, Whitespace)), 

90 (r'\+', Generic.Inserted, "insert"), 

91 (r'-', Generic.Deleted, "delete"), 

92 (r'(.*)(\n)', bygroups(Text, Whitespace)), 

93 ], 

94 'comment': [ 

95 (r'[^\]].*\n', Comment), 

96 (r'\]', Operator, "#pop"), 

97 ], 

98 'specialText': [ # darcs add [_CODE_] special operators for clarity 

99 (r'\n', Whitespace, "#pop"), # line-based 

100 (r'\[_[^_]*_]', Operator), 

101 ], 

102 'insert': [ 

103 include('specialText'), 

104 (r'\[', Generic.Inserted), 

105 (r'[^\n\[]+', Generic.Inserted), 

106 ], 

107 'delete': [ 

108 include('specialText'), 

109 (r'\[', Generic.Deleted), 

110 (r'[^\n\[]+', Generic.Deleted), 

111 ], 

112 } 

113 

114 

115class WDiffLexer(RegexLexer): 

116 """ 

117 A wdiff lexer. 

118 

119 Note that: 

120 

121 * It only works with normal output (without options like ``-l``). 

122 * If the target files contain "[-", "-]", "{+", or "+}", 

123 especially they are unbalanced, the lexer will get confused. 

124 """ 

125 

126 name = 'WDiff' 

127 url = 'https://www.gnu.org/software/wdiff/' 

128 aliases = ['wdiff'] 

129 filenames = ['*.wdiff'] 

130 mimetypes = [] 

131 version_added = '2.2' 

132 

133 flags = re.MULTILINE | re.DOTALL 

134 

135 # We can only assume "[-" after "[-" before "-]" is `nested`, 

136 # for instance wdiff to wdiff outputs. We have no way to 

137 # distinct these marker is of wdiff output from original text. 

138 

139 ins_op = r"\{\+" 

140 ins_cl = r"\+\}" 

141 del_op = r"\[\-" 

142 del_cl = r"\-\]" 

143 normal = r'[^{}[\]+-]+' # for performance 

144 tokens = { 

145 'root': [ 

146 (ins_op, Generic.Inserted, 'inserted'), 

147 (del_op, Generic.Deleted, 'deleted'), 

148 (normal, Text), 

149 (r'.', Text), 

150 ], 

151 'inserted': [ 

152 (ins_op, Generic.Inserted, '#push'), 

153 (del_op, Generic.Inserted, '#push'), 

154 (del_cl, Generic.Inserted, '#pop'), 

155 

156 (ins_cl, Generic.Inserted, '#pop'), 

157 (normal, Generic.Inserted), 

158 (r'.', Generic.Inserted), 

159 ], 

160 'deleted': [ 

161 (del_op, Generic.Deleted, '#push'), 

162 (ins_op, Generic.Deleted, '#push'), 

163 (ins_cl, Generic.Deleted, '#pop'), 

164 

165 (del_cl, Generic.Deleted, '#pop'), 

166 (normal, Generic.Deleted), 

167 (r'.', Generic.Deleted), 

168 ], 

169 }