Repository: nuttyartist/notes
File: src/customdocument.cpp (C++)

Code before:
            QString url = urlMatch.captured(1);
            urlMap[linkText] = url;
        }
    }

    return urlMap;
}

void CustomDocument::moveLineUp()
{
    qDebug() << "Moving line up";
    QTextCursor cursor = textCursor();

    if (cursor.blockNumber() > 0) {
        QString currentLine = cursor.block().text();
        const int currentHorizontalPosition = cursor.positionInBlock();
        qDebug() << "Current horizontal position: " << currentHorizontalPosition;

        cursor.movePosition(QTextCursor::StartOfBlock);
        cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
        cursor.removeSelectedText();
        // also remove empty line
        cursor.deletePreviousChar();

        // cursor is now at end of prev line, move to start
        if (!cursor.movePosition(QTextCursor::StartOfLine)) {
            qDebug() << "Could not move to start of line";
        }
        // insert the removed line again
        cursor.insertText(currentLine);
        cursor.insertBlock();

        // move cursor to previous line
        if (!cursor.movePosition(QTextCursor::PreviousBlock)) {
            qDebug() << "Could not move to previous block";
        }
        const int startPosition = cursor.position();
        qDebug() << "Start position (global): " << startPosition;
        qDebug() << "Moving cursor to position: " << startPosition + currentHorizontalPosition;
        cursor.setPosition(startPosition + currentHorizontalPosition);

        setTextCursor(cursor);
    } else {
        qDebug() << "Blocknumber is 0";
    }
}

void CustomDocument::moveLineDown()
{
    qDebug() << "Moving line down";
    QTextCursor cursor = textCursor();

Review comment (bug):
Changing these lines to the following solved the issue for me. Can you test?

```cpp
// cursor is now at end of prev line, move to start of block
        if (!cursor.movePosition(QTextCursor::StartOfBlock)) {
            qDebug() << "Could not move to start of block";
        }
```

Code after:
            QString url = urlMatch.captured(1);
            urlMap[linkText] = url;
        }
    }

    return urlMap;
}

void CustomDocument::moveBlockUp()
{
    QTextCursor cursor = textCursor();

    if (cursor.blockNumber() > 0) {
        QString currentBlock = cursor.block().text();
        const int currentHorizontalPosition = cursor.positionInBlock();

        cursor.movePosition(QTextCursor::StartOfBlock);
        cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
        cursor.removeSelectedText();
        // also remove empty line
        cursor.deletePreviousChar();

        // cursor is now at end of prev line, move to start
        if (!cursor.movePosition(QTextCursor::StartOfBlock)) {
            // block above is empty, this is fine
        }
        // insert the removed block again
        cursor.insertText(currentBlock);
        cursor.insertBlock();

        // move cursor to previous block
        if (!cursor.movePosition(QTextCursor::PreviousBlock)) {
            qDebug() << "Could not move to previous block";
        }
        const int startPosition = cursor.position();
        cursor.setPosition(startPosition + currentHorizontalPosition);

        setTextCursor(cursor);
    }
}

void CustomDocument::moveBlockDown()
{
    QTextCursor cursor = textCursor();

    QString currentBlock = cursor.block().text();
    const int currentHorizontalPosition = cursor.positionInBlock();

    cursor.movePosition(QTextCursor::StartOfBlock);
    cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
    cursor.removeSelectedText();