1 // Scintilla source code edit control
2 /** @file LexDiff.cxx
3  ** Lexer for diff results.
4  **/
5 // Copyright 1998-2001 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
7 
8 #include <stdlib.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include <stdarg.h>
12 #include <assert.h>
13 #include <ctype.h>
14 
15 #include "ILexer.h"
16 #include "Scintilla.h"
17 #include "SciLexer.h"
18 
19 #include "WordList.h"
20 #include "LexAccessor.h"
21 #include "Accessor.h"
22 #include "StyleContext.h"
23 #include "CharacterSet.h"
24 #include "LexerModule.h"
25 
26 using namespace Scintilla;
27 
AtEOL(Accessor & styler,Sci_PositionU i)28 static inline bool AtEOL(Accessor &styler, Sci_PositionU i) {
29 	return (styler[i] == '\n') ||
30 	       ((styler[i] == '\r') && (styler.SafeGetCharAt(i + 1) != '\n'));
31 }
32 
33 #define DIFF_BUFFER_START_SIZE 16
34 // Note that ColouriseDiffLine analyzes only the first DIFF_BUFFER_START_SIZE
35 // characters of each line to classify the line.
36 
ColouriseDiffLine(char * lineBuffer,Sci_Position endLine,Accessor & styler)37 static void ColouriseDiffLine(char *lineBuffer, Sci_Position endLine, Accessor &styler) {
38 	// It is needed to remember the current state to recognize starting
39 	// comment lines before the first "diff " or "--- ". If a real
40 	// difference starts then each line starting with ' ' is a whitespace
41 	// otherwise it is considered a comment (Only in..., Binary file...)
42 	if (0 == strncmp(lineBuffer, "diff ", 5)) {
43 		styler.ColourTo(endLine, SCE_DIFF_COMMAND);
44 	} else if (0 == strncmp(lineBuffer, "Index: ", 7)) {  // For subversion's diff
45 		styler.ColourTo(endLine, SCE_DIFF_COMMAND);
46 	} else if (0 == strncmp(lineBuffer, "---", 3) && lineBuffer[3] != '-') {
47 		// In a context diff, --- appears in both the header and the position markers
48 		if (lineBuffer[3] == ' ' && atoi(lineBuffer + 4) && !strchr(lineBuffer, '/'))
49 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
50 		else if (lineBuffer[3] == '\r' || lineBuffer[3] == '\n')
51 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
52 		else if (lineBuffer[3] == ' ')
53 			styler.ColourTo(endLine, SCE_DIFF_HEADER);
54 		else
55 			styler.ColourTo(endLine, SCE_DIFF_DELETED);
56 	} else if (0 == strncmp(lineBuffer, "+++ ", 4)) {
57 		// I don't know of any diff where "+++ " is a position marker, but for
58 		// consistency, do the same as with "--- " and "*** ".
59 		if (atoi(lineBuffer+4) && !strchr(lineBuffer, '/'))
60 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
61 		else
62 			styler.ColourTo(endLine, SCE_DIFF_HEADER);
63 	} else if (0 == strncmp(lineBuffer, "====", 4)) {  // For p4's diff
64 		styler.ColourTo(endLine, SCE_DIFF_HEADER);
65 	} else if (0 == strncmp(lineBuffer, "***", 3)) {
66 		// In a context diff, *** appears in both the header and the position markers.
67 		// Also ******** is a chunk header, but here it's treated as part of the
68 		// position marker since there is no separate style for a chunk header.
69 		if (lineBuffer[3] == ' ' && atoi(lineBuffer+4) && !strchr(lineBuffer, '/'))
70 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
71 		else if (lineBuffer[3] == '*')
72 			styler.ColourTo(endLine, SCE_DIFF_POSITION);
73 		else
74 			styler.ColourTo(endLine, SCE_DIFF_HEADER);
75 	} else if (0 == strncmp(lineBuffer, "? ", 2)) {    // For difflib
76 		styler.ColourTo(endLine, SCE_DIFF_HEADER);
77 	} else if (lineBuffer[0] == '@') {
78 		styler.ColourTo(endLine, SCE_DIFF_POSITION);
79 	} else if (lineBuffer[0] >= '0' && lineBuffer[0] <= '9') {
80 		styler.ColourTo(endLine, SCE_DIFF_POSITION);
81 	} else if (0 == strncmp(lineBuffer, "++", 2)) {
82 		styler.ColourTo(endLine, SCE_DIFF_PATCH_ADD);
83 	} else if (0 == strncmp(lineBuffer, "+-", 2)) {
84 		styler.ColourTo(endLine, SCE_DIFF_PATCH_DELETE);
85 	} else if (0 == strncmp(lineBuffer, "-+", 2)) {
86 		styler.ColourTo(endLine, SCE_DIFF_REMOVED_PATCH_ADD);
87 	} else if (0 == strncmp(lineBuffer, "--", 2)) {
88 		styler.ColourTo(endLine, SCE_DIFF_REMOVED_PATCH_DELETE);
89 	} else if (lineBuffer[0] == '-' || lineBuffer[0] == '<') {
90 		styler.ColourTo(endLine, SCE_DIFF_DELETED);
91 	} else if (lineBuffer[0] == '+' || lineBuffer[0] == '>') {
92 		styler.ColourTo(endLine, SCE_DIFF_ADDED);
93 	} else if (lineBuffer[0] == '!') {
94 		styler.ColourTo(endLine, SCE_DIFF_CHANGED);
95 	} else if (lineBuffer[0] != ' ') {
96 		styler.ColourTo(endLine, SCE_DIFF_COMMENT);
97 	} else {
98 		styler.ColourTo(endLine, SCE_DIFF_DEFAULT);
99 	}
100 }
101 
ColouriseDiffDoc(Sci_PositionU startPos,Sci_Position length,int,WordList * [],Accessor & styler)102 static void ColouriseDiffDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
103 	char lineBuffer[DIFF_BUFFER_START_SIZE] = "";
104 	styler.StartAt(startPos);
105 	styler.StartSegment(startPos);
106 	Sci_PositionU linePos = 0;
107 	for (Sci_PositionU i = startPos; i < startPos + length; i++) {
108 		if (AtEOL(styler, i)) {
109 			if (linePos < DIFF_BUFFER_START_SIZE) {
110 				lineBuffer[linePos] = 0;
111 			}
112 			ColouriseDiffLine(lineBuffer, i, styler);
113 			linePos = 0;
114 		} else if (linePos < DIFF_BUFFER_START_SIZE - 1) {
115 			lineBuffer[linePos++] = styler[i];
116 		} else if (linePos == DIFF_BUFFER_START_SIZE - 1) {
117 			lineBuffer[linePos++] = 0;
118 		}
119 	}
120 	if (linePos > 0) {	// Last line does not have ending characters
121 		if (linePos < DIFF_BUFFER_START_SIZE) {
122 			lineBuffer[linePos] = 0;
123 		}
124 		ColouriseDiffLine(lineBuffer, startPos + length - 1, styler);
125 	}
126 }
127 
FoldDiffDoc(Sci_PositionU startPos,Sci_Position length,int,WordList * [],Accessor & styler)128 static void FoldDiffDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
129 	Sci_Position curLine = styler.GetLine(startPos);
130 	Sci_Position curLineStart = styler.LineStart(curLine);
131 	int prevLevel = curLine > 0 ? styler.LevelAt(curLine - 1) : SC_FOLDLEVELBASE;
132 	int nextLevel;
133 
134 	do {
135 		const int lineType = styler.StyleAt(curLineStart);
136 		if (lineType == SCE_DIFF_COMMAND)
137 			nextLevel = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
138 		else if (lineType == SCE_DIFF_HEADER)
139 			nextLevel = (SC_FOLDLEVELBASE + 1) | SC_FOLDLEVELHEADERFLAG;
140 		else if (lineType == SCE_DIFF_POSITION && styler[curLineStart] != '-')
141 			nextLevel = (SC_FOLDLEVELBASE + 2) | SC_FOLDLEVELHEADERFLAG;
142 		else if (prevLevel & SC_FOLDLEVELHEADERFLAG)
143 			nextLevel = (prevLevel & SC_FOLDLEVELNUMBERMASK) + 1;
144 		else
145 			nextLevel = prevLevel;
146 
147 		if ((nextLevel & SC_FOLDLEVELHEADERFLAG) && (nextLevel == prevLevel))
148 			styler.SetLevel(curLine-1, prevLevel & ~SC_FOLDLEVELHEADERFLAG);
149 
150 		styler.SetLevel(curLine, nextLevel);
151 		prevLevel = nextLevel;
152 
153 		curLineStart = styler.LineStart(++curLine);
154 	} while (static_cast<Sci_Position>(startPos)+length > curLineStart);
155 }
156 
157 static const char *const emptyWordListDesc[] = {
158 	0
159 };
160 
161 LexerModule lmDiff(SCLEX_DIFF, ColouriseDiffDoc, "diff", FoldDiffDoc, emptyWordListDesc);
162