Avoid inserting another closing character when completing includes

When a closing character is already there, it shouldn't be appended.
Done by generalizing the same code for automatically inserted brackets
after C++ symbols.
This commit is contained in:
Thorbjørn Lindeijer
2009-07-24 13:48:21 +02:00
parent b49d715a1c
commit 5271117e5b

View File

@@ -1209,7 +1209,6 @@ bool CppCodeCompletion::completeInclude(const QTextCursor &cursor)
// Make completion for all relevant includes // Make completion for all relevant includes
if (ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::instance()->currentProject()) { if (ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::instance()->currentProject()) {
QStringList items;
QStringList includePaths = m_manager->projectInfo(project).includePaths; QStringList includePaths = m_manager->projectInfo(project).includePaths;
const QString currentFilePath = QFileInfo(m_editor->file()->fileName()).path(); const QString currentFilePath = QFileInfo(m_editor->file()->fileName()).path();
if (!includePaths.contains(currentFilePath)) if (!includePaths.contains(currentFilePath))
@@ -1221,22 +1220,17 @@ bool CppCodeCompletion::completeInclude(const QTextCursor &cursor)
realPath += QLatin1Char('/'); realPath += QLatin1Char('/');
realPath += directoryPrefix; realPath += directoryPrefix;
} }
items.append(m_manager->includesInPath(realPath)); foreach (const QString &itemText, m_manager->includesInPath(realPath)) {
}
if (!items.isEmpty()) {
foreach (const QString &itemText, items) {
TextEditor::CompletionItem item(this); TextEditor::CompletionItem item(this);
item.m_text += itemText; item.m_text += itemText;
// TODO: Icon for include files // TODO: Icon for include files
item.m_icon = m_icons.keywordIcon(); item.m_icon = m_icons.keywordIcon();
m_completions.append(item); m_completions.append(item);
} }
return true;
} }
} }
return false; return !m_completions.isEmpty();
} }
void CppCodeCompletion::completeNamespace(const QList<Symbol *> &candidates, void CppCodeCompletion::completeNamespace(const QList<Symbol *> &candidates,
@@ -1443,15 +1437,16 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
symbol = item.m_data.value<Symbol *>(); symbol = item.m_data.value<Symbol *>();
QString toInsert; QString toInsert;
QString extraChars;
int extraLength = 0; int extraLength = 0;
if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) { if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) {
toInsert = item.m_text; toInsert = item.m_text;
toInsert += QLatin1Char(')'); extraChars += QLatin1Char(')');
} 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.m_text; toInsert = item.m_text;
if (!toInsert.endsWith(QLatin1Char('/'))) if (!toInsert.endsWith(QLatin1Char('/')))
toInsert += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"'); extraChars += QLatin1Char((m_completionOperator == T_ANGLE_STRING_LITERAL) ? '>' : '"');
} else { } else {
toInsert = item.m_text; toInsert = item.m_text;
@@ -1459,8 +1454,6 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
//<< overview.prettyType(symbol->type()); //<< overview.prettyType(symbol->type());
if (m_autoInsertBrackets && symbol && symbol->type()) { if (m_autoInsertBrackets && symbol && symbol->type()) {
QString extraChars;
if (Function *function = symbol->type()->asFunctionType()) { if (Function *function = symbol->type()->asFunctionType()) {
// If the member is a function, automatically place the opening parenthesis, // If the member is a function, automatically place the opening parenthesis,
// except when it might take template parameters. // except when it might take template parameters.
@@ -1487,6 +1480,8 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
} }
} }
} }
}
}
// 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) {
@@ -1499,9 +1494,6 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
} }
toInsert += extraChars; toInsert += extraChars;
}
}
// Insert the remainder of the name // Insert the remainder of the name
int length = m_editor->position() - m_startPosition + extraLength; int length = m_editor->position() - m_startPosition + extraLength;