Skip to content

Commit

Permalink
DiffEditor: refactor loops and const correctness
Browse files Browse the repository at this point in the history
Change-Id: I21af8db55ff5a012de04b4934de1940babf75058
Reviewed-by: Orgad Shaneh <[email protected]>
  • Loading branch information
JarekKobus committed Jun 28, 2017
1 parent f07fda5 commit 228e4a7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 150 deletions.
16 changes: 8 additions & 8 deletions src/plugins/diffeditor/diffeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,15 @@ TextEditorWidget *DiffEditor::rightEditorWidget() const
void DiffEditor::documentHasChanged()
{
Utils::GuardLocker guard(m_ignoreChanges);
const QList<FileData> diffFileList = m_document->diffFiles();
const QList<FileData> &diffFileList = m_document->diffFiles();

updateDescription();
currentView()->setDiff(diffFileList, m_document->baseDirectory());

m_entriesComboBox->clear();
const int count = diffFileList.count();
for (int i = 0; i < count; i++) {
const DiffFileInfo leftEntry = diffFileList.at(i).leftFileInfo;
const DiffFileInfo rightEntry = diffFileList.at(i).rightFileInfo;
for (const FileData &diffFile : diffFileList) {
const DiffFileInfo &leftEntry = diffFile.leftFileInfo;
const DiffFileInfo &rightEntry = diffFile.rightFileInfo;
const QString leftShortFileName = Utils::FileName::fromString(leftEntry.fileName).fileName();
const QString rightShortFileName = Utils::FileName::fromString(rightEntry.fileName).fileName();
QString itemText;
Expand Down Expand Up @@ -528,11 +527,12 @@ void DiffEditor::reloadHasFinished(bool success)

int index = -1;
const QString startupFile = m_document->startupFile();
const QList<FileData> diffFileList = m_document->diffFiles();
const QList<FileData> &diffFileList = m_document->diffFiles();
const int count = diffFileList.count();
for (int i = 0; i < count; i++) {
const DiffFileInfo leftEntry = diffFileList.at(i).leftFileInfo;
const DiffFileInfo rightEntry = diffFileList.at(i).rightFileInfo;
const FileData &diffFile = diffFileList.at(i);
const DiffFileInfo &leftEntry = diffFile.leftFileInfo;
const DiffFileInfo &rightEntry = diffFile.rightFileInfo;
if ((m_currentFileChunk.first.isEmpty()
&& m_currentFileChunk.second.isEmpty()
&& startupFile.endsWith(rightEntry.fileName))
Expand Down
98 changes: 42 additions & 56 deletions src/plugins/diffeditor/differ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ static int commonOverlap(const QString &text1, const QString &text2)
return 0;
}

static QList<Diff> decode(const QList<Diff> &diffList,
const QStringList &lines)
static QList<Diff> decode(const QList<Diff> &diffList, const QStringList &lines)
{
QList<Diff> newDiffList;
newDiffList.reserve(diffList.count());
for (int i = 0; i < diffList.count(); i++) {
Diff diff = diffList.at(i);
for (Diff diff : diffList) {
QString text;
for (int j = 0; j < diff.text.count(); j++) {
const int idx = static_cast<ushort>(diff.text.at(j).unicode());
for (QChar c : diff.text) {
const int idx = static_cast<ushort>(c.unicode());
text += lines.value(idx);
}
diff.text = text;
Expand Down Expand Up @@ -170,7 +168,7 @@ static QList<Diff> cleanupOverlaps(const QList<Diff> &diffList)
if (delInsOverlap > thisDiff.text.count() / 2
|| delInsOverlap > nextDiff.text.count() / 2) {
thisDiff.text = thisDiff.text.left(thisDiff.text.count() - delInsOverlap);
Diff equality = Diff(Diff::Equal, nextDiff.text.left(delInsOverlap));
const Diff equality(Diff::Equal, nextDiff.text.left(delInsOverlap));
nextDiff.text = nextDiff.text.mid(delInsOverlap);
newDiffList.append(thisDiff);
newDiffList.append(equality);
Expand All @@ -183,7 +181,7 @@ static QList<Diff> cleanupOverlaps(const QList<Diff> &diffList)
if (insDelOverlap > thisDiff.text.count() / 2
|| insDelOverlap > nextDiff.text.count() / 2) {
nextDiff.text = nextDiff.text.left(nextDiff.text.count() - insDelOverlap);
Diff equality = Diff(Diff::Equal, thisDiff.text.left(insDelOverlap));
const Diff equality(Diff::Equal, thisDiff.text.left(insDelOverlap));
thisDiff.text = thisDiff.text.mid(insDelOverlap);
newDiffList.append(nextDiff);
newDiffList.append(equality);
Expand Down Expand Up @@ -265,9 +263,7 @@ void Differ::splitDiffList(const QList<Diff> &diffList,
leftDiffList->clear();
rightDiffList->clear();

for (int i = 0; i < diffList.count(); i++) {
const Diff diff = diffList.at(i);

for (const Diff &diff : diffList) {
if (diff.command != Diff::Delete)
rightDiffList->append(diff);
if (diff.command != Diff::Insert)
Expand Down Expand Up @@ -400,10 +396,9 @@ static QList<Diff> decodeReducedWhitespace(const QList<Diff> &input,
QList<Diff> output;

int counter = 0;
QMap<int, QString>::const_iterator it = codeMap.constBegin();
const QMap<int, QString>::const_iterator itEnd = codeMap.constEnd();
for (int i = 0; i < input.count(); i++) {
Diff diff = input.at(i);
auto it = codeMap.constBegin();
const auto itEnd = codeMap.constEnd();
for (Diff diff : input) {
const int diffCount = diff.text.count();
while ((it != itEnd) && (it.key() < counter + diffCount)) {
const int reversePosition = diffCount + counter - it.key();
Expand Down Expand Up @@ -439,8 +434,8 @@ void Differ::diffWithWhitespaceReduced(const QString &leftInput,

QMap<int, QString> leftCodeMap;
QMap<int, QString> rightCodeMap;
const QString leftString = encodeReducedWhitespace(leftInput, &leftCodeMap);
const QString rightString = encodeReducedWhitespace(rightInput, &rightCodeMap);
const QString &leftString = encodeReducedWhitespace(leftInput, &leftCodeMap);
const QString &rightString = encodeReducedWhitespace(rightInput, &rightCodeMap);

Differ differ;
QList<Diff> diffList = differ.diff(leftString, rightString);
Expand Down Expand Up @@ -475,11 +470,11 @@ void Differ::unifiedDiffWithWhitespaceReduced(const QString &leftInput,

QMap<int, QString> leftCodeMap;
QMap<int, QString> rightCodeMap;
const QString leftString = encodeReducedWhitespace(leftInput, &leftCodeMap);
const QString rightString = encodeReducedWhitespace(rightInput, &rightCodeMap);
const QString &leftString = encodeReducedWhitespace(leftInput, &leftCodeMap);
const QString &rightString = encodeReducedWhitespace(rightInput, &rightCodeMap);

Differ differ;
QList<Diff> diffList = differ.unifiedDiff(leftString, rightString);
const QList<Diff> &diffList = differ.unifiedDiff(leftString, rightString);

QList<Diff> leftDiffList;
QList<Diff> rightDiffList;
Expand Down Expand Up @@ -606,10 +601,9 @@ static QList<Diff> decodeExpandedWhitespace(const QList<Diff> &input,
QList<Diff> output;

int counter = 0;
QMap<int, QPair<int, QString> >::const_iterator it = codeMap.constBegin();
const QMap<int, QPair<int, QString> >::const_iterator itEnd = codeMap.constEnd();
for (int i = 0; i < input.count(); i++) {
Diff diff = input.at(i);
auto it = codeMap.constBegin();
const auto itEnd = codeMap.constEnd();
for (Diff diff : input) {
const int diffCount = diff.text.count();
while ((it != itEnd) && (it.key() < counter + diffCount)) {
const int replacementSize = it.value().first;
Expand Down Expand Up @@ -667,15 +661,15 @@ static bool diffWithWhitespaceExpandedInEqualities(const QList<Diff> &leftInput,
QMap<int, QPair<int, QString> > commonRightCodeMap;

while (l <= leftCount && r <= rightCount) {
Diff leftDiff = l < leftCount ? leftInput.at(l) : Diff(Diff::Equal);
Diff rightDiff = r < rightCount ? rightInput.at(r) : Diff(Diff::Equal);
const Diff leftDiff = l < leftCount ? leftInput.at(l) : Diff(Diff::Equal);
const Diff rightDiff = r < rightCount ? rightInput.at(r) : Diff(Diff::Equal);

if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
QMap<int, QPair<int, QString> > leftCodeMap;
QMap<int, QPair<int, QString> > rightCodeMap;

bool ok = false;
QString commonEquality = encodeExpandedWhitespace(leftDiff.text,
const QString &commonEquality = encodeExpandedWhitespace(leftDiff.text,
rightDiff.text,
&leftCodeMap,
&rightCodeMap,
Expand All @@ -684,18 +678,11 @@ static bool diffWithWhitespaceExpandedInEqualities(const QList<Diff> &leftInput,
return false;

// join code map positions with common maps
QMapIterator<int, QPair<int, QString> > itLeft(leftCodeMap);
while (itLeft.hasNext()) {
itLeft.next();
commonLeftCodeMap.insert(leftText.count() + itLeft.key(),
itLeft.value());
}
QMapIterator<int, QPair<int, QString> > itRight(rightCodeMap);
while (itRight.hasNext()) {
itRight.next();
commonRightCodeMap.insert(rightText.count() + itRight.key(),
itRight.value());
}
for (auto it = leftCodeMap.cbegin(), end = leftCodeMap.cend(); it != end; ++it)
commonLeftCodeMap.insert(leftText.count() + it.key(), it.value());

for (auto it = rightCodeMap.cbegin(), end = rightCodeMap.cend(); it != end; ++it)
commonRightCodeMap.insert(rightText.count() + it.key(), it.value());

leftText.append(commonEquality);
rightText.append(commonEquality);
Expand All @@ -715,7 +702,7 @@ static bool diffWithWhitespaceExpandedInEqualities(const QList<Diff> &leftInput,
}

Differ differ;
QList<Diff> diffList = differ.cleanupSemantics(
const QList<Diff> &diffList = differ.cleanupSemantics(
differ.diff(leftText, rightText));

QList<Diff> leftDiffList;
Expand Down Expand Up @@ -790,16 +777,16 @@ void Differ::ignoreWhitespaceBetweenEqualities(const QList<Diff> &leftInput,
int r = 0;

while (l <= leftCount && r <= rightCount) {
Diff leftDiff = l < leftCount
const Diff leftDiff = l < leftCount
? leftInput.at(l)
: Diff(Diff::Equal);
Diff rightDiff = r < rightCount
const Diff rightDiff = r < rightCount
? rightInput.at(r)
: Diff(Diff::Equal);

if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
Diff previousLeftDiff = l > 0 ? leftInput.at(l - 1) : Diff(Diff::Equal);
Diff previousRightDiff = r > 0 ? rightInput.at(r - 1) : Diff(Diff::Equal);
const Diff previousLeftDiff = l > 0 ? leftInput.at(l - 1) : Diff(Diff::Equal);
const Diff previousRightDiff = r > 0 ? rightInput.at(r - 1) : Diff(Diff::Equal);

if (previousLeftDiff.command == Diff::Delete
&& previousRightDiff.command == Diff::Insert) {
Expand Down Expand Up @@ -887,22 +874,22 @@ void Differ::diffBetweenEqualities(const QList<Diff> &leftInput,
int r = 0;

while (l <= leftCount && r <= rightCount) {
Diff leftDiff = l < leftCount
const Diff leftDiff = l < leftCount
? leftInput.at(l)
: Diff(Diff::Equal);
Diff rightDiff = r < rightCount
const Diff rightDiff = r < rightCount
? rightInput.at(r)
: Diff(Diff::Equal);

if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
Diff previousLeftDiff = l > 0 ? leftInput.at(l - 1) : Diff(Diff::Equal);
Diff previousRightDiff = r > 0 ? rightInput.at(r - 1) : Diff(Diff::Equal);
const Diff previousLeftDiff = l > 0 ? leftInput.at(l - 1) : Diff(Diff::Equal);
const Diff previousRightDiff = r > 0 ? rightInput.at(r - 1) : Diff(Diff::Equal);

if (previousLeftDiff.command == Diff::Delete
&& previousRightDiff.command == Diff::Insert) {
Differ differ;
differ.setDiffMode(Differ::CharMode);
QList<Diff> commonOutput = differ.cleanupSemantics(
const QList<Diff> commonOutput = differ.cleanupSemantics(
differ.diff(previousLeftDiff.text, previousRightDiff.text));

QList<Diff> outputLeftDiffList;
Expand Down Expand Up @@ -1008,7 +995,7 @@ QList<Diff> Differ::unifiedDiff(const QString &text1, const QString &text2)
QString encodedText2;
QStringList subtexts = encode(text1, text2, &encodedText1, &encodedText2);

DiffMode diffMode = m_currentDiffMode;
const DiffMode diffMode = m_currentDiffMode;
m_currentDiffMode = CharMode;

// Each different subtext is a separate symbol
Expand Down Expand Up @@ -1223,8 +1210,8 @@ QList<Diff> Differ::diffMyersSplit(
const QString text21 = text2.left(y);
const QString text22 = text2.mid(y);

QList<Diff> diffList1 = preprocess1AndDiff(text11, text21);
QList<Diff> diffList2 = preprocess1AndDiff(text12, text22);
const QList<Diff> &diffList1 = preprocess1AndDiff(text11, text21);
const QList<Diff> &diffList2 = preprocess1AndDiff(text12, text22);
return diffList1 + diffList2;
}

Expand Down Expand Up @@ -1331,8 +1318,7 @@ QString Differ::encode(const QString &text,
subtextStart = subtextEnd;

if (lineToCode->contains(line)) {
int code = lineToCode->value(line);
codes += QChar(static_cast<ushort>(code));
codes += QChar(static_cast<ushort>(lineToCode->value(line)));
} else {
lines->append(line);
lineToCode->insert(line, lines->count() - 1);
Expand Down Expand Up @@ -1429,7 +1415,7 @@ QList<Diff> Differ::cleanupSemantics(const QList<Diff> &diffList)
// equality index, equality data
QList<EqualityData> equalities;
for (int i = 0; i <= diffList.count(); i++) {
Diff diff = i < diffList.count()
const Diff diff = i < diffList.count()
? diffList.at(i)
: Diff(Diff::Equal); // dummy, ensure we process to the end
// even when diffList doesn't end with equality
Expand Down Expand Up @@ -1461,7 +1447,7 @@ QList<Diff> Differ::cleanupSemantics(const QList<Diff> &diffList)
QMap<int, bool> equalitiesToBeSplit;
int i = 0;
while (i < equalities.count()) {
const EqualityData data = equalities.at(i);
const EqualityData &data = equalities.at(i);
if (data.textCount <= qMax(data.deletesBefore, data.insertsBefore)
&& data.textCount <= qMax(data.deletesAfter, data.insertsAfter)) {
if (i > 0) {
Expand Down
16 changes: 5 additions & 11 deletions src/plugins/diffeditor/selectabletexteditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,10 @@ static QList<DiffSelection> subtractSelection(
void SelectableTextEditorWidget::setSelections(const QMap<int, QList<DiffSelection> > &selections)
{
m_diffSelections.clear();
QMapIterator<int, QList<DiffSelection> > itBlock(selections);
while (itBlock.hasNext()) {
itBlock.next();

const QList<DiffSelection> diffSelections = itBlock.value();
for (auto it = selections.cbegin(), end = selections.cend(); it != end; ++it) {
const QList<DiffSelection> diffSelections = it.value();
QList<DiffSelection> workingList;
for (int i = 0; i < diffSelections.count(); i++) {
const DiffSelection &diffSelection = diffSelections.at(i);

for (const DiffSelection &diffSelection : diffSelections) {
if (diffSelection.start == -1 && diffSelection.end == 0)
continue;

Expand All @@ -100,7 +95,7 @@ void SelectableTextEditorWidget::setSelections(const QMap<int, QList<DiffSelecti
}
workingList.append(diffSelection);
}
m_diffSelections.insert(itBlock.key(), workingList);
m_diffSelections.insert(it.key(), workingList);
}
}

Expand All @@ -114,8 +109,7 @@ void SelectableTextEditorWidget::paintBlock(QPainter *painter,
QList<DiffSelection> diffs = m_diffSelections.value(blockNumber);

QVector<QTextLayout::FormatRange> newSelections;
for (int i = 0; i < diffs.count(); i++) {
const DiffSelection &diffSelection = diffs.at(i);
for (const DiffSelection &diffSelection : diffs) {
if (diffSelection.format) {
QTextLayout::FormatRange formatRange;
formatRange.start = qMax(0, diffSelection.start);
Expand Down
Loading

0 comments on commit 228e4a7

Please sign in to comment.