From b27296896904929607da93fb01e9831aa8ee36ff Mon Sep 17 00:00:00 2001 From: Adam Higerd Date: Fri, 11 Oct 2024 19:50:05 -0500 Subject: [PATCH] Qt: handle newlines in TextBuffer print() --- .../qt/scripting/ScriptingTextBuffer.cpp | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/platform/qt/scripting/ScriptingTextBuffer.cpp b/src/platform/qt/scripting/ScriptingTextBuffer.cpp index 9a5de818d..da05bd2b9 100644 --- a/src/platform/qt/scripting/ScriptingTextBuffer.cpp +++ b/src/platform/qt/scripting/ScriptingTextBuffer.cpp @@ -51,26 +51,25 @@ void ScriptingTextBuffer::setBufferName(const QString& name) { void ScriptingTextBuffer::print(const QString& text) { QMutexLocker locker(&m_mutex); - QString split(text); - m_shim.cursor.beginEditBlock(); - while (m_shim.cursor.positionInBlock() + split.length() > m_dims.width()) { - int cut = m_dims.width() - m_shim.cursor.positionInBlock(); + for (QString split : text.split('\n')) { + while (m_shim.cursor.positionInBlock() + split.length() > m_dims.width()) { + int cut = m_dims.width() - m_shim.cursor.positionInBlock(); + if (!m_shim.cursor.atBlockEnd()) { + m_shim.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); + } + m_shim.cursor.insertText(split.left(cut)); + if (m_shim.cursor.atEnd()) { + m_shim.cursor.insertBlock(); + } else { + m_shim.cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, 1); + } + split = split.mid(cut); + } if (!m_shim.cursor.atBlockEnd()) { - m_shim.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); + m_shim.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, split.length()); } - m_shim.cursor.insertText(split.left(cut)); - if (m_shim.cursor.atEnd()) { - m_shim.cursor.insertBlock(); - } else { - m_shim.cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, 1); - } - split = split.mid(cut); + m_shim.cursor.insertText(split); } - if (!m_shim.cursor.atBlockEnd()) { - m_shim.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, split.length()); - } - m_shim.cursor.insertText(split); - m_shim.cursor.endEditBlock(); } void ScriptingTextBuffer::clear() {