Skip to content

Commit ff30ed2

Browse files
committed
Added support of comments to viewer
1 parent 0562ee1 commit ff30ed2

File tree

6 files changed

+54
-28
lines changed

6 files changed

+54
-28
lines changed

DiffFrontend/diffviewertext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ QString DiffViewerText::getText()
1616
return text;
1717
}
1818

19-
void DiffViewerText::setTextDescriptionFromJson(QJsonValue jsonVal,bool isTopLevel)
19+
void DiffViewerText::setTextDescriptionFromJson(QJsonValue jsonVal,QJsonObject comments, bool isTopLevel)
2020
{
2121
DiffViewerTextBuilder builder;
22-
text = builder.generateText(jsonVal, isTopLevel);
22+
text = builder.generateText(jsonVal,comments, isTopLevel);
2323
}

DiffFrontend/diffviewertext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class DiffViewerText : public QObject
1010
public:
1111
explicit DiffViewerText(QObject *parent = nullptr);
1212
QString getText();
13-
void setTextDescriptionFromJson(QJsonValue obj, bool isTopLevel = true);
13+
void setTextDescriptionFromJson(QJsonValue obj, QJsonObject comments, bool isTopLevel = true);
1414
private:
1515

1616
QJsonDocument loadedJson;

DiffFrontend/diffviewertextbuilder.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ DiffViewerTextBuilder::DiffViewerTextBuilder()
88

99
}
1010

11-
QString DiffViewerTextBuilder::generateText(QJsonValue jsonVal,bool isTopLevel = true)
11+
QString DiffViewerTextBuilder::generateText(QJsonValue jsonVal, QJsonObject comments, bool isTopLevel= true)
1212
{
1313
this->isTopLevel = isTopLevel;
14+
this->comments = comments;
1415
text.append("<pre>");
1516
if(isTopLevel){
1617
pasteTopLevel(jsonVal.toArray());
@@ -31,6 +32,23 @@ void DiffViewerTextBuilder::pasteTopLevel(const QJsonArray &array)
3132
loopArray(array);
3233
}
3334

35+
void DiffViewerTextBuilder::pasteNewLinesAndComments(int next_line){
36+
QJsonObject comment;
37+
while(cur_line < next_line){
38+
if(comments[QString::number(cur_line+diff_line)] != QJsonValue::Undefined) {
39+
comment = comments[QString::number(cur_line+diff_line)].toObject();
40+
int column_diff = comment["column"].toInt() - cur_column;
41+
pasteSpaces(column_diff);
42+
text.append("<font style=\"color:#cccccc;\">");
43+
text.append(comment["comment"].toString());
44+
text.append("</font>");
45+
46+
}
47+
text.append("\n");
48+
cur_column = 1;
49+
cur_line++;
50+
}
51+
}
3452

3553
void DiffViewerTextBuilder::pasteLexem(const QJsonObject &lex)
3654
{
@@ -57,17 +75,15 @@ void DiffViewerTextBuilder::pasteParent(QChar parent){
5775
cur_column++;
5876
}
5977

78+
6079
void DiffViewerTextBuilder::pasteSpacesBeforeParent(int line, int column)
6180
{
6281
if(cur_line == line){
6382
pasteSpaces(column - cur_column);
64-
cur_line = line;
6583
cur_column = column;
6684
} else {
67-
int line_delta = line - cur_line;
68-
text.append(QString(line_delta, '\n'));
85+
pasteNewLinesAndComments(line);
6986
pasteSpaces(column - 1);
70-
cur_line = line;
7187
cur_column = column;
7288
}
7389
}
@@ -78,9 +94,8 @@ void DiffViewerTextBuilder::genLexem(const QJsonObject &lex)
7894
if(getTrueLine(lexem_pos[0].toInt()) == cur_line){
7995
pasteSpaces(lexem_pos[1].toInt() - cur_column);
8096
} else {
81-
int line_delta = getTrueLine(lexem_pos[0].toInt()) - cur_line;
82-
text.append(QString(line_delta, '\n'));
83-
pasteSpaces(lexem_pos[1].toInt());
97+
pasteNewLinesAndComments(getTrueLine(lexem_pos[0].toInt()));
98+
pasteSpaces(lexem_pos[1].toInt() - cur_column);
8499
cur_line = getTrueLine(lexem_pos[0].toInt());
85100
}
86101
pasteLexem(lex);

DiffFrontend/diffviewertextbuilder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define DIFFVIEWERTEXTBUILDER_H
33

44
#include <QJsonDocument>
5+
#include <QJsonObject>
56
#include <QString>
67

78

@@ -11,10 +12,11 @@ class DiffViewerTextBuilder
1112
public:
1213
DiffViewerTextBuilder();
1314

14-
QString generateText(QJsonValue, bool);
15+
QString generateText(QJsonValue, QJsonObject, bool);
1516

1617
private:
1718
QString text;
19+
QJsonObject comments;
1820
bool isTopLevel;
1921

2022
int cur_line=1;
@@ -23,6 +25,7 @@ class DiffViewerTextBuilder
2325
int diff_line = 0;
2426

2527
int getTrueLine(int cur_line);
28+
void pasteNewLinesAndComments(int lines);
2629
void pasteTopLevel(const QJsonArray& doc);
2730
void pasteLexem(const QJsonObject& lex);
2831
void pasteParent(QChar parent);

DiffFrontend/mainwindow.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,25 @@ void MainWindow::on_actionloadFiles_triggered()
3131
QString file = "diff-backend.exe";
3232
qDebug() << file;
3333
backend_process->start(file,lispFiles);
34-
if(!backend_process->waitForFinished()){
34+
if (!backend_process->waitForFinished()) {
3535
qDebug() << "something is wrong";
3636
}
3737

3838
stats = Stat("stats.json");
3939
fillStatsTree();
40-
file1 = getJsonDocument("res1.json");
41-
file2 = getJsonDocument("res2.json");
42-
analyzeAST(file1,1);
43-
analyzeAST(file2,2);
40+
synTreeJson1 = getJsonDocument("res1.json");
41+
synTreeJson2 = getJsonDocument("res2.json");
42+
if (QFile::exists("comments1.json")) {
43+
commentsJsonObj1 = getJsonDocument("comments1.json").object();
44+
}
45+
if (QFile::exists("comments2.json")) {
46+
commentsJsonObj2 = getJsonDocument("comments2.json").object();
47+
}
48+
analyzeSynTree(synTreeJson1,1);
49+
analyzeSynTree(synTreeJson2,2);
4450

45-
doc1.setTextDescriptionFromJson(QJsonValue(file1.array()));
46-
doc2.setTextDescriptionFromJson(QJsonValue(file2.array()));
51+
doc1.setTextDescriptionFromJson(QJsonValue(synTreeJson1.array()),commentsJsonObj1);
52+
doc2.setTextDescriptionFromJson(QJsonValue(synTreeJson2.array()),commentsJsonObj2);
4753
ui->plainTextEdit->appendHtml(doc1.getText());
4854
ui->plainTextEdit_2->appendHtml(doc2.getText());
4955
}
@@ -99,7 +105,7 @@ void MainWindow::fillStatsTree()
99105
defunsStats->addChild(dels);
100106
}
101107

102-
void MainWindow::analyzeAST(QJsonDocument &doc, int num)
108+
void MainWindow::analyzeSynTree(QJsonDocument &doc, int num)
103109
{
104110
const auto& topLevelArray = doc.array();
105111
for(const auto& obj: topLevelArray){
@@ -117,7 +123,7 @@ QJsonDocument MainWindow::getJsonDocument(QString pathname)
117123
{
118124
QFile loadFile(pathname);
119125
if(!loadFile.open(QIODevice::ReadOnly)){
120-
qWarning("Couldn't open save file.");
126+
qWarning("Couldn't open file.");
121127
return QJsonDocument();
122128
}
123129

@@ -131,20 +137,20 @@ void MainWindow::on_treeWidget_itemClicked(QTreeWidgetItem *item, int column)
131137
qDebug() << item->text(0);
132138
QString text = item->text(0);
133139
if(text == "all"){
134-
doc1.setTextDescriptionFromJson(QJsonValue(file1.array()));
135-
doc2.setTextDescriptionFromJson(QJsonValue(file2.array()));
140+
doc1.setTextDescriptionFromJson(QJsonValue(synTreeJson1.array()), commentsJsonObj1);
141+
doc2.setTextDescriptionFromJson(QJsonValue(synTreeJson2.array()), commentsJsonObj2);
136142
ui->plainTextEdit->clear();
137143
ui->plainTextEdit_2->clear();
138144
ui->plainTextEdit->appendHtml(doc1.getText());
139145
ui->plainTextEdit_2->appendHtml(doc2.getText());
140146
} else {
141147
if(nameToObj1.contains(text)){
142-
doc1.setTextDescriptionFromJson(QJsonValue(nameToObj1[text]),false);
148+
doc1.setTextDescriptionFromJson(QJsonValue(nameToObj1[text]), commentsJsonObj1,false);
143149
ui->plainTextEdit->clear();
144150
ui->plainTextEdit->appendHtml(doc1.getText());
145151
}
146152
if(nameToObj2.contains(text)){
147-
doc2.setTextDescriptionFromJson(QJsonValue(nameToObj2[text]),false);
153+
doc2.setTextDescriptionFromJson(QJsonValue(nameToObj2[text]), commentsJsonObj2,false);
148154
ui->plainTextEdit_2->clear();
149155
ui->plainTextEdit_2->appendHtml(doc2.getText());
150156
}

DiffFrontend/mainwindow.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ private slots:
3434
private:
3535
Ui::MainWindow *ui;
3636
void fillStatsTree();
37-
void analyzeAST(QJsonDocument& doc, int num);
37+
void analyzeSynTree(QJsonDocument& doc, int num);
3838
QJsonDocument getJsonDocument(QString pathname);
39-
QJsonDocument file1;
40-
QJsonDocument file2;
39+
QJsonDocument synTreeJson1;
40+
QJsonDocument synTreeJson2;
41+
QJsonObject commentsJsonObj1;
42+
QJsonObject commentsJsonObj2;
4143

4244
};
4345
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)