Is it possible to create plugins for certain Java types?

Is it possible to create plugins which provide custom behaviour / indentation for classes which inherit from a special type,

org.apache.camel.builder.RouteBuilder
?
4 comments
Comment actions Permalink

Dunno about indentation, but I have made an inspection for certain subtypes, basically if you can get a PsiFile, then it is no problem:
For you it would look like this:

 
public abstract class SubtypesInspection extends BaseJavaLocalInspectionTool {

   public static final PsiElementVisitor EMPTY_VISITOR = new PsiElementVisitor() {
   };
   private static final
String PARENT_CLASS = "org.apache.camel.builder.RouteBuilder";

   @NotNull
   @Override
   public final
PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
      return isAllowed(holder) ? buildInternalVisitor(holder, isOnTheFly) : EMPTY_VISITOR;
   
}

   @NotNull
   @Override
   public final
PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly,
         @NotNull
LocalInspectionToolSession session) {
      return isAllowed(holder) ? buildInternalVisitor(holder, isOnTheFly) : EMPTY_VISITOR;
   
}

   private static boolean isAllowed(ProblemsHolder holder) {
      PsiFile file = holder.getFile();
      if
(file instanceof PsiJavaFile) {
         return extendsClass((PsiJavaFile) file, PARENT_CLASS);
      
}
      return false;
   
}

   private static boolean extendsClass(PsiJavaFile file, String parentClass) {
      PsiClass[] classes = file.getClasses();
      boolean
extendsClass = false;
      for
(int i = 0; i < classes.length; i++) {
         extendsClass = extendsClass(classes[i], parentClass);
         if
(extendsClass) {
            break;
         
}
      }
      return extendsClass;
   
}

   /** there might be better ways how to do it */
   private static boolean
extendsClass(PsiClass aClass, String parentClass) {
      boolean extendsClass = false;
      
PsiClassType[] extendsListTypes = aClass.getExtendsListTypes();
      for
(int j = 0; j < extendsListTypes.length; j++) {
         PsiClassType extendsListType = extendsListTypes[j];
         
PsiClass resolve = extendsListType.resolve();
         if
(resolve != null && resolve != aClass) {
            if (parentClass.equals(resolve.getQualifiedName())) {
               extendsClass = true;
               break;
            
} else {
               extendsClass = extendsClass(resolve, parentClass);
               if
(extendsClass) {
                  break;
               
}
            }
         }
      }
      return extendsClass;
   
}

   public abstract PsiElementVisitor buildInternalVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly);
}
0
Comment actions Permalink

Replacing the Java formatter for specific types of Java files is currently not supported. Providing "custom behavior" is most likely possible, depending on what exactly you mean by "behavior".

0
Comment actions Permalink

The idea is to make special text strings linkable, e.g. you have something like this

    .to("direct:foo")

which points to a direct route named 'foo'

    from("direct:foo)

I would like to be able to click on the to - direct route and have the editor scroll to the line with the from - direct route declaration.

Similar with

    to("xslt:foo.xsl")

where foo.xsl is a XSLT stylesheet on the classpath.

0
Comment actions Permalink

You need to implement a PsiReferenceContributor and to provide references that resolve to the routes. You can find plenty of examples for this in the IntelliJ IDEA Community Edition code.

0

Please sign in to leave a comment.