Commit 2e6972e1 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Improve line numbering for unified mode

Now there are two line numbers for unified mode, this helps to
differentiate between added/deleted line numbers
parent 4d83e0e9
......@@ -351,9 +351,9 @@ const LineHighlight *DiffEditor::highlightingForLine(int line)
return it == m_data.cend() ? nullptr : &(*it);
}
void DiffEditor::setLineNumberData(QVector<int> data, int maxLineNum)
void DiffEditor::setLineNumberData(QVector<int> lineNosA, QVector<int> lineNosB, int maxLineNum)
{
m_lineNumArea->setLineNumData(std::move(data));
m_lineNumArea->setLineNumData(std::move(lineNosA), std::move(lineNosB));
m_lineNumArea->setMaxLineNum(maxLineNum);
updateLineNumberAreaWidth(0);
}
......
......@@ -68,20 +68,30 @@ public:
{
clear();
m_data.clear();
setLineNumberData({}, 0);
setLineNumberData({}, {}, 0);
}
void appendData(const QVector<LineHighlight> &newData)
{
m_data.append(newData);
}
void setLineNumberData(QVector<int> data, int maxLineNum);
void setLineNumberData(QVector<int> lineNosA, QVector<int> lineNosB, int maxLineNum);
KTextEditor::Range selectionRange() const;
bool isHunkLine(int line) const;
void toggleFoldHunk(int block);
bool isHunkFolded(int blockNumber);
const LineHighlight *highlightingForLine(int line);
QColor removedColor() const
{
return red1;
}
QColor addedColor() const
{
return green1;
}
protected:
void resizeEvent(QResizeEvent *event) override;
......@@ -89,7 +99,6 @@ protected:
void contextMenuEvent(QContextMenuEvent *e) override;
private:
const LineHighlight *highlightingForLine(int line);
void updateLineNumberArea(const QRect &rect, int dy);
void updateLineNumAreaGeometry();
void updateLineNumberAreaWidth(int);
......
......@@ -9,10 +9,16 @@
#include <QTextBlock>
#include <KTextEditor/Editor>
// #include <KColorUtils>
static constexpr int Margin = 4;
struct LineNumColors {
const QPen &otherLine;
// const QPen &currentLine;
const QPen &added;
const QPen &removed;
};
LineNumArea::LineNumArea(DiffEditor *parent)
: QWidget(parent)
, textEdit(parent)
......@@ -22,7 +28,8 @@ LineNumArea::LineNumArea(DiffEditor *parent)
if (!e)
return;
auto theme = e->theme();
m_currentLineColor = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::EditorColorRole::CurrentLineNumber));
// m_currentLineBgColor = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::EditorColorRole::CurrentLine));
// m_currentLineColor = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::EditorColorRole::CurrentLineNumber));
m_otherLinesColor = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::EditorColorRole::LineNumbers));
m_borderColor = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::EditorColorRole::Separator));
auto bg = QColor::fromRgba(theme.editorColor(KSyntaxHighlighting::Theme::EditorColorRole::IconBorder));
......@@ -45,14 +52,19 @@ int LineNumArea::lineNumAreaWidth() const
return 13 + textEdit->fontMetrics().horizontalAdvance(u'9') * digits;
}
void LineNumArea::setLineNumData(QVector<int> data)
void LineNumArea::setLineNumData(QVector<int> leftLineNos, QVector<int> rightLineNos)
{
m_lineToNum = std::move(data);
m_lineToNumA = std::move(leftLineNos);
m_lineToNumB = std::move(rightLineNos);
}
QSize LineNumArea::sizeHint() const
{
return {lineNumAreaWidth() + textEdit->fontMetrics().height(), 0};
int lineNosWidth = lineNumAreaWidth();
if (!m_lineToNumB.empty()) {
lineNosWidth *= 2;
}
return {lineNosWidth + textEdit->fontMetrics().height(), 0};
}
static void paintTriangle(QPainter &painter, QColor c, int xOffset, int yOffset, int width, int height, bool open)
......@@ -88,7 +100,7 @@ static void paintTriangle(QPainter &painter, QColor c, int xOffset, int yOffset,
void LineNumArea::paintEvent(QPaintEvent *event)
{
if (m_lineToNum.isEmpty()) {
if (m_lineToNumA.isEmpty()) {
return;
}
QPainter painter(this);
......@@ -102,25 +114,45 @@ void LineNumArea::paintEvent(QPaintEvent *event)
top += textEdit->viewportMargins().top();
qreal bottom = top;
const QPen currentLine = m_currentLineColor;
// const QPen currentLine = m_currentLineColor;
const QPen otherLines = m_otherLinesColor;
painter.setFont(font());
QColor c = textEdit->addedColor();
c.setAlphaF(0.6);
const QPen added = c;
c = textEdit->removedColor();
c.setAlphaF(0.6);
const QPen removed = c;
LineNumColors colors{otherLines /*, currentLine*/, added, removed};
painter.setFont(font());
const int w = lineNumAreaWidth();
while (block.isValid() && top <= event->rect().bottom()) {
top = bottom;
bottom = top + textEdit->blockBoundingRect(block).height();
if (block.isVisible() && bottom >= event->rect().top()) {
int n = m_lineToNum.value(blockNumber, -1);
if (n > -1) {
const QString number = QString::number(n);
auto isCurrentLine = textEdit->textCursor().blockNumber() == blockNumber;
painter.setPen(isCurrentLine ? currentLine : otherLines);
QRect numRect(0, top, w, textEdit->fontMetrics().height());
numRect.adjust(0, 0, -(Margin * 2), 0);
painter.drawText(numRect, Qt::AlignRight | Qt::AlignVCenter | Qt::TextDontClip, number);
} else if (textEdit->isHunkLine(blockNumber)) {
// Current line background
// const auto isCurrentLine = textEdit->textCursor().blockNumber() == blockNumber;
// if (isCurrentLine) {
// painter.fillRect(0, top, rect().width(), textEdit->fontMetrics().height(), m_currentLineBgColor);
// }
// Line number left
int n = m_lineToNumA.value(blockNumber, -1);
QRect numRect(0, top, w, textEdit->fontMetrics().height());
drawLineNumber(painter, numRect, blockNumber, n, colors);
if (!m_lineToNumB.empty()) {
// we are in unified mode, draw the right line number
int n = m_lineToNumB.value(blockNumber, -1);
QRect numRect(w, top, w, textEdit->fontMetrics().height());
drawLineNumber(painter, numRect, blockNumber, n, colors);
}
// Hunk fold marker
if (textEdit->isHunkLine(blockNumber)) {
const int x = rect().width() - (textEdit->fontMetrics().height() + Margin);
const int y = top;
const int width = textEdit->fontMetrics().height();
......@@ -133,8 +165,36 @@ void LineNumArea::paintEvent(QPaintEvent *event)
++blockNumber;
}
painter.setPen(m_borderColor);
painter.drawLine(rect().topRight() - QPoint(1, 0), rect().bottomRight() - QPoint(1, 0));
// draw the line num area border
if (m_lineToNumB.empty()) {
// side by side
painter.setPen(m_borderColor);
painter.drawLine(rect().topRight() - QPoint(1, 0), rect().bottomRight() - QPoint(1, 0));
} else {
// unified
painter.setPen(m_borderColor);
painter.drawLine(QPoint(w, 0) - QPoint(1, 0), QPoint(w, rect().bottom()) - QPoint(1, 0));
painter.setPen(m_borderColor);
painter.drawLine(rect().topRight() - QPoint(1, 0), rect().bottomRight() - QPoint(1, 0));
}
}
void LineNumArea::drawLineNumber(QPainter &painter, QRect rect, int blockNumber, int num, const LineNumColors &c)
{
if (num < 0) {
return;
}
const QString number = QString::number(num);
QPen p = c.otherLine;
auto hl = textEdit->highlightingForLine(blockNumber);
if (hl) {
p = hl->added ? c.added : c.removed;
}
painter.setPen(p);
rect.adjust(0, 0, -(Margin * 2), 0);
painter.drawText(rect, Qt::AlignRight | Qt::AlignVCenter | Qt::TextDontClip, number);
}
void LineNumArea::mousePressEvent(QMouseEvent *e)
......
......@@ -16,7 +16,7 @@ public:
int lineNumAreaWidth() const;
QSize sizeHint() const override;
void setLineNumData(QVector<int>);
void setLineNumData(QVector<int> leftLineNos, QVector<int> rightLineNos);
void setMaxLineNum(int n)
{
maxLineNum = n;
......@@ -27,11 +27,15 @@ protected:
void mousePressEvent(QMouseEvent *e) override;
private:
void drawLineNumber(QPainter &painter, QRect rect, int blockNumber, int num, const struct LineNumColors &c);
class DiffEditor *const textEdit;
QColor m_currentLineColor;
// QColor m_currentLineColor;
// QBrush m_currentLineBgColor;
QColor m_otherLinesColor;
QColor m_borderColor;
QVector<int> m_lineToNum;
QVector<int> m_lineToNumA;
QVector<int> m_lineToNumB;
int maxLineNum = 0;
};
......
......@@ -589,8 +589,8 @@ void DiffWidget::parseAndShowDiff(const QByteArray &raw)
m_right->appendData(rightHlts);
m_left->appendPlainText(leftText);
m_right->appendPlainText(rightText);
m_left->setLineNumberData(lineNumsA, maxLineNoFound);
m_right->setLineNumberData(lineNumsB, maxLineNoFound);
m_left->setLineNumberData(lineNumsA, {}, maxLineNoFound);
m_right->setLineNumberData(lineNumsB, {}, maxLineNoFound);
m_lineToRawDiffLine += lineToRawDiffLine;
m_lineToDiffHunkLine += linesWithHunkHeading;
m_linesWithFileName += linesWithFileName;
......@@ -615,7 +615,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
QVector<LineHighlight> hlts;
// Line numbers that will be shown in the editor
QVector<int> lineNums;
QVector<int> lineNumsA;
QVector<int> lineNumsB;
// Changed lines of hunk, used to determine differences between two lines
HunkChangedLines hunkChangedLinesA;
......@@ -651,7 +652,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
if (m_params.flags.testFlag(DiffParams::ShowFileName)) {
lines.append(QStringLiteral("%1 → %2").arg(Utils::fileNameFromPath(srcFile), Utils::fileNameFromPath(tgtFile)));
lineNums.append(-1);
lineNumsA.append(-1);
lineNumsB.append(-1);
linesWithFileName.append(lineNo);
lineNo++;
}
......@@ -668,7 +670,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
// const QString headingRight = QStringLiteral("@@ ") + match.captured(2) + match.captured(3) /* + QStringLiteral(" ") + tgtFile*/;
lines.push_back(line);
lineNums.append(-1);
lineNumsA.append(-1);
lineNumsB.append(-1);
linesWithHunkHeading.append({lineNo, i, false});
lineNo++;
......@@ -689,8 +692,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
l = l.mid(1);
lines.append(l);
lineNums.append(srcLine++);
tgtLine++;
lineNumsA.append(srcLine++);
lineNumsB.append(tgtLine++);
lineNo++;
// lineB++;
} else if (l.startsWith(QLatin1Char('+'))) {
......@@ -702,7 +705,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
h.changes.push_back({0, Change::FullBlock});
lines.append(l);
hlts.push_back(h);
lineNums.append(tgtLine++);
lineNumsA.append(-1);
lineNumsB.append(tgtLine++);
lineToRawDiffLine.append({lineNo, j, false});
hunkChangedLinesB.emplace_back(l, lineNo, h.added);
......@@ -715,7 +719,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
h.changes.push_back({0, Change::FullBlock});
hlts.push_back(h);
lineNums.append(srcLine++);
lineNumsA.append(srcLine++);
lineNumsB.append(-1);
lines.append(l);
hunkChangedLinesA.emplace_back(l, lineNo, h.added);
lineToRawDiffLine.append({lineNo, j, false});
......@@ -725,7 +730,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
i = j - 1;
markInlineDiffs(hunkChangedLinesA, hunkChangedLinesB, hlts, hlts);
// add line number for current line
lineNums.append(-1);
lineNumsA.append(-1);
lineNumsB.append(-1);
// add new line
lines.append(QString());
......@@ -736,7 +742,8 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
markInlineDiffs(hunkChangedLinesA, hunkChangedLinesB, hlts, hlts);
// add new line
lines.append(QString());
lineNums.append(-1);
lineNumsA.append(-1);
lineNumsB.append(-1);
lineNo += 1;
break;
}
......@@ -749,7 +756,7 @@ void DiffWidget::parseAndShowDiffUnified(const QByteArray &raw)
m_left->appendData(hlts);
m_left->appendPlainText(lines.join(QLatin1Char('\n')));
m_left->setLineNumberData(lineNums, maxLineNoFound);
m_left->setLineNumberData(lineNumsA, lineNumsB, maxLineNoFound);
m_lineToDiffHunkLine += linesWithHunkHeading;
m_lineToRawDiffLine += lineToRawDiffLine;
m_linesWithFileName += linesWithFileName;
......
Supports Markdown
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