Commit 2c51e0c9 authored by Thorbjørn Lindeijer's avatar Thorbjørn Lindeijer
Browse files

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
parent a7677319
...@@ -438,6 +438,7 @@ CppCodeCompletion::CppCodeCompletion(CppModelManager *manager) ...@@ -438,6 +438,7 @@ CppCodeCompletion::CppCodeCompletion(CppModelManager *manager)
m_manager(manager), m_manager(manager),
m_editor(0), m_editor(0),
m_startPosition(-1), m_startPosition(-1),
m_shouldRestartCompletion(false),
m_forcedCompletion(false), m_forcedCompletion(false),
m_completionOperator(T_EOF_SYMBOL), m_completionOperator(T_EOF_SYMBOL),
m_objcEnabled(true) m_objcEnabled(true)
...@@ -632,8 +633,13 @@ TextEditor::ITextEditable *CppCodeCompletion::editor() const ...@@ -632,8 +633,13 @@ TextEditor::ITextEditable *CppCodeCompletion::editor() const
int CppCodeCompletion::startPosition() const int CppCodeCompletion::startPosition() const
{ return m_startPosition; } { return m_startPosition; }
bool CppCodeCompletion::shouldRestartCompletion()
{ return m_shouldRestartCompletion; }
bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor) bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
{ {
m_editor = editor;
const int pos = editor->position(); const int pos = editor->position();
unsigned token = T_EOF_SYMBOL; unsigned token = T_EOF_SYMBOL;
...@@ -649,6 +655,14 @@ bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor) ...@@ -649,6 +655,14 @@ bool CppCodeCompletion::triggersCompletion(TextEditor::ITextEditable *editor)
} }
return true; 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; return false;
...@@ -1751,6 +1765,8 @@ bool CppCodeCompletion::typedCharCompletes(const TextEditor::CompletionItem &ite ...@@ -1751,6 +1765,8 @@ bool CppCodeCompletion::typedCharCompletes(const TextEditor::CompletionItem &ite
void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar typedChar) void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar typedChar)
{ {
m_shouldRestartCompletion = false; // Enabled for specific cases
Symbol *symbol = 0; Symbol *symbol = 0;
if (item.data.isValid()) { if (item.data.isValid()) {
...@@ -1780,10 +1796,13 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar t ...@@ -1780,10 +1796,13 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar t
typedChar = QChar(); typedChar = QChar();
} else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL) { } else if (m_completionOperator == T_STRING_LITERAL || m_completionOperator == T_ANGLE_STRING_LITERAL) {
toInsert = item.text; toInsert = item.text;
if (!toInsert.endsWith(QLatin1Char('/'))) if (!toInsert.endsWith(QLatin1Char('/'))) {
extraChars += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"'); extraChars += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"');
else if (typedChar == QLatin1Char('/')) // Eat the slash } else {
typedChar = QChar(); m_shouldRestartCompletion = true; // Re-trigger for subdirectory
if (typedChar == QLatin1Char('/')) // Eat the slash
typedChar = QChar();
}
} else { } else {
toInsert = item.text; toInsert = item.text;
...@@ -1867,6 +1886,12 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar t ...@@ -1867,6 +1886,12 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item, QChar t
--cursorOffset; --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 // Avoid inserting characters that are already there
for (int i = 0; i < extraChars.length(); ++i) { for (int i = 0; i < extraChars.length(); ++i) {
const QChar a = extraChars.at(i); const QChar a = extraChars.at(i);
......
...@@ -72,6 +72,7 @@ public: ...@@ -72,6 +72,7 @@ public:
TextEditor::ITextEditable *editor() const; TextEditor::ITextEditable *editor() const;
int startPosition() const; int startPosition() const;
bool shouldRestartCompletion();
QList<TextEditor::CompletionItem> getCompletions(); QList<TextEditor::CompletionItem> getCompletions();
bool supportsEditor(TextEditor::ITextEditable *editor); bool supportsEditor(TextEditor::ITextEditable *editor);
bool triggersCompletion(TextEditor::ITextEditable *editor); bool triggersCompletion(TextEditor::ITextEditable *editor);
...@@ -144,6 +145,7 @@ private: ...@@ -144,6 +145,7 @@ private:
CppModelManager *m_manager; CppModelManager *m_manager;
TextEditor::ITextEditable *m_editor; TextEditor::ITextEditable *m_editor;
int m_startPosition; // Position of the cursor from which completion started int m_startPosition; // Position of the cursor from which completion started
bool m_shouldRestartCompletion;
bool m_forcedCompletion; bool m_forcedCompletion;
unsigned m_completionOperator; unsigned m_completionOperator;
......
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