package com.dubreuia;

import com.dubreuia.model.Storage;
import com.dubreuia.processors.Processor;
import com.dubreuia.processors.ProcessorFactory;
import static com.dubreuia.utils.PsiFiles.isPsiFileExcluded;
import static com.dubreuia.utils.PsiFiles.isPsiFileInProject;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManagerAdapter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import java.util.List;
import org.apache.log4j.Level;
import org.jetbrains.annotations.NotNull;

public class SaveActionManager extends FileDocumentManagerAdapter {

    public static final Logger LOGGER = Logger.getInstance(SaveActionManager.class);

    static {
        LOGGER.setLevel(Level.DEBUG);
    }

    private final Storage storage = ServiceManager.getService(Storage.class);

    @Override
    public void beforeDocumentSaving(@NotNull Document document) {
        for (Project project : ProjectManager.getInstance().getOpenProjects()) {
            PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
            if (isPsiFileEligible(project, psiFile)) {
                processPsiFile(project, psiFile);
            }
        }
    }

    /**
     * The psi files seems to be shared between projects, so we need to check if the file is physically
     * in that project before reformating, or else the file is formatted twice and intellij will ask to
     * confirm unlocking of non-project file in the other project.
     */
    private boolean isPsiFileEligible(Project project, PsiFile psiFile) {
        return psiFile != null &&
                project.isInitialized() &&
                !project.isDisposed() &&
                isPsiFileInProject(project, psiFile) &&
                !isPsiFileExcluded(psiFile, storage.getExclusions());
    }

    private void processPsiFile(Project project, PsiFile psiFile) {
        List<Processor> processors = ProcessorFactory.INSTANCE.getSaveActionsProcessors(project, psiFile, storage);
        LOGGER.debug("Running processors " + processors + ", file " + psiFile + ", project " + project);
        for (Processor processor : processors) {
            processor.writeToFile();
        }
    }

}
@@ -146,131 +146,8 @@
 ry;%0A
-import static com.dubreuia.utils.PsiFiles.isPsiFileExcluded;%0Aimport static com.dubreuia.utils.PsiFiles.isPsiFileInProject;%0A
 impo
@@ -534,31 +534,8 @@
 le;%0A
-import java.util.List;%0A
 impo
@@ -604,16 +604,164 @@
 tNull;%0A%0A
+import java.util.List;%0A%0Aimport static com.dubreuia.utils.PsiFiles.isPsiFileExcluded;%0Aimport static com.dubreuia.utils.PsiFiles.isPsiFileInProject;%0A%0A
 public c
@@ -2041,16 +2041,71 @@
 sions())
+ &&%0A                psiFile.getModificationStamp() != 0
 ;%0A    %7D%0A

fix error when creating new java file