From b8b37cb0b5760481fdc8cc5126eb7ba84e4541c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Wed, 16 Sep 2009 16:48:14 +0200 Subject: [PATCH] Fixed the completion to take into account auto parentheses insertion Also skip semicolons in when auto parentheses insertion is enabled. Done with mae. --- src/plugins/cpptools/cppcodecompletion.cpp | 22 ++++++++++++++++++---- src/plugins/texteditor/basetexteditor.cpp | 7 ++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp index 224e265f8af..718c2f02ed0 100644 --- a/src/plugins/cpptools/cppcodecompletion.cpp +++ b/src/plugins/cpptools/cppcodecompletion.cpp @@ -1468,6 +1468,11 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item) QString toInsert; QString extraChars; int extraLength = 0; + int cursorOffset = 0; + + bool autoParenthesesEnabled = false; + if (TextEditor::BaseTextEditor *edit = qobject_cast(m_editor->widget())) + autoParenthesesEnabled = edit->tabSettings().m_autoParentheses; if (m_completionOperator == T_SIGNAL || m_completionOperator == T_SLOT) { toInsert = item.m_text; @@ -1497,14 +1502,21 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item) } else if (! function->isAmbiguous()) { extraChars += QLatin1Char('('); + // If the function doesn't return anything, automatically place the semicolon, + // unless we're doing a scope completion (then it might be function definition). + bool endWithSemicolon = function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON; + // If the function takes no arguments, automatically place the closing parenthesis if (item.m_duplicateCount == 0 && ! function->hasArguments()) { extraChars += QLatin1Char(')'); - - // If the function doesn't return anything, automatically place the semicolon, - // unless we're doing a scope completion (then it might be function definition). - if (function->returnType()->isVoidType() && m_completionOperator != T_COLON_COLON) { + if (endWithSemicolon) extraChars += QLatin1Char(';'); + } else if (autoParenthesesEnabled) { + extraChars += QLatin1Char(')'); + --cursorOffset; + if (endWithSemicolon) { + extraChars += QLatin1Char(';'); + --cursorOffset; } } } @@ -1528,6 +1540,8 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item) int length = m_editor->position() - m_startPosition + extraLength; m_editor->setCurPos(m_startPosition); m_editor->replace(length, toInsert); + if (cursorOffset) + m_editor->setCurPos(m_editor->position() + cursorOffset); } bool CppCodeCompletion::partiallyComplete(const QList &completionItems) diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index 06603180181..bde4bb569d0 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -1091,9 +1091,10 @@ void BaseTextEditor::keyPressEvent(QKeyEvent *e) bool skip = false; QChar first = text.at(0); - if (first == QLatin1Char(')')) { - skip = (first == lookAhead); - } else if (first == QLatin1Char(']')) { + if (first == QLatin1Char(')') + || first == QLatin1Char(']') + || first == QLatin1Char(';') + ) { skip = (first == lookAhead); } else if (first == QLatin1Char('\"') || first == QLatin1Char('\'')) { if (first == lookAhead) {