forked from qt-creator/qt-creator
DiffEditor: refactor loops and const correctness
Change-Id: I21af8db55ff5a012de04b4934de1940babf75058 Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -390,16 +390,15 @@ TextEditorWidget *DiffEditor::rightEditorWidget() const
|
|||||||
void DiffEditor::documentHasChanged()
|
void DiffEditor::documentHasChanged()
|
||||||
{
|
{
|
||||||
Utils::GuardLocker guard(m_ignoreChanges);
|
Utils::GuardLocker guard(m_ignoreChanges);
|
||||||
const QList<FileData> diffFileList = m_document->diffFiles();
|
const QList<FileData> &diffFileList = m_document->diffFiles();
|
||||||
|
|
||||||
updateDescription();
|
updateDescription();
|
||||||
currentView()->setDiff(diffFileList, m_document->baseDirectory());
|
currentView()->setDiff(diffFileList, m_document->baseDirectory());
|
||||||
|
|
||||||
m_entriesComboBox->clear();
|
m_entriesComboBox->clear();
|
||||||
const int count = diffFileList.count();
|
for (const FileData &diffFile : diffFileList) {
|
||||||
for (int i = 0; i < count; i++) {
|
const DiffFileInfo &leftEntry = diffFile.leftFileInfo;
|
||||||
const DiffFileInfo leftEntry = diffFileList.at(i).leftFileInfo;
|
const DiffFileInfo &rightEntry = diffFile.rightFileInfo;
|
||||||
const DiffFileInfo rightEntry = diffFileList.at(i).rightFileInfo;
|
|
||||||
const QString leftShortFileName = Utils::FileName::fromString(leftEntry.fileName).fileName();
|
const QString leftShortFileName = Utils::FileName::fromString(leftEntry.fileName).fileName();
|
||||||
const QString rightShortFileName = Utils::FileName::fromString(rightEntry.fileName).fileName();
|
const QString rightShortFileName = Utils::FileName::fromString(rightEntry.fileName).fileName();
|
||||||
QString itemText;
|
QString itemText;
|
||||||
@@ -528,11 +527,12 @@ void DiffEditor::reloadHasFinished(bool success)
|
|||||||
|
|
||||||
int index = -1;
|
int index = -1;
|
||||||
const QString startupFile = m_document->startupFile();
|
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();
|
const int count = diffFileList.count();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
const DiffFileInfo leftEntry = diffFileList.at(i).leftFileInfo;
|
const FileData &diffFile = diffFileList.at(i);
|
||||||
const DiffFileInfo rightEntry = diffFileList.at(i).rightFileInfo;
|
const DiffFileInfo &leftEntry = diffFile.leftFileInfo;
|
||||||
|
const DiffFileInfo &rightEntry = diffFile.rightFileInfo;
|
||||||
if ((m_currentFileChunk.first.isEmpty()
|
if ((m_currentFileChunk.first.isEmpty()
|
||||||
&& m_currentFileChunk.second.isEmpty()
|
&& m_currentFileChunk.second.isEmpty()
|
||||||
&& startupFile.endsWith(rightEntry.fileName))
|
&& startupFile.endsWith(rightEntry.fileName))
|
||||||
|
@@ -85,16 +85,14 @@ static int commonOverlap(const QString &text1, const QString &text2)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QList<Diff> decode(const QList<Diff> &diffList,
|
static QList<Diff> decode(const QList<Diff> &diffList, const QStringList &lines)
|
||||||
const QStringList &lines)
|
|
||||||
{
|
{
|
||||||
QList<Diff> newDiffList;
|
QList<Diff> newDiffList;
|
||||||
newDiffList.reserve(diffList.count());
|
newDiffList.reserve(diffList.count());
|
||||||
for (int i = 0; i < diffList.count(); i++) {
|
for (Diff diff : diffList) {
|
||||||
Diff diff = diffList.at(i);
|
|
||||||
QString text;
|
QString text;
|
||||||
for (int j = 0; j < diff.text.count(); j++) {
|
for (QChar c : diff.text) {
|
||||||
const int idx = static_cast<ushort>(diff.text.at(j).unicode());
|
const int idx = static_cast<ushort>(c.unicode());
|
||||||
text += lines.value(idx);
|
text += lines.value(idx);
|
||||||
}
|
}
|
||||||
diff.text = text;
|
diff.text = text;
|
||||||
@@ -170,7 +168,7 @@ static QList<Diff> cleanupOverlaps(const QList<Diff> &diffList)
|
|||||||
if (delInsOverlap > thisDiff.text.count() / 2
|
if (delInsOverlap > thisDiff.text.count() / 2
|
||||||
|| delInsOverlap > nextDiff.text.count() / 2) {
|
|| delInsOverlap > nextDiff.text.count() / 2) {
|
||||||
thisDiff.text = thisDiff.text.left(thisDiff.text.count() - delInsOverlap);
|
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);
|
nextDiff.text = nextDiff.text.mid(delInsOverlap);
|
||||||
newDiffList.append(thisDiff);
|
newDiffList.append(thisDiff);
|
||||||
newDiffList.append(equality);
|
newDiffList.append(equality);
|
||||||
@@ -183,7 +181,7 @@ static QList<Diff> cleanupOverlaps(const QList<Diff> &diffList)
|
|||||||
if (insDelOverlap > thisDiff.text.count() / 2
|
if (insDelOverlap > thisDiff.text.count() / 2
|
||||||
|| insDelOverlap > nextDiff.text.count() / 2) {
|
|| insDelOverlap > nextDiff.text.count() / 2) {
|
||||||
nextDiff.text = nextDiff.text.left(nextDiff.text.count() - insDelOverlap);
|
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);
|
thisDiff.text = thisDiff.text.mid(insDelOverlap);
|
||||||
newDiffList.append(nextDiff);
|
newDiffList.append(nextDiff);
|
||||||
newDiffList.append(equality);
|
newDiffList.append(equality);
|
||||||
@@ -265,9 +263,7 @@ void Differ::splitDiffList(const QList<Diff> &diffList,
|
|||||||
leftDiffList->clear();
|
leftDiffList->clear();
|
||||||
rightDiffList->clear();
|
rightDiffList->clear();
|
||||||
|
|
||||||
for (int i = 0; i < diffList.count(); i++) {
|
for (const Diff &diff : diffList) {
|
||||||
const Diff diff = diffList.at(i);
|
|
||||||
|
|
||||||
if (diff.command != Diff::Delete)
|
if (diff.command != Diff::Delete)
|
||||||
rightDiffList->append(diff);
|
rightDiffList->append(diff);
|
||||||
if (diff.command != Diff::Insert)
|
if (diff.command != Diff::Insert)
|
||||||
@@ -400,10 +396,9 @@ static QList<Diff> decodeReducedWhitespace(const QList<Diff> &input,
|
|||||||
QList<Diff> output;
|
QList<Diff> output;
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
QMap<int, QString>::const_iterator it = codeMap.constBegin();
|
auto it = codeMap.constBegin();
|
||||||
const QMap<int, QString>::const_iterator itEnd = codeMap.constEnd();
|
const auto itEnd = codeMap.constEnd();
|
||||||
for (int i = 0; i < input.count(); i++) {
|
for (Diff diff : input) {
|
||||||
Diff diff = input.at(i);
|
|
||||||
const int diffCount = diff.text.count();
|
const int diffCount = diff.text.count();
|
||||||
while ((it != itEnd) && (it.key() < counter + diffCount)) {
|
while ((it != itEnd) && (it.key() < counter + diffCount)) {
|
||||||
const int reversePosition = diffCount + counter - it.key();
|
const int reversePosition = diffCount + counter - it.key();
|
||||||
@@ -439,8 +434,8 @@ void Differ::diffWithWhitespaceReduced(const QString &leftInput,
|
|||||||
|
|
||||||
QMap<int, QString> leftCodeMap;
|
QMap<int, QString> leftCodeMap;
|
||||||
QMap<int, QString> rightCodeMap;
|
QMap<int, QString> rightCodeMap;
|
||||||
const QString leftString = encodeReducedWhitespace(leftInput, &leftCodeMap);
|
const QString &leftString = encodeReducedWhitespace(leftInput, &leftCodeMap);
|
||||||
const QString rightString = encodeReducedWhitespace(rightInput, &rightCodeMap);
|
const QString &rightString = encodeReducedWhitespace(rightInput, &rightCodeMap);
|
||||||
|
|
||||||
Differ differ;
|
Differ differ;
|
||||||
QList<Diff> diffList = differ.diff(leftString, rightString);
|
QList<Diff> diffList = differ.diff(leftString, rightString);
|
||||||
@@ -475,11 +470,11 @@ void Differ::unifiedDiffWithWhitespaceReduced(const QString &leftInput,
|
|||||||
|
|
||||||
QMap<int, QString> leftCodeMap;
|
QMap<int, QString> leftCodeMap;
|
||||||
QMap<int, QString> rightCodeMap;
|
QMap<int, QString> rightCodeMap;
|
||||||
const QString leftString = encodeReducedWhitespace(leftInput, &leftCodeMap);
|
const QString &leftString = encodeReducedWhitespace(leftInput, &leftCodeMap);
|
||||||
const QString rightString = encodeReducedWhitespace(rightInput, &rightCodeMap);
|
const QString &rightString = encodeReducedWhitespace(rightInput, &rightCodeMap);
|
||||||
|
|
||||||
Differ differ;
|
Differ differ;
|
||||||
QList<Diff> diffList = differ.unifiedDiff(leftString, rightString);
|
const QList<Diff> &diffList = differ.unifiedDiff(leftString, rightString);
|
||||||
|
|
||||||
QList<Diff> leftDiffList;
|
QList<Diff> leftDiffList;
|
||||||
QList<Diff> rightDiffList;
|
QList<Diff> rightDiffList;
|
||||||
@@ -606,10 +601,9 @@ static QList<Diff> decodeExpandedWhitespace(const QList<Diff> &input,
|
|||||||
QList<Diff> output;
|
QList<Diff> output;
|
||||||
|
|
||||||
int counter = 0;
|
int counter = 0;
|
||||||
QMap<int, QPair<int, QString> >::const_iterator it = codeMap.constBegin();
|
auto it = codeMap.constBegin();
|
||||||
const QMap<int, QPair<int, QString> >::const_iterator itEnd = codeMap.constEnd();
|
const auto itEnd = codeMap.constEnd();
|
||||||
for (int i = 0; i < input.count(); i++) {
|
for (Diff diff : input) {
|
||||||
Diff diff = input.at(i);
|
|
||||||
const int diffCount = diff.text.count();
|
const int diffCount = diff.text.count();
|
||||||
while ((it != itEnd) && (it.key() < counter + diffCount)) {
|
while ((it != itEnd) && (it.key() < counter + diffCount)) {
|
||||||
const int replacementSize = it.value().first;
|
const int replacementSize = it.value().first;
|
||||||
@@ -667,15 +661,15 @@ static bool diffWithWhitespaceExpandedInEqualities(const QList<Diff> &leftInput,
|
|||||||
QMap<int, QPair<int, QString> > commonRightCodeMap;
|
QMap<int, QPair<int, QString> > commonRightCodeMap;
|
||||||
|
|
||||||
while (l <= leftCount && r <= rightCount) {
|
while (l <= leftCount && r <= rightCount) {
|
||||||
Diff leftDiff = l < leftCount ? leftInput.at(l) : Diff(Diff::Equal);
|
const Diff leftDiff = l < leftCount ? leftInput.at(l) : Diff(Diff::Equal);
|
||||||
Diff rightDiff = r < rightCount ? rightInput.at(r) : Diff(Diff::Equal);
|
const Diff rightDiff = r < rightCount ? rightInput.at(r) : Diff(Diff::Equal);
|
||||||
|
|
||||||
if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
|
if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
|
||||||
QMap<int, QPair<int, QString> > leftCodeMap;
|
QMap<int, QPair<int, QString> > leftCodeMap;
|
||||||
QMap<int, QPair<int, QString> > rightCodeMap;
|
QMap<int, QPair<int, QString> > rightCodeMap;
|
||||||
|
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
QString commonEquality = encodeExpandedWhitespace(leftDiff.text,
|
const QString &commonEquality = encodeExpandedWhitespace(leftDiff.text,
|
||||||
rightDiff.text,
|
rightDiff.text,
|
||||||
&leftCodeMap,
|
&leftCodeMap,
|
||||||
&rightCodeMap,
|
&rightCodeMap,
|
||||||
@@ -684,18 +678,11 @@ static bool diffWithWhitespaceExpandedInEqualities(const QList<Diff> &leftInput,
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// join code map positions with common maps
|
// join code map positions with common maps
|
||||||
QMapIterator<int, QPair<int, QString> > itLeft(leftCodeMap);
|
for (auto it = leftCodeMap.cbegin(), end = leftCodeMap.cend(); it != end; ++it)
|
||||||
while (itLeft.hasNext()) {
|
commonLeftCodeMap.insert(leftText.count() + it.key(), it.value());
|
||||||
itLeft.next();
|
|
||||||
commonLeftCodeMap.insert(leftText.count() + itLeft.key(),
|
for (auto it = rightCodeMap.cbegin(), end = rightCodeMap.cend(); it != end; ++it)
|
||||||
itLeft.value());
|
commonRightCodeMap.insert(rightText.count() + it.key(), it.value());
|
||||||
}
|
|
||||||
QMapIterator<int, QPair<int, QString> > itRight(rightCodeMap);
|
|
||||||
while (itRight.hasNext()) {
|
|
||||||
itRight.next();
|
|
||||||
commonRightCodeMap.insert(rightText.count() + itRight.key(),
|
|
||||||
itRight.value());
|
|
||||||
}
|
|
||||||
|
|
||||||
leftText.append(commonEquality);
|
leftText.append(commonEquality);
|
||||||
rightText.append(commonEquality);
|
rightText.append(commonEquality);
|
||||||
@@ -715,7 +702,7 @@ static bool diffWithWhitespaceExpandedInEqualities(const QList<Diff> &leftInput,
|
|||||||
}
|
}
|
||||||
|
|
||||||
Differ differ;
|
Differ differ;
|
||||||
QList<Diff> diffList = differ.cleanupSemantics(
|
const QList<Diff> &diffList = differ.cleanupSemantics(
|
||||||
differ.diff(leftText, rightText));
|
differ.diff(leftText, rightText));
|
||||||
|
|
||||||
QList<Diff> leftDiffList;
|
QList<Diff> leftDiffList;
|
||||||
@@ -790,16 +777,16 @@ void Differ::ignoreWhitespaceBetweenEqualities(const QList<Diff> &leftInput,
|
|||||||
int r = 0;
|
int r = 0;
|
||||||
|
|
||||||
while (l <= leftCount && r <= rightCount) {
|
while (l <= leftCount && r <= rightCount) {
|
||||||
Diff leftDiff = l < leftCount
|
const Diff leftDiff = l < leftCount
|
||||||
? leftInput.at(l)
|
? leftInput.at(l)
|
||||||
: Diff(Diff::Equal);
|
: Diff(Diff::Equal);
|
||||||
Diff rightDiff = r < rightCount
|
const Diff rightDiff = r < rightCount
|
||||||
? rightInput.at(r)
|
? rightInput.at(r)
|
||||||
: Diff(Diff::Equal);
|
: Diff(Diff::Equal);
|
||||||
|
|
||||||
if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
|
if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
|
||||||
Diff previousLeftDiff = l > 0 ? leftInput.at(l - 1) : Diff(Diff::Equal);
|
const Diff previousLeftDiff = l > 0 ? leftInput.at(l - 1) : Diff(Diff::Equal);
|
||||||
Diff previousRightDiff = r > 0 ? rightInput.at(r - 1) : Diff(Diff::Equal);
|
const Diff previousRightDiff = r > 0 ? rightInput.at(r - 1) : Diff(Diff::Equal);
|
||||||
|
|
||||||
if (previousLeftDiff.command == Diff::Delete
|
if (previousLeftDiff.command == Diff::Delete
|
||||||
&& previousRightDiff.command == Diff::Insert) {
|
&& previousRightDiff.command == Diff::Insert) {
|
||||||
@@ -887,22 +874,22 @@ void Differ::diffBetweenEqualities(const QList<Diff> &leftInput,
|
|||||||
int r = 0;
|
int r = 0;
|
||||||
|
|
||||||
while (l <= leftCount && r <= rightCount) {
|
while (l <= leftCount && r <= rightCount) {
|
||||||
Diff leftDiff = l < leftCount
|
const Diff leftDiff = l < leftCount
|
||||||
? leftInput.at(l)
|
? leftInput.at(l)
|
||||||
: Diff(Diff::Equal);
|
: Diff(Diff::Equal);
|
||||||
Diff rightDiff = r < rightCount
|
const Diff rightDiff = r < rightCount
|
||||||
? rightInput.at(r)
|
? rightInput.at(r)
|
||||||
: Diff(Diff::Equal);
|
: Diff(Diff::Equal);
|
||||||
|
|
||||||
if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
|
if (leftDiff.command == Diff::Equal && rightDiff.command == Diff::Equal) {
|
||||||
Diff previousLeftDiff = l > 0 ? leftInput.at(l - 1) : Diff(Diff::Equal);
|
const Diff previousLeftDiff = l > 0 ? leftInput.at(l - 1) : Diff(Diff::Equal);
|
||||||
Diff previousRightDiff = r > 0 ? rightInput.at(r - 1) : Diff(Diff::Equal);
|
const Diff previousRightDiff = r > 0 ? rightInput.at(r - 1) : Diff(Diff::Equal);
|
||||||
|
|
||||||
if (previousLeftDiff.command == Diff::Delete
|
if (previousLeftDiff.command == Diff::Delete
|
||||||
&& previousRightDiff.command == Diff::Insert) {
|
&& previousRightDiff.command == Diff::Insert) {
|
||||||
Differ differ;
|
Differ differ;
|
||||||
differ.setDiffMode(Differ::CharMode);
|
differ.setDiffMode(Differ::CharMode);
|
||||||
QList<Diff> commonOutput = differ.cleanupSemantics(
|
const QList<Diff> commonOutput = differ.cleanupSemantics(
|
||||||
differ.diff(previousLeftDiff.text, previousRightDiff.text));
|
differ.diff(previousLeftDiff.text, previousRightDiff.text));
|
||||||
|
|
||||||
QList<Diff> outputLeftDiffList;
|
QList<Diff> outputLeftDiffList;
|
||||||
@@ -1008,7 +995,7 @@ QList<Diff> Differ::unifiedDiff(const QString &text1, const QString &text2)
|
|||||||
QString encodedText2;
|
QString encodedText2;
|
||||||
QStringList subtexts = encode(text1, text2, &encodedText1, &encodedText2);
|
QStringList subtexts = encode(text1, text2, &encodedText1, &encodedText2);
|
||||||
|
|
||||||
DiffMode diffMode = m_currentDiffMode;
|
const DiffMode diffMode = m_currentDiffMode;
|
||||||
m_currentDiffMode = CharMode;
|
m_currentDiffMode = CharMode;
|
||||||
|
|
||||||
// Each different subtext is a separate symbol
|
// Each different subtext is a separate symbol
|
||||||
@@ -1223,8 +1210,8 @@ QList<Diff> Differ::diffMyersSplit(
|
|||||||
const QString text21 = text2.left(y);
|
const QString text21 = text2.left(y);
|
||||||
const QString text22 = text2.mid(y);
|
const QString text22 = text2.mid(y);
|
||||||
|
|
||||||
QList<Diff> diffList1 = preprocess1AndDiff(text11, text21);
|
const QList<Diff> &diffList1 = preprocess1AndDiff(text11, text21);
|
||||||
QList<Diff> diffList2 = preprocess1AndDiff(text12, text22);
|
const QList<Diff> &diffList2 = preprocess1AndDiff(text12, text22);
|
||||||
return diffList1 + diffList2;
|
return diffList1 + diffList2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1331,8 +1318,7 @@ QString Differ::encode(const QString &text,
|
|||||||
subtextStart = subtextEnd;
|
subtextStart = subtextEnd;
|
||||||
|
|
||||||
if (lineToCode->contains(line)) {
|
if (lineToCode->contains(line)) {
|
||||||
int code = lineToCode->value(line);
|
codes += QChar(static_cast<ushort>(lineToCode->value(line)));
|
||||||
codes += QChar(static_cast<ushort>(code));
|
|
||||||
} else {
|
} else {
|
||||||
lines->append(line);
|
lines->append(line);
|
||||||
lineToCode->insert(line, lines->count() - 1);
|
lineToCode->insert(line, lines->count() - 1);
|
||||||
@@ -1429,7 +1415,7 @@ QList<Diff> Differ::cleanupSemantics(const QList<Diff> &diffList)
|
|||||||
// equality index, equality data
|
// equality index, equality data
|
||||||
QList<EqualityData> equalities;
|
QList<EqualityData> equalities;
|
||||||
for (int i = 0; i <= diffList.count(); i++) {
|
for (int i = 0; i <= diffList.count(); i++) {
|
||||||
Diff diff = i < diffList.count()
|
const Diff diff = i < diffList.count()
|
||||||
? diffList.at(i)
|
? diffList.at(i)
|
||||||
: Diff(Diff::Equal); // dummy, ensure we process to the end
|
: Diff(Diff::Equal); // dummy, ensure we process to the end
|
||||||
// even when diffList doesn't end with equality
|
// even when diffList doesn't end with equality
|
||||||
@@ -1461,7 +1447,7 @@ QList<Diff> Differ::cleanupSemantics(const QList<Diff> &diffList)
|
|||||||
QMap<int, bool> equalitiesToBeSplit;
|
QMap<int, bool> equalitiesToBeSplit;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (i < equalities.count()) {
|
while (i < equalities.count()) {
|
||||||
const EqualityData data = equalities.at(i);
|
const EqualityData &data = equalities.at(i);
|
||||||
if (data.textCount <= qMax(data.deletesBefore, data.insertsBefore)
|
if (data.textCount <= qMax(data.deletesBefore, data.insertsBefore)
|
||||||
&& data.textCount <= qMax(data.deletesAfter, data.insertsAfter)) {
|
&& data.textCount <= qMax(data.deletesAfter, data.insertsAfter)) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
|
@@ -75,15 +75,10 @@ static QList<DiffSelection> subtractSelection(
|
|||||||
void SelectableTextEditorWidget::setSelections(const QMap<int, QList<DiffSelection> > &selections)
|
void SelectableTextEditorWidget::setSelections(const QMap<int, QList<DiffSelection> > &selections)
|
||||||
{
|
{
|
||||||
m_diffSelections.clear();
|
m_diffSelections.clear();
|
||||||
QMapIterator<int, QList<DiffSelection> > itBlock(selections);
|
for (auto it = selections.cbegin(), end = selections.cend(); it != end; ++it) {
|
||||||
while (itBlock.hasNext()) {
|
const QList<DiffSelection> diffSelections = it.value();
|
||||||
itBlock.next();
|
|
||||||
|
|
||||||
const QList<DiffSelection> diffSelections = itBlock.value();
|
|
||||||
QList<DiffSelection> workingList;
|
QList<DiffSelection> workingList;
|
||||||
for (int i = 0; i < diffSelections.count(); i++) {
|
for (const DiffSelection &diffSelection : diffSelections) {
|
||||||
const DiffSelection &diffSelection = diffSelections.at(i);
|
|
||||||
|
|
||||||
if (diffSelection.start == -1 && diffSelection.end == 0)
|
if (diffSelection.start == -1 && diffSelection.end == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@@ -100,7 +95,7 @@ void SelectableTextEditorWidget::setSelections(const QMap<int, QList<DiffSelecti
|
|||||||
}
|
}
|
||||||
workingList.append(diffSelection);
|
workingList.append(diffSelection);
|
||||||
}
|
}
|
||||||
m_diffSelections.insert(itBlock.key(), workingList);
|
m_diffSelections.insert(it.key(), workingList);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -114,8 +109,7 @@ void SelectableTextEditorWidget::paintBlock(QPainter *painter,
|
|||||||
QList<DiffSelection> diffs = m_diffSelections.value(blockNumber);
|
QList<DiffSelection> diffs = m_diffSelections.value(blockNumber);
|
||||||
|
|
||||||
QVector<QTextLayout::FormatRange> newSelections;
|
QVector<QTextLayout::FormatRange> newSelections;
|
||||||
for (int i = 0; i < diffs.count(); i++) {
|
for (const DiffSelection &diffSelection : diffs) {
|
||||||
const DiffSelection &diffSelection = diffs.at(i);
|
|
||||||
if (diffSelection.format) {
|
if (diffSelection.format) {
|
||||||
QTextLayout::FormatRange formatRange;
|
QTextLayout::FormatRange formatRange;
|
||||||
formatRange.start = qMax(0, diffSelection.start);
|
formatRange.start = qMax(0, diffSelection.start);
|
||||||
|
@@ -152,8 +152,8 @@ SideDiffEditorWidget::SideDiffEditorWidget(QWidget *parent)
|
|||||||
SelectableTextEditorWidget::setDisplaySettings(settings);
|
SelectableTextEditorWidget::setDisplaySettings(settings);
|
||||||
|
|
||||||
connect(this, &TextEditorWidget::tooltipRequested, [this](const QPoint &point, int position) {
|
connect(this, &TextEditorWidget::tooltipRequested, [this](const QPoint &point, int position) {
|
||||||
int block = document()->findBlock(position).blockNumber();
|
const int block = document()->findBlock(position).blockNumber();
|
||||||
auto it = m_fileInfo.constFind(block);
|
const auto it = m_fileInfo.constFind(block);
|
||||||
if (it != m_fileInfo.constEnd())
|
if (it != m_fileInfo.constEnd())
|
||||||
ToolTip::show(point, it.value().fileName, this);
|
ToolTip::show(point, it.value().fileName, this);
|
||||||
else
|
else
|
||||||
@@ -197,8 +197,9 @@ void SideDiffEditorWidget::applyFontSettings()
|
|||||||
|
|
||||||
QString SideDiffEditorWidget::lineNumber(int blockNumber) const
|
QString SideDiffEditorWidget::lineNumber(int blockNumber) const
|
||||||
{
|
{
|
||||||
if (m_lineNumbers.contains(blockNumber))
|
const auto it = m_lineNumbers.constFind(blockNumber);
|
||||||
return QString::number(m_lineNumbers.value(blockNumber));
|
if (it != m_lineNumbers.constEnd())
|
||||||
|
return QString::number(it.value());
|
||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,8 +216,7 @@ bool SideDiffEditorWidget::selectionVisible(int blockNumber) const
|
|||||||
bool SideDiffEditorWidget::replacementVisible(int blockNumber) const
|
bool SideDiffEditorWidget::replacementVisible(int blockNumber) const
|
||||||
{
|
{
|
||||||
return isChunkLine(blockNumber) || (isFileLine(blockNumber)
|
return isChunkLine(blockNumber) || (isFileLine(blockNumber)
|
||||||
&& TextDocumentLayout::isFolded(
|
&& TextDocumentLayout::isFolded(document()->findBlockByNumber(blockNumber)));
|
||||||
document()->findBlockByNumber(blockNumber)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QColor SideDiffEditorWidget::replacementPenColor(int blockNumber) const
|
QColor SideDiffEditorWidget::replacementPenColor(int blockNumber) const
|
||||||
@@ -232,8 +232,8 @@ QString SideDiffEditorWidget::plainTextFromSelection(const QTextCursor &cursor)
|
|||||||
if (startPosition == endPosition)
|
if (startPosition == endPosition)
|
||||||
return QString(); // no selection
|
return QString(); // no selection
|
||||||
|
|
||||||
QTextBlock startBlock = document()->findBlock(startPosition);
|
const QTextBlock startBlock = document()->findBlock(startPosition);
|
||||||
QTextBlock endBlock = document()->findBlock(endPosition);
|
const QTextBlock endBlock = document()->findBlock(endPosition);
|
||||||
QTextBlock block = startBlock;
|
QTextBlock block = startBlock;
|
||||||
QString text;
|
QString text;
|
||||||
bool textInserted = false;
|
bool textInserted = false;
|
||||||
@@ -283,26 +283,17 @@ int SideDiffEditorWidget::blockNumberForFileIndex(int fileIndex) const
|
|||||||
if (fileIndex < 0 || fileIndex >= m_fileInfo.count())
|
if (fileIndex < 0 || fileIndex >= m_fileInfo.count())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
QMap<int, DiffFileInfo>::const_iterator it
|
return (m_fileInfo.constBegin() + fileIndex).key();
|
||||||
= m_fileInfo.constBegin();
|
|
||||||
for (int i = 0; i < fileIndex; i++)
|
|
||||||
++it;
|
|
||||||
|
|
||||||
return it.key();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int SideDiffEditorWidget::fileIndexForBlockNumber(int blockNumber) const
|
int SideDiffEditorWidget::fileIndexForBlockNumber(int blockNumber) const
|
||||||
{
|
{
|
||||||
QMap<int, DiffFileInfo>::const_iterator it = m_fileInfo.constBegin();
|
|
||||||
QMap<int, DiffFileInfo>::const_iterator itEnd = m_fileInfo.constEnd();
|
|
||||||
|
|
||||||
int i = -1;
|
int i = -1;
|
||||||
while (it != itEnd) {
|
for (auto it = m_fileInfo.cbegin(), end = m_fileInfo.cend(); it != end; ++it, ++i) {
|
||||||
if (it.key() > blockNumber)
|
if (it.key() > blockNumber)
|
||||||
break;
|
break;
|
||||||
++it;
|
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,8 +302,7 @@ int SideDiffEditorWidget::chunkIndexForBlockNumber(int blockNumber) const
|
|||||||
if (m_chunkInfo.isEmpty())
|
if (m_chunkInfo.isEmpty())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
QMap<int, QPair<int, int> >::const_iterator it
|
auto it = m_chunkInfo.upperBound(blockNumber);
|
||||||
= m_chunkInfo.upperBound(blockNumber);
|
|
||||||
if (it == m_chunkInfo.constBegin())
|
if (it == m_chunkInfo.constBegin())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@@ -537,8 +527,6 @@ SideBySideDiffEditorWidget::SideBySideDiffEditorWidget(QWidget *parent)
|
|||||||
|
|
||||||
connect(m_leftEditor, &QPlainTextEdit::cursorPositionChanged,
|
connect(m_leftEditor, &QPlainTextEdit::cursorPositionChanged,
|
||||||
this, &SideBySideDiffEditorWidget::leftCursorPositionChanged);
|
this, &SideBySideDiffEditorWidget::leftCursorPositionChanged);
|
||||||
// connect(m_leftEditor->document()->documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged,
|
|
||||||
// this, &SideBySideDiffEditorWidget::leftDocumentSizeChanged);
|
|
||||||
|
|
||||||
connect(m_rightEditor->verticalScrollBar(), &QAbstractSlider::valueChanged,
|
connect(m_rightEditor->verticalScrollBar(), &QAbstractSlider::valueChanged,
|
||||||
this, &SideBySideDiffEditorWidget::rightVSliderChanged);
|
this, &SideBySideDiffEditorWidget::rightVSliderChanged);
|
||||||
@@ -686,9 +674,8 @@ void SideBySideDiffEditorWidget::showDiff()
|
|||||||
QString leftTexts, rightTexts;
|
QString leftTexts, rightTexts;
|
||||||
int blockNumber = 0;
|
int blockNumber = 0;
|
||||||
QChar separator = QLatin1Char('\n');
|
QChar separator = QLatin1Char('\n');
|
||||||
for (int i = 0; i < m_controller.m_contextFileData.count(); i++) {
|
for (const FileData &contextFileData : m_controller.m_contextFileData) {
|
||||||
QString leftText, rightText;
|
QString leftText, rightText;
|
||||||
const FileData &contextFileData = m_controller.m_contextFileData.at(i);
|
|
||||||
|
|
||||||
leftFormats[blockNumber].append(DiffSelection(&m_controller.m_fileLineFormat));
|
leftFormats[blockNumber].append(DiffSelection(&m_controller.m_fileLineFormat));
|
||||||
rightFormats[blockNumber].append(DiffSelection(&m_controller.m_fileLineFormat));
|
rightFormats[blockNumber].append(DiffSelection(&m_controller.m_fileLineFormat));
|
||||||
@@ -710,7 +697,7 @@ void SideBySideDiffEditorWidget::showDiff()
|
|||||||
blockNumber++;
|
blockNumber++;
|
||||||
} else {
|
} else {
|
||||||
for (int j = 0; j < contextFileData.chunks.count(); j++) {
|
for (int j = 0; j < contextFileData.chunks.count(); j++) {
|
||||||
ChunkData chunkData = contextFileData.chunks.at(j);
|
const ChunkData &chunkData = contextFileData.chunks.at(j);
|
||||||
|
|
||||||
int leftLineNumber = chunkData.leftStartingLineNumber;
|
int leftLineNumber = chunkData.leftStartingLineNumber;
|
||||||
int rightLineNumber = chunkData.rightStartingLineNumber;
|
int rightLineNumber = chunkData.rightStartingLineNumber;
|
||||||
@@ -730,8 +717,7 @@ void SideBySideDiffEditorWidget::showDiff()
|
|||||||
m_leftEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
|
m_leftEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
|
||||||
m_rightEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
|
m_rightEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
|
||||||
|
|
||||||
for (int k = 0; k < chunkData.rows.count(); k++) {
|
for (const RowData &rowData : chunkData.rows) {
|
||||||
RowData rowData = chunkData.rows.at(k);
|
|
||||||
TextLineData leftLineData = rowData.leftLine;
|
TextLineData leftLineData = rowData.leftLine;
|
||||||
TextLineData rightLineData = rowData.rightLine;
|
TextLineData rightLineData = rowData.rightLine;
|
||||||
if (leftLineData.textLineType == TextLineData::TextLine) {
|
if (leftLineData.textLineType == TextLineData::TextLine) {
|
||||||
@@ -762,19 +748,17 @@ void SideBySideDiffEditorWidget::showDiff()
|
|||||||
rightFormats[blockNumber].append(DiffSelection(&m_spanLineFormat));
|
rightFormats[blockNumber].append(DiffSelection(&m_spanLineFormat));
|
||||||
}
|
}
|
||||||
|
|
||||||
QMapIterator<int, int> itLeft(leftLineData.changedPositions);
|
for (auto it = leftLineData.changedPositions.cbegin(),
|
||||||
while (itLeft.hasNext()) {
|
end = leftLineData.changedPositions.cend(); it != end; ++it) {
|
||||||
itLeft.next();
|
|
||||||
leftFormats[blockNumber].append(
|
leftFormats[blockNumber].append(
|
||||||
DiffSelection(itLeft.key(), itLeft.value(),
|
DiffSelection(it.key(), it.value(),
|
||||||
&m_controller.m_leftCharFormat));
|
&m_controller.m_leftCharFormat));
|
||||||
}
|
}
|
||||||
|
|
||||||
QMapIterator<int, int> itRight(rightLineData.changedPositions);
|
for (auto it = rightLineData.changedPositions.cbegin(),
|
||||||
while (itRight.hasNext()) {
|
end = rightLineData.changedPositions.cend(); it != end; ++it) {
|
||||||
itRight.next();
|
|
||||||
rightFormats[blockNumber].append(
|
rightFormats[blockNumber].append(
|
||||||
DiffSelection(itRight.key(), itRight.value(),
|
DiffSelection(it.key(), it.value(),
|
||||||
&m_controller.m_rightCharFormat));
|
&m_controller.m_rightCharFormat));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -851,8 +835,7 @@ void SideBySideDiffEditorWidget::slotLeftJumpToOriginalFileRequested(
|
|||||||
// The same file (e.g. in git diff), jump to the line number taken from the right editor.
|
// The same file (e.g. in git diff), jump to the line number taken from the right editor.
|
||||||
// Warning: git show SHA^ vs SHA or git diff HEAD vs Index
|
// Warning: git show SHA^ vs SHA or git diff HEAD vs Index
|
||||||
// (when Working tree has changed in meantime) will not work properly.
|
// (when Working tree has changed in meantime) will not work properly.
|
||||||
for (int i = 0; i < fileData.chunks.count(); i++) {
|
for (const ChunkData &chunkData : fileData.chunks) {
|
||||||
const ChunkData chunkData = fileData.chunks.at(i);
|
|
||||||
|
|
||||||
int leftLineNumber = chunkData.leftStartingLineNumber;
|
int leftLineNumber = chunkData.leftStartingLineNumber;
|
||||||
int rightLineNumber = chunkData.rightStartingLineNumber;
|
int rightLineNumber = chunkData.rightStartingLineNumber;
|
||||||
|
@@ -325,13 +325,13 @@ QString UnifiedDiffEditorWidget::showChunk(const ChunkData &chunkData,
|
|||||||
// could have been added
|
// could have been added
|
||||||
for (int k = 0; k < blockDelta; k++)
|
for (int k = 0; k < blockDelta; k++)
|
||||||
(*selections)[*blockNumber + blockCount + 1 + k].append(&m_controller.m_leftLineFormat);
|
(*selections)[*blockNumber + blockCount + 1 + k].append(&m_controller.m_leftLineFormat);
|
||||||
QMapIterator<int, int> itPos(lineData.changedPositions);
|
|
||||||
while (itPos.hasNext()) {
|
for (auto it = lineData.changedPositions.cbegin(),
|
||||||
itPos.next();
|
end = lineData.changedPositions.cend(); it != end; ++it) {
|
||||||
const int startPos = itPos.key() < 0
|
const int startPos = it.key() < 0
|
||||||
? 1 : itPos.key() + 1;
|
? 1 : it.key() + 1;
|
||||||
const int endPos = itPos.value() < 0
|
const int endPos = it.value() < 0
|
||||||
? itPos.value() : itPos.value() + 1;
|
? it.value() : it.value() + 1;
|
||||||
(*selections)[*blockNumber + blockCount + 1].append(
|
(*selections)[*blockNumber + blockCount + 1].append(
|
||||||
DiffSelection(startPos, endPos, &m_controller.m_leftCharFormat));
|
DiffSelection(startPos, endPos, &m_controller.m_leftCharFormat));
|
||||||
}
|
}
|
||||||
@@ -365,13 +365,13 @@ QString UnifiedDiffEditorWidget::showChunk(const ChunkData &chunkData,
|
|||||||
|
|
||||||
for (int k = 0; k < blockDelta; k++)
|
for (int k = 0; k < blockDelta; k++)
|
||||||
(*selections)[*blockNumber + blockCount + 1 + k].append(&m_controller.m_rightLineFormat);
|
(*selections)[*blockNumber + blockCount + 1 + k].append(&m_controller.m_rightLineFormat);
|
||||||
QMapIterator<int, int> itPos(lineData.changedPositions);
|
|
||||||
while (itPos.hasNext()) {
|
for (auto it = lineData.changedPositions.cbegin(),
|
||||||
itPos.next();
|
end = lineData.changedPositions.cend(); it != end; ++it) {
|
||||||
const int startPos = itPos.key() < 0
|
const int startPos = it.key() < 0
|
||||||
? 1 : itPos.key() + 1;
|
? 1 : it.key() + 1;
|
||||||
const int endPos = itPos.value() < 0
|
const int endPos = it.value() < 0
|
||||||
? itPos.value() : itPos.value() + 1;
|
? it.value() : it.value() + 1;
|
||||||
(*selections)[*blockNumber + blockCount + 1].append
|
(*selections)[*blockNumber + blockCount + 1].append
|
||||||
(DiffSelection(startPos, endPos, &m_controller.m_rightCharFormat));
|
(DiffSelection(startPos, endPos, &m_controller.m_rightCharFormat));
|
||||||
}
|
}
|
||||||
@@ -448,8 +448,7 @@ void UnifiedDiffEditorWidget::showDiff()
|
|||||||
|
|
||||||
QMap<int, QList<DiffSelection> > selections;
|
QMap<int, QList<DiffSelection> > selections;
|
||||||
|
|
||||||
for (int i = 0; i < m_controller.m_contextFileData.count(); i++) {
|
for (const FileData &fileData : m_controller.m_contextFileData) {
|
||||||
const FileData &fileData = m_controller.m_contextFileData.at(i);
|
|
||||||
const QString leftFileInfo = QLatin1String("--- ")
|
const QString leftFileInfo = QLatin1String("--- ")
|
||||||
+ fileData.leftFileInfo.fileName + QLatin1Char('\n');
|
+ fileData.leftFileInfo.fileName + QLatin1Char('\n');
|
||||||
const QString rightFileInfo = QLatin1String("+++ ")
|
const QString rightFileInfo = QLatin1String("+++ ")
|
||||||
@@ -509,28 +508,17 @@ int UnifiedDiffEditorWidget::blockNumberForFileIndex(int fileIndex) const
|
|||||||
if (fileIndex < 0 || fileIndex >= m_fileInfo.count())
|
if (fileIndex < 0 || fileIndex >= m_fileInfo.count())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
QMap<int, QPair<DiffFileInfo, DiffFileInfo> >::const_iterator it
|
return (m_fileInfo.constBegin() + fileIndex).key();
|
||||||
= m_fileInfo.constBegin();
|
|
||||||
for (int i = 0; i < fileIndex; i++)
|
|
||||||
++it;
|
|
||||||
|
|
||||||
return it.key();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int UnifiedDiffEditorWidget::fileIndexForBlockNumber(int blockNumber) const
|
int UnifiedDiffEditorWidget::fileIndexForBlockNumber(int blockNumber) const
|
||||||
{
|
{
|
||||||
QMap<int, QPair<DiffFileInfo, DiffFileInfo> >::const_iterator it
|
|
||||||
= m_fileInfo.constBegin();
|
|
||||||
QMap<int, QPair<DiffFileInfo, DiffFileInfo> >::const_iterator itEnd
|
|
||||||
= m_fileInfo.constEnd();
|
|
||||||
|
|
||||||
int i = -1;
|
int i = -1;
|
||||||
while (it != itEnd) {
|
for (auto it = m_fileInfo.cbegin(), end = m_fileInfo.cend(); it != end; ++it, ++i) {
|
||||||
if (it.key() > blockNumber)
|
if (it.key() > blockNumber)
|
||||||
break;
|
break;
|
||||||
++it;
|
|
||||||
++i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -539,8 +527,7 @@ int UnifiedDiffEditorWidget::chunkIndexForBlockNumber(int blockNumber) const
|
|||||||
if (m_chunkInfo.isEmpty())
|
if (m_chunkInfo.isEmpty())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
QMap<int, QPair<int, int> >::const_iterator it
|
auto it = m_chunkInfo.upperBound(blockNumber);
|
||||||
= m_chunkInfo.upperBound(blockNumber);
|
|
||||||
if (it == m_chunkInfo.constBegin())
|
if (it == m_chunkInfo.constBegin())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
@@ -577,14 +564,12 @@ void UnifiedDiffEditorWidget::jumpToOriginalFile(const QTextCursor &cursor)
|
|||||||
const int leftLineNumber = m_leftLineNumbers.value(blockNumber, -1);
|
const int leftLineNumber = m_leftLineNumbers.value(blockNumber, -1);
|
||||||
if (leftLineNumber >= 0) {
|
if (leftLineNumber >= 0) {
|
||||||
if (leftFileName == rightFileName) {
|
if (leftFileName == rightFileName) {
|
||||||
for (int i = 0; i < fileData.chunks.count(); i++) {
|
for (const ChunkData &chunkData : fileData.chunks) {
|
||||||
const ChunkData chunkData = fileData.chunks.at(i);
|
|
||||||
|
|
||||||
int newLeftLineNumber = chunkData.leftStartingLineNumber;
|
int newLeftLineNumber = chunkData.leftStartingLineNumber;
|
||||||
int newRightLineNumber = chunkData.rightStartingLineNumber;
|
int newRightLineNumber = chunkData.rightStartingLineNumber;
|
||||||
|
|
||||||
for (int j = 0; j < chunkData.rows.count(); j++) {
|
for (const RowData &rowData : chunkData.rows) {
|
||||||
const RowData rowData = chunkData.rows.at(j);
|
|
||||||
if (rowData.leftLine.textLineType == TextLineData::TextLine)
|
if (rowData.leftLine.textLineType == TextLineData::TextLine)
|
||||||
newLeftLineNumber++;
|
newLeftLineNumber++;
|
||||||
if (rowData.rightLine.textLineType == TextLineData::TextLine)
|
if (rowData.rightLine.textLineType == TextLineData::TextLine)
|
||||||
|
Reference in New Issue
Block a user