From ff4e559c4689bb88d1323e0d097b7be3582c9323 Mon Sep 17 00:00:00 2001
From: Christian Kamm <christian.d.kamm@nokia.com>
Date: Mon, 2 Nov 2009 10:31:44 +0100
Subject: [PATCH] Improve completion for function declarations.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Instead of just completing
void A::foo(|) -> void A::foo(int i|), we now complete
void A::foo(|) -> void A::foo(int i) const|
where | represents the place of the cursor.

Reviewed-by: Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>
---
 src/plugins/cpptools/cppcodecompletion.cpp | 36 ++++++++++++++--------
 1 file changed, 24 insertions(+), 12 deletions(-)

diff --git a/src/plugins/cpptools/cppcodecompletion.cpp b/src/plugins/cpptools/cppcodecompletion.cpp
index 1d77d7c8679..30fb1ce2ed9 100644
--- a/src/plugins/cpptools/cppcodecompletion.cpp
+++ b/src/plugins/cpptools/cppcodecompletion.cpp
@@ -212,12 +212,21 @@ protected:
     { _item = newCompletionItem(name->unqualifiedNameId()); }
 };
 
+struct CompleteFunctionDeclaration
+{
+    explicit CompleteFunctionDeclaration(Function *f = 0)
+        : function(f)
+    {}
+
+    Function *function;
+};
 
 } // namespace Internal
 } // namespace CppTools
 
 using namespace CppTools::Internal;
 
+Q_DECLARE_METATYPE(CompleteFunctionDeclaration)
 
 void FakeToolTipFrame::paintEvent(QPaintEvent *)
 {
@@ -1071,18 +1080,13 @@ bool CppCodeCompletion::completeConstructorOrFunction(const QList<TypeOfExpressi
                     Overview overview;
                     overview.setShowArgumentNames(true);
 
-                    // get rid of parentheses and cv-qualifiers
-                    QString completion = overview(f->type());
-                    if (f->isVolatile() || f->isConst())
-                        completion = completion.mid(1, completion.lastIndexOf(')') - 1);
-                    else
-                        completion = completion.mid(1, completion.size() - 2);
-
-                    if (completion.size()) {
-                        TextEditor::CompletionItem item(this);
-                        item.text = completion;
-                        m_completions.append(item);
-                    }
+                    // gets: "parameter list) cv-spec",
+                    QString completion = overview(f->type()).mid(1);
+
+                    TextEditor::CompletionItem item(this);
+                    item.text = completion;
+                    item.data = QVariant::fromValue(CompleteFunctionDeclaration(f));
+                    m_completions.append(item);
                 }
                 return true;
             }
@@ -1560,6 +1564,14 @@ void CppCodeCompletion::complete(const TextEditor::CompletionItem &item)
                 }
             }
         }
+
+        if (m_autoInsertBrackets && item.data.canConvert<CompleteFunctionDeclaration>()) {
+            // everything from the closing parenthesis on are extra chars, to
+            // make sure an auto-inserted ")" gets replaced by ") const" if necessary
+            int closingParen = toInsert.lastIndexOf(QLatin1Char(')'));
+            extraChars = toInsert.mid(closingParen);
+            toInsert.truncate(closingParen);
+        }
     }
 
     // Avoid inserting characters that are already there
-- 
GitLab