[Solved]: Source code formatter: inline comments not being formatted [message #1815829] |
Fri, 11 October 2019 15:20  |
Eclipse User |
|
|
|
Hi!
I am writing a custom application to format Java source files in the same way that Eclipse's formatter does.
The only failing test I have at the moment is checking if my application can format JDK 8's java.util.HashMap source code in the same way that Eclipse does.
I have only 8 differences, all of them caused by this type of comment:

(on the left is the hashmap as formatted by Eclipse, on the right is my version)
I have created a CodeFormatter using both ToolFactory.M_FORMAT_EXISTING and ToolFactory.M_FORMAT_NEW, with no difference in the results:
// Using ToolFactory.M_FORMAT_NEW
// CodeFormatter codeFormatter =
// ToolFactory.createCodeFormatter(GoogleStyleOptions.getGoogleOptions());
// Using ToolFactory.M_FORMAT_EXISTING
CodeFormatter codeFormatter =
ToolFactory.createCodeFormatter(GoogleStyleOptions.getGoogleOptions(), ToolFactory.M_FORMAT_EXISTING);
TextEdit textEdit = codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS,
sourceCodeToFormat,
FROM_THE_BEGINNING,
sourceCodeToFormat.length(),
ZERO_INITIAL_INDENTATION,
LINE_SEPARATOR);
IDocument doc = new Document(sourceCodeToFormat);
textEdit.apply(doc);
return doc.get();
I am using the same XML file to configure the formatter both of my Eclipse and my custom application, and have checked that they are identical in runtime.
What my formatter is missing, would you know?
Thanks in advance and kind regards!
[Updated on: Mon, 14 October 2019 05:07] by Moderator Report message to a moderator
|
|
|
|
Re: Source code formatter: inline comments not being formatted [message #1815870 is a reply to message #1815833] |
Mon, 14 October 2019 05:06  |
Eclipse User |
|
|
|
Thanks, Ed.
Fortunately I was able to solve this during the weekend.
The problem was that a mistake on my side. I had a hardcoded option, FORMATTER_COMMENT_PRESERVE_WHITE_SPACE_BETWEEN_CODE_AND_LINE_COMMENT, forgotten in the code that was overriding the configuration provided by the files.
Once I fixed this, the formatting worked as expected.
Kind regards,
Rodrigo
|
|
|
Powered by
FUDForum. Page generated in 0.09155 seconds