1#include <SourceCodeReview.h>
2
3#include <FileDiffView.h>
4#include <LineNumberArea.h>
5
6#include <QLabel>
7#include <QVBoxLayout>
8
9SourceCodeReview::SourceCodeReview(const QString &filePath, const QString &sourceCode, int commentLine, QWidget *parent)
10 : QFrame(parent)
11{
12 auto lines = sourceCode.split("\n");
13 const auto hunkDescription = lines.takeFirst();
14 auto hunkRemoved = false;
15
16#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
17 const auto flag = Qt::SkipEmptyParts;
18#else
19 const auto flag = QString::SkipEmptyParts;
20#endif
21
22 if (hunkDescription.split("@@", flag).count() == 1)
23 {
24 --commentLine;
25 hunkRemoved = true;
26 }
27
28 auto linesCount = 0;
29 QString summary;
30
31 for (auto i = 0, j = lines.count() - 1; i <= 4 && j >= 0; ++i, --j)
32 {
33 if (!lines[j].isEmpty() && lines[j].count() > 1)
34 {
35 ++linesCount;
36 summary.prepend(lines[j] + QString::fromUtf8("\n"));
37
38 if (lines[j].startsWith("-"))
39 --i;
40 }
41 }
42
43 if (!hunkRemoved)
44 {
45 ++linesCount;
46 summary.prepend(hunkDescription + QString::fromUtf8("\n"));
47 }
48
49 const auto diff = new FileDiffView();
50 diff->addNumberArea(new LineNumberArea(diff));
51 diff->setStartingLine(commentLine - linesCount + 1);
52 diff->setUnifiedDiff(true);
53 diff->loadDiff(summary.trimmed());
54 diff->setTextInteractionFlags(Qt::NoTextInteraction);
55 diff->setMinimumWidth(800);
56 diff->show();
57
58 const auto height = diff->getHeight();
59 diff->setFixedHeight(height + 10);
60
61 const auto mainLayout = new QVBoxLayout(this);
62 mainLayout->setContentsMargins(QMargins());
63 mainLayout->setSpacing(0);
64 mainLayout->addWidget(new QLabel(filePath));
65 mainLayout->addWidget(diff);
66}
67