Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: codegeex message display issue #81

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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