Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [pdt-dev] Extending PDT: type inference questions

hi,

if you only want to content assist for ClassRegistry::init(B)-> you could follow http://wiki.eclipse.org/Extending_PDT_2.2#Code_assist_strategies then call context via
getStatementText() and parse this text manually in class XYZCompletionStrategy(extends ClassMembersStrategy),instead of call context.getLhsTypes().

for example you could use the following code in XYZCompletionStrategy#apply:


    public void apply(ICompletionReporter reporter) throws BadLocationException {
        ICompletionContext context = getContext();
        if (!(context instanceof ClassMemberContext)) {
            return;
        }

        ClassMemberContext concreteContext = (ClassMemberContext) context;
        CompletionRequestor requestor = concreteContext
                .getCompletionRequestor();

        String prefix = concreteContext.getPrefix();
        boolean isParentCall = isParentCall(concreteContext);
        String suffix = getSuffix(concreteContext);

        SourceRange replaceRange = null;
        if (suffix.equals("")) {
            replaceRange = getReplacementRange(concreteContext);
        } else {
            replaceRange = getReplacementRangeWithBraces(concreteContext);
        }

        PHPVersion phpVersion = concreteContext.getPhpVersion();
        Set<String> magicMethods = new HashSet<String>();
        magicMethods.addAll(Arrays.asList(PHPMagicMethods
                .getMethods(phpVersion)));

        boolean exactName = requestor.isContextInformationMode();
        List<IMethod> result = new LinkedList<IMethod>();
        ISourceModuleContext sourceModuleContext = new FileContext(
                concreteContext.getSourceModule(),
                SourceParserUtil.getModuleDeclaration(concreteContext
                        .getSourceModule()));
        String textSequence = concreteContext.getStatementText().toString();
        if (textSequence.indexOf("ClassRegistry::init(") >= 0) {
            textSequence = textSequence.substring(textSequence
                    .indexOf("ClassRegistry::init(")
                    + "ClassRegistry::init(".length());
            textSequence = textSequence.substring(0, textSequence
                    .lastIndexOf(")->"));
            String className = textSequence.substring(1,
                    textSequence.length() - 1);
            IType[] types = PHPTypeInferenceUtils.getModelElements(
                    new PHPClassType(className), sourceModuleContext,
                    concreteContext.getOffset());
            for (IType type : types) {
                try {
                    ITypeHierarchy hierarchy = getCompanion()
                            .getSuperTypeHierarchy(type, null);

                    IMethod[] methods = isParentCall ? PHPModelUtils
                            .getSuperTypeHierarchyMethod(type, hierarchy,
                                    prefix, exactName, null) : PHPModelUtils
                            .getTypeHierarchyMethod(type, hierarchy, prefix,
                                    exactName, null);

                    boolean inConstructor = isInConstructor(type, type
                            .getMethods(), concreteContext);
                    for (IMethod method : removeOverriddenElements(Arrays
                            .asList(methods))) {

                        if ((!isConstructor(method) || inConstructor
                                && isSuperConstructor(method, type,
                                        concreteContext))
                                && !isFiltered(method, type, concreteContext)) {
                            if (magicMethods.contains(method.getElementName())) {
                                reporter.reportMethod(method, suffix,
                                        replaceRange,
                                        ProposalExtraInfo.MAGIC_METHOD);
                            } else {
                                result.add(method);
                            }
                        }
                    }
                } catch (CoreException e) {
                    PHPCorePlugin.log(e);
                }
            }
            for (IMethod method : result) {
                reporter.reportMethod((IMethod) method, suffix, replaceRange);
            }
        }

    }

of course the above code is just an example,you need make some changes


to make ctrl+click and hover on method bbb(ClassRegistry::init('B')->bbb()) work,you still need class XYZGoalEvaluatorFactory.

Hope this works for you:)

On Fri, Dec 31, 2010 at 10:41 PM, Kooper <victor.kupriyanov@xxxxxxxxx> wrote:
Hello,

It is really simple - here it is:

<?php

class ClassRegistry {
       public static function init($classname) {
               $a = new $classname();

               if ($a) {
                       return $a;
               }

               return null;
       }
}

class A {
       public function aaa() { echo "A"; }
}

class B {
       public function bbb() { echo "B"; }
}

ClassRegistry::init(B)->


I'm not using quotes around class name in method call currently (it is
because of quotes are included in value of classname in GoalEvaluator
- just didn't handle this yet)

--
Victor Kupriyanov

On Thu, Dec 30, 2010 at 4:00 PM, Roy Ganor <roy@xxxxxxxx> wrote:
> Hi Victor,
>
> Can you provide a code snippet of your ClassRegistry class? Including the "init" static method.
>
> Thanks,
> Roy
> -----Original Message-----
> From: pdt-dev-bounces@xxxxxxxxxxx [mailto:pdt-dev-bounces@xxxxxxxxxxx] On Behalf Of Kooper
> Sent: Wednesday, December 29, 2010 12:55 PM
> To: pdt-dev@xxxxxxxxxxx
> Subject: [pdt-dev] Extending PDT: type inference questions
>
> Hello,
>
> I'm trying to write a custom extension to PDT 2.2 that adds code assistance for
> object instantiation for particular framework. I'm following the guidelines at
> http://wiki.eclipse.org/Extending_PDT_2.2#Type_inference_hinting and have a
> working code for exactly the case from the article:
>
> $myObject = ClassRegistry::init('MyClass');
> $myObject->... // A list of methods of MyClass is shown ok
>
> However it seems like the same type inference approach does not work when
> not using variable assignment:
>
> ClassRegistry::init('MyClass')->... // No proposals available
>
> Tracing goals evaluation factory for the latter case I found that this
> code snippet does not bring up evaluation of any ExpressionTypeGoal goals for
> context of the call, but starts with evaluation of MethodElementReturnTypeGoal
> that later leads to evaluation of method 'ClassRegistry::init's code.
>
> Attempting to implement evaluation of MethodElementReturnTypeGoal I was
> unable to access call context and actual arguments' values. Neither I
> succeeded in attempts to obtain MethodReturnTypeGoal object (which has
> getArguments() method)
>
>
> I also tried to utilize code assist strategy via ClassMemberContext. At
> first glance it looked promising. However I was unable to find out reciever
> class for '->' operator, as ClassMemberContext.getLhsTypes() returns empty
> list for the example above.
>
> Albeit I'm actually have access to call context with ClassMemberContext, I'm
> not happy with it - as it provides only text of call context via
> getStatementText() and it turns out that I have to parse this text manually.
>
> * So, can anybody help me by pointing directions that I should dig further?
> * Is there an access from MethodElementReturnTypeGoal to call context or actual
> call arguments?
> * Why there is no attempt to evaluate goal for call context prior to
> goal for method's content?
> * Is there a way to access goal evaluator from ClassMemberContext?
> * Do I have a broken installation or facing a known bug?
>
>
> Thank you in advance.
>
> --
> Victor Kupriyanov
> _______________________________________________
> pdt-dev mailing list
> pdt-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/pdt-dev
> _______________________________________________
> pdt-dev mailing list
> pdt-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/pdt-dev
>
_______________________________________________
pdt-dev mailing list
pdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/pdt-dev



--

Thanks!

Best Regards!

Zhao

Back to the top