Change the behaviour of chatmarkdownhelper to only work when a single character is added.

This is because if pasting a link for example * or _ could be treated as formatting. We now assume that any pasted text is done as plain text and is wanted as is.
This commit is contained in:
James Graham
2026-02-15 12:24:53 +00:00
parent 98a224ebdf
commit a438173403
3 changed files with 27 additions and 39 deletions

View File

@@ -159,15 +159,15 @@ TestCase {
compare(chatMarkdownHelper.checkText("test"), true);
compare(chatMarkdownHelper.checkFormats([]), true);
textEdit.insert(4, "**b");
compare(chatMarkdownHelper.checkText("testb"), true);
compare(chatMarkdownHelper.checkFormats([RichFormat.Bold]), true);
compare(chatMarkdownHelper.checkText("test**b"), true);
compare(chatMarkdownHelper.checkFormats([]), true);
textEdit.clear();
textEdit.insert(0, "test");
compare(chatMarkdownHelper.checkText("test"), true);
compare(chatMarkdownHelper.checkFormats([]), true);
textEdit.insert(2, "**b");
compare(chatMarkdownHelper.checkText("tebst"), true);
compare(chatMarkdownHelper.checkFormats([RichFormat.Bold]), true);
compare(chatMarkdownHelper.checkText("te**bst"), true);
compare(chatMarkdownHelper.checkFormats([]), true);
}
}

View File

@@ -188,17 +188,12 @@ void ChatMarkdownHelper::checkMarkdown(int position, int charsRemoved, int chars
// This can happen when formatting is applied.
if (charsAdded == charsRemoved) {
return;
}
auto cursor = m_textItem->textCursor();
if (cursor.isNull()) {
} else if (charsRemoved > charsAdded || charsAdded - charsRemoved > 1) {
updatePosition(std::max(0, position - charsRemoved + charsAdded));
return;
}
if (charsRemoved > charsAdded) {
updatePosition(std::max(0, position - charsRemoved + charsAdded));
}
checkMarkdownForward(charsAdded - charsRemoved);
checkMarkdownForward();
}
void ChatMarkdownHelper::updatePosition(int position)
@@ -226,40 +221,33 @@ void ChatMarkdownHelper::updatePosition(int position)
}
}
void ChatMarkdownHelper::checkMarkdownForward(int charsAdded)
void ChatMarkdownHelper::checkMarkdownForward()
{
if (charsAdded <= 0) {
return;
}
auto cursor = m_textItem->textCursor();
if (cursor.isNull()) {
return;
}
for (auto i = 1; i <= charsAdded; ++i) {
cursor.setPosition(m_startPos);
const auto atBlockStart = cursor.atBlockStart();
cursor.setPosition(m_endPos, QTextCursor::KeepAnchor);
const auto currentMarkdown = cursor.selectedText();
cursor.setPosition(m_endPos);
cursor.setPosition(m_endPos + 1, QTextCursor::KeepAnchor);
const auto nextChar = cursor.selectedText();
cursor.setPosition(m_startPos);
const auto atBlockStart = cursor.atBlockStart();
cursor.setPosition(m_endPos, QTextCursor::KeepAnchor);
const auto currentMarkdown = cursor.selectedText();
cursor.setPosition(m_endPos);
cursor.setPosition(m_endPos + 1, QTextCursor::KeepAnchor);
const auto nextChar = cursor.selectedText();
const auto result = checkSequence(currentMarkdown, nextChar, atBlockStart);
if (!result) {
++m_startPos;
m_endPos = m_startPos;
continue;
;
}
if (!*result) {
++m_endPos;
continue;
;
}
complete();
const auto result = checkSequence(currentMarkdown, nextChar, atBlockStart);
if (!result) {
++m_startPos;
m_endPos = m_startPos;
return;
}
if (!*result) {
++m_endPos;
return;
}
complete();
}
void ChatMarkdownHelper::complete()

View File

@@ -42,6 +42,6 @@ private:
void checkMarkdown(int position, int charsRemoved, int charsAdded);
void updatePosition(int position);
void checkMarkdownForward(int charsAdded);
void checkMarkdownForward();
void complete();
};