<application-components>
<component>
<implementation-class>my.plugin.OnFileSaveComponent</implementation-class>
</component>
</application-components>
2. Implement com.intellij.openapi.components.ApplicationComponent:
package my.plugin;
import com.intellij.AppTopics;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter;
import com.intellij.util.messages.MessageBus;
import com.intellij.util.messages.MessageBusConnection;
import org.jetbrains.annotations.NotNull;
public class OnFileSaveComponent implements ApplicationComponent {
@NotNull
public String getComponentName() {
return "My On-Save Component";
}
public void initComponent() {
MessageBus bus = ApplicationManager.getApplication().getMessageBus();
MessageBusConnection connection = bus.connect();
connection.subscribe(AppTopics.FILE_DOCUMENT_SYNC,
new FileDocumentManagerAdapter() {
@Override
public void beforeDocumentSaving(Document document) {
// create your custom logic here
}
});
}
public void disposeComponent() {
}
}
That's it! :)
So how could it be used? If you'd like to perform some synchronization with a remote server on each file save in IntelliJ, probably, this code snippet would help you.
2 comments:
Thanks for this, I'm doing some fun work on an intelliJ plugin - do you have examples of how to do other things, like... get called when the user sets a breakpoint, or if the current file in the editor is an interface or enum?
Will answer in a dedicated blog post
Post a Comment