Commit a0faf279 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Delay the automatic completion.

parent ecdee327
......@@ -658,7 +658,7 @@ bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
QChar characterUnderCursor = editor->characterAt(pos);
if (!characterUnderCursor.isLetterOrNumber()) {
const int startOfName = findStartOfName(pos);
if (pos - startOfName == 3) {
if (pos - startOfName >= 3) {
const QChar firstCharacter = editor->characterAt(startOfName);
if (firstCharacter.isLetter() || firstCharacter == QLatin1Char('_')) {
// Finally check that we're not inside a comment or string (code copied from startOfOperator)
......@@ -1725,6 +1725,11 @@ void CppCodeCompletion::completions(QList<TextEditor::CompletionItem> *completio
}
}
}
if (completions->size() == 1) {
if (key == completions->first().text)
completions->clear();
}
}
QList<TextEditor::CompletionItem> CppCodeCompletion::removeDuplicates(const QList<TextEditor::CompletionItem> &items)
......
......@@ -61,6 +61,7 @@
#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtCore/QTimeLine>
#include <QtCore/QTime>
#include <QtGui/QAbstractTextDocumentLayout>
#include <QtGui/QApplication>
#include <QtGui/QKeyEvent>
......@@ -240,6 +241,11 @@ BaseTextEditor::BaseTextEditor(QWidget *parent)
d->m_highlightBlocksTimer->setSingleShot(true);
connect(d->m_highlightBlocksTimer, SIGNAL(timeout()), this, SLOT(_q_highlightBlocks()));
d->m_requestAutoCompletionTimer = new QTimer(this);
d->m_requestAutoCompletionTimer->setSingleShot(true);
d->m_requestAutoCompletionTimer->setInterval(500);
connect(d->m_requestAutoCompletionTimer, SIGNAL(timeout()), this, SLOT(_q_requestAutoCompletion()));
d->m_animator = 0;
d->m_searchResultFormat.setBackground(QColor(0xffef0b));
......@@ -1380,18 +1386,38 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e)
setTextCursor(cursor);
}
skip_event:
skip_event:
if (!ro && e->key() == Qt::Key_Delete && d->m_parenthesesMatchingEnabled)
d->m_parenthesesMatchingTimer->start(50);
if (!ro && d->m_contentsChanged && !e->text().isEmpty() && e->text().at(0).isPrint())
emit requestAutoCompletion(editableInterface(), false);
if (!ro && d->m_contentsChanged && !e->text().isEmpty() && e->text().at(0).isPrint()) {
maybeRequestAutoCompletion(e->text().at(0));
}
if (e != original_e)
delete e;
}
void BaseTextEditor::maybeRequestAutoCompletion(const QChar &ch)
{
if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
d->m_requestAutoCompletionRevision = document()->revision();
d->m_requestAutoCompletionTimer->start();
} else {
d->m_requestAutoCompletionTimer->stop();
emit requestAutoCompletion(editableInterface(), false);
}
}
void BaseTextEditor::_q_requestAutoCompletion()
{
d->m_requestAutoCompletionTimer->stop();
if (d->m_requestAutoCompletionRevision == document()->revision())
emit requestAutoCompletion(editableInterface(), false);
}
void BaseTextEditor::insertCodeSnippet(const QTextCursor &cursor_arg, const QString &snippet)
{
if ((snippet.count('$') % 2) != 0) {
......@@ -1908,6 +1934,8 @@ BaseTextEditorPrivate::BaseTextEditorPrivate()
m_moveLineUndoHack(false),
m_findScopeVerticalBlockSelection(0),
m_highlightBlocksTimer(0),
m_requestAutoCompletionRevision(0),
m_requestAutoCompletionTimer(0),
m_cursorBlockNumber(-1),
m_inKeyPressEvent(false)
{
......
......@@ -496,6 +496,7 @@ signals:
void requestQuickFix(TextEditor::ITextEditable *editor);
private:
void maybeRequestAutoCompletion(const QChar &ch);
void indentOrUnindent(bool doIndent);
void handleHomeKey(bool anchor);
void handleBackspaceKey();
......@@ -527,13 +528,17 @@ private:
void universalHelper(); // test function for development
// parentheses matcher
private slots:
// auto completion
void _q_requestAutoCompletion();
// parentheses matcher
void _q_matchParentheses();
void _q_highlightBlocks();
void slotSelectionChanged();
void _q_animateUpdate(int position, QPointF lastPos, QRectF rect);
void doFoo();
};
......
......@@ -263,6 +263,9 @@ public:
BaseTextEditorPrivateHighlightBlocks m_highlightBlocksInfo;
QTimer *m_highlightBlocksTimer;
int m_requestAutoCompletionRevision;
QTimer *m_requestAutoCompletionTimer;
QPointer<BaseTextEditorAnimator> m_animator;
int m_cursorBlockNumber;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment