Extension Point for Statement Completed
Hello Everyone,
I need to run a piece of logic every time the user completes a statement (or possibly finishes a line). Preferably, it should work in multiple languages. I found a great list of extension points, but I cannot tell which one I need. Which extension point should I look at?
Thanks so much in advance. This is a truly awesome IDE.
请先登录再写评论。
It depends on your definition of "completes a statement" and "finishes a line". Note that people don't always write code from left to right, top to bottom, they edit existing code a lot. And probably it'll be easier to help you if you explain which problem you're trying to solve.
Hi Peter,
Thanks so much for getting back to me so quickly. Specifically, I am trying to write a plugin that will inspect each line of code for defects in the background as the code is written or edited. The plugin will then create an icon with defect info when it finds a defect and provide options for resolving the defect. Obviously, the IDE already does this for many things. However, I am interested in using custom filters, custom explanations, custom icons, and custom solutions.
Ideally, I would like to be able to run these background inspections on each line of code after it is written and after editing of an already existing line is complete. If that is not possible, being able to run them after a statement is completed will be sufficient. Being able to obtain a reference to the corresponding portion of the PSI tree will also be useful to analyze for defects and generate new code.
Thanks so much :)
Thanks for the explanation, but you still haven't provided a definition of "statement finished" and what it means for an existing line to be complete.
The usual approach to highlighting is to write an Annotator or a LocalInspectionTool that's run automatically on the whole file after any change, and provides the warnings or errors. It's usually quite performant (especially if caching is employed) and there's no need to track precisely which fragments have changed, especially given that they can anyway affect other, untouched parts of file.
Thanks for the info. If scanning the whole file after any change is not too resource intensive, it should work well enough. To be more specific, I am interested in detecting the following defects:
1. Incorrect use of internal libraries (one incorrectly being called before the other for certain use cases, etc)
2. Incorrect code styling (based on organization style)
3. Code smells (specific ones that are problematic)
For this reason, I am only interested in lines which are compilable. For example the line below would not be of interest
SomeObject myNewObject =
however, the below line is of interest
SomeObject myNewObject = CustomLibrary.getNewObject("wrong_object");
What I mean by a line being "complete" is that the user (developer) does one of the following two things:
1. Actively types or pastes something that, after a keystroke or paste operation, is a valid statement
or
2. Edits an existing valid statement and it becomes a valid statement again
The idea is that this inspection should be done in the background as soon as a possible defect is created.
I don't need to track which fragments have changed if it isn't too resource intensive to run the inspection on the whole file. It is not a problem if the inspection runs on parts of the file that have not changed. If I was to write an annotator or Local Inspection Tool, what extension points should I look at?
Start on an annotator: http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/annotator.html
I think when you start writing that you will see you don't need to worry about whether a statement is "complete" or not.
Thanks so much. I'll take a look and see how it goes.