-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSqlEditor.cpp
More file actions
132 lines (112 loc) · 4.15 KB
/
Copy pathSqlEditor.cpp
File metadata and controls
132 lines (112 loc) · 4.15 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "SqlEditor.h"
#include <QAbstractItemView>
#include <QKeyEvent>
#include <QScrollBar>
#include <QStringListModel>
#include <QTextCursor>
namespace {
// A practical subset of SQL keywords - enough to be useful for the kind
// of SELECT/JOIN/WHERE queries this app generates and lets users tweak.
const QStringList kSqlKeywords = {
"SELECT", "FROM", "WHERE", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER",
"ON", "AND", "OR", "NOT", "IN", "LIKE", "IS", "NULL", "AS", "DISTINCT",
"GROUP", "BY", "ORDER", "HAVING", "LIMIT", "OFFSET", "ASC", "DESC",
"INSERT", "INTO", "VALUES", "UPDATE", "SET", "DELETE", "CREATE",
"TABLE", "COUNT", "SUM", "AVG", "MIN", "MAX", "UNION", "ALL", "EXISTS",
"BETWEEN", "CASE", "WHEN", "THEN", "ELSE", "END"
};
// Characters that end an identifier - the popup closes once one is typed.
const QString kWordBoundary = "~!@#$%^&*()_+{}|:\"<>?,./;'[]\\-= \t\n";
} // namespace
SqlEditor::SqlEditor(QWidget* parent)
: QPlainTextEdit(parent)
, m_keywords(kSqlKeywords)
{
setTabChangesFocus(false);
m_completer = new QCompleter(this);
m_completer->setCaseSensitivity(Qt::CaseInsensitive);
m_completer->setCompletionMode(QCompleter::PopupCompletion);
m_completer->setWidget(this);
connect(m_completer, QOverload<const QString&>::of(&QCompleter::activated),
this, &SqlEditor::insertCompletion);
updateCompleterModel();
}
void SqlEditor::setSchemaWords(const QStringList& words)
{
m_schemaWords = words;
m_schemaWords.removeDuplicates();
updateCompleterModel();
}
void SqlEditor::updateCompleterModel()
{
QStringList all = m_keywords + m_schemaWords;
all.removeDuplicates();
m_completer->setModel(new QStringListModel(all, m_completer));
}
QString SqlEditor::textUnderCursor() const
{
QTextCursor tc = textCursor();
tc.select(QTextCursor::WordUnderCursor);
return tc.selectedText();
}
void SqlEditor::insertCompletion(const QString& completion)
{
if (m_completer->widget() != this) {
return;
}
QTextCursor tc = textCursor();
const int extra = completion.length() - m_completer->completionPrefix().length();
tc.movePosition(QTextCursor::Left);
tc.movePosition(QTextCursor::EndOfWord);
tc.insertText(completion.right(extra));
setTextCursor(tc);
}
void SqlEditor::focusInEvent(QFocusEvent* event)
{
m_completer->setWidget(this);
QPlainTextEdit::focusInEvent(event);
}
void SqlEditor::keyPressEvent(QKeyEvent* event)
{
if (m_completer->popup()->isVisible()) {
// Let these keys drive the popup instead of the editor.
switch (event->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
event->ignore();
return;
default:
break;
}
}
// Ctrl+Space always forces the popup open, even with an empty prefix.
const bool isShortcut = event->modifiers().testFlag(Qt::ControlModifier)
&& event->key() == Qt::Key_Space;
if (!isShortcut) {
QPlainTextEdit::keyPressEvent(event);
}
const bool ctrlOrShift = event->modifiers().testFlag(Qt::ControlModifier)
|| event->modifiers().testFlag(Qt::ShiftModifier);
if (ctrlOrShift && event->text().isEmpty() && !isShortcut) {
return;
}
const bool hasOtherModifier = event->modifiers() != Qt::NoModifier && !ctrlOrShift;
const QString prefix = textUnderCursor();
if (!isShortcut
&& (hasOtherModifier || event->text().isEmpty() || prefix.length() < 2
|| (!event->text().isEmpty() && kWordBoundary.contains(event->text().right(1))))) {
m_completer->popup()->hide();
return;
}
if (prefix != m_completer->completionPrefix()) {
m_completer->setCompletionPrefix(prefix);
m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
}
QRect popupRect = cursorRect();
popupRect.setWidth(m_completer->popup()->sizeHintForColumn(0)
+ m_completer->popup()->verticalScrollBar()->sizeHint().width());
m_completer->complete(popupRect);
}