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

Qt: honor control characters in ScriptingTextBuffer::print #3350

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
76 changes: 60 additions & 16 deletions src/platform/qt/scripting/ScriptingTextBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,70 @@ void ScriptingTextBuffer::setBufferName(const QString& name) {
emit bufferNameChanged(name);
}

void ScriptingTextBuffer::lineBreak() {
bool nextBlockExists = m_shim.cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, 1);
if (!nextBlockExists) {
m_shim.cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::MoveAnchor, 1);
m_shim.cursor.insertBlock();
}
}

void ScriptingTextBuffer::carriageReturn() {
m_shim.cursor.movePosition(QTextCursor::StartOfLine, QTextCursor::MoveAnchor, 1);
}

void ScriptingTextBuffer::tab() {
QTextCursor& cursor = m_shim.cursor;
int column = cursor.positionInBlock();
int move = tabStop - (column % tabStop) + 1;
if (column + move >= m_dims.width()) {
lineBreak();
} else if (column + move <= cursor.block().length()) {
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, move - 1);
} else {
move = column + move - cursor.block().length();
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::MoveAnchor, 1);
cursor.insertText(QString(move, ' '));
}
}

void ScriptingTextBuffer::insertString(const QString& text) {
QTextCursor& cursor = m_shim.cursor;
if (cursor.positionInBlock() >= m_dims.width()) {
lineBreak();
}
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor, text.length());
cursor.insertText(text);
}

void ScriptingTextBuffer::print(const QString& text) {
QMutexLocker locker(&m_mutex);
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);

QTextCursor& cursor = m_shim.cursor;
QString toInsert;

for (const QChar& ch : text) {
int column = cursor.positionInBlock();
if (ch == '\t' || ch == '\n' || ch == '\r' || column + toInsert.length() >= m_dims.width()) {
insertString(toInsert);
toInsert.clear();
}
if (!m_shim.cursor.atBlockEnd()) {
m_shim.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, split.length());
switch (ch.unicode()) {
case '\t':
tab();
break;
case '\n':
lineBreak();
break;
case '\r':
carriageReturn();
break;
default:
toInsert += ch;
}
m_shim.cursor.insertText(split);
}
if (!toInsert.isEmpty()) {
insertString(toInsert);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/platform/qt/scripting/ScriptingTextBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public slots:
void bufferNameChanged(const QString&);

private:
enum { tabStop = 4 };

struct ScriptingBufferShim : public mScriptTextBuffer {
ScriptingTextBuffer* p;
QTextCursor cursor;
Expand All @@ -44,6 +46,11 @@ public slots:
QMutex m_mutex;
QString m_name;

void lineBreak();
void carriageReturn();
void tab();
void insertString(const QString& text);

static void init(struct mScriptTextBuffer*, const char* name);
static void deinit(struct mScriptTextBuffer*);

Expand Down