Format doesn't save

Hi :)

I found an awesome plugin at https://gist.github.com/marcosscriven/3badfb3001d7e18aa668/ and https://bitbucket.org/atlassian/intellij-headless-formatter/src/25ecf92a52b6?at=master

Theoretically it formats any intellij project from the command line. Unfortunately, both, the gist and bitbucket code, doesn't work for me. So I combined them and tried to get them work... unsuccessfully :( I just want to run the formatter from the command line (headless).

This is my current code

package xxx;

import com.intellij.codeInsight.actions.AbstractLayoutCodeProcessor;
import com.intellij.codeInsight.actions.OptimizeImportsProcessor;
import com.intellij.codeInsight.actions.ReformatCodeAction;
import com.intellij.codeInsight.actions.ReformatCodeProcessor;
import com.intellij.ide.impl.ProjectUtil;
import com.intellij.idea.IdeaApplication;
import com.intellij.idea.Main;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ApplicationStarter;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.PlatformUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.lang.reflect.InvocationTargetException;

public class CodeFormatApplication extends IdeaApplication {

    public CodeFormatApplication(String[] args) {
        super(args);
    }

    @NotNull
    @Override
    public ApplicationStarter getStarter() {
        return new ApplicationStarter() {
            @Override
            public String getCommandName() {
                return "codeformat";
            }

            @Override
            public void premain(String[] args) {
            }

            @Override
            public void main(String[] args) {
                final String projectPath = "/home/robert/Projects/Playground/Java";
                System.out.println("Load project " + projectPath);
                final Project project = ProjectUtil.openOrImport(projectPath, null, false);
                project.save();

                ApplicationManager.getApplication().runWriteAction(() -> {
                    VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
                });

                System.out.println("Starting code formatting.");
                final Module[] modules = ModuleManager.getInstance(project).getModules();
                for (Module module : modules) {
                    System.out.println("Reformatting code for module: " + module.getName());
                    AbstractLayoutCodeProcessor processor = new OptimizeImportsProcessor(new ReformatCodeProcessor(project, module, false));
                    // Reformat only Java classes
                    ReformatCodeAction.registerFileMaskFilter(processor, "*.java");
                    processor.run();
                }

                FileDocumentManager.getInstance().saveAllDocuments();
                System.out.println("Done");
            }
        };
    }

    public static void main(String[] args) throws InvocationTargetException, InterruptedException {
        final String[] emptyArgs = new String[]{};
        System.setProperty(PlatformUtils.PLATFORM_PREFIX_KEY, PlatformUtils.getPlatformPrefix(PlatformUtils.IDEA_CE_PREFIX));
        System.setProperty("java.awt.headless", "true");
        Main.setFlags(emptyArgs);

        SwingUtilities.invokeLater(() -> SwingUtilities.invokeLater(() -> new CodeFormatApplication(emptyArgs).run()));
    }
}


I created a new, normal java project and added all libraries from intellij/lib/* and also the tools.jar from /usr/lib/jvm/java-8-oracle/lib/tools.jar to the project. The IDE starts headless and prints all the output. However, the changes aren't saved to the file. :_|

Maybe someone can help me out here? :D

My output also prints some errors, but I guess these have nothing to do with the problem...

ERROR: Assertion failed
java.lang.Throwable
     at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:144)
     at com.intellij.openapi.diagnostic.Logger.assertTrue(Logger.java:153)
     at org.jetbrains.ide.BuiltInServerManagerImpl$2.run(BuiltInServerManagerImpl.java:93)
     at com.intellij.openapi.application.impl.ApplicationImpl$8.run(ApplicationImpl.java:365)
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
     at java.lang.Thread.run(Thread.java:745)
     at org.jetbrains.ide.PooledThreadExecutor$1$1.run(PooledThreadExecutor.java:55)
log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Load project /home/robert/Projects/Playground/Java
Starting code formatting.
Reformatting code for module: Java
Done

2 comments
Comment actions Permalink

The formatter can be run from the terminal now. Like the inspection tool.
https://confluence.jetbrains.com/display/IDEADEV/Command-Line+Source+Code+Formatter

Closed for me!

0

Please sign in to leave a comment.