krasa / EclipseCodeFormatter

IntelliJ plugin - use Eclipse's code formatter directly from IntelliJ. https://plugins.jetbrains.com/plugin/6546
Apache License 2.0
485 stars 117 forks source link

Only format if no compile errors #132

Open markiewb opened 7 years ago

markiewb commented 7 years ago

If you invoke the formatter on a file with compile errors (f.e. missing paranthesis) the code is messed up. How about checking for compile errors before formatting?

    private boolean hasErrors(PsiFile psiFile) {
        final AtomicBoolean hasErrors = new AtomicBoolean(false);

        psiFile.accept(new PsiRecursiveElementWalkingVisitor() {
            @Override
            public void visitElement(PsiElement element) {
                super.visitElement(element);
                //FIXME does PsiErrorElementUtil.hasErrors() the same?
                if (!element.isValid() || !(element instanceof PsiErrorElement)) {
                    //contains compile-errors, which may lead to strange results in formatting
                    hasErrors.set(true);
                }
            }
        });
        return hasErrors.get();
    }
markiewb commented 7 years ago

I provided code for the save-actions plugin, which does the check - https://github.com/dubreuia/intellij-plugin-save-actions/pull/77. But the issues occures even without the save-actions plugin

krasa commented 7 years ago

Hmm, I never noticed that, for me it would just not format anything if there was some ugly syntax error.

xinkunZ commented 7 years ago

@krasa Add this feature will be better, isn't it? SEE: dubreuia/intellij-plugin-save-actions#76.