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