Skip to content

Commit 29e30ee

Browse files
authored
fix: Fix Ctrl+Enter and Ctrl+Shift+Enter (Megaxela#13)
Ctrl+Enter should be the same as End + Enter Ctrl+Shift+Enter should be the same as Up + End + Enter if it's not at the first line. Now they work as expected. 1. Ctrl+Enter ``` <cursor>{ ``` Old: ``` { <cursor> ``` Now: ``` { <tab><cursor> ``` 2. Ctrl+Enter ``` {<cursor>} ``` Old: ``` { <tab><cursor> } ``` Now: ``` {} <cursor> ``` 3. Ctrl+Shift+Enter ``` { <cursor> ``` Old: ``` { <cursor> ``` Now: ``` { <tab><cursor> ```
1 parent f57ca8a commit 29e30ee

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/internal/QCodeEditor.cpp

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,34 @@ void QCodeEditor::keyPressEvent(QKeyEvent *e)
587587

588588
if (!completerSkip)
589589
{
590+
if ((e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter))
591+
{
592+
QKeyEvent pureEnter(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
593+
if (e->modifiers() == Qt::ControlModifier)
594+
{
595+
moveCursor(QTextCursor::EndOfBlock);
596+
keyPressEvent(&pureEnter);
597+
return;
598+
}
599+
else if (e->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier))
600+
{
601+
if (textCursor().blockNumber() == 0)
602+
{
603+
moveCursor(QTextCursor::StartOfBlock);
604+
insertPlainText("\n");
605+
moveCursor(QTextCursor::PreviousBlock);
606+
moveCursor(QTextCursor::EndOfBlock);
607+
}
608+
else
609+
{
610+
moveCursor(QTextCursor::PreviousBlock);
611+
moveCursor(QTextCursor::EndOfBlock);
612+
keyPressEvent(&pureEnter);
613+
}
614+
return;
615+
}
616+
}
617+
590618
if (e->key() == Qt::Key_Tab && e->modifiers() == Qt::NoModifier)
591619
{
592620
if (textCursor().hasSelection())
@@ -622,7 +650,7 @@ void QCodeEditor::keyPressEvent(QKeyEvent *e)
622650
// Have Qt Edior like behaviour, if {|} and enter is pressed indent the two
623651
// parenthesis
624652
if (m_autoIndentation && (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) &&
625-
charUnderCursor() == '}' && charUnderCursor(-1) == '{')
653+
e->modifiers() == Qt::NoModifier && charUnderCursor(-1) == '{' && charUnderCursor() == '}')
626654
{
627655
insertPlainText("\n" + indentationSpaces + (m_replaceTab ? m_tabReplace : "\t") + "\n" + indentationSpaces);
628656

@@ -634,7 +662,7 @@ void QCodeEditor::keyPressEvent(QKeyEvent *e)
634662

635663
// Auto-indent for single "{" without "}"
636664
if (m_autoIndentation && (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) &&
637-
charUnderCursor(-1) == '{')
665+
e->modifiers() == Qt::NoModifier && charUnderCursor(-1) == '{')
638666
{
639667
insertPlainText("\n" + indentationSpaces + (m_replaceTab ? m_tabReplace : "\t"));
640668
return;
@@ -715,34 +743,9 @@ void QCodeEditor::keyPressEvent(QKeyEvent *e)
715743
}
716744
}
717745

718-
if ((e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) && !(e->modifiers() & Qt::AltModifier))
746+
if ((e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) && e->modifiers() == Qt::NoModifier)
719747
{
720-
if (e->modifiers() == Qt::ControlModifier)
721-
{
722-
moveCursor(QTextCursor::EndOfBlock);
723-
}
724-
else if (e->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier))
725-
{
726-
moveCursor(QTextCursor::StartOfBlock);
727-
QString insertText = "\n";
728-
int blockNumber = textCursor().blockNumber();
729-
if (blockNumber > 0)
730-
{
731-
insertText.prepend(QRegularExpression("^\\s*")
732-
.match(document()->findBlockByNumber(blockNumber - 1).text())
733-
.captured());
734-
}
735-
insertPlainText(insertText);
736-
moveCursor(QTextCursor::PreviousBlock);
737-
moveCursor(QTextCursor::EndOfBlock);
738-
return;
739-
}
740-
else
741-
{
742-
indentationSpaces = indentationSpaces.left(textCursor().columnNumber());
743-
}
744-
745-
insertPlainText("\n" + indentationSpaces);
748+
insertPlainText("\n" + indentationSpaces.left(textCursor().columnNumber()));
746749
return;
747750
}
748751

0 commit comments

Comments
 (0)