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
This commit is contained in:
Thorbjørn Lindeijer
2010-07-14 12:37:49 +02:00
parent a76773191d
commit 2c51e0c9da
2 changed files with 30 additions and 3 deletions

View File

@@ -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);