Take input from other field

I have the following scenario. I have two fields to select, first a field with a java class via a pedefined scope selectable. Now the problem is the second field, it should take the information of the class and should allow a scope of informationen from this class selectable, method names, method parameters etc.

I tried the approach from the Shapes tutorial:

new ListScope(node/Color/.staticFields()) {
  public string getName(node<> child) {
    child : StaticFieldDeclaration.name;
  }
};

and used it for my purpose like:

new ListScope(node/<my_Class>/.methods()) {
  public string getName(node<> child) {
    child : InstanceMethodDeclaration.name;
  }
};

and it works, but the problem the <my_Class> changes depending on what is used on the first field.

I tried this approach:

new ListScope(enclosingNode.ancestor<concept = TestClassRef>.testClass.methods()) {
  public string getName(node<> child) {
    child : InstanceMethodDeclaration.name;
  }
};

But this does return an empty sequence, because the scope initializes instantly when it is created. Just for clarification, "TestClassRef" holds the "testClass" as reference which is a "InstanceMethodDeclaration". Is there anything I'm doing completetly wrong here? I can't find anything in any tutorial or in the documentation.

I appreciate all you help!
7 comments
The approach you took should work, so I guess there's an error in some detail.
Two guesses:
  1. Would using referenceNode instead of enclosingNode in your scope query work?
  2. The methods() method only returns the methods declared in the class, not the inherited ones. The "getSuperTypes()" method would give you access to all the super-classes and implemented interfaces.
0
Sadly doesn't seem to work with referenceNode either. Maybe some more information about my DSL helps.

root
  • children
    • TestClassRef
    • TestMethodRef

TestMethodRef
  • references
    • InstanceMethodDeclaration

TestClassRef
  • references
    • ClassConcept

All concepts just extend BaseConcept. In TestMethodRef I created a constrain, to my InstanceMethodDeclaration as a reference scope. This is just defined the same as in my previous post:

new ListScope(referenceNode.ancestor<concept = TestClassRef>.testClass.methods()) {
  public string getName(node<> child) {
    child : InstanceMethodDeclaration.name;
  }
};

I don't know if I did something wrong in the editors, in TestClassRef and TestMethodRef I just defined the two references with link declaration and {name}. In my root I just put my children inside % and that's it. The sequence of my Scope is still empty and I don't know why. I want to fill in my TestMethodRef it says: No variants for "". I would be very grateful if someone can point out the error here :)
0
Ok, in this case you really need to use enclosingNode, as you were originally. Then it all works just fine fore me. I'm attaching a sample project I've created following your instructions.

Project10.zip (85KB)
0
Just wondering, did you attach the sample project already? It would help me a lot, I appreciate it.
0
Sorry, forgot to add the link to the comment. Now you should be able to grab it.
0
WoW, it works like a charm. Such a silly mistake of mine, I missed the + in "enclosingNode.ancestor<concept = TestClassRef, +>.testClass.methods()". It's just new for me everything, I'm learning MPS a few weeks now, but day for day I know more. Thank you very much for your help, I should maybe read the documentation more carefully next time. Thanks again!
0
You are welcome! Feel free to ask if you bump into another problem.
0

Please sign in to leave a comment.