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#ifndef KATEVI_EMULATED_COMMAND_BAR_COMPLETER_H
8#define KATEVI_EMULATED_COMMAND_BAR_COMPLETER_H
9
10#include <functional>
11
12#include <QStringList>
13
14namespace KTextEditor
15{
16class ViewPrivate;
17}
18
19class QLineEdit;
20class QCompleter;
21class QStringListModel;
22class QKeyEvent;
23
24namespace KateVi
25{
26class ActiveMode;
27class EmulatedCommandBar;
28
29struct CompletionStartParams {
30 static CompletionStartParams createModeSpecific(const QStringList &completions,
31 int wordStartPos,
32 std::function<QString(const QString &)> completionTransform = std::function<QString(const QString &)>())
33 {
34 CompletionStartParams completionStartParams;
35 completionStartParams.completionType = ModeSpecific;
36 completionStartParams.completions = completions;
37 completionStartParams.wordStartPos = wordStartPos;
38 completionStartParams.completionTransform = completionTransform;
39 return completionStartParams;
40 }
41 static CompletionStartParams invalid()
42 {
43 CompletionStartParams completionStartParams;
44 completionStartParams.completionType = None;
45 return completionStartParams;
46 }
47 enum CompletionType { None, ModeSpecific, WordFromDocument };
48 CompletionType completionType = None;
49 int wordStartPos = -1;
50 QStringList completions;
51 std::function<QString(const QString &)> completionTransform;
52};
53
54class Completer
55{
56public:
57 enum class CompletionInvocation { ExtraContext, NormalContext };
58 Completer(EmulatedCommandBar *emulatedCommandBar, KTextEditor::ViewPrivate *view, QLineEdit *edit);
59 void startCompletion(const CompletionStartParams &completionStartParams);
60 void deactivateCompletion();
61 bool isCompletionActive() const;
62 bool isNextTextChangeDueToCompletionChange() const;
63 bool completerHandledKeypress(const QKeyEvent *keyEvent);
64 void editTextChanged(const QString &newText);
65 void setCurrentMode(ActiveMode *currentMode);
66
67private:
68 QLineEdit *m_edit;
69 KTextEditor::ViewPrivate *m_view;
70 ActiveMode *m_currentMode = nullptr;
71
72 void setCompletionIndex(int index);
73 void currentCompletionChanged();
74 void updateCompletionPrefix();
75 CompletionStartParams activateWordFromDocumentCompletion();
76 QString wordBeforeCursor();
77 int wordBeforeCursorBegin();
78 void abortCompletionAndResetToPreCompletion();
79
80 QCompleter *m_completer;
81 QStringListModel *m_completionModel;
82 QString m_textToRevertToIfCompletionAborted;
83 int m_cursorPosToRevertToIfCompletionAborted = 0;
84 bool m_isNextTextChangeDueToCompletionChange = false;
85 CompletionStartParams m_currentCompletionStartParams;
86 CompletionStartParams::CompletionType m_currentCompletionType = CompletionStartParams::None;
87};
88}
89#endif
90

source code of ktexteditor/src/vimode/emulatedcommandbar/completer.h