2013-02-15 12:49:50 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-02-15 12:49:50 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2013-02-15 12:49:50 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2013-02-15 12:49:50 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
#include "sidebysidediffeditorwidget.h"
|
2014-02-13 16:43:28 +01:00
|
|
|
#include "selectabletexteditorwidget.h"
|
2017-05-09 12:19:11 +02:00
|
|
|
#include "diffeditorconstants.h"
|
2015-03-10 14:19:38 +01:00
|
|
|
#include "diffeditordocument.h"
|
2014-02-28 10:40:20 +01:00
|
|
|
#include "diffutils.h"
|
2014-02-24 11:10:17 +01:00
|
|
|
|
2015-02-26 13:22:35 +01:00
|
|
|
#include <QMenu>
|
2016-07-13 15:55:18 +02:00
|
|
|
#include <QPainter>
|
|
|
|
|
#include <QScrollBar>
|
|
|
|
|
#include <QTextBlock>
|
2015-03-10 14:19:38 +01:00
|
|
|
#include <QVBoxLayout>
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
2016-07-13 15:55:18 +02:00
|
|
|
#include <texteditor/textdocumentlayout.h>
|
2013-02-15 12:49:50 +01:00
|
|
|
#include <texteditor/texteditorsettings.h>
|
2013-06-05 15:25:42 +02:00
|
|
|
#include <texteditor/fontsettings.h>
|
2013-06-03 15:17:34 +02:00
|
|
|
#include <texteditor/displaysettings.h>
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2017-05-09 12:19:11 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2013-05-24 13:22:46 +02:00
|
|
|
#include <coreplugin/minisplitter.h>
|
2013-08-14 13:52:13 +02:00
|
|
|
|
2013-06-18 14:09:54 +02:00
|
|
|
#include <utils/tooltip/tooltip.h>
|
|
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
using namespace Core;
|
2013-02-15 12:49:50 +01:00
|
|
|
using namespace TextEditor;
|
2014-08-28 18:46:39 +02:00
|
|
|
using namespace Utils;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-02-21 17:03:00 +01:00
|
|
|
namespace DiffEditor {
|
2015-01-30 14:55:56 +01:00
|
|
|
namespace Internal {
|
2015-01-30 14:46:20 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
class SideDiffEditorWidget : public SelectableTextEditorWidget
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2014-01-30 14:27:50 +01:00
|
|
|
SideDiffEditorWidget(QWidget *parent = 0);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, file info
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, DiffFileInfo> fileInfo() const { return m_fileInfo; }
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-04-26 11:50:10 +02:00
|
|
|
void setLineNumber(int blockNumber, int lineNumber);
|
2014-02-13 16:43:28 +01:00
|
|
|
void setFileInfo(int blockNumber, const DiffFileInfo &fileInfo);
|
2017-06-02 10:13:36 +03:00
|
|
|
void setSkippedLines(int blockNumber, int skippedLines, const QString &contextInfo = QString()) {
|
|
|
|
|
m_skippedLines[blockNumber] = qMakePair(skippedLines, contextInfo);
|
2014-02-13 16:43:28 +01:00
|
|
|
setSeparator(blockNumber, true);
|
|
|
|
|
}
|
|
|
|
|
void setChunkIndex(int startBlockNumber, int blockCount, int chunkIndex);
|
|
|
|
|
void setSeparator(int blockNumber, bool separator) {
|
|
|
|
|
m_separators[blockNumber] = separator;
|
|
|
|
|
}
|
|
|
|
|
bool isFileLine(int blockNumber) const {
|
|
|
|
|
return m_fileInfo.contains(blockNumber);
|
|
|
|
|
}
|
2013-05-23 13:36:27 +02:00
|
|
|
int blockNumberForFileIndex(int fileIndex) const;
|
|
|
|
|
int fileIndexForBlockNumber(int blockNumber) const;
|
2014-02-13 16:43:28 +01:00
|
|
|
int chunkIndexForBlockNumber(int blockNumber) const;
|
|
|
|
|
bool isChunkLine(int blockNumber) const {
|
|
|
|
|
return m_skippedLines.contains(blockNumber);
|
|
|
|
|
}
|
2013-05-07 14:02:08 +02:00
|
|
|
void clearAll(const QString &message);
|
2013-04-25 17:37:20 +02:00
|
|
|
void clearAllData();
|
2015-01-30 16:59:25 +01:00
|
|
|
void saveState();
|
|
|
|
|
void restoreState();
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2015-06-01 17:55:31 +02:00
|
|
|
void setDisplaySettings(const DisplaySettings &ds) override;
|
2013-06-03 15:17:34 +02:00
|
|
|
|
2013-07-01 13:15:27 +02:00
|
|
|
signals:
|
|
|
|
|
void jumpToOriginalFileRequested(int diffFileIndex,
|
|
|
|
|
int lineNumber,
|
|
|
|
|
int columnNumber);
|
2014-02-13 16:43:28 +01:00
|
|
|
void contextMenuRequested(QMenu *menu,
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int chunkIndex);
|
2013-07-01 13:15:27 +02:00
|
|
|
|
2013-02-15 12:49:50 +01:00
|
|
|
protected:
|
2015-06-01 17:55:31 +02:00
|
|
|
int extraAreaWidth(int *markWidthPtr = 0) const override {
|
2014-02-13 16:43:28 +01:00
|
|
|
return SelectableTextEditorWidget::extraAreaWidth(markWidthPtr);
|
|
|
|
|
}
|
2015-06-01 17:55:31 +02:00
|
|
|
void applyFontSettings() override;
|
|
|
|
|
|
|
|
|
|
QString lineNumber(int blockNumber) const override;
|
|
|
|
|
int lineNumberDigits() const override;
|
|
|
|
|
bool selectionVisible(int blockNumber) const override;
|
|
|
|
|
bool replacementVisible(int blockNumber) const override;
|
|
|
|
|
QColor replacementPenColor(int blockNumber) const override;
|
|
|
|
|
QString plainTextFromSelection(const QTextCursor &cursor) const override;
|
|
|
|
|
void mouseDoubleClickEvent(QMouseEvent *e) override;
|
2017-07-30 08:53:27 +03:00
|
|
|
void keyPressEvent(QKeyEvent *e) override;
|
2015-06-01 17:55:31 +02:00
|
|
|
void contextMenuEvent(QContextMenuEvent *e) override;
|
|
|
|
|
void paintEvent(QPaintEvent *e) override;
|
|
|
|
|
void scrollContentsBy(int dx, int dy) override;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
|
|
|
|
private:
|
2013-06-05 15:25:42 +02:00
|
|
|
void paintSeparator(QPainter &painter, QColor &color, const QString &text,
|
|
|
|
|
const QTextBlock &block, int top);
|
2013-04-26 11:50:10 +02:00
|
|
|
void jumpToOriginalFile(const QTextCursor &cursor);
|
2013-04-25 17:37:20 +02:00
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, visual line number.
|
2013-04-26 11:50:10 +02:00
|
|
|
QMap<int, int> m_lineNumbers;
|
2016-07-13 15:55:18 +02:00
|
|
|
int m_lineNumberDigits = 1;
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, fileInfo. Set for file lines only.
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, DiffFileInfo> m_fileInfo;
|
2017-06-02 10:13:36 +03:00
|
|
|
// block number, skipped lines and context info. Set for chunk lines only.
|
|
|
|
|
QMap<int, QPair<int, QString> > m_skippedLines;
|
2014-02-13 16:43:28 +01:00
|
|
|
// start block number, block count of a chunk, chunk index inside a file.
|
|
|
|
|
QMap<int, QPair<int, int> > m_chunkInfo;
|
2013-08-14 13:52:13 +02:00
|
|
|
// block number, separator. Set for file, chunk or span line.
|
2013-04-09 10:26:31 +02:00
|
|
|
QMap<int, bool> m_separators;
|
2013-06-05 15:25:42 +02:00
|
|
|
QColor m_fileLineForeground;
|
|
|
|
|
QColor m_chunkLineForeground;
|
|
|
|
|
QColor m_textForeground;
|
2014-07-11 12:55:31 +02:00
|
|
|
QByteArray m_state;
|
2013-02-15 12:49:50 +01:00
|
|
|
};
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
SideDiffEditorWidget::SideDiffEditorWidget(QWidget *parent)
|
2016-07-13 15:55:18 +02:00
|
|
|
: SelectableTextEditorWidget("DiffEditor.SideDiffEditor", parent)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-06-03 15:17:34 +02:00
|
|
|
DisplaySettings settings = displaySettings();
|
|
|
|
|
settings.m_textWrapping = false;
|
|
|
|
|
settings.m_displayLineNumbers = true;
|
|
|
|
|
settings.m_highlightCurrentLine = false;
|
|
|
|
|
settings.m_displayFoldingMarkers = true;
|
|
|
|
|
settings.m_markTextChanges = false;
|
|
|
|
|
settings.m_highlightBlocks = false;
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::setDisplaySettings(settings);
|
2013-06-03 15:17:34 +02:00
|
|
|
|
2014-09-26 11:37:54 +02:00
|
|
|
connect(this, &TextEditorWidget::tooltipRequested, [this](const QPoint &point, int position) {
|
2017-06-27 11:59:11 +02:00
|
|
|
const int block = document()->findBlock(position).blockNumber();
|
|
|
|
|
const auto it = m_fileInfo.constFind(block);
|
2014-08-28 18:46:39 +02:00
|
|
|
if (it != m_fileInfo.constEnd())
|
2014-12-08 10:57:05 +01:00
|
|
|
ToolTip::show(point, it.value().fileName, this);
|
2014-08-28 18:46:39 +02:00
|
|
|
else
|
|
|
|
|
ToolTip::hide();
|
|
|
|
|
});
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-30 16:59:25 +01:00
|
|
|
void SideDiffEditorWidget::saveState()
|
2014-07-11 12:55:31 +02:00
|
|
|
{
|
|
|
|
|
if (!m_state.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
2015-01-30 16:59:25 +01:00
|
|
|
m_state = SelectableTextEditorWidget::saveState();
|
2014-07-11 12:55:31 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-30 16:59:25 +01:00
|
|
|
void SideDiffEditorWidget::restoreState()
|
2014-07-11 12:55:31 +02:00
|
|
|
{
|
|
|
|
|
if (m_state.isNull())
|
|
|
|
|
return;
|
|
|
|
|
|
2015-01-30 16:59:25 +01:00
|
|
|
SelectableTextEditorWidget::restoreState(m_state);
|
2014-07-11 12:55:31 +02:00
|
|
|
m_state.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::setDisplaySettings(const DisplaySettings &ds)
|
2013-06-03 15:17:34 +02:00
|
|
|
{
|
|
|
|
|
DisplaySettings settings = displaySettings();
|
|
|
|
|
settings.m_visualizeWhitespace = ds.m_visualizeWhitespace;
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::setDisplaySettings(settings);
|
2013-06-03 15:17:34 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::applyFontSettings()
|
2013-06-05 15:25:42 +02:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::applyFontSettings();
|
2015-02-03 23:54:47 +02:00
|
|
|
const FontSettings &fs = textDocument()->fontSettings();
|
2013-06-05 15:25:42 +02:00
|
|
|
m_fileLineForeground = fs.formatFor(C_DIFF_FILE_LINE).foreground();
|
|
|
|
|
m_chunkLineForeground = fs.formatFor(C_DIFF_CONTEXT_LINE).foreground();
|
|
|
|
|
m_textForeground = fs.toTextCharFormat(C_TEXT).foreground().color();
|
|
|
|
|
update();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
QString SideDiffEditorWidget::lineNumber(int blockNumber) const
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2017-06-27 11:59:11 +02:00
|
|
|
const auto it = m_lineNumbers.constFind(blockNumber);
|
|
|
|
|
if (it != m_lineNumbers.constEnd())
|
|
|
|
|
return QString::number(it.value());
|
2013-04-26 11:50:10 +02:00
|
|
|
return QString();
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
int SideDiffEditorWidget::lineNumberDigits() const
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-09 10:26:31 +02:00
|
|
|
return m_lineNumberDigits;
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
bool SideDiffEditorWidget::selectionVisible(int blockNumber) const
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-09 10:26:31 +02:00
|
|
|
return !m_separators.value(blockNumber, false);
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
bool SideDiffEditorWidget::replacementVisible(int blockNumber) const
|
2013-04-10 13:51:04 +02:00
|
|
|
{
|
2013-04-25 17:37:20 +02:00
|
|
|
return isChunkLine(blockNumber) || (isFileLine(blockNumber)
|
2017-06-27 11:59:11 +02:00
|
|
|
&& TextDocumentLayout::isFolded(document()->findBlockByNumber(blockNumber)));
|
2013-04-10 13:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
QColor SideDiffEditorWidget::replacementPenColor(int blockNumber) const
|
2013-06-05 15:25:42 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(blockNumber)
|
|
|
|
|
return m_chunkLineForeground;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
QString SideDiffEditorWidget::plainTextFromSelection(const QTextCursor &cursor) const
|
2013-04-23 09:09:20 +02:00
|
|
|
{
|
|
|
|
|
const int startPosition = cursor.selectionStart();
|
|
|
|
|
const int endPosition = cursor.selectionEnd();
|
|
|
|
|
if (startPosition == endPosition)
|
|
|
|
|
return QString(); // no selection
|
|
|
|
|
|
2017-06-27 11:59:11 +02:00
|
|
|
const QTextBlock startBlock = document()->findBlock(startPosition);
|
|
|
|
|
const QTextBlock endBlock = document()->findBlock(endPosition);
|
2013-04-23 09:09:20 +02:00
|
|
|
QTextBlock block = startBlock;
|
|
|
|
|
QString text;
|
|
|
|
|
bool textInserted = false;
|
|
|
|
|
while (block.isValid() && block.blockNumber() <= endBlock.blockNumber()) {
|
|
|
|
|
if (selectionVisible(block.blockNumber())) {
|
|
|
|
|
if (block == startBlock) {
|
|
|
|
|
if (block == endBlock)
|
|
|
|
|
text = cursor.selectedText(); // just one line text
|
|
|
|
|
else
|
|
|
|
|
text = block.text().mid(startPosition - block.position());
|
|
|
|
|
} else {
|
|
|
|
|
if (textInserted)
|
|
|
|
|
text += QLatin1Char('\n');
|
|
|
|
|
if (block == endBlock)
|
2017-04-14 10:24:28 +02:00
|
|
|
text += block.text().leftRef(endPosition - block.position());
|
2013-04-23 09:09:20 +02:00
|
|
|
else
|
|
|
|
|
text += block.text();
|
|
|
|
|
}
|
|
|
|
|
textInserted = true;
|
|
|
|
|
}
|
|
|
|
|
block = block.next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return convertToPlainText(text);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::setLineNumber(int blockNumber, int lineNumber)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2013-04-26 11:50:10 +02:00
|
|
|
const QString lineNumberString = QString::number(lineNumber);
|
2013-02-15 12:49:50 +01:00
|
|
|
m_lineNumbers.insert(blockNumber, lineNumber);
|
2013-04-26 11:50:10 +02:00
|
|
|
m_lineNumberDigits = qMax(m_lineNumberDigits, lineNumberString.count());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideDiffEditorWidget::setFileInfo(int blockNumber, const DiffFileInfo &fileInfo)
|
2013-08-14 13:52:13 +02:00
|
|
|
{
|
|
|
|
|
m_fileInfo[blockNumber] = fileInfo;
|
|
|
|
|
setSeparator(blockNumber, true);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideDiffEditorWidget::setChunkIndex(int startBlockNumber, int blockCount, int chunkIndex)
|
|
|
|
|
{
|
|
|
|
|
m_chunkInfo.insert(startBlockNumber, qMakePair(blockCount, chunkIndex));
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
int SideDiffEditorWidget::blockNumberForFileIndex(int fileIndex) const
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
|
|
|
|
if (fileIndex < 0 || fileIndex >= m_fileInfo.count())
|
|
|
|
|
return -1;
|
|
|
|
|
|
2017-06-27 11:59:11 +02:00
|
|
|
return (m_fileInfo.constBegin() + fileIndex).key();
|
2013-05-23 13:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
int SideDiffEditorWidget::fileIndexForBlockNumber(int blockNumber) const
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
|
|
|
|
int i = -1;
|
2017-06-27 11:59:11 +02:00
|
|
|
for (auto it = m_fileInfo.cbegin(), end = m_fileInfo.cend(); it != end; ++it, ++i) {
|
2013-05-23 13:36:27 +02:00
|
|
|
if (it.key() > blockNumber)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-06-27 11:59:11 +02:00
|
|
|
|
2013-05-23 13:36:27 +02:00
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
int SideDiffEditorWidget::chunkIndexForBlockNumber(int blockNumber) const
|
|
|
|
|
{
|
|
|
|
|
if (m_chunkInfo.isEmpty())
|
|
|
|
|
return -1;
|
|
|
|
|
|
2017-06-27 11:59:11 +02:00
|
|
|
auto it = m_chunkInfo.upperBound(blockNumber);
|
2014-02-13 16:43:28 +01:00
|
|
|
if (it == m_chunkInfo.constBegin())
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
--it;
|
|
|
|
|
|
|
|
|
|
if (blockNumber < it.key() + it.value().first)
|
|
|
|
|
return it.value().second;
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::clearAll(const QString &message)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
|
|
|
|
setBlockSelection(false);
|
|
|
|
|
clear();
|
|
|
|
|
clearAllData();
|
2014-09-26 11:37:54 +02:00
|
|
|
setExtraSelections(TextEditorWidget::OtherSelection,
|
2014-02-13 16:43:28 +01:00
|
|
|
QList<QTextEdit::ExtraSelection>());
|
2013-05-07 14:02:08 +02:00
|
|
|
setPlainText(message);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::clearAllData()
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
|
|
|
|
m_lineNumberDigits = 1;
|
2013-04-25 17:37:20 +02:00
|
|
|
m_lineNumbers.clear();
|
2013-05-07 14:02:08 +02:00
|
|
|
m_fileInfo.clear();
|
2013-04-25 17:37:20 +02:00
|
|
|
m_skippedLines.clear();
|
2014-02-13 16:43:28 +01:00
|
|
|
m_chunkInfo.clear();
|
2013-04-25 17:37:20 +02:00
|
|
|
m_separators.clear();
|
2014-02-13 16:43:28 +01:00
|
|
|
setSelections(QMap<int, QList<DiffSelection> >());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
2015-01-30 16:59:25 +01:00
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::scrollContentsBy(int dx, int dy)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::scrollContentsBy(dx, dy);
|
2013-02-15 12:49:50 +01:00
|
|
|
// TODO: update only chunk lines
|
|
|
|
|
viewport()->update();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::paintSeparator(QPainter &painter,
|
2013-06-05 15:25:42 +02:00
|
|
|
QColor &color,
|
|
|
|
|
const QString &text,
|
|
|
|
|
const QTextBlock &block,
|
|
|
|
|
int top)
|
2013-04-25 17:37:20 +02:00
|
|
|
{
|
|
|
|
|
QPointF offset = contentOffset();
|
|
|
|
|
painter.save();
|
2013-06-05 15:25:42 +02:00
|
|
|
|
|
|
|
|
QColor foreground = color;
|
|
|
|
|
if (!foreground.isValid())
|
|
|
|
|
foreground = m_textForeground;
|
|
|
|
|
if (!foreground.isValid())
|
|
|
|
|
foreground = palette().foreground().color();
|
|
|
|
|
|
|
|
|
|
painter.setPen(foreground);
|
|
|
|
|
|
2013-04-25 17:37:20 +02:00
|
|
|
const QString replacementText = QLatin1String(" {")
|
|
|
|
|
+ foldReplacementText(block)
|
|
|
|
|
+ QLatin1String("}; ");
|
|
|
|
|
const int replacementTextWidth = fontMetrics().width(replacementText) + 24;
|
|
|
|
|
int x = replacementTextWidth + offset.x();
|
2014-02-13 16:43:28 +01:00
|
|
|
if (x < document()->documentMargin()
|
2015-02-03 23:54:47 +02:00
|
|
|
|| !TextDocumentLayout::isFolded(block)) {
|
2013-04-25 17:37:20 +02:00
|
|
|
x = document()->documentMargin();
|
2014-02-13 16:43:28 +01:00
|
|
|
}
|
2013-04-25 17:37:20 +02:00
|
|
|
const QString elidedText = fontMetrics().elidedText(text,
|
|
|
|
|
Qt::ElideRight,
|
|
|
|
|
viewport()->width() - x);
|
|
|
|
|
QTextLayout *layout = block.layout();
|
|
|
|
|
QTextLine textLine = layout->lineAt(0);
|
|
|
|
|
QRectF lineRect = textLine.naturalTextRect().translated(offset.x(), top);
|
|
|
|
|
QRect clipRect = contentsRect();
|
|
|
|
|
clipRect.setLeft(x);
|
|
|
|
|
painter.setClipRect(clipRect);
|
|
|
|
|
painter.drawText(QPointF(x, lineRect.top() + textLine.ascent()),
|
|
|
|
|
elidedText);
|
|
|
|
|
painter.restore();
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::mouseDoubleClickEvent(QMouseEvent *e)
|
2013-04-26 11:50:10 +02:00
|
|
|
{
|
|
|
|
|
if (e->button() == Qt::LeftButton && !(e->modifiers() & Qt::ShiftModifier)) {
|
|
|
|
|
QTextCursor cursor = cursorForPosition(e->pos());
|
|
|
|
|
jumpToOriginalFile(cursor);
|
2013-07-01 11:38:16 +02:00
|
|
|
e->accept();
|
|
|
|
|
return;
|
2013-04-26 11:50:10 +02:00
|
|
|
}
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::mouseDoubleClickEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 08:53:27 +03:00
|
|
|
void SideDiffEditorWidget::keyPressEvent(QKeyEvent *e)
|
|
|
|
|
{
|
|
|
|
|
if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
|
|
|
|
|
jumpToOriginalFile(textCursor());
|
|
|
|
|
e->accept();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
SelectableTextEditorWidget::keyPressEvent(e);
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideDiffEditorWidget::contextMenuEvent(QContextMenuEvent *e)
|
|
|
|
|
{
|
|
|
|
|
QPointer<QMenu> menu = createStandardContextMenu();
|
|
|
|
|
|
|
|
|
|
QTextCursor cursor = cursorForPosition(e->pos());
|
|
|
|
|
const int blockNumber = cursor.blockNumber();
|
|
|
|
|
|
|
|
|
|
emit contextMenuRequested(menu, fileIndexForBlockNumber(blockNumber),
|
|
|
|
|
chunkIndexForBlockNumber(blockNumber));
|
|
|
|
|
|
2015-01-29 08:00:52 +01:00
|
|
|
connect(this, &SideDiffEditorWidget::destroyed, menu.data(), &QMenu::deleteLater);
|
2014-02-13 16:43:28 +01:00
|
|
|
menu->exec(e->globalPos());
|
|
|
|
|
delete menu;
|
2013-04-26 11:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::jumpToOriginalFile(const QTextCursor &cursor)
|
2013-04-26 11:50:10 +02:00
|
|
|
{
|
2013-05-07 14:02:08 +02:00
|
|
|
if (m_fileInfo.isEmpty())
|
2013-04-26 11:50:10 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const int blockNumber = cursor.blockNumber();
|
|
|
|
|
if (!m_lineNumbers.contains(blockNumber))
|
|
|
|
|
return;
|
|
|
|
|
|
2013-07-01 13:15:27 +02:00
|
|
|
const int lineNumber = m_lineNumbers.value(blockNumber);
|
2016-07-13 15:55:18 +02:00
|
|
|
const int columnNumber = cursor.positionInBlock();
|
2013-04-26 11:50:10 +02:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
emit jumpToOriginalFileRequested(fileIndexForBlockNumber(blockNumber),
|
|
|
|
|
lineNumber, columnNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QString skippedText(int skippedNumber)
|
|
|
|
|
{
|
|
|
|
|
if (skippedNumber > 0)
|
|
|
|
|
return SideBySideDiffEditorWidget::tr("Skipped %n lines...", 0, skippedNumber);
|
|
|
|
|
if (skippedNumber == -2)
|
|
|
|
|
return SideBySideDiffEditorWidget::tr("Binary files differ");
|
|
|
|
|
return SideBySideDiffEditorWidget::tr("Skipped unknown number of lines...");
|
2013-04-26 11:50:10 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideDiffEditorWidget::paintEvent(QPaintEvent *e)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
SelectableTextEditorWidget::paintEvent(e);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
QPainter painter(viewport());
|
2013-02-15 12:49:50 +01:00
|
|
|
QPointF offset = contentOffset();
|
2017-10-24 13:44:33 +02:00
|
|
|
QTextBlock currentBlock = firstVisibleBlock();
|
2013-02-21 17:14:07 +01:00
|
|
|
|
|
|
|
|
while (currentBlock.isValid()) {
|
|
|
|
|
if (currentBlock.isVisible()) {
|
|
|
|
|
qreal top = blockBoundingGeometry(currentBlock).translated(offset).top();
|
|
|
|
|
qreal bottom = top + blockBoundingRect(currentBlock).height();
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-02-21 17:14:07 +01:00
|
|
|
if (top > e->rect().bottom())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
if (bottom >= e->rect().top()) {
|
2013-04-25 17:37:20 +02:00
|
|
|
const int blockNumber = currentBlock.blockNumber();
|
2013-02-21 17:14:07 +01:00
|
|
|
|
2017-06-02 10:13:36 +03:00
|
|
|
auto it = m_skippedLines.constFind(blockNumber);
|
|
|
|
|
if (it != m_skippedLines.constEnd()) {
|
|
|
|
|
QString skippedRowsText = '[' + skippedText(it->first) + ']';
|
|
|
|
|
if (!it->second.isEmpty())
|
|
|
|
|
skippedRowsText += ' ' + it->second;
|
2013-06-05 15:25:42 +02:00
|
|
|
paintSeparator(painter, m_chunkLineForeground,
|
|
|
|
|
skippedRowsText, currentBlock, top);
|
2013-04-25 17:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
const DiffFileInfo fileInfo = m_fileInfo.value(blockNumber);
|
2013-05-07 14:02:08 +02:00
|
|
|
if (!fileInfo.fileName.isEmpty()) {
|
|
|
|
|
const QString fileNameText = fileInfo.typeInfo.isEmpty()
|
|
|
|
|
? fileInfo.fileName
|
2014-02-13 16:43:28 +01:00
|
|
|
: tr("[%1] %2").arg(fileInfo.typeInfo)
|
|
|
|
|
.arg(fileInfo.fileName);
|
2013-06-05 15:25:42 +02:00
|
|
|
paintSeparator(painter, m_fileLineForeground,
|
|
|
|
|
fileNameText, currentBlock, top);
|
2013-02-21 17:14:07 +01:00
|
|
|
}
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2013-02-21 17:14:07 +01:00
|
|
|
currentBlock = currentBlock.next();
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
2013-04-25 17:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
2013-02-15 12:49:50 +01:00
|
|
|
//////////////////
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
SideBySideDiffEditorWidget::SideBySideDiffEditorWidget(QWidget *parent)
|
2013-12-16 16:19:40 +01:00
|
|
|
: QWidget(parent)
|
2016-07-13 15:55:18 +02:00
|
|
|
, m_controller(this)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-01-30 14:27:50 +01:00
|
|
|
m_leftEditor = new SideDiffEditorWidget(this);
|
2013-02-15 12:49:50 +01:00
|
|
|
m_leftEditor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
|
m_leftEditor->setReadOnly(true);
|
2014-08-28 17:00:12 +02:00
|
|
|
connect(TextEditorSettings::instance(), &TextEditorSettings::displaySettingsChanged,
|
|
|
|
|
m_leftEditor, &SideDiffEditorWidget::setDisplaySettings);
|
2013-09-19 17:59:27 +02:00
|
|
|
m_leftEditor->setDisplaySettings(TextEditorSettings::displaySettings());
|
|
|
|
|
m_leftEditor->setCodeStyle(TextEditorSettings::codeStyle());
|
2015-01-28 14:08:42 +01:00
|
|
|
connect(m_leftEditor, &SideDiffEditorWidget::jumpToOriginalFileRequested,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::slotLeftJumpToOriginalFileRequested);
|
|
|
|
|
connect(m_leftEditor, &SideDiffEditorWidget::contextMenuRequested,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::slotLeftContextMenuRequested,
|
2014-07-28 22:25:27 +03:00
|
|
|
Qt::DirectConnection);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
m_rightEditor = new SideDiffEditorWidget(this);
|
2013-02-15 12:49:50 +01:00
|
|
|
m_rightEditor->setReadOnly(true);
|
2014-08-28 17:00:12 +02:00
|
|
|
connect(TextEditorSettings::instance(), &TextEditorSettings::displaySettingsChanged,
|
|
|
|
|
m_rightEditor, &SideDiffEditorWidget::setDisplaySettings);
|
2013-09-19 17:59:27 +02:00
|
|
|
m_rightEditor->setDisplaySettings(TextEditorSettings::displaySettings());
|
|
|
|
|
m_rightEditor->setCodeStyle(TextEditorSettings::codeStyle());
|
2015-01-28 14:08:42 +01:00
|
|
|
connect(m_rightEditor, &SideDiffEditorWidget::jumpToOriginalFileRequested,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::slotRightJumpToOriginalFileRequested);
|
|
|
|
|
connect(m_rightEditor, &SideDiffEditorWidget::contextMenuRequested,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::slotRightContextMenuRequested,
|
2014-07-28 22:25:27 +03:00
|
|
|
Qt::DirectConnection);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2013-09-19 17:59:27 +02:00
|
|
|
connect(TextEditorSettings::instance(),
|
2015-01-28 14:08:42 +01:00
|
|
|
&TextEditorSettings::fontSettingsChanged,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::setFontSettings);
|
2013-09-19 17:59:27 +02:00
|
|
|
setFontSettings(TextEditorSettings::fontSettings());
|
2013-06-05 15:25:42 +02:00
|
|
|
|
2015-01-28 14:08:42 +01:00
|
|
|
connect(m_leftEditor->verticalScrollBar(), &QAbstractSlider::valueChanged,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::leftVSliderChanged);
|
|
|
|
|
connect(m_leftEditor->verticalScrollBar(), &QAbstractSlider::actionTriggered,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::leftVSliderChanged);
|
2013-05-22 16:33:44 +02:00
|
|
|
|
2015-01-28 14:08:42 +01:00
|
|
|
connect(m_leftEditor->horizontalScrollBar(), &QAbstractSlider::valueChanged,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::leftHSliderChanged);
|
|
|
|
|
connect(m_leftEditor->horizontalScrollBar(), &QAbstractSlider::actionTriggered,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::leftHSliderChanged);
|
2013-05-22 16:33:44 +02:00
|
|
|
|
2015-01-28 14:08:42 +01:00
|
|
|
connect(m_leftEditor, &QPlainTextEdit::cursorPositionChanged,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::leftCursorPositionChanged);
|
2013-05-22 16:33:44 +02:00
|
|
|
|
2015-01-28 14:08:42 +01:00
|
|
|
connect(m_rightEditor->verticalScrollBar(), &QAbstractSlider::valueChanged,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::rightVSliderChanged);
|
|
|
|
|
connect(m_rightEditor->verticalScrollBar(), &QAbstractSlider::actionTriggered,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::rightVSliderChanged);
|
2013-05-22 16:33:44 +02:00
|
|
|
|
2015-01-28 14:08:42 +01:00
|
|
|
connect(m_rightEditor->horizontalScrollBar(), &QAbstractSlider::valueChanged,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::rightHSliderChanged);
|
|
|
|
|
connect(m_rightEditor->horizontalScrollBar(), &QAbstractSlider::actionTriggered,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::rightHSliderChanged);
|
2013-05-22 16:33:44 +02:00
|
|
|
|
2015-01-28 14:08:42 +01:00
|
|
|
connect(m_rightEditor, &QPlainTextEdit::cursorPositionChanged,
|
|
|
|
|
this, &SideBySideDiffEditorWidget::rightCursorPositionChanged);
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2015-02-03 23:54:47 +02:00
|
|
|
m_splitter = new MiniSplitter(this);
|
2013-02-15 12:49:50 +01:00
|
|
|
m_splitter->addWidget(m_leftEditor);
|
|
|
|
|
m_splitter->addWidget(m_rightEditor);
|
|
|
|
|
QVBoxLayout *l = new QVBoxLayout(this);
|
2013-05-22 16:33:44 +02:00
|
|
|
l->setMargin(0);
|
2013-02-15 12:49:50 +01:00
|
|
|
l->addWidget(m_splitter);
|
2014-08-13 13:28:37 +02:00
|
|
|
setFocusProxy(m_rightEditor);
|
2017-05-09 12:19:11 +02:00
|
|
|
|
|
|
|
|
m_leftContext = new IContext(this);
|
|
|
|
|
m_leftContext->setWidget(m_leftEditor);
|
|
|
|
|
m_leftContext->setContext(Core::Context(Core::Id(Constants::SIDE_BY_SIDE_VIEW_ID).withSuffix(1)));
|
|
|
|
|
Core::ICore::addContextObject(m_leftContext);
|
|
|
|
|
m_rightContext = new IContext(this);
|
|
|
|
|
m_rightContext->setWidget(m_rightEditor);
|
|
|
|
|
m_rightContext->setContext(Core::Context(Core::Id(Constants::SIDE_BY_SIDE_VIEW_ID).withSuffix(2)));
|
|
|
|
|
Core::ICore::addContextObject(m_rightContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SideBySideDiffEditorWidget::~SideBySideDiffEditorWidget()
|
|
|
|
|
{
|
|
|
|
|
Core::ICore::removeContextObject(m_leftContext);
|
|
|
|
|
Core::ICore::removeContextObject(m_rightContext);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditorWidget *SideBySideDiffEditorWidget::leftEditorWidget() const
|
|
|
|
|
{
|
|
|
|
|
return m_leftEditor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditorWidget *SideBySideDiffEditorWidget::rightEditorWidget() const
|
|
|
|
|
{
|
|
|
|
|
return m_rightEditor;
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2015-01-30 16:59:25 +01:00
|
|
|
void SideBySideDiffEditorWidget::setDocument(DiffEditorDocument *document)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.setDocument(document);
|
2016-11-23 11:20:12 +01:00
|
|
|
clear();
|
|
|
|
|
QList<FileData> diffFileList;
|
|
|
|
|
QString workingDirectory;
|
|
|
|
|
if (document) {
|
|
|
|
|
diffFileList = document->diffFiles();
|
|
|
|
|
workingDirectory = document->baseDirectory();
|
|
|
|
|
}
|
|
|
|
|
setDiff(diffFileList, workingDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DiffEditorDocument *SideBySideDiffEditorWidget::diffDocument() const
|
|
|
|
|
{
|
|
|
|
|
return m_controller.document();
|
2013-12-16 16:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::clear(const QString &message)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
2016-07-13 15:55:18 +02:00
|
|
|
const bool oldIgnore = m_controller.m_ignoreCurrentIndexChange;
|
|
|
|
|
m_controller.m_ignoreCurrentIndexChange = true;
|
2015-01-30 16:59:25 +01:00
|
|
|
setDiff(QList<FileData>(), QString());
|
2013-05-07 14:02:08 +02:00
|
|
|
m_leftEditor->clearAll(message);
|
|
|
|
|
m_rightEditor->clearAll(message);
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.m_ignoreCurrentIndexChange = oldIgnore;
|
2013-05-07 14:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::setDiff(const QList<FileData> &diffFileList,
|
|
|
|
|
const QString &workingDirectory)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
Q_UNUSED(workingDirectory)
|
2013-04-25 17:37:20 +02:00
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
const bool oldIgnore = m_controller.m_ignoreCurrentIndexChange;
|
|
|
|
|
m_controller.m_ignoreCurrentIndexChange = true;
|
2015-01-30 16:59:25 +01:00
|
|
|
m_leftEditor->clear();
|
|
|
|
|
m_rightEditor->clear();
|
|
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.m_contextFileData = diffFileList;
|
|
|
|
|
if (m_controller.m_contextFileData.isEmpty()) {
|
2017-07-03 11:26:52 +02:00
|
|
|
const QString msg = tr("No difference.");
|
2015-01-30 16:59:25 +01:00
|
|
|
m_leftEditor->setPlainText(msg);
|
|
|
|
|
m_rightEditor->setPlainText(msg);
|
|
|
|
|
} else {
|
|
|
|
|
showDiff();
|
|
|
|
|
}
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.m_ignoreCurrentIndexChange = oldIgnore;
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::setCurrentDiffFileIndex(int diffFileIndex)
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
2016-07-13 15:55:18 +02:00
|
|
|
if (m_controller.m_ignoreCurrentIndexChange)
|
2014-02-13 16:43:28 +01:00
|
|
|
return;
|
|
|
|
|
|
2013-05-23 13:36:27 +02:00
|
|
|
const int blockNumber = m_leftEditor->blockNumberForFileIndex(diffFileIndex);
|
|
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
const bool oldIgnore = m_controller.m_ignoreCurrentIndexChange;
|
|
|
|
|
m_controller.m_ignoreCurrentIndexChange = true;
|
2013-05-23 13:36:27 +02:00
|
|
|
QTextBlock leftBlock = m_leftEditor->document()->findBlockByNumber(blockNumber);
|
|
|
|
|
QTextCursor leftCursor = m_leftEditor->textCursor();
|
|
|
|
|
leftCursor.setPosition(leftBlock.position());
|
|
|
|
|
m_leftEditor->setTextCursor(leftCursor);
|
2015-04-16 15:31:25 +02:00
|
|
|
m_leftEditor->verticalScrollBar()->setValue(blockNumber);
|
2013-05-23 13:36:27 +02:00
|
|
|
|
|
|
|
|
QTextBlock rightBlock = m_rightEditor->document()->findBlockByNumber(blockNumber);
|
|
|
|
|
QTextCursor rightCursor = m_rightEditor->textCursor();
|
|
|
|
|
rightCursor.setPosition(rightBlock.position());
|
|
|
|
|
m_rightEditor->setTextCursor(rightCursor);
|
2015-04-16 15:31:25 +02:00
|
|
|
m_rightEditor->verticalScrollBar()->setValue(blockNumber);
|
2013-05-23 13:36:27 +02:00
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.m_ignoreCurrentIndexChange = oldIgnore;
|
2013-05-23 13:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-30 16:59:25 +01:00
|
|
|
void SideBySideDiffEditorWidget::setHorizontalSync(bool sync)
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2015-01-30 16:59:25 +01:00
|
|
|
m_horizontalSync = sync;
|
|
|
|
|
rightHSliderChanged();
|
|
|
|
|
}
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2015-01-30 16:59:25 +01:00
|
|
|
void SideBySideDiffEditorWidget::saveState()
|
|
|
|
|
{
|
|
|
|
|
m_leftEditor->saveState();
|
|
|
|
|
m_rightEditor->saveState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBySideDiffEditorWidget::restoreState()
|
|
|
|
|
{
|
|
|
|
|
m_leftEditor->restoreState();
|
|
|
|
|
m_rightEditor->restoreState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBySideDiffEditorWidget::showDiff()
|
|
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
QMap<int, QList<DiffSelection> > leftFormats;
|
|
|
|
|
QMap<int, QList<DiffSelection> > rightFormats;
|
|
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
QString leftTexts, rightTexts;
|
2013-04-09 10:26:31 +02:00
|
|
|
int blockNumber = 0;
|
|
|
|
|
QChar separator = QLatin1Char('\n');
|
2017-06-27 11:59:11 +02:00
|
|
|
for (const FileData &contextFileData : m_controller.m_contextFileData) {
|
2013-08-14 13:52:13 +02:00
|
|
|
QString leftText, rightText;
|
2013-04-25 17:37:20 +02:00
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_controller.m_fileLineFormat));
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_controller.m_fileLineFormat));
|
2013-05-07 14:02:08 +02:00
|
|
|
m_leftEditor->setFileInfo(blockNumber, contextFileData.leftFileInfo);
|
|
|
|
|
m_rightEditor->setFileInfo(blockNumber, contextFileData.rightFileInfo);
|
2013-08-14 13:52:13 +02:00
|
|
|
leftText = separator;
|
|
|
|
|
rightText = separator;
|
2013-04-25 17:37:20 +02:00
|
|
|
blockNumber++;
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
int lastLeftLineNumber = -1;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
if (contextFileData.binaryFiles) {
|
2016-07-13 15:55:18 +02:00
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_controller.m_chunkLineFormat));
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_controller.m_chunkLineFormat));
|
2014-02-13 16:43:28 +01:00
|
|
|
m_leftEditor->setSkippedLines(blockNumber, -2);
|
|
|
|
|
m_rightEditor->setSkippedLines(blockNumber, -2);
|
|
|
|
|
leftText += separator;
|
|
|
|
|
rightText += separator;
|
|
|
|
|
blockNumber++;
|
|
|
|
|
} else {
|
|
|
|
|
for (int j = 0; j < contextFileData.chunks.count(); j++) {
|
2017-06-27 11:59:11 +02:00
|
|
|
const ChunkData &chunkData = contextFileData.chunks.at(j);
|
2014-02-13 16:43:28 +01:00
|
|
|
|
|
|
|
|
int leftLineNumber = chunkData.leftStartingLineNumber;
|
|
|
|
|
int rightLineNumber = chunkData.rightStartingLineNumber;
|
|
|
|
|
|
|
|
|
|
if (!chunkData.contextChunk) {
|
|
|
|
|
const int skippedLines = leftLineNumber - lastLeftLineNumber - 1;
|
|
|
|
|
if (skippedLines > 0) {
|
2016-07-13 15:55:18 +02:00
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_controller.m_chunkLineFormat));
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_controller.m_chunkLineFormat));
|
2017-06-02 10:13:36 +03:00
|
|
|
m_leftEditor->setSkippedLines(blockNumber, skippedLines, chunkData.contextInfo);
|
|
|
|
|
m_rightEditor->setSkippedLines(blockNumber, skippedLines, chunkData.contextInfo);
|
2014-02-13 16:43:28 +01:00
|
|
|
leftText += separator;
|
|
|
|
|
rightText += separator;
|
|
|
|
|
blockNumber++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_leftEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
|
|
|
|
|
m_rightEditor->setChunkIndex(blockNumber, chunkData.rows.count(), j);
|
|
|
|
|
|
2017-06-27 11:59:11 +02:00
|
|
|
for (const RowData &rowData : chunkData.rows) {
|
2014-02-13 16:43:28 +01:00
|
|
|
TextLineData leftLineData = rowData.leftLine;
|
|
|
|
|
TextLineData rightLineData = rowData.rightLine;
|
|
|
|
|
if (leftLineData.textLineType == TextLineData::TextLine) {
|
|
|
|
|
leftText += leftLineData.text;
|
|
|
|
|
lastLeftLineNumber = leftLineNumber;
|
|
|
|
|
leftLineNumber++;
|
|
|
|
|
m_leftEditor->setLineNumber(blockNumber, leftLineNumber);
|
|
|
|
|
} else if (leftLineData.textLineType == TextLineData::Separator) {
|
|
|
|
|
m_leftEditor->setSeparator(blockNumber, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rightLineData.textLineType == TextLineData::TextLine) {
|
|
|
|
|
rightText += rightLineData.text;
|
|
|
|
|
rightLineNumber++;
|
|
|
|
|
m_rightEditor->setLineNumber(blockNumber, rightLineNumber);
|
|
|
|
|
} else if (rightLineData.textLineType == TextLineData::Separator) {
|
|
|
|
|
m_rightEditor->setSeparator(blockNumber, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!rowData.equal) {
|
|
|
|
|
if (rowData.leftLine.textLineType == TextLineData::TextLine)
|
2016-07-13 15:55:18 +02:00
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_controller.m_leftLineFormat));
|
2014-02-13 16:43:28 +01:00
|
|
|
else
|
|
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_spanLineFormat));
|
|
|
|
|
if (rowData.rightLine.textLineType == TextLineData::TextLine)
|
2016-07-13 15:55:18 +02:00
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_controller.m_rightLineFormat));
|
2014-02-13 16:43:28 +01:00
|
|
|
else
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_spanLineFormat));
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 11:59:11 +02:00
|
|
|
for (auto it = leftLineData.changedPositions.cbegin(),
|
|
|
|
|
end = leftLineData.changedPositions.cend(); it != end; ++it) {
|
2014-02-13 16:43:28 +01:00
|
|
|
leftFormats[blockNumber].append(
|
2017-06-27 11:59:11 +02:00
|
|
|
DiffSelection(it.key(), it.value(),
|
2016-07-13 15:55:18 +02:00
|
|
|
&m_controller.m_leftCharFormat));
|
2014-02-13 16:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
2017-06-27 11:59:11 +02:00
|
|
|
for (auto it = rightLineData.changedPositions.cbegin(),
|
|
|
|
|
end = rightLineData.changedPositions.cend(); it != end; ++it) {
|
2014-02-13 16:43:28 +01:00
|
|
|
rightFormats[blockNumber].append(
|
2017-06-27 11:59:11 +02:00
|
|
|
DiffSelection(it.key(), it.value(),
|
2016-07-13 15:55:18 +02:00
|
|
|
&m_controller.m_rightCharFormat));
|
2014-02-13 16:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
leftText += separator;
|
|
|
|
|
rightText += separator;
|
|
|
|
|
blockNumber++;
|
|
|
|
|
}
|
2013-04-25 17:37:20 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
if (j == contextFileData.chunks.count() - 1) { // the last chunk
|
|
|
|
|
int skippedLines = -2;
|
|
|
|
|
if (chunkData.contextChunk) {
|
|
|
|
|
// if it's context chunk
|
|
|
|
|
skippedLines = chunkData.rows.count();
|
|
|
|
|
} else if (!contextFileData.lastChunkAtTheEndOfFile
|
|
|
|
|
&& !contextFileData.contextChunksIncluded) {
|
|
|
|
|
// if not a context chunk and not a chunk at the end of file
|
|
|
|
|
// and context lines not included
|
|
|
|
|
skippedLines = -1; // unknown count skipped by the end of file
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (skippedLines >= -1) {
|
2016-07-13 15:55:18 +02:00
|
|
|
leftFormats[blockNumber].append(DiffSelection(&m_controller.m_chunkLineFormat));
|
|
|
|
|
rightFormats[blockNumber].append(DiffSelection(&m_controller.m_chunkLineFormat));
|
2014-02-13 16:43:28 +01:00
|
|
|
m_leftEditor->setSkippedLines(blockNumber, skippedLines);
|
|
|
|
|
m_rightEditor->setSkippedLines(blockNumber, skippedLines);
|
|
|
|
|
leftText += separator;
|
|
|
|
|
rightText += separator;
|
|
|
|
|
blockNumber++;
|
|
|
|
|
} // otherwise nothing skipped
|
2013-04-25 17:37:20 +02:00
|
|
|
}
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-16 14:26:07 +02:00
|
|
|
leftText.replace(QLatin1Char('\r'), QLatin1Char(' '));
|
|
|
|
|
rightText.replace(QLatin1Char('\r'), QLatin1Char(' '));
|
2013-08-14 13:52:13 +02:00
|
|
|
leftTexts += leftText;
|
|
|
|
|
rightTexts += rightText;
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
2013-05-07 14:02:08 +02:00
|
|
|
|
2013-08-14 13:52:13 +02:00
|
|
|
if (leftTexts.isEmpty() && rightTexts.isEmpty())
|
2013-05-07 14:02:08 +02:00
|
|
|
return;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
const bool oldIgnore = m_controller.m_ignoreCurrentIndexChange;
|
|
|
|
|
m_controller.m_ignoreCurrentIndexChange = true;
|
2014-02-13 16:43:28 +01:00
|
|
|
m_leftEditor->clear();
|
2013-08-14 13:52:13 +02:00
|
|
|
m_leftEditor->setPlainText(leftTexts);
|
2014-02-13 16:43:28 +01:00
|
|
|
m_rightEditor->clear();
|
2013-08-14 13:52:13 +02:00
|
|
|
m_rightEditor->setPlainText(rightTexts);
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.m_ignoreCurrentIndexChange = oldIgnore;
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
m_leftEditor->setSelections(leftFormats);
|
|
|
|
|
m_rightEditor->setSelections(rightFormats);
|
2013-06-05 15:25:42 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::setFontSettings(
|
2015-02-03 23:54:47 +02:00
|
|
|
const FontSettings &fontSettings)
|
2013-06-05 15:25:42 +02:00
|
|
|
{
|
2014-02-13 16:43:28 +01:00
|
|
|
m_spanLineFormat = fontSettings.toTextCharFormat(C_LINE_NUMBER);
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.setFontSettings(fontSettings);
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::slotLeftJumpToOriginalFileRequested(
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int lineNumber,
|
|
|
|
|
int columnNumber)
|
2013-07-01 13:15:27 +02:00
|
|
|
{
|
2016-07-13 15:55:18 +02:00
|
|
|
if (diffFileIndex < 0 || diffFileIndex >= m_controller.m_contextFileData.count())
|
2013-07-01 13:15:27 +02:00
|
|
|
return;
|
|
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
const FileData fileData = m_controller.m_contextFileData.at(diffFileIndex);
|
2013-07-01 13:15:27 +02:00
|
|
|
const QString leftFileName = fileData.leftFileInfo.fileName;
|
|
|
|
|
const QString rightFileName = fileData.rightFileInfo.fileName;
|
|
|
|
|
if (leftFileName == rightFileName) {
|
|
|
|
|
// 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.
|
2017-06-27 11:59:11 +02:00
|
|
|
for (const ChunkData &chunkData : fileData.chunks) {
|
2014-07-04 09:15:01 +02:00
|
|
|
|
|
|
|
|
int leftLineNumber = chunkData.leftStartingLineNumber;
|
|
|
|
|
int rightLineNumber = chunkData.rightStartingLineNumber;
|
|
|
|
|
|
2013-07-01 13:15:27 +02:00
|
|
|
for (int j = 0; j < chunkData.rows.count(); j++) {
|
|
|
|
|
const RowData rowData = chunkData.rows.at(j);
|
|
|
|
|
if (rowData.leftLine.textLineType == TextLineData::TextLine)
|
|
|
|
|
leftLineNumber++;
|
|
|
|
|
if (rowData.rightLine.textLineType == TextLineData::TextLine)
|
|
|
|
|
rightLineNumber++;
|
|
|
|
|
if (leftLineNumber == lineNumber) {
|
2014-02-27 12:22:47 +01:00
|
|
|
int colNr = rowData.equal ? columnNumber : 0;
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.jumpToOriginalFile(leftFileName, rightLineNumber, colNr);
|
2013-07-01 13:15:27 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// different file (e.g. in Tools | Diff...)
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.jumpToOriginalFile(leftFileName, lineNumber, columnNumber);
|
2013-07-01 13:15:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::slotRightJumpToOriginalFileRequested(
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int lineNumber,
|
|
|
|
|
int columnNumber)
|
2013-07-01 13:15:27 +02:00
|
|
|
{
|
2016-07-13 15:55:18 +02:00
|
|
|
if (diffFileIndex < 0 || diffFileIndex >= m_controller.m_contextFileData.count())
|
2013-07-01 13:15:27 +02:00
|
|
|
return;
|
|
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
const FileData fileData = m_controller.m_contextFileData.at(diffFileIndex);
|
2013-07-01 13:15:27 +02:00
|
|
|
const QString fileName = fileData.rightFileInfo.fileName;
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.jumpToOriginalFile(fileName, lineNumber, columnNumber);
|
2013-07-01 13:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-13 16:43:28 +01:00
|
|
|
void SideBySideDiffEditorWidget::slotLeftContextMenuRequested(QMenu *menu,
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int chunkIndex)
|
|
|
|
|
{
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.addCodePasterAction(menu);
|
|
|
|
|
m_controller.addApplyAction(menu, diffFileIndex, chunkIndex);
|
2014-02-13 16:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SideBySideDiffEditorWidget::slotRightContextMenuRequested(QMenu *menu,
|
|
|
|
|
int diffFileIndex,
|
|
|
|
|
int chunkIndex)
|
|
|
|
|
{
|
|
|
|
|
menu->addSeparator();
|
|
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.addCodePasterAction(menu);
|
|
|
|
|
m_controller.addRevertAction(menu, diffFileIndex, chunkIndex);
|
2014-02-13 16:43:28 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::leftVSliderChanged()
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2015-01-30 16:59:25 +01:00
|
|
|
m_rightEditor->verticalScrollBar()->setValue(m_leftEditor->verticalScrollBar()->value());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::rightVSliderChanged()
|
2013-02-15 12:49:50 +01:00
|
|
|
{
|
2015-01-30 16:59:25 +01:00
|
|
|
m_leftEditor->verticalScrollBar()->setValue(m_rightEditor->verticalScrollBar()->value());
|
2013-02-15 12:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::leftHSliderChanged()
|
2013-05-22 16:33:44 +02:00
|
|
|
{
|
2015-01-30 16:59:25 +01:00
|
|
|
if (m_horizontalSync)
|
|
|
|
|
m_rightEditor->horizontalScrollBar()->setValue(m_leftEditor->horizontalScrollBar()->value());
|
2013-05-22 16:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::rightHSliderChanged()
|
2013-05-22 16:33:44 +02:00
|
|
|
{
|
2015-01-30 16:59:25 +01:00
|
|
|
if (m_horizontalSync)
|
|
|
|
|
m_leftEditor->horizontalScrollBar()->setValue(m_rightEditor->horizontalScrollBar()->value());
|
2013-05-22 16:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::leftCursorPositionChanged()
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
|
|
|
|
leftVSliderChanged();
|
|
|
|
|
leftHSliderChanged();
|
2013-12-16 16:19:40 +01:00
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
if (m_controller.m_ignoreCurrentIndexChange)
|
2014-02-13 16:43:28 +01:00
|
|
|
return;
|
|
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
const bool oldIgnore = m_controller.m_ignoreCurrentIndexChange;
|
|
|
|
|
m_controller.m_ignoreCurrentIndexChange = true;
|
2015-01-30 16:59:25 +01:00
|
|
|
emit currentDiffFileIndexChanged(
|
|
|
|
|
m_leftEditor->fileIndexForBlockNumber(m_leftEditor->textCursor().blockNumber()));
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.m_ignoreCurrentIndexChange = oldIgnore;
|
2013-05-23 13:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
void SideBySideDiffEditorWidget::rightCursorPositionChanged()
|
2013-05-23 13:36:27 +02:00
|
|
|
{
|
|
|
|
|
rightVSliderChanged();
|
|
|
|
|
rightHSliderChanged();
|
2013-12-16 16:19:40 +01:00
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
if (m_controller.m_ignoreCurrentIndexChange)
|
2014-02-13 16:43:28 +01:00
|
|
|
return;
|
|
|
|
|
|
2016-07-13 15:55:18 +02:00
|
|
|
const bool oldIgnore = m_controller.m_ignoreCurrentIndexChange;
|
|
|
|
|
m_controller.m_ignoreCurrentIndexChange = true;
|
2015-01-30 16:59:25 +01:00
|
|
|
emit currentDiffFileIndexChanged(
|
|
|
|
|
m_rightEditor->fileIndexForBlockNumber(m_rightEditor->textCursor().blockNumber()));
|
2016-07-13 15:55:18 +02:00
|
|
|
m_controller.m_ignoreCurrentIndexChange = oldIgnore;
|
2013-05-23 13:36:27 +02:00
|
|
|
}
|
|
|
|
|
|
2015-01-30 14:55:56 +01:00
|
|
|
} // namespace Internal
|
2013-02-21 17:03:00 +01:00
|
|
|
} // namespace DiffEditor
|
2013-02-15 12:49:50 +01:00
|
|
|
|
2014-01-30 14:27:50 +01:00
|
|
|
#include "sidebysidediffeditorwidget.moc"
|