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.checkText("test"), true);
compare(chatMarkdownHelper.checkFormats([]), true); compare(chatMarkdownHelper.checkFormats([]), true);
textEdit.insert(4, "**b"); textEdit.insert(4, "**b");
compare(chatMarkdownHelper.checkText("testb"), true); compare(chatMarkdownHelper.checkText("test**b"), true);
compare(chatMarkdownHelper.checkFormats([RichFormat.Bold]), true); compare(chatMarkdownHelper.checkFormats([]), true);
textEdit.clear(); textEdit.clear();
textEdit.insert(0, "test"); textEdit.insert(0, "test");
compare(chatMarkdownHelper.checkText("test"), true); compare(chatMarkdownHelper.checkText("test"), true);
compare(chatMarkdownHelper.checkFormats([]), true); compare(chatMarkdownHelper.checkFormats([]), true);
textEdit.insert(2, "**b"); textEdit.insert(2, "**b");
compare(chatMarkdownHelper.checkText("tebst"), true); compare(chatMarkdownHelper.checkText("te**bst"), true);
compare(chatMarkdownHelper.checkFormats([RichFormat.Bold]), 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. // This can happen when formatting is applied.
if (charsAdded == charsRemoved) { if (charsAdded == charsRemoved) {
return; return;
} } else if (charsRemoved > charsAdded || charsAdded - charsRemoved > 1) {
auto cursor = m_textItem->textCursor(); updatePosition(std::max(0, position - charsRemoved + charsAdded));
if (cursor.isNull()) {
return; return;
} }
if (charsRemoved > charsAdded) { checkMarkdownForward();
updatePosition(std::max(0, position - charsRemoved + charsAdded));
}
checkMarkdownForward(charsAdded - charsRemoved);
} }
void ChatMarkdownHelper::updatePosition(int position) 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(); auto cursor = m_textItem->textCursor();
if (cursor.isNull()) { if (cursor.isNull()) {
return; return;
} }
for (auto i = 1; i <= charsAdded; ++i) { cursor.setPosition(m_startPos);
cursor.setPosition(m_startPos); const auto atBlockStart = cursor.atBlockStart();
const auto atBlockStart = cursor.atBlockStart(); cursor.setPosition(m_endPos, QTextCursor::KeepAnchor);
cursor.setPosition(m_endPos, QTextCursor::KeepAnchor); const auto currentMarkdown = cursor.selectedText();
const auto currentMarkdown = cursor.selectedText(); cursor.setPosition(m_endPos);
cursor.setPosition(m_endPos); cursor.setPosition(m_endPos + 1, QTextCursor::KeepAnchor);
cursor.setPosition(m_endPos + 1, QTextCursor::KeepAnchor); const auto nextChar = cursor.selectedText();
const auto nextChar = cursor.selectedText();
const auto result = checkSequence(currentMarkdown, nextChar, atBlockStart); const auto result = checkSequence(currentMarkdown, nextChar, atBlockStart);
if (!result) { if (!result) {
++m_startPos; ++m_startPos;
m_endPos = m_startPos; m_endPos = m_startPos;
continue; return;
;
}
if (!*result) {
++m_endPos;
continue;
;
}
complete();
} }
if (!*result) {
++m_endPos;
return;
}
complete();
} }
void ChatMarkdownHelper::complete() void ChatMarkdownHelper::complete()

View File

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