Skip to content

Commit

Permalink
fix(CodeEditor): MoveToStartOfLine and MoveToEndOfLine behaviors
Browse files Browse the repository at this point in the history
Ported from cpeditor/QCodeEditor#41

It fixes #774 and replaces #1149.
  • Loading branch information
ouuan committed Feb 13, 2024
1 parent 6901896 commit 2932d0b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Editor/CodeEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,34 @@ void CodeEditor::keyPressEvent(QKeyEvent *e)
setTextCursor(cursor);
}

const auto shift = e->modifiers().testFlag(Qt::ShiftModifier);
e->setModifiers(e->modifiers() & ~Qt::ShiftModifier); // to match Shift+Home to MoveToStartOfLine

if (e->matches(QKeySequence::MoveToStartOfLine))
{
auto cursor = textCursor();
const auto line = cursor.block().text();
const auto leadingSpaceCount = QRegularExpression("^\\s*").match(line).capturedLength();
const auto cursorPos = cursor.positionInBlock();
const auto moveMode = shift ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor;
if (cursorPos > leadingSpaceCount)
cursor.movePosition(QTextCursor::PreviousCharacter, moveMode, cursorPos - leadingSpaceCount);
else
cursor.movePosition(QTextCursor::StartOfBlock, moveMode);
setTextCursor(cursor);
return;
}

if (e->matches(QKeySequence::MoveToEndOfLine))
{
auto cursor = textCursor();
cursor.movePosition(QTextCursor::EndOfBlock, shift ? QTextCursor::KeepAnchor : QTextCursor::MoveAnchor);
setTextCursor(cursor);
return;
}

e->setModifiers(e->modifiers() | (shift ? Qt::ShiftModifier : Qt::NoModifier));

QPlainTextEdit::keyPressEvent(e);
}

Expand Down

0 comments on commit 2932d0b

Please sign in to comment.