Triggering Action based on Editor Events
Answered
Hi!
I would like to call the iterateContent() function everytime the user completes code statements in the editor.
With code statements i mean things like:
Variable declaration e.g private int test;
functions e.g private void doSomething(){}
and so on
Currently I'm calling iterateContent() evertyime ';' or '}' is typed in the editor but that's not really what I want. Is there a more efficient/convenient way to do this?
public class GetSourceCode extends AnAction {
static {
final TypedAction typedAction = TypedAction.getInstance();
typedAction.setupRawHandler(new TypedHandler(typedAction.getRawHandler()));
}
@Override
public void update(AnActionEvent e) {
// Using the event, evaluate the context, and enable or disable the action.
// Set the availability based on whether a project is open
Project project = e.getProject();
e.getPresentation().setEnabledAndVisible(project != null);
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
// Using the event, implement an action. For example, create and show a dialog.
}
}
public class TypedHandler implements TypedActionHandler {
private TypedActionHandler myOriginalActionHandler;
public TypedHandler(TypedActionHandler originalHandler) {
myOriginalActionHandler = originalHandler;
}
@Override
public void execute(@NotNull Editor editor, char charTyped, @NotNull DataContext dataContext) {
myOriginalActionHandler.execute(editor, charTyped, dataContext);
if (charTyped == ';' || charTyped == '}') {
Project project = editor.getProject();
iterateContent(project);
}
}
private void iterateContent(Project project) {
//iterate through project files; check if instanceof PsiJavaFile; print out PsiClass.getText()
}
}
Please sign in to leave a comment.
Onuryi,
Depending on the TypedHandler to track the code may be a bit risky. Have you considered listening to the PSI changes with PsiTreeChangeListener (also PsiTreeChangeAdapter)
You can register it with:
Thanks Jakub!
I will have a look at it!
Jakub Chrzanowski
I tried using the PsiTreeChangeListener but somehow I get a weird output. My code looks like following:
p
private
i
I gues my mistake is that I'm working with the PsiTreeChangeEvent instead of using ChachedValue and PsiModificationTracker. Are there any examples on how to use them together?