how to triggered inspections after changes in method body

Answered

Hi all,

I am developing a new plugin for idea which inspects code with a specific type annotation. I could not figure out how to trigger inspection on other files after changing something in method body. I will explain it with following example code:

These are 2 java files: TestClass.java and Main.java

TestClass.java

public class TestClass {
@Version(min = "16.0", max = "16.8")
public void testMetod(int test, @Version(min = "16.9") String text) {
System.out.println(text);
}
}
Main.java

public class Main {
public static void main(String[] args) {

TestClass testClazz = new TestClass();

String message = "testMethod1";
testClazz.testMetod(0, message);
}

}

Purpose of the plugin is to compare the values of "@Version" with a property in pom.xml file and highlight the relevant code pieces if version is not between given range in annotation.

Prerequisites:
Both files must be opened in Editor in idea and they must be inspected once.

Case A:
1) Be sure prerequisites are filled.
2) Change "min = "16.9"" to "min = "17.0"" in method signature of testMehod in TestClass.java
3) Switch active tab from TestClass.java to Main.java.
4) Right after this, the inspections will be triggered automatically.

Case B:
1) Be sure prerequisites are filled.
2) Change "System.out.println(text);" to "System.out.println();" or some other string inside "testMethod" in TestClass.java
3) Switch active tab from TestClass.java to Main.java
4) There will be no inspection on Main.java and all the highlights from first inspection are still there.

Thats the case i want to fix. I want that the inspection will be triggered again automatically. How can i do that? What should i invalidate so that the inspection will be triggered when changing active tabs?

Thanks for your help.

 

14 comments
Comment actions Permalink

That's intentional and very important not to run inspections on other files (and actually in the same file in another 'parts') when only code block is changed. Otherwise inspections would run on each typing, this will kill the performance.

I don't understand your problem, sorry. There is no parameters in method body: do you mean parameters or call arguments? you would like to highlight text in 'Main' class when what is changed?

 

Anna

0
Comment actions Permalink

Hi Anna,

thank you for your comment. 

That's intentional and very important not to run inspections on other files (and actually in the same file in another 'parts') when only code block is changed. Otherwise inspections would run on each typing, this will kill the performance.

What I want to do is not that exactly.

I don't understand your problem, sorry

You have right. I formulated it not that good. Sorry. I edited my post now and i hope it is more obvious now. You can read it again above.

you would like to highlight text in 'Main' class when what is changed?

Yes, i want to do it only when i switch active open file tab to Main.class in editor. 

 

0
Comment actions Permalink

Why do you need to rehighlight Main when print(text) is replaced with print()? How does it influence the code in Main?

0
Comment actions Permalink

This is required and is one of the main purposes of the plugin. If the parameter is used inside the method and if the versions does not match, it must be highlighted as error.

It is not rehighlighting actually, but reinspecting after a change in method body. I need to invalidate a state of something but i cant find it. Do you have an idea?

0
Comment actions Permalink

do you mean that your inspection is started on Main when you change the method body in TestClass?

0
Comment actions Permalink

Not directly after changes in method body. only when i switch the current open file in editor in idea

0
Comment actions Permalink

If inspections are run but the results are not applied to editor, then it's a bug. Please submit to the issue tracker. You would be able to restart daemon and rehighlight everything but that's performance consuming, so better to wait for a fix. Thanks

0
Comment actions Permalink

 No, it is not a bug. inspections are not run. that is what i want to achieve. Which daemon do you mean?

0
Comment actions Permalink

if you switch to Main now after editing the body inside TestClass: your inspection is started or not?

0
Comment actions Permalink

no it is not started :) it starts after i changed something in method signature, but not after i changed something in method body.

If you wish i can show you live by sharing my screen with you.

0
Comment actions Permalink

Ok, that's actually good that inspection is not started (due to performance). One won't be happy if change in some comment would rehighlight his long and complex file which he just left for a moment.

You can attach your listener (please check that the changes are inside 'your methods' at least) and then restart DaemonCodeAnalyser but this way your users would have to wait on each small change in the method - be aware.

1
Comment actions Permalink

Cool thank you. Is this happening every time when i do changes in method signature? or closing and opening current open file in editor?

0
Comment actions Permalink

PsiTreeChange is generated on each change, clients are responsible to filter only appropriate changes

0
Comment actions Permalink

ok i got it. so thank you Anna. now it works exactly i wanted.

For those who want to know how i solved it. It my first time with developing an idea plugin. I might use something wrong:

I added a new PsiTreeChangeListener listener to PsiManager. Then i check if the change in code is the one i am looking for. In my case, if it is method parameter and the parameter has that specific annotation. if yes, then i restart DaemonCodeAnalyser. Then my inspection will be triggered automatically if i change the view in editor.

0

Please sign in to leave a comment.