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 | |
14 | namespace KTextEditor |
15 | { |
16 | class ViewPrivate; |
17 | } |
18 | |
19 | class QLineEdit; |
20 | class QCompleter; |
21 | class QStringListModel; |
22 | class QKeyEvent; |
23 | |
24 | namespace KateVi |
25 | { |
26 | class ActiveMode; |
27 | class EmulatedCommandBar; |
28 | |
29 | struct 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 | |
54 | class Completer |
55 | { |
56 | public: |
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 | |
67 | private: |
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 |