Skip to content

Commit

Permalink
fix: codegeex message display issue
Browse files Browse the repository at this point in the history
fix code edit component display issue
  • Loading branch information
Lighto-Ku authored and deepin-mozart committed Nov 16, 2023
1 parent a4fc277 commit e530e2c
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 28 deletions.
11 changes: 8 additions & 3 deletions src/plugins/codegeex/data/messagedata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ MessageData::MessageData(const QString &id, Type type)
void MessageData::updateData(const QString &data)
{
QStringList lines = data.split("\n");
if (lines.last().isEmpty())
lines.removeLast();
lines.removeAll("");

if (lines.length() < msgDataLines.length())
return;
Expand All @@ -36,7 +35,13 @@ void MessageData::updateData(const QString &data)

msgData = data;
msgDataLines = lines;
// qInfo() << "update msg line" << msgDataLines;
// qInfo() << "update msg line" << msgDataLines;
}

void MessageData::appendData(const QStringList &data)
{
msgDataLines.append(data);
msgData.append(data.join("\n"));
}

QString MessageData::messageID() const
Expand Down
1 change: 1 addition & 0 deletions src/plugins/codegeex/data/messagedata.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MessageData
explicit MessageData(const QString &id, Type type);

void updateData(const QString &data);
void appendData(const QStringList &data);

QString messageID() const;
Type messageType() const;
Expand Down
74 changes: 49 additions & 25 deletions src/plugins/codegeex/widgets/messagecomponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,8 @@ MessageComponent::MessageComponent(const MessageData &msgData, QWidget *parent)
}
void MessageComponent::updateMessage(const MessageData &msgData)
{
if (currentUpdateState == CodeEdit && msgData.messageLines().last() == "```") {
curUpdateEdit->cleanFinalLine();
currentUpdateState = Label;
messageData = msgData;
if (!createCodeEdit(msgData))
return;
} else if (curUpdateLabel && currentUpdateState == Label && msgData.messageLines().last().startsWith("```")) {
if (curUpdateLabel->text() == "``" || curUpdateLabel->text() == "`") {
msgLayout->removeWidget(curUpdateLabel);
delete curUpdateLabel;
curUpdateLabel = nullptr;
}
currentUpdateState = CodeEdit;
curUpdateEdit = new CodeEditComponent(this);
curUpdateEdit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
curUpdateEdit->setReadOnly(true);
curUpdateEdit->setUpdateHeight(true);
curUpdateEdit->showButtons(CodeEditComponent::CopyAndInsert);
msgLayout->addWidget(curUpdateEdit);
}

switch (currentUpdateState) {
case Label:
Expand All @@ -57,14 +40,8 @@ void MessageComponent::updateMessage(const MessageData &msgData)
curUpdateLabel->setText(msgData.messageLines().last());
break;
case CodeEdit:
if (msgData.messageLines().at(msgData.messageLines().length() - 2) == "```") {
curUpdateEdit->cleanFinalLine();
currentUpdateState = Label;
updateMessage(msgData);
return;
}
if (curUpdateEdit) {
int startIndex = msgData.messageLines().lastIndexOf(QRegularExpression("```([a-z]+|[A-Z]+)"));
int startIndex = msgData.messageLines().lastIndexOf(QRegularExpression("```([a-z]*|[A-Z]*)"));
curUpdateEdit->updateCode(msgData.messageLines().mid(startIndex + 1));
}
break;
Expand Down Expand Up @@ -123,3 +100,50 @@ void MessageComponent::initMessageSection()
msgLayout = new QVBoxLayout;
qobject_cast<QVBoxLayout*>(layout())->addLayout(msgLayout);
}

bool MessageComponent::createCodeEdit(const MessageData &newData)
{
QStringList newLines = newData.messageLines();
QStringList oldLines = messageData.messageLines();
QStringList addedLines = newLines.mid(oldLines.count());

for (int i = 0; i < addedLines.count(); ++i) {
QString addedLine = addedLines.at(i);
if (addedLine.contains("`")) {
if (i != 0) {
MessageData addedMsgData = messageData;
addedMsgData.appendData(addedLines.mid(0, i));
updateMessage(addedMsgData);
}

QRegExp re("```([a-z]*|[A-Z]*)");
if (re.exactMatch(addedLine) && currentUpdateState == Label) {
// create new code edit component
messageData.appendData({ addedLine });
currentUpdateState = CodeEdit;
curUpdateEdit = new CodeEditComponent(this);
curUpdateEdit->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Expanding);
curUpdateEdit->setReadOnly(true);
curUpdateEdit->setUpdateHeight(true);
curUpdateEdit->showButtons(CodeEditComponent::CopyAndInsert);
msgLayout->addWidget(curUpdateEdit);
return true;
} else if (addedLine == "```" && currentUpdateState == CodeEdit) {
// end the code edit component update
messageData.appendData({ addedLine });
currentUpdateState = Label;
curUpdateLabel = new DLabel(this);
curUpdateLabel->setWordWrap(true);
msgLayout->addWidget(curUpdateLabel);
if (i != (addedLines.count() - 1))
updateMessage(newData);

return false;
}

return false;
}
}

return true;
}
1 change: 1 addition & 0 deletions src/plugins/codegeex/widgets/messagecomponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MessageComponent : public DWidget
void initMessageSection();

void createCurrentUpdateWidget();
bool createCodeEdit(const MessageData &newData);

DLabel *senderHead { nullptr };
DLabel *senderName { nullptr };
Expand Down

0 comments on commit e530e2c

Please sign in to comment.