diff --git a/src/libs/utils/completingtextedit.cpp b/src/libs/utils/completingtextedit.cpp index b870995668f..6d7476e80f1 100644 --- a/src/libs/utils/completingtextedit.cpp +++ b/src/libs/utils/completingtextedit.cpp @@ -59,7 +59,10 @@ public: void insertCompletion(const QString &completion); QString textUnderCursor() const; + bool acceptsCompletionPrefix(const QString &prefix) const; + QCompleter *m_completer; + int m_completionLengthThreshold; private: CompletingTextEdit *m_backPointer; @@ -67,6 +70,7 @@ private: CompletingTextEditPrivate::CompletingTextEditPrivate(CompletingTextEdit *textEdit) : m_completer(0), + m_completionLengthThreshold(3), m_backPointer(textEdit) { } @@ -90,6 +94,11 @@ QString CompletingTextEditPrivate::textUnderCursor() const return tc.selectedText(); } +bool CompletingTextEditPrivate::acceptsCompletionPrefix(const QString &prefix) const +{ + return prefix.length() >= m_completionLengthThreshold; +} + CompletingTextEdit::CompletingTextEdit(QWidget *parent) : QTextEdit(parent), d(new CompletingTextEditPrivate(this)) @@ -121,6 +130,16 @@ QCompleter *CompletingTextEdit::completer() const return d->m_completer; } +int CompletingTextEdit::completionLengthThreshold() const +{ + return d->m_completionLengthThreshold; +} + +void CompletingTextEdit::setCompletionLengthThreshold(int len) +{ + d->m_completionLengthThreshold = len; +} + void CompletingTextEdit::keyPressEvent(QKeyEvent *e) { if (completer() && completer()->popup()->isVisible()) { @@ -150,8 +169,8 @@ void CompletingTextEdit::keyPressEvent(QKeyEvent *e) const QString newCompletionPrefix = d->textUnderCursor(); const QChar lastChar = e->text().isEmpty() ? QChar() : e->text().right(1).at(0); - if (!isShortcut && (hasModifier || e->text().isEmpty() || newCompletionPrefix.length() < 3 - || isEndOfWordChar(lastChar))) { + if (!isShortcut && (hasModifier || e->text().isEmpty() || isEndOfWordChar(lastChar) + || !d->acceptsCompletionPrefix(newCompletionPrefix))) { completer()->popup()->hide(); return; } diff --git a/src/libs/utils/completingtextedit.h b/src/libs/utils/completingtextedit.h index 9c69150f357..dbe4259af56 100644 --- a/src/libs/utils/completingtextedit.h +++ b/src/libs/utils/completingtextedit.h @@ -46,6 +46,8 @@ namespace Utils { class QTCREATOR_UTILS_EXPORT CompletingTextEdit : public QTextEdit { Q_OBJECT + Q_PROPERTY(int completionLengthThreshold + READ completionLengthThreshold WRITE setCompletionLengthThreshold) public: CompletingTextEdit(QWidget *parent = 0); @@ -54,6 +56,9 @@ public: void setCompleter(QCompleter *c); QCompleter *completer() const; + int completionLengthThreshold() const; + void setCompletionLengthThreshold(int len); + protected: void keyPressEvent(QKeyEvent *e); void focusInEvent(QFocusEvent *e);