Extend Java Psi parsing with new methods and new types?

We're developing a plugin for the Gosu programming language.  Gosu's type system extends existing types with new methods and properties and also adds new types.  We'd like to provide some or all of these features to Java source files inside IntelliJ.  For instance, Gosu extends String with several useful methods and properties.  Is there some way to extend the Java Psi parser to provide our own Psi so that the following will parse in a Java file?

String Hi = "hi".capitalize();

Gosu provides the capitalize() method and our plugin is capable of producing Psi for the method call.  I would even be satisfied if we could get a crack at simply stubbing out the psi for the expression to avoid the parse error.

Gosu also provides whole types that are not known to Java.  The type system is pluggable where a new metatype can be defined on equal footing with any other type in the system via a custom type loader. For example, a type loader for XML schemas can resolve type names corresponding with elements defined in XSD files.  Gosu's parser doesn't care where a type comes from; it's view of a type is unified through abstract type information; all types loaded in Gosu implement a set of standard interfaces to provide type information about types, methods, properties, etc.  Our Psi parser for Gosu resolves references to new types and new features to existing types.  So, for example, the following reference to an XML schema type is legal in Gosu in the presence of the Gosu XML schema type loader:

var address : xsd.location.Address

I'd like to extend these new types to Java as well.  So I'd like to somehow plug in to the Java Psi parser to make the following declaration legal in a Java file:

xsd.location.Address address;


Fwiw, we have the compiler side of things taken care of; we've tricked javac into compiling Java source with references to Gosu type information.  Now the hard part seems to be tricking the IDE's parser to do the same.

Scott

0
6 comments

As far as I've understood, you're not adding any new syntax to Java, you're just defining classes and methods that don't actually exist. For adding methods or fields to existing Java classes, you can use the PsiAugmentProvider API. Adding classes from XSD and such is done in exactly the same way as adding classes from Gosu itself; the PsiElementFinder API can help you with that.

0

Thanks, Dmitry.  I'm experimenting with PsiAugmentProvider.  So far no luck.  Say I have some code in a Java class like so:

public class MyClass {
public void foo() {
String hello = "hello";
hello.myMethod();
}
}

Where myMethod() is defined elsewhere in a Gosu enhancement class.  What I'm finding is that PsiAugmentProvider is never called for the java.lang.String class for normal parsing of the MyClass Java file.  I do, however, see it called for completion e.g., after dot is pressed.  Even so, the custom PsiMethods our augmentor provides aren't used in the completion popup, they are lost somewhere along the way after the call into getAugments().  Is PsiAugmentorProvider designed for my needs or is it serving some other need?  Specifically, should MyClass parse with no errors if I've correctly implemented PsiAugmentorProvider to return a PsiMethod for myMethod()?

Thanks.

Scott

0

PsiAugmentProvider isn't implemented yet for extending JDK and library classes. Looks like you'll need this so please leave a request in the tracker. It should work for "regular" project classes though.

0

Gotcha.

_http://youtrack.jetbrains.com/issue/IDEA-84093

When can I expect the change?  Thanks.

0

Hope within a next week or two.

0

Hi.  I'm implementing PsiElementFinder as suggested by Dmitry. My implementation returns a PsiClass corresponding with a specific resource file type. My PsiClass#getMethods() impl returns a set of methods corresponding with the type, but aren't really part of the Psi.  I'm expecting these methods to show up in code completion, but they don't.  How can I get my PsiClass methods to work with code completion?  Or is that not possible with my synthetic PsiClasses?  Thanks!

0

Please sign in to leave a comment.