From 2c51e0c9da9074a0ee7e9ce4666a777f7a03affd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= <thorbjorn.lindeijer@nokia.com> Date: Wed, 14 Jul 2010 12:37:49 +0200 Subject: [PATCH] Automatically trigger completion for C++ editor after three characters Similar behaviour as for the QML editor. However, for now without removing the completion box when you have typed the whole word, since it could still be useful for automatically inserting other characters. Task-number: QTCREATORBUG-67 --- src/plugins/cpptools/cppcodecompletion.cpp | 31 +++++++++++++++++++--- src/plugins/cpptools/cppcodecompletion.h | 2 ++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp index 3f90f644c05..d3e46ea06c5 100644 --- a/src/plugins/cpptools/cppcodecompletion.cpp +++ b/src/plugins/cpptools/cppcodecompletion.cpp @@ -438,6 +438,7 @@ CppCodeCompletion::CppCodeCompletion(CppModelManager *manager) m_manager(manager), m_editor(0), m_startPosition(-1), + m_shouldRestartCompletion(false), m_forcedCompletion(false), m_completionOperator(T_EOF_SYMBOL), m_objcEnabled(true) @@ -632,8 +633,13 @@ TextEditor::ITextEditable *CppCodeCompletion::editor() const int CppCodeCompletion::startPosition() const { return m_startPosition; } +bool CppCodeCompletion::shouldRestartCompletion() +{ return m_shouldRestartCompletion; } + bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor) { + m_editor = editor; + const int pos = editor->position(); unsigned token = T_EOF_SYMBOL; @@ -649,6 +655,14 @@ bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor) } return true; + } else { + // Trigger completion after at least three characters of a name have been typed + const int startOfName = findStartOfName(pos); + if (pos - startOfName > 2) { + const QChar firstCharacter = editor->characterAt(startOfName); + if (firstCharacter.isLetter() || firstCharacter == QLatin1Char('_')) + return true; + } } return false; @@ -1751,6 +1765,8 @@ bool CppCodeCompletion::typedCharCompletes(const TextEditor::CompletionItem &ite void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar typedChar) { + m_shouldRestartCompletion = false; // Enabled for specific cases + Symbol *symbol = 0; if (item.data.isValid()) { @@ -1780,10 +1796,13 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar t typedChar = QChar(); } else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL) { toInsert = item.text; - if (!toInsert.endsWith(QLatin1Char('/'))) + if (!toInsert.endsWith(QLatin1Char('/'))) { extraChars += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"'); - else if (typedChar == QLatin1Char('/')) // Eat the slash - typedChar = QChar(); + } else { + m_shouldRestartCompletion = true; // Re-trigger for subdirectory + if (typedChar == QLatin1Char('/')) // Eat the slash + typedChar = QChar(); + } } else { toInsert = item.text; @@ -1867,6 +1886,12 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar t --cursorOffset; } + if (!extraChars.isEmpty() && extraChars.length() + cursorOffset > 0) { + const QChar c = extraChars.at(extraChars.length() - 1 + cursorOffset); + if (c == QLatin1Char('.') || c == QLatin1Char('(')) + m_shouldRestartCompletion = true; + } + // Avoid inserting characters that are already there for (int i = 0; i < extraChars.length(); ++i) { const QChar a = extraChars.at(i); diff --git a/src/plugins/cpptools/cppcodecompletion.h b/src/plugins/cpptools/cppcodecompletion.h index 406d4a2f872..15ac30ab4d6 100644 --- a/src/plugins/cpptools/cppcodecompletion.h +++ b/src/plugins/cpptools/cppcodecompletion.h @@ -72,6 +72,7 @@ public: TextEditor::ITextEditable *editor() const; int startPosition() const; + bool shouldRestartCompletion(); QList<TextEditor::CompletionItem> getCompletions(); bool supportsEditor(TextEditor::ITextEditable *editor); bool triggersCompletion(TextEditor::ITextEditable *editor); @@ -144,6 +145,7 @@ private: CppModelManager *m_manager; TextEditor::ITextEditable *m_editor; int m_startPosition; // Position of the cursor from which completion started + bool m_shouldRestartCompletion; bool m_forcedCompletion; unsigned m_completionOperator; -- GitLab