| 475 | } |
| 476 | |
| 477 | void QCodeEditor::keyPressEvent(QKeyEvent *e) |
| 478 | { |
| 479 | auto completerSkip = proceedCompleterBegin(e); |
| 480 | |
| 481 | if (!completerSkip) |
| 482 | { |
| 483 | const int tabSize = m_tabReplace.size() > 0 ? m_tabReplace.size() : 4; |
| 484 | |
| 485 | // Toggle line comments with Ctrl+/ when a language is set |
| 486 | if (e->key() == Qt::Key_Slash |
| 487 | && e->modifiers().testFlag(Qt::ControlModifier) |
| 488 | && m_languageHint != LanguageHint::Generic) |
| 489 | { |
| 490 | toggleLineComment(); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | if (m_replaceTab && e->key() == Qt::Key_Tab |
| 495 | && e->modifiers() == Qt::NoModifier) |
| 496 | { |
| 497 | // Indent the whole block when the selection spans multiple lines |
| 498 | auto cursor = textCursor(); |
| 499 | if (cursor.hasSelection() |
| 500 | && document()->findBlock(cursor.selectionStart()) |
| 501 | != document()->findBlock(cursor.selectionEnd())) |
| 502 | { |
| 503 | changeBlockIndent(true); |
| 504 | return; |
| 505 | } |
| 506 | |
| 507 | insertPlainText(m_tabReplace); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // Shortcut for moving the selected lines (or current line) to the left |
| 512 | if (m_replaceTab && e->key() == Qt::Key_Backtab) |
| 513 | { |
| 514 | changeBlockIndent(false); |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | // Auto indentation |
| 519 | int indentationLevel = getIndentationSpaces(); |
| 520 | |
| 521 | int tabCounts = indentationLevel / tabSize; |
| 522 | |
| 523 | // Have Qt Editor like behaviour, if {|} and enter is pressed indent the |
| 524 | // two parenthesis |
| 525 | if (m_autoIndentation |
| 526 | && (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) |
| 527 | && charUnderCursor() == '}' && charUnderCursor(-1) == '{') |
| 528 | { |
| 529 | int charsBack = 0; |
| 530 | insertPlainText("\n"); |
| 531 | |
| 532 | if (m_replaceTab) |
| 533 | insertPlainText(QString(indentationLevel + tabSize, ' ')); |
| 534 | else |