[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [kdev-php] completion: CodeCompletion ImplementationItem: extend to support member variables
From:       Niko Sams <niko.sams () gmail ! com>
Date:       2013-11-20 19:56:13
Message-ID: E1VjDsj-0005XM-9W () scm ! kde ! org
[Download RAW message or body]

Git commit acbc72c1552eaf5e8a4001572e7754c3730b4963 by Niko Sams.
Committed on 20/11/2013 at 19:53.
Pushed by nsams into branch 'master'.

CodeCompletion ImplementationItem: extend to support member variables

Example:
class A {
    protected $x = 1;
}
class B extends A {
}
this change will allow overriding $x in B by adding "protected $x = "

M  +56   -56   completion/context.cpp
M  +57   -45   completion/implementationitem.cpp
M  +2    -1    completion/implementationitem.h

http://commits.kde.org/kdev-php/acbc72c1552eaf5e8a4001572e7754c3730b4963

diff --git a/completion/context.cpp b/completion/context.cpp
index 435428a..fa3694e 100644
--- a/completion/context.cpp
+++ b/completion/context.cpp
@@ -1254,64 +1254,64 @@ QList<CompletionTreeItemPointer> \
                CodeCompletionContext::completionItems(bool& ab
                     //TODO: always add __construct, __destruct and maby other magic \
                functions
                     // get all visible declarations and add inherited to the \
                completion items
                     foreach(const DeclarationDepthPair& decl, \
                ctx->allDeclarations(ctx->range().end, m_duContext->topContext(), \
                false)) {
-                        if (decl.first->isFunctionDeclaration()) {
-                            ClassFunctionDeclaration *method = \
                dynamic_cast<ClassFunctionDeclaration*>(decl.first);
-                            if (method) {
-                                if (decl.second == 0) {
-                                    // this function is already implemented
-                                    alreadyImplemented << \
                decl.first->indexedIdentifier().getIndex();
-                                    continue;
-                                }
-                                // skip already implemented functions
-                                if \
                (alreadyImplemented.contains(decl.first->indexedIdentifier().getIndex())) \
                {
-                                    continue;
-                                }
-                                // skip non-static functions when requested
-                                if (filterNonStatic && !method->isStatic()) {
-                                    continue;
-                                }
-                                // skip static functions when requested
-                                if (filterStatic && method->isStatic()) {
-                                    continue;
-                                }
-                                // always skip private functions
-                                if (method->accessPolicy() == Declaration::Private) \
                {
-                                    continue;
-                                }
-                                // skip public functions when requested
-                                if (filterPublic && method->accessPolicy() == \
                Declaration::Public) {
-                                    // make sure no non-public base methods are \
                added
-                                    alreadyImplemented << \
                decl.first->indexedIdentifier().getIndex();
-                                    continue;
-                                }
-                                // skip final methods
-                                if (method->isFinal()) {
-                                    // make sure no non-final base methods are added
-                                    alreadyImplemented << \
                decl.first->indexedIdentifier().getIndex();
-                                    continue;
-                                }
-                                // make sure we inherit or implement the base class \
                of this method
-                                if (!method->context() || \
                !method->context()->owner()) {
-                                    kDebug() << "invalid parent context/owner:" << \
                method->toString();
-                                    continue;
-                                }
-                                if \
(!currentClass->isPublicBaseClass(dynamic_cast<ClassDeclaration*>(method->context()->owner()),
                
-                                                                        \
                m_duContext->topContext())) {
-                                    continue;
-                                }
-
-                                ImplementationItem::HelperType itype;
-                                if (method->isAbstract()) {
-                                    itype = ImplementationItem::Implement;
-                                } else {
-                                    itype = ImplementationItem::Override;
-                                }
-
-                                items << CompletionTreeItemPointer(new \
                ImplementationItem(itype, DeclarationPointer(decl.first),
-                                                                    \
                KDevelop::CodeCompletionContext::Ptr(this), decl.second));
-                                // don't add identical items twice to the completion \
choices +                        ClassMemberDeclaration *member = \
dynamic_cast<ClassMemberDeclaration*>(decl.first); +                        if \
(member) { +                            if (decl.second == 0) {
+                                // this function is already implemented
                                 alreadyImplemented << \
decl.first->indexedIdentifier().getIndex(); +                                \
continue;  }
+                            // skip already implemented functions
+                            if \
(alreadyImplemented.contains(decl.first->indexedIdentifier().getIndex())) { +         \
continue; +                            }
+                            // skip non-static functions when requested
+                            if (filterNonStatic && !member->isStatic()) {
+                                continue;
+                            }
+                            // skip static functions when requested
+                            if (filterStatic && member->isStatic()) {
+                                continue;
+                            }
+                            // always skip private functions
+                            if (member->accessPolicy() == Declaration::Private) {
+                                continue;
+                            }
+                            // skip public functions when requested
+                            if (filterPublic && member->accessPolicy() == \
Declaration::Public) { +                                // make sure no non-public \
base members are added +                                alreadyImplemented << \
decl.first->indexedIdentifier().getIndex(); +                                \
continue; +                            }
+                            // skip final members
+                            if (member->isFinal()) {
+                                // make sure no non-final base members are added
+                                alreadyImplemented << \
decl.first->indexedIdentifier().getIndex(); +                                \
continue; +                            }
+                            // make sure we inherit or implement the base class of \
this member +                            if (!member->context() || \
!member->context()->owner()) { +                                kDebug() << "invalid \
parent context/owner:" << member->toString(); +                                \
continue; +                            }
+                            if \
(!currentClass->isPublicBaseClass(dynamic_cast<ClassDeclaration*>(member->context()->owner()),
 +                                                                    \
m_duContext->topContext())) { +                                continue;
+                            }
+
+                            ImplementationItem::HelperType itype;
+                            if (!member->isFunctionDeclaration()) {
+                                itype = ImplementationItem::OverrideVar;
+                            } else if (member->isAbstract()) {
+                                itype = ImplementationItem::Implement;
+                            } else {
+                                itype = ImplementationItem::Override;
+                            }
+
+                            items << CompletionTreeItemPointer(new \
ImplementationItem(itype, DeclarationPointer(decl.first), +                           \
KDevelop::CodeCompletionContext::Ptr(this), decl.second)); +                          \
// don't add identical items twice to the completion choices +                        \
alreadyImplemented << decl.first->indexedIdentifier().getIndex();  }
                     }
                 }
diff --git a/completion/implementationitem.cpp b/completion/implementationitem.cpp
index cf6838c..d67ea54 100644
--- a/completion/implementationitem.cpp
+++ b/completion/implementationitem.cpp
@@ -55,6 +55,7 @@ QVariant ImplementationItem::data(const QModelIndex& index, int \
role, const Code  if (index.column() == KTextEditor::CodeCompletionModel::Icon) {
             switch (m_type) {
             case Override:
+            case OverrideVar:
                 RETURN_CACHED_ICON("CTparents");
             case Implement:
                 RETURN_CACHED_ICON("CTsuppliers");
@@ -66,6 +67,7 @@ QVariant ImplementationItem::data(const QModelIndex& index, int \
role, const Code  QString prefix;
             switch (m_type) {
             case Override:
+            case OverrideVar:
                 prefix = i18n("Override");
                 break;
             case Implement:
@@ -175,24 +177,28 @@ void ImplementationItem::execute(KTextEditor::Document* \
document, const KTextEdi  bool isConstructorOrDestructor = false;
         bool isInterface = false;
 
-        if (ClassMethodDeclaration* method = \
dynamic_cast<ClassMethodDeclaration*>(m_declaration.data())) { +        if \
(ClassMemberDeclaration* member = \
                dynamic_cast<ClassMemberDeclaration*>(m_declaration.data())) {
             // NOTE: it should _never_ be private - but that's the completionmodel / \
                context / worker's job
             if (!modifiers.contains("public") && !modifiers.contains("protected")) {
-                if (method->accessPolicy() == Declaration::Protected) {
+                if (member->accessPolicy() == Declaration::Protected) {
                     replText += "protected ";
                 } else {
                     replText += "public ";
                 }
             }
-            if (!modifiers.contains("static") && method->isStatic()) {
+            if (!modifiers.contains("static") && member->isStatic()) {
                 replText += "static ";
             }
-            functionName = method->prettyName().str();
+            functionName = member->identifier().toString();
 
-            isConstructorOrDestructor = method->isConstructor() || \
method->isDestructor(); +            ClassMethodDeclaration* method = \
dynamic_cast<ClassMethodDeclaration*>(m_declaration.data()); +            if (method) \
{ +                functionName = method->prettyName().str();
+                isConstructorOrDestructor = method->isConstructor() || \
method->isDestructor(); +            }
 
-            if (method->context() && method->context()->owner()) {
-                ClassDeclaration* classDec = \
dynamic_cast<ClassDeclaration*>(method->context()->owner()); +            if \
(member->context() && member->context()->owner()) { +                \
ClassDeclaration* classDec = \
dynamic_cast<ClassDeclaration*>(member->context()->owner());  if (classDec) {
                     isInterface = (classDec->classType() == \
ClassDeclarationData::Interface);  }
@@ -202,52 +208,58 @@ void ImplementationItem::execute(KTextEditor::Document* \
document, const KTextEdi  functionName = m_declaration->identifier().toString();
         }
 
-        if (!modifiers.contains("function")) {
-            replText += "function ";
-        }
+        if (m_type == ImplementationItem::OverrideVar) {
+            replText += "$" + functionName + " = ";
+        } else {
+            if (!modifiers.contains("function")) {
+                replText += "function ";
+            }
 
-        replText += functionName;
+            replText += functionName;
+
+            {
+                // get argument list
+                QString arguments;
+                createArgumentList(*this, arguments, 0, true);
+                replText += arguments;
+            }
 
-        {
-            // get argument list
             QString arguments;
-            createArgumentList(*this, arguments, 0, true);
-            replText += arguments;
-        }
+            QVector<Declaration*> parameters;
+            if (DUChainUtils::getArgumentContext(m_declaration.data()))
+                parameters = \
DUChainUtils::getArgumentContext(m_declaration.data())->localDeclarations(); +        \
arguments = '('; +            bool first = true;
+            foreach(Declaration* dec, parameters) {
+                if (first)
+                    first = false;
+                else
+                    arguments += ", ";
+
+                arguments += '$' + dec->identifier().toString();
+            }
+            arguments += ')';
 
-        QString arguments;
-        QVector<Declaration*> parameters;
-        if (DUChainUtils::getArgumentContext(m_declaration.data()))
-            parameters = \
                DUChainUtils::getArgumentContext(m_declaration.data())->localDeclarations();
                
-        arguments = '(';
-        bool first = true;
-        foreach(Declaration* dec, parameters) {
-            if (first)
-                first = false;
-            else
-                arguments += ", ";
-
-            arguments += '$' + dec->identifier().toString();
-        }
-        arguments += ')';
+            bool voidReturnType = false;
+            if (FunctionType::Ptr::dynamicCast(m_declaration->abstractType())) {
+                AbstractType::Ptr retType = \
FunctionType::Ptr::staticCast(m_declaration->abstractType())->returnType(); +         \
if (retType->equals(new IntegralType(IntegralType::TypeVoid))) { +                    \
voidReturnType = true; +                }
+            }
 
-        bool voidReturnType = false;
-        if (FunctionType::Ptr::dynamicCast(m_declaration->abstractType())) {
-            AbstractType::Ptr retType = \
                FunctionType::Ptr::staticCast(m_declaration->abstractType())->returnType();
                
-            if (retType->equals(new IntegralType(IntegralType::TypeVoid))) {
-                voidReturnType = true;
+            replText += QString("\n%1{\n%1    ").arg(indendation);
+            if (isInterface || m_type == ImplementationItem::Implement) {
+            } else if (!isConstructorOrDestructor && !voidReturnType) {
+                replText += QString("$ret = parent::%2%3;\n%1    return \
$ret;").arg(indendation).arg(functionName).arg(arguments); +            } else {
+                replText += \
QString("parent::%1%2;").arg(functionName).arg(arguments);  }
-        }
+            replText += QString("\n%1}\n%1")
+                    .arg(indendation);
 
-        replText += QString("\n%1{\n%1    ").arg(indendation);
-        if (isInterface || m_type == ImplementationItem::Implement) {
-        } else if (!isConstructorOrDestructor && !voidReturnType) {
-            replText += QString("$ret = parent::%2%3;\n%1    return \
                $ret;").arg(indendation).arg(functionName).arg(arguments);
-        } else {
-            replText += QString("parent::%1%2;").arg(functionName).arg(arguments);
         }
-        replText += QString("\n%1}\n%1")
-                .arg(indendation);
+
 
         //TODO: properly place the cursor inside the {} part
         document->replaceText(replaceRange, replText);
diff --git a/completion/implementationitem.h b/completion/implementationitem.h
index 6763cdb..6f173fc 100644
--- a/completion/implementationitem.h
+++ b/completion/implementationitem.h
@@ -34,7 +34,8 @@ class ImplementationItem : public NormalDeclarationCompletionItem
 public:
     enum HelperType {
         Override,
-        Implement
+        Implement,
+        OverrideVar
     };
 
     explicit ImplementationItem(HelperType type, KDevelop::DeclarationPointer decl = \
KDevelop::DeclarationPointer(), KSharedPtr<KDevelop::CodeCompletionContext> context = \
KSharedPtr<KDevelop::CodeCompletionContext>(), int _inheritanceDepth = 0)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic