KTextEditor

interactivesedreplacemode.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2016 Simon St James <kdedevel@etotheipiplusone.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "interactivesedreplacemode.h"
8
9#include <QKeyEvent>
10#include <QLabel>
11
12using namespace KateVi;
13
14InteractiveSedReplaceMode::InteractiveSedReplaceMode(EmulatedCommandBar *emulatedCommandBar,
15 MatchHighlighter *matchHighlighter,
16 InputModeManager *viInputModeManager,
17 KTextEditor::ViewPrivate *view)
18 : ActiveMode(emulatedCommandBar, matchHighlighter, viInputModeManager, view)
19 , m_isActive(false)
20{
21 m_interactiveSedReplaceLabel = new QLabel();
22 m_interactiveSedReplaceLabel->setObjectName(QStringLiteral("interactivesedreplace"));
23}
24
25void InteractiveSedReplaceMode::activate(std::shared_ptr<SedReplace::InteractiveSedReplacer> interactiveSedReplace)
26{
27 Q_ASSERT_X(interactiveSedReplace->currentMatch().isValid(),
28 "startInteractiveSearchAndReplace",
29 "KateCommands shouldn't initiate an interactive sed replace with no initial match");
30
31 m_isActive = true;
32 m_interactiveSedReplacer = interactiveSedReplace;
33
34 hideAllWidgetsExcept(m_interactiveSedReplaceLabel);
35 m_interactiveSedReplaceLabel->show();
36 updateInteractiveSedReplaceLabelText();
37
38 updateMatchHighlight(interactiveSedReplace->currentMatch());
39 moveCursorTo(interactiveSedReplace->currentMatch().start());
40}
41
42bool InteractiveSedReplaceMode::handleKeyPress(const QKeyEvent *keyEvent)
43{
44 // TODO - it would be better to use e.g. keyEvent->key() == Qt::Key_Y instead of keyEvent->text() == "y",
45 // but this would require some slightly dicey changes to the "feed key press" code in order to make it work
46 // with mappings and macros.
47 if (keyEvent->text() == QLatin1String("y") || keyEvent->text() == QLatin1String("n")) {
48 const KTextEditor::Cursor cursorPosIfFinalMatch = m_interactiveSedReplacer->currentMatch().start();
49 if (keyEvent->text() == QLatin1String("y")) {
50 m_interactiveSedReplacer->replaceCurrentMatch();
51 } else {
52 m_interactiveSedReplacer->skipCurrentMatch();
53 }
54 updateMatchHighlight(m_interactiveSedReplacer->currentMatch());
55 updateInteractiveSedReplaceLabelText();
56 moveCursorTo(m_interactiveSedReplacer->currentMatch().start());
57
58 if (!m_interactiveSedReplacer->currentMatch().isValid()) {
59 moveCursorTo(cursorPosIfFinalMatch);
60 finishInteractiveSedReplace();
61 }
62 return true;
63 } else if (keyEvent->text() == QLatin1String("l")) {
64 m_interactiveSedReplacer->replaceCurrentMatch();
65 finishInteractiveSedReplace();
66 return true;
67 } else if (keyEvent->text() == QLatin1String("q")) {
68 finishInteractiveSedReplace();
69 return true;
70 } else if (keyEvent->text() == QLatin1String("a")) {
71 m_interactiveSedReplacer->replaceAllRemaining();
72 finishInteractiveSedReplace();
73 return true;
74 }
75 return false;
76}
77
78void InteractiveSedReplaceMode::deactivate(bool wasAborted)
79{
80 Q_UNUSED(wasAborted);
81 m_isActive = false;
82 m_interactiveSedReplaceLabel->hide();
83}
84
85QWidget *InteractiveSedReplaceMode::label()
86{
87 return m_interactiveSedReplaceLabel;
88}
89
90void InteractiveSedReplaceMode::updateInteractiveSedReplaceLabelText()
91{
92 m_interactiveSedReplaceLabel->setText(m_interactiveSedReplacer->currentMatchReplacementConfirmationMessage() + QLatin1String(" (y/n/a/q/l)"));
93}
94
95void InteractiveSedReplaceMode::finishInteractiveSedReplace()
96{
97 deactivate(false);
98 closeWithStatusMessage(m_interactiveSedReplacer->finalStatusReportMessage());
99 m_interactiveSedReplacer.reset();
100}
The Cursor represents a position in a Document.
Definition cursor.h:75
A KateViewBarWidget that attempts to emulate some of the features of Vim's own command bar,...
void setText(const QString &)
void keyEvent(KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier, int delay)
void hide()
void show()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.