Eclipse-Formatter-Plugin fails due to wrong assumptions
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start by reproducing the minimal Test class with the Eclipse API snippet, then read SnippetFormatter.java and its toReplacements() method. Trace how the non-whitespace comparison leads to the Eclipse formatter returning null. Done means the sample source can be formatted through the plugin without this failure, with the behavior verified by a regression test.
Written by the indexing model from the issue text.
Description
The Eclipse-Plugin has sometimes a problem when I call the formatter programatically inside Eclipse. I am using the Snippet from https://help.eclipse.org/latest/topic/org.eclipse.jdt.doc.isv/guide/jdt_api_codeformatter.htm to format a complete Java-File. I have created a minimal Test-Class:
package test;
/**
* Test Class
*
* Generated
*
*/
public class Test
{
}
The standalone-Formatter-Jar adds a HTML-Tag
before the "Generated". Unfortunately, when I call the plugin via the Eclipse-APIs:
final TextEdit edit =
codeFormatter.format(
CodeFormatter.K_COMPILATION_UNIT, // format a compilation unit
content, // source to format
0, // starting position
content.length(), // length
0, // initial indentation
"\r\n" // line separator
);
the method returns a null because it can not format the content. I have tracked the problem back to the class SnippetFormatter.java, Method toReplacements(). This method has the condition which checks the unformatted and the formatted source:
if (!NOT_WHITESPACE.retainFrom(source).equals(NOT_WHITESPACE.retainFrom(replacement))) {
throw new IllegalArgumentException(
"source = \"" + source + "\", replacement = \"" + replacement + "\"");
}
This condition checks that there are only whitespace-changes in the new formatted source. The Javadoc says:
under the assumption that they differ in whitespace alone.
Unfortunately, this assumption is not correct. The source has (as mentioned above) an additional non-whitespace change and the IllegalArgumentException is thrown. This Exception causes the Eclipse-Formatter-Class to fail and to return only null, thereby denying to format the source.
- Dominant language
- Java
- Stars
- 6.2k
- Forks
- 936
- Avg merge
- 6m
- Merged PRs (30d)
- 3
Getting set up
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from google/google-java-format
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
google/google-java-format#1094 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 15/100
google/google-java-format#1450 · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
google/google-java-format#1439 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 62/100
google/google-java-format#1436 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
google/google-java-format#1428 · 4 comments ·
Maintainers usually reply within 1 day
All issues in google/google-java-format
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
nextcloud/notes-android#3367 ·
Maintainers usually reply within 1 day
-
:wave: team-triage a:chore in:isolated-projects in:kotlin-dsl
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day