From 5e64ca758ba7cec35f121080b3ee31b54fecb098 Mon Sep 17 00:00:00 2001 From: liuyangming Date: Thu, 16 Nov 2023 16:45:55 +0800 Subject: [PATCH] fix: codegeex message display issue fix code edit component display issue --- src/plugins/codegeex/data/messagedata.cpp | 11 ++- src/plugins/codegeex/data/messagedata.h | 1 + .../codegeex/widgets/messagecomponent.cpp | 74 ++++++++++++------- .../codegeex/widgets/messagecomponent.h | 1 + 4 files changed, 59 insertions(+), 28 deletions(-) diff --git a/src/plugins/codegeex/data/messagedata.cpp b/src/plugins/codegeex/data/messagedata.cpp index ee7926e6f..af46de967 100644 --- a/src/plugins/codegeex/data/messagedata.cpp +++ b/src/plugins/codegeex/data/messagedata.cpp @@ -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; @@ -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 diff --git a/src/plugins/codegeex/data/messagedata.h b/src/plugins/codegeex/data/messagedata.h index e0238d77e..b6c58bf40 100644 --- a/src/plugins/codegeex/data/messagedata.h +++ b/src/plugins/codegeex/data/messagedata.h @@ -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; diff --git a/src/plugins/codegeex/widgets/messagecomponent.cpp b/src/plugins/codegeex/widgets/messagecomponent.cpp index 72aed1e47..c70989033 100644 --- a/src/plugins/codegeex/widgets/messagecomponent.cpp +++ b/src/plugins/codegeex/widgets/messagecomponent.cpp @@ -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: @@ -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; @@ -123,3 +100,50 @@ void MessageComponent::initMessageSection() msgLayout = new QVBoxLayout; qobject_cast(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; +} diff --git a/src/plugins/codegeex/widgets/messagecomponent.h b/src/plugins/codegeex/widgets/messagecomponent.h index 06e9f3459..9ebee0341 100644 --- a/src/plugins/codegeex/widgets/messagecomponent.h +++ b/src/plugins/codegeex/widgets/messagecomponent.h @@ -37,6 +37,7 @@ class MessageComponent : public DWidget void initMessageSection(); void createCurrentUpdateWidget(); + bool createCodeEdit(const MessageData &newData); DLabel *senderHead { nullptr }; DLabel *senderName { nullptr };