EclipseCodeFormatter icon indicating copy to clipboard operation
EclipseCodeFormatter copied to clipboard

Only format if no compile errors

Open markiewb opened this issue 8 years ago • 3 comments
trafficstars

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 avatar Apr 10 '17 07:04 markiewb

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

markiewb avatar Apr 10 '17 07:04 markiewb

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

krasa avatar Apr 10 '17 08:04 krasa

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

xinkunZ avatar Apr 12 '17 06:04 xinkunZ