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(widgetToKeepVisible: m_interactiveSedReplaceLabel);
35 m_interactiveSedReplaceLabel->show();
36 updateInteractiveSedReplaceLabelText();
37
38 updateMatchHighlight(matchRange: interactiveSedReplace->currentMatch());
39 moveCursorTo(cursorPos: 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(matchRange: m_interactiveSedReplacer->currentMatch());
55 updateInteractiveSedReplaceLabelText();
56 moveCursorTo(cursorPos: m_interactiveSedReplacer->currentMatch().start());
57
58 if (!m_interactiveSedReplacer->currentMatch().isValid()) {
59 moveCursorTo(cursorPos: 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(wasAborted: false);
98 closeWithStatusMessage(exitStatusMessage: m_interactiveSedReplacer->finalStatusReportMessage());
99 m_interactiveSedReplacer.reset();
100}
101

source code of ktexteditor/src/vimode/emulatedcommandbar/interactivesedreplacemode.cpp