Skip to content

Commit d3593c4

Browse files
author
Bhathiya Perera
committed
Merge branch 'master' of github.com:JaDogg/elseba
2 parents 6c6c142 + 5135ddd commit d3593c4

10 files changed

Lines changed: 578 additions & 144 deletions

CodeEditor/codeeditor.cpp

Lines changed: 86 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#include <QTextStream>
4646
#include "CodeEditor/codeeditor.h"
4747

48-
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) {
48+
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent), c(0) {
4949
lineNumberArea = new LineNumberArea(this);
5050

5151
connect(this, SIGNAL(blockCountChanged(int)), this,
@@ -60,6 +60,50 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) {
6060
p.setColor(QPalette::Text, Qt::white);
6161
this->setPalette(p);
6262
}
63+
64+
void CodeEditor::setCompleter(QCompleter *completer) {
65+
if (c)
66+
QObject::disconnect(c, 0, this, 0);
67+
68+
c = completer;
69+
70+
if (!c)
71+
return;
72+
73+
c->setWidget(this);
74+
// c->setCompletionMode(QCompleter::PopupCompletion);
75+
// c->setCaseSensitivity(Qt::CaseInsensitive);
76+
QObject::connect(c, SIGNAL(activated(QString)), this,
77+
SLOT(insertCompletion(QString)));
78+
}
79+
80+
QCompleter *CodeEditor::completer() const { return c; }
81+
82+
void CodeEditor::insertCompletion(const QString &completion) {
83+
if (c->widget() != this)
84+
return;
85+
QTextCursor tc = textCursor();
86+
int extra = c->completionPrefix().length();
87+
tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, extra);
88+
tc.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
89+
tc.insertText(completion);
90+
tc.movePosition(QTextCursor::Right);
91+
setTextCursor(tc);
92+
}
93+
94+
QString CodeEditor::textUnderCursor() const {
95+
96+
QTextCursor tc = textCursor();
97+
tc.select(QTextCursor::WordUnderCursor);
98+
return tc.selectedText();
99+
}
100+
101+
void CodeEditor::focusInEvent(QFocusEvent *e) {
102+
if (c)
103+
c->setWidget(this);
104+
QPlainTextEdit::focusInEvent(e);
105+
}
106+
63107
int CodeEditor::lineNumberAreaWidth() {
64108
int digits = 1;
65109
int max = qMax(1, blockCount());
@@ -149,50 +193,50 @@ bool CodeEditor::KeepIndent() {
149193
return true;
150194
}
151195
void CodeEditor::keyPressEvent(QKeyEvent *e) {
152-
switch (e->key()) {
153-
case Qt::Key_Backtab:
154-
SelectLineMarginBlock();
155-
{
156-
QString text("");
157-
QStringList lines = this->textCursor().selection().toPlainText().split(
158-
QRegExp("\n|\r\n|\r"));
159-
foreach (QString line, lines) {
160-
line.replace(QRegExp("^( | | | )(.*)"), "\\2");
161-
text.append(line);
162-
text.append("\n");
163-
}
164-
text.truncate(text.length() - 1);
165-
this->textCursor().insertText(text);
166-
}
167-
break;
168-
case Qt::Key_Tab:
169-
if (this->textCursor().hasSelection()) {
170-
SelectLineMarginBlock();
171-
{
172-
QString text("");
173-
QStringList lines = this->textCursor().selection().toPlainText().split(
174-
QRegExp("\n|\r\n|\r"));
175-
foreach (QString line, lines) {
176-
text.append(" ");
177-
text.append(line);
178-
text.append("\n");
179-
}
180-
text.truncate(text.length() - 1);
181-
this->textCursor().insertText(text);
182-
}
183-
} else {
184-
this->insertPlainText(" ");
185-
}
186-
break;
187-
case Qt::Key_Enter:
188-
case Qt::Key_Return:
189-
if (!KeepIndent()) {
190-
QPlainTextEdit::keyPressEvent(e);
196+
if (c && c->popup()->isVisible()) {
197+
// The following keys are forwarded by the completer to the widget
198+
switch (e->key()) {
199+
case Qt::Key_Enter:
200+
case Qt::Key_Return:
201+
case Qt::Key_Escape:
202+
case Qt::Key_Tab:
203+
case Qt::Key_Backtab:
204+
e->ignore();
205+
return; // let the completer do default behavior
206+
default:
207+
break;
191208
}
192-
break;
193-
default:
209+
}
210+
211+
bool isShortcut = ((e->modifiers() & Qt::ControlModifier) &&
212+
e->key() == Qt::Key_Space); // CTRL+Space
213+
if (!c || !isShortcut)
194214
QPlainTextEdit::keyPressEvent(e);
215+
216+
const bool ctrlOrShift =
217+
e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
218+
219+
if (!c || (ctrlOrShift && e->text().isEmpty())) {
220+
return;
221+
}
222+
223+
bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
224+
QString completionPrefix = textUnderCursor();
225+
226+
if (!isShortcut &&
227+
(hasModifier || e->text().isEmpty() || completionPrefix.length() < 2)) {
228+
c->popup()->hide();
229+
return;
230+
}
231+
232+
if (completionPrefix != c->completionPrefix()) {
233+
c->setCompletionPrefix(completionPrefix);
234+
c->popup()->setCurrentIndex(c->completionModel()->index(0, 0));
195235
}
236+
QRect cr = cursorRect();
237+
cr.setWidth(c->popup()->sizeHintForColumn(0) +
238+
c->popup()->verticalScrollBar()->sizeHint().width());
239+
c->complete(cr);
196240
}
197241

198242
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) {

CodeEditor/codeeditor.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#include <QPlainTextEdit>
4545
#include <QObject>
46+
#include <QCompleter>
4647

4748
QT_BEGIN_NAMESPACE
4849
class QPaintEvent;
@@ -60,20 +61,26 @@ class CodeEditor : public QPlainTextEdit {
6061
CodeEditor(QWidget *parent = 0);
6162
void lineNumberAreaPaintEvent(QPaintEvent *event);
6263
int lineNumberAreaWidth();
64+
void setCompleter(QCompleter *c);
65+
QCompleter *completer() const;
6366

6467
protected:
6568
void resizeEvent(QResizeEvent *event);
69+
void keyPressEvent(QKeyEvent *e);
70+
void focusInEvent(QFocusEvent *e);
6671

6772
private slots:
6873
void updateLineNumberAreaWidth(int newBlockCount);
6974
void updateLineNumberArea(const QRect &, int);
75+
void insertCompletion(const QString &completion);
7076

7177
private:
7278
QWidget *lineNumberArea;
73-
void keyPressEvent(QKeyEvent *e);
74-
void SelectLineMarginBlock();
79+
QCompleter *c;
7580
QString GetLine();
81+
QString textUnderCursor() const;
7682
bool KeepIndent();
83+
void SelectLineMarginBlock();
7784
};
7885

7986
class LineNumberArea : public QWidget {

CodeEditor/pythonsyntaxhighlighter.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,68 @@ PythonSyntaxHighlighter::PythonSyntaxHighlighter(QTextDocument *parent)
187187
<< "sorted"
188188
<< "intern";
189189

190+
exceptions = QStringList() << "BaseException"
191+
<< "SystemExit"
192+
<< "KeyboardInterrupt"
193+
<< "GeneratorExit"
194+
<< "Exception"
195+
<< "StopIteration"
196+
<< "ArithmeticError"
197+
<< "FloatingPointError"
198+
<< "OverflowError"
199+
<< "ZeroDivisionError"
200+
<< "AssertionError"
201+
<< "AttributeError"
202+
<< "BufferError"
203+
<< "EOFError"
204+
<< "ImportError"
205+
<< "LookupError"
206+
<< "IndexError"
207+
<< "KeyError"
208+
<< "MemoryError"
209+
<< "NameError"
210+
<< "UnboundLocalError"
211+
<< "OSError"
212+
<< "BlockingIOError"
213+
<< "ChildProcessError"
214+
<< "ConnectionError"
215+
<< "BrokenPipeError"
216+
<< "ConnectionAbortedError"
217+
<< "ConnectionRefusedError"
218+
<< "ConnectionResetError"
219+
<< "FileExistsError"
220+
<< "FileNotFoundError"
221+
<< "InterruptedError"
222+
<< "IsADirectoryError"
223+
<< "NotADirectoryError"
224+
<< "PermissionError"
225+
<< "ProcessLookupError"
226+
<< "TimeoutError"
227+
<< "ReferenceError"
228+
<< "RuntimeError"
229+
<< "NotImplementedError"
230+
<< "SyntaxError"
231+
<< "IndentationError"
232+
<< "TabError"
233+
<< "SystemError"
234+
<< "TypeError"
235+
<< "ValueError"
236+
<< "UnicodeError"
237+
<< "UnicodeDecodeError"
238+
<< "UnicodeEncodeError"
239+
<< "UnicodeTranslateError"
240+
<< "Warning"
241+
<< "DeprecationWarning"
242+
<< "PendingDeprecationWarning"
243+
<< "RuntimeWarning"
244+
<< "SyntaxWarning"
245+
<< "UserWarning"
246+
<< "FutureWarning"
247+
<< "ImportWarning"
248+
<< "UnicodeWarning"
249+
<< "BytesWarning"
250+
<< "ResourceWarning";
251+
190252
setStyles();
191253
mSearchRegex = tr("");
192254
mSearchHighlight = getTextCharFormat("black", "bold", "yellow");
@@ -208,6 +270,7 @@ void PythonSyntaxHighlighter::setStyles() {
208270
basicStyles.insert("numbers", getTextCharFormat("cyan"));
209271
basicStyles.insert("bugs", getTextCharFormat("yellow", "bold", "red"));
210272
basicStyles.insert("hackish", getTextCharFormat("royalblue", "bold"));
273+
basicStyles.insert("except", getTextCharFormat("royalblue", "underline"));
211274
basicStyles.insert("private", getTextCharFormat("white", "italic"));
212275
basicStyles.insert("bytes", getTextCharFormat("lightsteelblue"));
213276
}
@@ -226,6 +289,11 @@ void PythonSyntaxHighlighter::initializeRules() {
226289
basicStyles.value("brace")));
227290
}
228291

292+
foreach (QString currExcept, exceptions) {
293+
rules.append(HighlightingRule(QString("\\b%1\\b").arg(currExcept), 0,
294+
basicStyles.value("except")));
295+
}
296+
229297
rules.append(
230298
HighlightingRule("\\b__[\\w_]+__\\b", 0, basicStyles.value("hackish")));
231299
rules.append(

CodeEditor/pythonsyntaxhighlighter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class PythonSyntaxHighlighter : public QSyntaxHighlighter {
7474
QStringList operators;
7575
QStringList braces;
7676
QStringList builtins;
77+
QStringList exceptions;
7778
QString mSearchRegex;
7879
QTextCharFormat mSearchHighlight;
7980
QHash<QString, QTextCharFormat> basicStyles;

0 commit comments

Comments
 (0)