Commit 5219faf0 authored by jkobus's avatar jkobus Committed by Jarek Kobus

Refactor code

Create a common method for a similar code.
Simplify some logical operations.

Change-Id: If25703c51ea96fe5a64698f6fa3ffd6f5cb1b2c5
Reviewed-by: default avatarFriedemann Kleint <Friedemann.Kleint@digia.com>
parent 5f5b7fa4
......@@ -370,6 +370,41 @@ bool DiffEditorWidget::isEqual(const QList<Diff> &diffList, int diffNumber) cons
return false;
}
QList<TextLineData> DiffEditorWidget::assemblyRows(const QStringList &lines,
const QMap<int, int> &lineSpans,
const QMap<int, int> &changedPositions,
QMap<int, int> *outputChangedPositions) const
{
QList<TextLineData> data;
int spanOffset = 0;
int pos = 0;
QMap<int, int>::ConstIterator changedIt = changedPositions.constBegin();
QMap<int, int>::ConstIterator changedEnd = changedPositions.constEnd();
const int lineCount = lines.count();
for (int i = 0; i <= lineCount; i++) {
for (int j = 0; j < lineSpans.value(i); j++) {
data.append(TextLineData(TextLineData::Separator));
spanOffset++;
}
if (i < lineCount) {
const int textLength = lines.at(i).count() + 1;
pos += textLength;
data.append(lines.at(i));
}
while (changedIt != changedEnd) {
if (changedIt.key() >= pos)
break;
const int startPos = changedIt.key() + spanOffset;
const int endPos = changedIt.value() + spanOffset;
if (outputChangedPositions)
outputChangedPositions->insert(startPos, endPos);
++changedIt;
}
}
return data;
}
ChunkData DiffEditorWidget::calculateOriginalData(const QList<Diff> &diffList) const
{
......@@ -404,9 +439,9 @@ ChunkData DiffEditorWidget::calculateOriginalData(const QList<Diff> &diffList) c
const bool equal = isEqual(diffList, i);
if (diff.command == Diff::Insert) {
lastRightLineEqual = lastRightLineEqual ? equal : false;
lastRightLineEqual = lastRightLineEqual && equal;
} else if (diff.command == Diff::Delete) {
lastLeftLineEqual = lastLeftLineEqual ? equal : false;
lastLeftLineEqual = lastLeftLineEqual && equal;
}
const int lastLeftPos = currentLeftPos;
......@@ -428,14 +463,14 @@ ChunkData DiffEditorWidget::calculateOriginalData(const QList<Diff> &diffList) c
currentLeftLineOffset++;
leftLines.append(QString());
currentLeftPos++;
lastLeftLineEqual = line.count() ? equal : true;
lastLeftLineEqual = !line.count() || equal;
}
if (diff.command != Diff::Delete) {
currentRightLine++;
currentRightLineOffset++;
rightLines.append(QString());
currentRightPos++;
lastRightLineEqual = line.count() ? equal : true;
lastRightLineEqual = !line.count() || equal;
}
}
......@@ -502,69 +537,14 @@ ChunkData DiffEditorWidget::calculateOriginalData(const QList<Diff> &diffList) c
}
}
QList<TextLineData> leftData;
int spanOffset = 0;
int pos = 0;
QMap<int, int>::ConstIterator leftChangedIt = leftChangedPositions.constBegin();
for (int i = 0; i < leftLines.count(); i++) {
for (int j = 0; j < leftLineSpans.value(i); j++) {
leftData.append(TextLineData(TextLineData::Separator));
spanOffset++;
}
const int textLength = leftLines.at(i).count() + 1;
pos += textLength;
leftData.append(leftLines.at(i));
while (leftChangedIt != leftChangedPositions.constEnd()) {
if (leftChangedIt.key() >= pos)
break;
const int startPos = leftChangedIt.key() + spanOffset;
const int endPos = leftChangedIt.value() + spanOffset;
chunkData.changedLeftPositions.insert(startPos, endPos);
leftChangedIt++;
}
}
while (leftChangedIt != leftChangedPositions.constEnd()) {
if (leftChangedIt.key() >= pos)
break;
const int startPos = leftChangedIt.key() + spanOffset;
const int endPos = leftChangedIt.value() + spanOffset;
chunkData.changedLeftPositions.insert(startPos, endPos);
leftChangedIt++;
}
QList<TextLineData> rightData;
spanOffset = 0;
pos = 0;
QMap<int, int>::ConstIterator rightChangedIt = rightChangedPositions.constBegin();
for (int i = 0; i < rightLines.count(); i++) {
for (int j = 0; j < rightLineSpans.value(i); j++) {
rightData.append(TextLineData(TextLineData::Separator));
spanOffset++;
}
const int textLength = rightLines.at(i).count() + 1;
pos += textLength;
rightData.append(rightLines.at(i));
while (rightChangedIt != rightChangedPositions.constEnd()) {
if (rightChangedIt.key() >= pos)
break;
const int startPos = rightChangedIt.key() + spanOffset;
const int endPos = rightChangedIt.value() + spanOffset;
chunkData.changedRightPositions.insert(startPos, endPos);
rightChangedIt++;
}
}
while (rightChangedIt != rightChangedPositions.constEnd()) {
if (rightChangedIt.key() >= pos)
break;
const int startPos = rightChangedIt.key() + spanOffset;
const int endPos = rightChangedIt.value() + spanOffset;
chunkData.changedRightPositions.insert(startPos, endPos);
rightChangedIt++;
}
QList<TextLineData> leftData = assemblyRows(leftLines,
leftLineSpans,
leftChangedPositions,
&chunkData.changedLeftPositions);
QList<TextLineData> rightData = assemblyRows(rightLines,
rightLineSpans,
rightChangedPositions,
&chunkData.changedRightPositions);
// fill ending separators
for (int i = leftData.count(); i < rightData.count(); i++)
......
......@@ -129,6 +129,10 @@ private:
QTextCursor &cursor,
const QMap<int, int> &positions) const;
void colorDiff(const FileData &fileData);
QList<TextLineData> assemblyRows(const QStringList &lines,
const QMap<int, int> &lineSpans,
const QMap<int, int> &changedPositions,
QMap<int, int> *outputChangedPositions) const;
ChunkData calculateOriginalData(const QList<Diff> &diffList) const;
FileData calculateContextData(const ChunkData &originalData) const;
void showDiff();
......
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