Open wmarkow opened 6 years ago
I think this would be useful, but it would probably have to run outside of a Maven project, since most C/C++ projects don't use Maven to build. Running outside of a Maven project is being discussed as problematic on #261 , but it may be possible to create a separate mojo for formatting C/C++ code outside of a project.
I did some job on embedding Eclipse CDT into fromatter-maven-plgin. Some commits are available in my cdt_#265 branch.
There are some issues with Eclipse CDT formatter:
mvn clean
so the missing jar will be downloaded and copied into local Maven repo. Then I can execute mvn clean install
to compile and build the formatter-maven-plugin.org.eclipse.cdt.core.CCorePlugin.log(IStatus)
and now fun begins because this log method can throw exception.org.eclipse.cdt.core.CCorePlugin.log(IStatus)
will not be called at all. So far so good. But if you C/C++ will have some formatting warnings that we will have a NullPointerException thrown because CCorePlugin is not "prepared" to be executed outside of the Eclipse CDT. net.revelc.code.formatter.cpp.DefaultCppFormatter.java can catch this kind of exception; in this case the file will not be formatted.org.eclipse.cdt.core.CCorePlugin.log(IStatus)
. Because some of these fields are private, and some methods called are static, I have used a reflection to inject objects into those fields. I tried to use inheritance but I've failed (with reflection it was easy). This is what net.revelc.code.formatter.cpp.CppFormatter is doing.I have prepared unit tests to cover some specific cases. I didn't create a separate mojo for C/C++ formatting, I just added this to a FormatterMojo. I have checked this and it works with my C/C++ code. I know that most C/C++ projects doesn't use Maven to build. Maybe it could be used together with nar-maven-plugin.
@wmarkow It sounds like you've made some good progress. I think before this would be ready for a pull request, I'd want to see the relevant CDT jars for formatting be published to Maven Central, and for it to better support formatting outside of an Eclipse CDT project.
It would be good to format C/C++ code as well. Eclipse CDT can be used as a C/C++ development IDE. It has already a formatter that seems to work in a similar way like for standard Java development.
I think it could use the CodeFormatter class from org.eclipse.cdt.core jar. At the beginning it could format the following files:
With this solution it would be possible to format a large C/C++ project pretty fast. Let me know what you think.