RefactorEventListener

Answered

Hi! I'm trying to build a plugin that will listen for refactoring (renaming, moving etc) and when its done, do a Git add followed by a Git commit.

I have managed to get RefactoringElementProvider to work but its not perfect. It will trigger an event when I refactor, but is too early. I need a trigger that will happen when the refactoring is done (when every renaming, variables etc. is done).

I have looked into RefactoringEventListener but cannot seem to get it to work. 

 

What I've done:

Main class


MessageBus messageBus;

MessageBusConnection messageBusConnection;

public Main() throws GitAPIException {

messageBus = ApplicationManager.getApplication().getMessageBus();
messageBusConnection = messageBus.connect();
messageBusConnection.subscribe(RefactoringEventListener.REFACTORING_EVENT_TOPIC, new RefacListener());

}

 

RefacListener

public class RefacListener implements RefactoringEventListener {
@Override
public void refactoringStarted(@NotNull String refactoringId, @Nullable RefactoringEventData beforeData) {
System.out.println("start");
}

@Override
public void refactoringDone(@NotNull String refactoringId, @Nullable RefactoringEventData afterData) {
System.out.println("done");
}

@Override
public void conflictsDetected(@NotNull String refactoringId, @NotNull RefactoringEventData conflictsData) {
System.out.println("conflict");
}

@Override
public void undoRefactoring(@NotNull String refactoringId) {
System.out.println("undo");
}

}

 

plugin.xml

<application-components>
<component>
<implementation-class>Main</implementation-class>
</component>
</application-components>

 

When the framework calls

myProject.getMessageBus()
.syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(refactoringId, getAfterData(writableUsageInfos));

my listeners is not on the subscriber list which I think could be the issue. If so, could I get some help with it?

 

I'm not sure if I'm completely wrong or just missing something, any help is appreciated

 

 

0
2 comments

Instead of registering this event in ApplicationComponent, do it in ProjectComponent. Create your MessageBus with Project as well:

 

public class Main implements ProjectComponent {
public Main(@NotNull Project project) {
MessageBus messageBus = project.getMessageBus();
MessageBusConnection messageBusConnection = messageBus.connect();
messageBusConnection.subscribe(RefactoringEventListener.REFACTORING_EVENT_TOPIC, new RefacListener());
}
}
0
Avatar
Permanently deleted user

Ok thanks. That worked actually, but the main problem is still there: it will trigger the event before the renaming is done in the files.

I.e after the renaming of main to main2 I will notice the change, but will miss that the constructor also has been changed.

Is there a better way to wait until the refactoring is completely done?

0

Please sign in to leave a comment.