Implement structural search support for Apex and Visualforce

Issue #2050 resolved
Tristan Coignion created an issue

I would like to create a Structural Search with Intellij and Illuminated Cloud particularly for Apex files, in order to set up custom inspections for my team.

Optionally, it could be great to have this for other SF languages such as VisualForce

Original Post

Official response

  • Scott Wells repo owner

    Thanks for the example. There may be another way to do this right now. You can create new rules for PMD Apex using XPath, and IC2 now supports real-time integration of PMD Apex so that violations are reported immediately. Certainly the two rules you describe here could be implemented using XPath. Given the tight integration with PMD Apex now and its simple extensibility via XPath, that might just be my overall response to this request vs. creating a "public API" (I hate quoting repeatedly, but this is a situation that warrants it) for IC2's Apex parse tree.

Comments (10)

  1. Scott Wells repo owner

    Thanks for filing. For those interested in this, it would be useful to provide concrete examples of the types of searches you'd like to be able to execute as exposing the PSI (effectively parse tree/AST) as a "public API" like this can limit the ability to evolve that tree structure in the future, so I'd prefer to start with limited exposure based on concrete use cases.

  2. Tristan Coignion reporter

    In my codebase, we want to centralize database operations in few utility classes. And I’d like to warn the developer when they use raw database operation (SOQL query, insert, update..) instead of using the utility classes. I would like to search for these things so that I can create an inspection of this search.

    Then this code

    SObject myObject;
    insert myObject;
    

    would have a warning a even maybe suggest a replacement such as CustomInserter.insert(myObject);

    A second use case, is to suggest using String.format instead of addings strings manually. This code

    String res = "" + var1 + "is a result of : " + var 2 + " and " + var3 
    

    would then be replaced by

    String res = String.format("{0} is a result of {1} and {2}", new List<Object>{var1, var2, var3})
    

    As I write this, I realise it might be easier for you to just make new inspections in the plugin rather than providing the users with the tools to make them themselves. However, I think it would still be useful in the long term to be able to build these structural search ourselves.

  3. Scott Wells repo owner

    Thanks for the example. There may be another way to do this right now. You can create new rules for PMD Apex using XPath, and IC2 now supports real-time integration of PMD Apex so that violations are reported immediately. Certainly the two rules you describe here could be implemented using XPath. Given the tight integration with PMD Apex now and its simple extensibility via XPath, that might just be my overall response to this request vs. creating a "public API" (I hate quoting repeatedly, but this is a situation that warrants it) for IC2's Apex parse tree.

  4. Tristan Coignion reporter

    Oh, you’re right ! I actually did not know it was possible to extend PMD at all. Well, thanks a lot for the redirect and constructive responses.

    By the way, as I’m here, just wanted to say, I love the IDE, keep going with the good work ! 😃

  5. Scott Wells repo owner

    Okay. I think I'm going to resolve this as possible via another supported avenue, namely real-time integration of custom PMD Apex rules implemented via XPath. Obviously if some situation arises that cannot be handled in that manner, this can either be reopened or a new, more specific enhancement can be logged, but I honestly think you'll find incredible flexibility via the prescribed alternative.

  6. Scott Wells repo owner

    I'll provide a few pointers to help get you started as I contributed a number of custom rules and rule improvements to PMD Apex a few months ago including several implemented via XPath. First, you'll want to look at some of these as examples:

    https://github.com/pmd/pmd/tree/master/pmd-apex/src/main/resources/category/apex

    Specifically look for rules with <property name="xpath">. And you'll find the PMD rule designer invaluable for getting the correct XPath for various nodes in a syntax tree based on node state. You pretty much enter the code that you are looking for, click on the specific language token(s) in the resulting AST that represent the search state, and it will show you the XPath for finding it. That then becomes part of your rule definition.

    Let me know if that doesn't help.

  7. Tristan Coignion reporter

    Hello, thanks for the tips ! I actually managed to make a little rule of my own, and I’m very satisfied with it :D

    Have a good day,

    Tristan

  8. Log in to comment