Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Eclipse-Formatter-Plugin fails due to wrong assumptions

Đang mở
#1,172 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
java
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện lớp Test tối thiểu với đoạn mã API Eclipse, sau đó đọc SnippetFormatter.java và phương thức toReplacements() của nó. Theo dõi cách phép so sánh không có khoảng trắng dẫn đến việc Eclipse formatter trả về null. Được xem là hoàn tất khi mã nguồn mẫu có thể được định dạng thông qua plugin mà không gặp lỗi này, với hành vi được xác minh bằng một regression test.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Eclipse

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.

Ngôn ngữ chính
Java
Star
6.2k
Fork
936
Merge trung bình
6 phút
Pull request đã merge (30 ngày)
3

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của google/google-java-format

Tất cả issue của google/google-java-format

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.