package com.reason.ide;

import com.intellij.AppTopics;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.AbstractProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.util.messages.MessageBusConnection;
import com.reason.ide.format.ReformatOnSave;
import org.jetbrains.annotations.NotNull;

public class ReasonDocumentManager extends AbstractProjectComponent {

    private MessageBusConnection m_busConnection;

    protected ReasonDocumentManager(Project project) {
        super(project);
    }

    @NotNull
    @Override
    public String getComponentName() {
        return "ReasonDocumentManager";
    }

    @Override
    public void projectOpened() {
        Boolean reasonReformatOnSave = Boolean.valueOf(System.getProperty("reasonReformatOnSave"));
        if (reasonReformatOnSave) {
            Notifications.Bus.notify(new RmlNotification("Refmt", "reformat on save is enabled", NotificationType.INFORMATION));
            m_busConnection = ApplicationManager.getApplication().getMessageBus().connect();
            m_busConnection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new ReformatOnSave(myProject));
        }
    }

    @Override
    public void projectClosed() {
        m_busConnection.disconnect();
    }
}

@@ -460,16 +460,59 @@
 NotNull;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Apublic
@@ -576,16 +576,30 @@
 nent %7B%0A%0A
+    @Nullable%0A
     priv
@@ -641,16 +641,16 @@
 ction;%0A%0A
-
     prot
@@ -677,16 +677,25 @@
 Manager(
+@NotNull 
 Project 
@@ -1416,24 +1416,67 @@
 tClosed() %7B%0A
+        if (m_busConnection != null) %7B%0A    
         m_bu
@@ -1497,20 +1497,30 @@
 sconnect();%0A
+        %7D%0A
     %7D%0A%7D%0A

fix a NPE