how to find extension methods
Closed this issue · 13 comments
so - i am in chapter 6 and i am reading about labels, page 105, i get to the code that reads:
a.type.representation
i foolishly decide to stretch my (admittedly weak) xtend muscles - i can see that "a" is an Attribute from the method arguments, and if i look at Attribute, i can see the method getType which returns an AttributeType. then i look at AttributeType for getRepresentation method, which i can't find, but AttributeType extends EObject - so i search on up the tree, but i still can't find any method getRepresentation - then, my "aha, extension method" light turns on, so now i want to look for an extension method representation. i went to the "File Search" workspace dialog, but i can't find representation (ok, i can find the word, because it is used in the header description for all the Entity classes).
soldiering on, i continue my reading and i discover that TypeRepresentation in an injected extension with a method representation - so, i use both "Open Type" and "File Search" to look for TypeRepresentation, but neither search method locates it.
would you mind tutoring me a bit on how it find extensions and extension methods?
maybe i have a more serious problem, i went to add the def text method, but i got a error when i injected TypeRepresenation: TypeRepresentation cannot be resolved to a type.
/*
- generated by Xtext
*/
package org.example.entities.ui.labeling
import com.google.inject.Inject
/**
-
Provides labels for EObjects.
-
See https://www.eclipse.org/Xtext/documentation/304_ide_concepts.html#label-provider
*/
class EntitiesLabelProvider extends org.eclipse.xtext.ui.label.DefaultEObjectLabelProvider {@Inject
new(org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider delegate) {
super(delegate);
}// Labels and icons can be computed like this:
// def text(Greeting ele) {
// 'A greeting to ' + ele.name
// }
//
// def image(Greeting ele) {
// 'Greeting.gif'
// }
@Inject extension TypeRepresentation
}
ok - i found TypeRepresentation in your github directory structure under xtend-gen - but, although my directory structure gas xtend-gen, it does not have TypeRepresentation in that directory.
Hi
concerning locating extension methods, I think the best way is to "Navigate" to them, using Eclipse features: Ctrl+Click or F3 on the method invocation. In general, in Eclipse, that's the best way of inspecting code (also in Java).
TypeRepresentation.xtend can be found here https://github.com/LorenzoBettini/packtpub-xtext-book-examples/blob/master/org.example.entities.ui/src/org/example/entities/ui/labeling/TypeRepresentation.xtend ; as said in the book
To get a representation of the AttributeType element, we use an injected extension,
TypeRepresentation (it is similar to the way we generate Java code for attribute
types in the generator of Chapter 5, Code Generation),
It is not shown in the book because it is similar to something already shown, but it's available in the code of the example (see the URL above).
I hope this clarifies your questions.
thanks for helping me - i just misunderstood the text you quoted in the book, and i know about navigate - i'm going to close this now - i'm in the testing chapter now.
You're welcome! :)
The testing chapter is my favorite!
Please, make sure to read also the Errata (the main README of this repository) since there have been some changes with the new versions of Xtext.
Finally, please consider writing a review of the book (e.g., on Amazon) :)
It's the README.md in the root of the repository, which is also rendered as formatted text if you scroll down at the main URL: https://github.com/LorenzoBettini/packtpub-xtext-book-examples/
even though you tried to (pre)help me - i still failed and had trouble with CompilationTestHelper. i selected method 2 and wrote: EntitiesInjectorProviderCustom (ok, i actually just copied yours - more or less). i got some warnings:
Discouraged access: The type 'IPartialContentAssistParser' is not API (restriction on required library '/Volumes/Users/david/Documents/eclipsecon/eclipsecon2015/XText_Tutorial/eclipse/plugins/org.eclipse.xtext.ui.codetemplates.ui_2.8.0.v201503040413.jar') PartialEntitiesContentAssistParser.java /org.example.entities.ui/src-gen/org/example/entities/ui/contentassist/antlr line 10 Java Problem
i ignore the warnings since i have no idea what they mean. i wrote the EntitiesGeneratorTest:
@RunWith(XtextRunner)
@InjectWith(EntitiesInjectorProviderCustom)
class EntitiesGeneratorTest {
@Inject extension CompilationTestHelper
@Test
def void testGeneratedCode() {
'''
entity MyEntity {
string myAttribute;
}
'''.assertCompilesTo(
'''
...
'''
)
}
}
i still could not resolve CompilationTestHelper and had to add: org.eclipse.xtext.xbase.junit to the MANIFEST.MF to get an import. when i tried to run it as a junit test, i got:
Caused by: java.lang.ClassNotFoundException: org.eclipse.jdt.internal.compiler.batch.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 75 more
do you know where compiler.batch.Main is located?
never mind - org.eclipse.jdt.core was on the very next page.
after i got to the end of chapter 7 - i wanted to change the Terminals file (a big mistake at my skill level).
first, i just copied the original Terminals to org.example.entities and renamed it TerminalsCopy.xtext - but, after i ran the mwe workflow, i got a lot of TerminalCopyxxx classes missing. i started adding them as just pass through extends of the original Terminalsxxx class - but, after a doing a few of them, i got lazy and decided that was probably the wrong thing to be doing - so i changed the name of the file from TerminalsCopy to Terminals thinking i would trick the workflow. now, i am even in worse shape with error flags everywhere.
is there someplace to read about the correct way to customize the Terminals.xtext file?
If you want to change a specific grammar rule, which is defined in the super grammar, you can simply redefine it in your grammar... is that what you needed?
ok, i was doing that:
Model:
elements+=ElementSpecifcation*;
terminal WS : (' '|'\t'|'\r')+;
terminal INT returns ecore::EInt : WS;// RULE_INT can never be run, WS will override
terminal ID : WS;// RULE_ID can never be run, WS will override
terminal NUMBER : ('0'..'9')+;
terminal STARTOFELEMENT : '----------------------------------------------------------------------';
terminal NL : '\n';
terminal LETTER : ('a'..'z'|'A'..'Z');// any letter
terminal PUNCTUATION : (','|'_'|'-');
terminal WORD : LETTER (LETTER)*;// can be more than 1 letter, but no spaces
but, well - i felt like i was letting xtext down - and that i might be ridiculed by the xtext masters.
is there a discussion on how to check the xtext project into git?