Commit 228e4a75 authored by Jarek Kobus's avatar Jarek Kobus

DiffEditor: refactor loops and const correctness

Change-Id: I21af8db55ff5a012de04b4934de1940babf75058
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent f07fda59
......@@ -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;
......@@ -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))
......
This diff is collapsed.
......@@ -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;
......@@ -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);
}
}
......@@ -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);
......
......@@ -152,8 +152,8 @@ SideDiffEditorWidget::SideDiffEditorWidget(QWidget *parent)
SelectableTextEditorWidget::setDisplaySettings(settings);
connect(this, &TextEditorWidget::tooltipRequested, [this](const QPoint &point, int position) {
int block = document()->findBlock(position).blockNumber();
auto it = m_fileInfo.constFind(block);
const int block = document()->findBlock(position).blockNumber();
const auto it = m_fileInfo.constFind(block);
if (it != m_fileInfo.constEnd())
ToolTip::show(point, it.value().fileName, this);
else
......@@ -197,8 +197,9 @@ void SideDiffEditorWidget::applyFontSettings()
QString SideDiffEditorWidget::lineNumber(int blockNumber) const
{
if (m_lineNumbers.contains(blockNumber))
return QString::number(m_lineNumbers.value(blockNumber));
const auto it = m_lineNumbers.constFind(blockNumber);
if (it != m_lineNumbers.constEnd())
return QString::number(it.value());
return QString();
}
......@@ -215,8 +216,7 @@ bool SideDiffEditorWidget::selectionVisible(int blockNumber) const
bool SideDiffEditorWidget::replacementVisible(int blockNumber) const
{
return isChunkLine(blockNumber) || (isFileLine(blockNumber)
&& TextDocumentLayout::isFolded(
document()->findBlockByNumber(blockNumber)));
&& TextDocumentLayout::isFolded(document()->findBlockByNumber(blockNumber)));
}
QColor SideDiffEditorWidget::replacementPenColor(int blockNumber) const
......@@ -232,8 +232,8 @@ QString SideDiffEditorWidget::plainTextFromSelection(const QTextCursor &cursor)
if (startPosition == endPosition)
return QString(); // no selection
QTextBlock startBlock = document()->findBlock(startPosition);
QTextBlock endBlock = document()->findBlock(endPosition);
const QTextBlock startBlock = document()->findBlock(startPosition);
const QTextBlock endBlock = document()->findBlock(endPosition);
QTextBlock block = startBlock;
QString text;
bool textInserted = false;
......@@ -283,26 +283,17 @@ int SideDiffEditorWidget::blockNumberForFileIndex(int fileIndex) const
if (fileIndex < 0 || fileIndex >= m_fileInfo.count())
return -1;
QMap<int, DiffFileInfo>::const_iterator it
= m_fileInfo.constBegin();
for (int i = 0; i < fileIndex; i++)
++it;
return it.key();
return (m_fileInfo.constBegin() + fileIndex).key();
}
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;
while (it != itEnd) {
for (auto it = m_fileInfo.cbegin(), end = m_fileInfo.cend(); it != end; ++it, ++i) {
if (it.key() > blockNumber)
break;
++it;
++i;
}
return i;
}
......@@ -311,8 +302,7 @@ int SideDiffEditorWidget::chunkIndexForBlockNumber(int blockNumber) const
if (m_chunkInfo.isEmpty())
return -1;
QMap<int, QPair<int, int> >::const_iterator it
= m_chunkInfo.upperBound(blockNumber);
auto it = m_chunkInfo.upperBound(blockNumber);
if (it == m_chunkInfo.constBegin())
return -1;
......@@ -537,8 +527,6 @@ SideBySideDiffEditorWidget::SideBySideDiffEditorWidget(QWidget *parent)
connect(m_leftEditor, &QPlainTextEdit::cursorPositionChanged,
this, &SideBySideDiffEditorWidget::leftCursorPositionChanged);
// connect(m_leftEditor->document()->documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged,
// this, &SideBySideDiffEditorWidget::leftDocumentSizeChanged);
connect(m_rightEditor->verticalScrollBar(), &QAbstractSlider::valueChanged,
this, &SideBySideDiffEditorWidget::rightVSliderChanged);
......@@ -686,9 +674,8 @@ void SideBySideDiffEditorWidget::showDiff()
QString leftTexts, rightTexts;
int blockNumber = 0;
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;
const FileData &contextFileData = m_controller.m_contextFileData.at(i);
leftFormats[blockNumber].append(DiffSelection(&m_controller.m_fileLineFormat));
rightFormats[blockNumber].append(DiffSelection(&m_controller.m_fileLineFormat));
......@@ -710,7 +697,7 @@ void SideBySideDiffEditorWidget::showDiff()
blockNumber++;
} else {
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 rightLineNumber = chunkData.rightStartingLineNumber;
......@@ -730,8 +717,7 @@ void SideBySideDiffEditorWidget::showDiff()
m_leftEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
m_rightEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
for (int k = 0; k < chunkData.rows.count(); k++) {
RowData rowData = chunkData.rows.at(k);
for (const RowData &rowData : chunkData.rows) {
TextLineData leftLineData = rowData.leftLine;
TextLineData rightLineData = rowData.rightLine;
if (leftLineData.textLineType == TextLineData::TextLine) {
......@@ -762,19 +748,17 @@ void SideBySideDiffEditorWidget::showDiff()
rightFormats[blockNumber].append(DiffSelection(&m_spanLineFormat));
}
QMapIterator<int, int> itLeft(leftLineData.changedPositions);
while (itLeft.hasNext()) {
itLeft.next();
for (auto it = leftLineData.changedPositions.cbegin(),
end = leftLineData.changedPositions.cend(); it != end; ++it) {
leftFormats[blockNumber].append(
DiffSelection(itLeft.key(), itLeft.value(),
DiffSelection(it.key(), it.value(),
&m_controller.m_leftCharFormat));
}
QMapIterator<int, int> itRight(rightLineData.changedPositions);
while (itRight.hasNext()) {
itRight.next();
for (auto it = rightLineData.changedPositions.cbegin(),
end = rightLineData.changedPositions.cend(); it != end; ++it) {
rightFormats[blockNumber].append(
DiffSelection(itRight.key(), itRight.value(),
DiffSelection(it.key(), it.value(),
&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.
// Warning: git show SHA^ vs SHA or git diff HEAD vs Index
// (when Working tree has changed in meantime) will not work properly.
for (int i = 0; i < fileData.chunks.count(); i++) {
const ChunkData chunkData = fileData.chunks.at(i);
for (const ChunkData &chunkData : fileData.chunks) {
int leftLineNumber = chunkData.leftStartingLineNumber;
int rightLineNumber = chunkData.rightStartingLineNumber;
......
......@@ -325,13 +325,13 @@ QString UnifiedDiffEditorWidget::showChunk(const ChunkData &chunkData,
// could have been added
for (int k = 0; k < blockDelta; k++)
(*selections)[*blockNumber + blockCount + 1 + k].append(&m_controller.m_leftLineFormat);
QMapIterator<int, int> itPos(lineData.changedPositions);
while (itPos.hasNext()) {
itPos.next();
const int startPos = itPos.key() < 0
? 1 : itPos.key() + 1;
const int endPos = itPos.value() < 0
? itPos.value() : itPos.value() + 1;
for (auto it = lineData.changedPositions.cbegin(),
end = lineData.changedPositions.cend(); it != end; ++it) {
const int startPos = it.key() < 0
? 1 : it.key() + 1;
const int endPos = it.value() < 0
? it.value() : it.value() + 1;
(*selections)[*blockNumber + blockCount + 1].append(
DiffSelection(startPos, endPos, &m_controller.m_leftCharFormat));
}
......@@ -365,13 +365,13 @@ QString UnifiedDiffEditorWidget::showChunk(const ChunkData &chunkData,
for (int k = 0; k < blockDelta; k++)
(*selections)[*blockNumber + blockCount + 1 + k].append(&m_controller.m_rightLineFormat);
QMapIterator<int, int> itPos(lineData.changedPositions);
while (itPos.hasNext()) {
itPos.next();
const int startPos = itPos.key() < 0
? 1 : itPos.key() + 1;
const int endPos = itPos.value() < 0
? itPos.value() : itPos.value() + 1;
for (auto it = lineData.changedPositions.cbegin(),
end = lineData.changedPositions.cend(); it != end; ++it) {
const int startPos = it.key() < 0
? 1 : it.key() + 1;
const int endPos = it.value() < 0
? it.value() : it.value() + 1;
(*selections)[*blockNumber + blockCount + 1].append
(DiffSelection(startPos, endPos, &m_controller.m_rightCharFormat));
}
......@@ -448,8 +448,7 @@ void UnifiedDiffEditorWidget::showDiff()
QMap<int, QList<DiffSelection> > selections;
for (int i = 0; i < m_controller.m_contextFileData.count(); i++) {
const FileData &fileData = m_controller.m_contextFileData.at(i);
for (const FileData &fileData : m_controller.m_contextFileData) {
const QString leftFileInfo = QLatin1String("--- ")
+ fileData.leftFileInfo.fileName + QLatin1Char('\n');
const QString rightFileInfo = QLatin1String("+++ ")
......@@ -509,28 +508,17 @@ int UnifiedDiffEditorWidget::blockNumberForFileIndex(int fileIndex) const
if (fileIndex < 0 || fileIndex >= m_fileInfo.count())
return -1;
QMap<int, QPair<DiffFileInfo, DiffFileInfo> >::const_iterator it
= m_fileInfo.constBegin();
for (int i = 0; i < fileIndex; i++)
++it;
return it.key();
return (m_fileInfo.constBegin() + fileIndex).key();
}
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;
while (it != itEnd) {
for (auto it = m_fileInfo.cbegin(), end = m_fileInfo.cend(); it != end; ++it, ++i) {
if (it.key() > blockNumber)
break;
++it;
++i;
}
return i;
}
......@@ -539,8 +527,7 @@ int UnifiedDiffEditorWidget::chunkIndexForBlockNumber(int blockNumber) const
if (m_chunkInfo.isEmpty())
return -1;
QMap<int, QPair<int, int> >::const_iterator it
= m_chunkInfo.upperBound(blockNumber);
auto it = m_chunkInfo.upperBound(blockNumber);
if (it == m_chunkInfo.constBegin())
return -1;
......@@ -577,14 +564,12 @@ void UnifiedDiffEditorWidget::jumpToOriginalFile(const QTextCursor &cursor)
const int leftLineNumber = m_leftLineNumbers.value(blockNumber, -1);
if (leftLineNumber >= 0) {
if (leftFileName == rightFileName) {
for (int i = 0; i < fileData.chunks.count(); i++) {
const ChunkData chunkData = fileData.chunks.at(i);
for (const ChunkData &chunkData : fileData.chunks) {
int newLeftLineNumber = chunkData.leftStartingLineNumber;
int newRightLineNumber = chunkData.rightStartingLineNumber;
for (int j = 0; j < chunkData.rows.count(); j++) {
const RowData rowData = chunkData.rows.at(j);
for (const RowData &rowData : chunkData.rows) {
if (rowData.leftLine.textLineType == TextLineData::TextLine)
newLeftLineNumber++;
if (rowData.rightLine.textLineType == TextLineData::TextLine)
......
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