Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eclipse-Formatter-Plugin fails due to wrong assumptions #1172

Open
contitleine opened this issue Oct 7, 2024 · 0 comments
Open

Eclipse-Formatter-Plugin fails due to wrong assumptions #1172

contitleine opened this issue Oct 7, 2024 · 0 comments
Labels

Comments

@contitleine
Copy link

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.

@cushon cushon added the Eclipse label Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants