Simplify and fix chat markdown helper and add additional tests
This commit is contained in:
@@ -119,7 +119,7 @@ QQC2.Control {
|
||||
|
||||
height: Math.max(Math.min(chatScrollView.contentHeight + bottomPadding + topPadding, Kirigami.Units.gridUnit * 10), Kirigami.Units.gridUnit * 5)
|
||||
leftPadding: rightPadding
|
||||
rightPadding: (root.width - chatBarSizeHelper.availableWidth) / 2
|
||||
rightPadding: (root.width - chatBarSizeHelper.availableWidth) / 2 + Kirigami.Units.largeSpacing
|
||||
topPadding: Kirigami.Units.smallSpacing
|
||||
bottomPadding: Kirigami.Units.smallSpacing
|
||||
|
||||
|
||||
@@ -69,6 +69,20 @@ std::optional<bool> checkSequence(const QString ¤tString, const QString &n
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
bool checkSequenceBackwards(const QString ¤tString)
|
||||
{
|
||||
auto it = syntax.cbegin();
|
||||
while ((it = std::find_if(it,
|
||||
syntax.cend(),
|
||||
[currentString](const MarkdownSyntax &syntax) {
|
||||
return syntax.sequence.endsWith(currentString);
|
||||
}))
|
||||
!= syntax.cend()) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::optional<MarkdownSyntax> syntaxForSequence(const QString &sequence)
|
||||
{
|
||||
const auto it = std::find_if(syntax.cbegin(), syntax.cend(), [sequence](const MarkdownSyntax &syntax) {
|
||||
@@ -118,75 +132,93 @@ void ChatMarkdownHelper::setTextItem(ChatTextItemHelper *textItem)
|
||||
|
||||
void ChatMarkdownHelper::updateStart()
|
||||
{
|
||||
m_startPos = *m_textItem->cursorPosition();
|
||||
m_endPos = m_startPos;
|
||||
if (m_startPos == 0) {
|
||||
m_currentState = Pre;
|
||||
if (!m_textItem) {
|
||||
return;
|
||||
}
|
||||
const auto newCursorPosition = m_textItem->cursorPosition();
|
||||
if (newCursorPosition) {
|
||||
updatePosition(*newCursorPosition);
|
||||
}
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::checkMarkdown(int position, int charsRemoved, int charsAdded)
|
||||
{
|
||||
updatePosition(position);
|
||||
|
||||
// This can happen when formatting is applied.
|
||||
if (charsAdded == charsRemoved) {
|
||||
return;
|
||||
}
|
||||
auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (charsRemoved - charsAdded > 0) {
|
||||
if (position < m_startPos) {
|
||||
m_startPos = position;
|
||||
}
|
||||
m_endPos -= charsRemoved;
|
||||
cursor.setPosition(m_endPos);
|
||||
cursor.setPosition(m_endPos + (cursor.atBlockEnd() ? 0 : 1), QTextCursor::KeepAnchor);
|
||||
const auto nextChar = cursor.selectedText();
|
||||
m_currentState = m_startPos == 0 || nextChar == u' ' ? Pre : None;
|
||||
if (charsRemoved > charsAdded) {
|
||||
updatePosition(std::max(0, position - charsRemoved + charsAdded));
|
||||
}
|
||||
|
||||
checkMarkdownForward(charsAdded - charsRemoved);
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::updatePosition(int position)
|
||||
{
|
||||
if (position == m_endPos) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto i = 1; i <= charsAdded - charsRemoved; ++i) {
|
||||
m_startPos = position;
|
||||
m_endPos = position;
|
||||
|
||||
if (m_startPos <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
cursor.setPosition(m_startPos);
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
|
||||
while (checkSequenceBackwards(cursor.selectedText()) && m_startPos > 0) {
|
||||
--m_startPos;
|
||||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
|
||||
}
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::checkMarkdownForward(int charsAdded)
|
||||
{
|
||||
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 result = checkSequence(currentMarkdown, nextChar, cursor.atBlockStart());
|
||||
|
||||
switch (m_currentState) {
|
||||
case None:
|
||||
if (nextChar == u' ' || cursor.atBlockEnd()) {
|
||||
m_currentState = Pre;
|
||||
}
|
||||
const auto result = checkSequence(currentMarkdown, nextChar, atBlockStart);
|
||||
if (!result) {
|
||||
++m_startPos;
|
||||
m_endPos = m_startPos;
|
||||
break;
|
||||
case Pre:
|
||||
if (!result && RichFormat::formatsAtCursor(cursor).length() == 0) {
|
||||
m_currentState = None;
|
||||
} else if (result && !*result) {
|
||||
m_currentState = Started;
|
||||
++m_endPos;
|
||||
break;
|
||||
}
|
||||
++m_startPos;
|
||||
m_endPos = m_startPos;
|
||||
break;
|
||||
case Started:
|
||||
if (!result) {
|
||||
m_currentState = Pre;
|
||||
++m_startPos;
|
||||
m_endPos = m_startPos;
|
||||
break;
|
||||
} else if (!*result) {
|
||||
++m_endPos;
|
||||
break;
|
||||
}
|
||||
complete();
|
||||
break;
|
||||
continue;
|
||||
;
|
||||
}
|
||||
if (!*result) {
|
||||
++m_endPos;
|
||||
continue;
|
||||
;
|
||||
}
|
||||
|
||||
complete();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,29 +232,24 @@ void ChatMarkdownHelper::complete()
|
||||
cursor.setPosition(m_startPos);
|
||||
cursor.setPosition(m_endPos, QTextCursor::KeepAnchor);
|
||||
const auto syntax = syntaxForSequence(cursor.selectedText());
|
||||
if (!syntax) {
|
||||
return;
|
||||
}
|
||||
cursor.removeSelectedText();
|
||||
|
||||
if (m_currentFormats.contains(syntax->format)) {
|
||||
m_currentFormats.remove(syntax->format);
|
||||
} else if (syntax->closable) {
|
||||
m_currentFormats.insert(syntax->format, m_startPos);
|
||||
}
|
||||
|
||||
cursor.setPosition(m_startPos);
|
||||
cursor.setPosition(m_startPos + 1, QTextCursor::KeepAnchor);
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
||||
const auto nextChar = cursor.selectedText();
|
||||
const auto result = checkSequence({}, nextChar, cursor.atBlockStart());
|
||||
m_currentState = result ? Started : Pre;
|
||||
|
||||
// cursor.setPosition(m_startPos + 1);
|
||||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
||||
if (syntax) {
|
||||
const auto formatType = RichFormat::typeForFormat(syntax->format);
|
||||
if (formatType == RichFormat::Block) {
|
||||
Q_EMIT unhandledBlockFormat(syntax->format);
|
||||
} else {
|
||||
m_textItem->mergeFormatOnCursor(syntax->format, cursor);
|
||||
}
|
||||
cursor.setPosition(m_startPos);
|
||||
cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);
|
||||
|
||||
const auto formatType = RichFormat::typeForFormat(syntax->format);
|
||||
if (formatType == RichFormat::Block) {
|
||||
Q_EMIT unhandledBlockFormat(syntax->format);
|
||||
} else {
|
||||
m_textItem->mergeFormatOnCursor(syntax->format, cursor);
|
||||
}
|
||||
|
||||
m_startPos = result ? m_startPos : m_startPos + 1;
|
||||
@@ -231,14 +258,4 @@ void ChatMarkdownHelper::complete()
|
||||
cursor.endEditBlock();
|
||||
}
|
||||
|
||||
void ChatMarkdownHelper::handleExternalFormatChange()
|
||||
{
|
||||
auto cursor = m_textItem->textCursor();
|
||||
if (cursor.isNull()) {
|
||||
return;
|
||||
}
|
||||
cursor.setPosition(m_startPos);
|
||||
m_currentState = RichFormat::formatsAtCursor(cursor).length() > 0 ? Pre : None;
|
||||
}
|
||||
|
||||
#include "moc_chatmarkdownhelper.cpp"
|
||||
|
||||
@@ -22,8 +22,6 @@ public:
|
||||
ChatTextItemHelper *textItem() const;
|
||||
void setTextItem(ChatTextItemHelper *textItem);
|
||||
|
||||
void handleExternalFormatChange();
|
||||
|
||||
Q_SIGNALS:
|
||||
void textItemChanged();
|
||||
|
||||
@@ -36,21 +34,14 @@ Q_SIGNALS:
|
||||
void unhandledBlockFormat(RichFormat::Format format);
|
||||
|
||||
private:
|
||||
enum State {
|
||||
None,
|
||||
Pre,
|
||||
Started,
|
||||
};
|
||||
|
||||
QPointer<ChatTextItemHelper> m_textItem;
|
||||
|
||||
State m_currentState = None;
|
||||
int m_startPos = 0;
|
||||
int m_endPos = 0;
|
||||
void updateStart();
|
||||
|
||||
QHash<RichFormat::Format, int> m_currentFormats;
|
||||
|
||||
void checkMarkdown(int position, int charsRemoved, int charsAdded);
|
||||
void updatePosition(int position);
|
||||
void checkMarkdownForward(int charsAdded);
|
||||
void complete();
|
||||
};
|
||||
|
||||
@@ -160,6 +160,14 @@ bool RichFormat::hasFormat(QTextCursor cursor, Format format)
|
||||
}
|
||||
}
|
||||
|
||||
bool RichFormat::hasAnyFormat(QTextCursor cursor, QList<Format> formats)
|
||||
{
|
||||
for (const auto &format : formats) {
|
||||
return hasFormat(cursor, format);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QList<RichFormat::Format> RichFormat::formatsAtCursor(const QTextCursor &cursor)
|
||||
{
|
||||
QList<Format> formats;
|
||||
|
||||
@@ -107,6 +107,13 @@ public:
|
||||
*/
|
||||
static bool hasFormat(QTextCursor cursor, Format format);
|
||||
|
||||
/**
|
||||
* @brief Whether the given QTextCursor has any of the given Formats.
|
||||
*
|
||||
* @sa Format, QTextCursor
|
||||
*/
|
||||
static bool hasAnyFormat(QTextCursor cursor, QList<Format> formats);
|
||||
|
||||
/**
|
||||
* @brief A lsit of Formats on the given QTextCursor.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user