Commit d03927f3 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Jarek Kobus

DiffEditor: Fix no newline handling

Change-Id: I3278ae80d7ee010942122723b3e8bb8c729e7182
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent 121dfaec
......@@ -297,6 +297,25 @@ void DiffEditor::Internal::DiffEditorPlugin::testMakePatch_data()
///////////
rows.clear();
rows << RowData(_("ABCD"));
rows << RowData(_(""), TextLineData::Separator);
rows << RowData(_(""), TextLineData::Separator);
chunk.rows = rows;
patchText = header + _("@@ -1,2 +1,1 @@\n"
"-ABCD\n"
"-\n"
"+ABCD\n"
"\\ No newline at end of file\n");
QTest::newRow("Two last EOLs removed") << chunk
<< fileName
<< fileName
<< true
<< patchText;
///////////
rows.clear();
rows << RowData(_("ABCD"));
rows << RowData(TextLineData::Separator, _(""));
......@@ -418,6 +437,26 @@ void DiffEditor::Internal::DiffEditorPlugin::testMakePatch_data()
<< fileName
<< false
<< patchText;
///////////
rows.clear();
rows << RowData(_("ABCD"));
rows << RowData(TextLineData::Separator, _(""));
rows << RowData(_(""), _("EFGH"));
chunk.rows = rows;
patchText = header + _("@@ -1,1 +1,3 @@\n"
" ABCD\n"
"+\n"
"+EFGH\n"
"\\ No newline at end of file\n");
QTest::newRow("Blank line followed by No newline")
<< chunk
<< fileName
<< fileName
<< true
<< patchText;
}
void DiffEditor::Internal::DiffEditorPlugin::testMakePatch()
......@@ -748,6 +787,37 @@ void DiffEditor::Internal::DiffEditorPlugin::testReadPatch_data()
QTest::newRow("2 chunks - first ends with blank line") << patch
<< fileDataList4;
//////////////
patch = _("diff --git a/file foo.txt b/file foo.txt\n"
"index 1234567..9876543 100644\n"
"--- a/file foo.txt\n"
"+++ b/file foo.txt\n"
"@@ -1,1 +1,3 @@ void DiffEditor::ctor()\n"
" ABCD\n"
"+\n"
"+EFGH\n"
"\\ No newline at end of file\n");
fileData1.leftFileInfo = DiffFileInfo(_("file foo.txt"), _("1234567"));
fileData1.rightFileInfo = DiffFileInfo(_("file foo.txt"), _("9876543"));
fileData1.fileOperation = FileData::ChangeFile;
chunkData1.leftStartingLineNumber = 0;
chunkData1.rightStartingLineNumber = 0;
rows1.clear();
rows1 << RowData(_("ABCD"));
rows1 << RowData(TextLineData::Separator, _(""));
rows1 << RowData(_(""), _("EFGH"));
chunkData1.rows = rows1;
fileData1.chunks.clear();
fileData1.chunks << chunkData1;
QList<FileData> fileDataList5;
fileDataList5 << fileData1;
QTest::newRow("Blank line followed by No newline") << patch
<< fileDataList5;
}
void DiffEditor::Internal::DiffEditorPlugin::testReadPatch()
......
......@@ -362,15 +362,33 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,
int rightLineCount = 0;
QList<TextLineData> leftBuffer, rightBuffer;
int lastEqualRow = -1;
int rowToBeSplit = -1;
if (lastChunk) {
for (int i = chunkData.rows.count(); i > 0; i--) {
if (chunkData.rows.at(i - 1).equal) {
if (i != chunkData.rows.count())
lastEqualRow = i - 1;
// Detect the case when the last equal line is followed by
// only separators on left or on right. In that case
// the last equal line needs to be split.
const int rowCount = chunkData.rows.count();
int i = 0;
for (i = rowCount; i > 0; i--) {
const RowData &rowData = chunkData.rows.at(i - 1);
if (rowData.leftLine.textLineType != TextLineData::Separator
|| rowData.rightLine.textLineType != TextLineData::TextLine)
break;
}
}
const int leftSeparator = i;
for (i = rowCount; i > 0; i--) {
const RowData &rowData = chunkData.rows.at(i - 1);
if (rowData.rightLine.textLineType != TextLineData::Separator
|| rowData.leftLine.textLineType != TextLineData::TextLine)
break;
}
const int rightSeparator = i;
const int commonSeparator = qMin(leftSeparator, rightSeparator);
if (commonSeparator > 0
&& commonSeparator < rowCount
&& chunkData.rows.at(commonSeparator - 1).equal)
rowToBeSplit = commonSeparator - 1;
}
for (int i = 0; i <= chunkData.rows.count(); i++) {
......@@ -379,7 +397,7 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,
: RowData(TextLineData(TextLineData::Separator)); // dummy,
// ensure we process buffers to the end.
// rowData will be equal
if (rowData.equal && i != lastEqualRow) {
if (rowData.equal && i != rowToBeSplit) {
if (leftBuffer.count()) {
for (int j = 0; j < leftBuffer.count(); j++) {
const QString line = makePatchLine(QLatin1Char('-'),
......@@ -530,8 +548,6 @@ static QList<RowData> readLines(const QString &patch,
Diff &last = diffList.last();
if (last.text.isEmpty())
break;
if (last.text.at(0) == newLine) // there is a new line
break;
if (last.command == Diff::Equal) {
if (noNewLineInEqual >= 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment