-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwingcompleter.cpp
More file actions
87 lines (71 loc) · 2.78 KB
/
wingcompleter.cpp
File metadata and controls
87 lines (71 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/****************************************************************************
**
** Copyright (C) 2025-2028 WingSummer
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** You should have received a copy of the GNU General Public License version 3
** along with this program. If not, see <https://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "wingcompleter.h"
#include "wingcodeedit.h"
#include "wingcodepopup.h"
#include <QScrollBar>
WingCompleter::WingCompleter(WingCodeEdit *editor)
: QCompleter(editor), m_triggerAmount(3) {
setCompletionMode(QCompleter::PopupCompletion);
setCaseSensitivity(Qt::CaseInsensitive);
setCompletionRole(Qt::DisplayRole);
setFilterMode(Qt::MatchStartsWith);
if (editor) {
editor->setCompleter(this);
}
m_popUp = new WingCodePopup(editor);
setPopup(m_popUp);
}
WingCompleter::~WingCompleter() { m_popUp->deleteLater(); }
QStringList WingCompleter::triggerList() const { return m_triggerList; }
void WingCompleter::setTriggerList(const QStringList &triggers) {
m_triggerList = triggers;
}
void WingCompleter::trigger(const QString &trigger, const QString &content,
const QRect &cursorRect) {
if (_enabled && processTrigger(trigger, content)) {
auto cr = cursorRect;
cr.setWidth(m_popUp->sizeHintForColumn(0) +
m_popUp->verticalScrollBar()->sizeHint().width());
auto idx = completionModel()->index(0, 0);
m_popUp->setCurrentIndex(idx);
complete(cr); // popup it up!
m_popUp->showTooltip(idx);
}
}
bool WingCompleter::processTrigger(const QString &trigger,
const QString &content) {
if (!_enabled) {
return false;
}
Q_UNUSED(trigger);
auto seps = wordSeperators();
auto r =
std::find_if(content.crbegin(), content.crend(),
[seps](const QChar &ch) { return seps.contains(ch); });
auto idx = std::distance(r, content.crend());
setCompletionPrefix(content.sliced(idx));
return true;
}
bool WingCompleter::enabled() const { return _enabled; }
void WingCompleter::setEnabled(bool newEnabled) { _enabled = newEnabled; }
QString WingCompleter::wordSeperators() const {
static QString eow(QStringLiteral("~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-="));
return eow;
}
qint32 WingCompleter::triggerAmount() const { return m_triggerAmount; }
void WingCompleter::setTriggerAmount(qint32 newTriggerAmount) {
m_triggerAmount = newTriggerAmount;
}