From 4780996c468f6f9194c0876012077974f027d5aa Mon Sep 17 00:00:00 2001 From: David Schulz Date: Mon, 26 Sep 2022 14:46:48 +0200 Subject: [PATCH] Core: Go to pattern start when pressing home in locator If the cursor is already at that position move the cursor to the start of the line edit. This mimics the editor behavior that home moves the cursor to the relevant position for that line, and if the cursor is at that position move to the actual start of the line. Change-Id: Ieedbc0de0f93b0ffa3f3b64ccbcb04471b004e8e Reviewed-by: Eike Ziller --- .../coreplugin/locator/locatorwidget.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/plugins/coreplugin/locator/locatorwidget.cpp b/src/plugins/coreplugin/locator/locatorwidget.cpp index 85051bc7c2b..71c0d5f90ce 100644 --- a/src/plugins/coreplugin/locator/locatorwidget.cpp +++ b/src/plugins/coreplugin/locator/locatorwidget.cpp @@ -718,6 +718,39 @@ bool LocatorWidget::eventFilter(QObject *obj, QEvent *event) QToolTip::hideText(); auto keyEvent = static_cast(event); + + if (keyEvent->matches(QKeySequence::MoveToStartOfBlock) + || keyEvent->matches(QKeySequence::SelectStartOfBlock) + || keyEvent->matches(QKeySequence::MoveToStartOfLine) + || keyEvent->matches(QKeySequence::SelectStartOfLine)) { + const int filterEndIndex = currentText().indexOf(' '); + if (filterEndIndex > 0 && filterEndIndex < currentText().length() - 1) { + const bool startsWithShortcutString + = Utils::anyOf(Locator::filters(), + [shortcutString = currentText().left(filterEndIndex)]( + const ILocatorFilter *filter) { + return filter->isEnabled() && !filter->isHidden() + && filter->shortcutString() == shortcutString; + }); + if (startsWithShortcutString) { + const int cursorPosition = m_fileLineEdit->cursorPosition(); + const int patternStart = filterEndIndex + 1; + const bool mark = keyEvent->matches(QKeySequence::SelectStartOfBlock) + || keyEvent->matches(QKeySequence::SelectStartOfLine); + if (cursorPosition == patternStart) { + m_fileLineEdit->home(mark); + } else { + const int diff = m_fileLineEdit->cursorPosition() - patternStart; + if (diff < 0) + m_fileLineEdit->cursorForward(mark, qAbs(diff)); + else + m_fileLineEdit->cursorBackward(mark, diff); + } + return true; + } + } + } + switch (keyEvent->key()) { case Qt::Key_PageUp: case Qt::Key_PageDown: