KTextEditor

completer.h
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;
22class QKeyEvent;
23
24namespace KateVi
25{
26class ActiveMode;
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 {
48 None,
49 ModeSpecific,
50 WordFromDocument
51 };
52 CompletionType completionType = None;
53 int wordStartPos = -1;
54 QStringList completions;
55 std::function<QString(const QString &)> completionTransform;
56};
57
58class Completer
59{
60public:
61 enum class CompletionInvocation {
62 ExtraContext,
63 NormalContext
64 };
65 Completer(EmulatedCommandBar *emulatedCommandBar, KTextEditor::ViewPrivate *view, QLineEdit *edit);
66 void startCompletion(const CompletionStartParams &completionStartParams);
67 void deactivateCompletion();
68 bool isCompletionActive() const;
69 bool isNextTextChangeDueToCompletionChange() const;
70 bool completerHandledKeypress(const QKeyEvent *keyEvent);
71 void editTextChanged(const QString &newText);
72 void setCurrentMode(ActiveMode *currentMode);
73
74private:
75 QLineEdit *m_edit;
76 KTextEditor::ViewPrivate *m_view;
77 ActiveMode *m_currentMode = nullptr;
78
79 void setCompletionIndex(int index);
80 void currentCompletionChanged();
81 void updateCompletionPrefix();
82 CompletionStartParams activateWordFromDocumentCompletion();
83 QString wordBeforeCursor();
84 int wordBeforeCursorBegin();
85 void abortCompletionAndResetToPreCompletion();
86
87 QCompleter *m_completer;
88 QStringListModel *m_completionModel;
89 QString m_textToRevertToIfCompletionAborted;
90 int m_cursorPosToRevertToIfCompletionAborted = 0;
91 bool m_isNextTextChangeDueToCompletionChange = false;
92 CompletionStartParams m_currentCompletionStartParams;
93 CompletionStartParams::CompletionType m_currentCompletionType = CompletionStartParams::None;
94};
95}
96#endif
A KateViewBarWidget that attempts to emulate some of the features of Vim's own command bar,...
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Jan 24 2025 11:55:25 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.