Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

Line Seperator issue between windows and unix

未關閉
#604 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
35/100
Issue 類型
缺陷
描述清晰度
基本清楚
活躍度
停滯
技術堆疊
java
領域
tooling

研究方向

使用 google-java-format-1.10.0-all-deps.jar 和 MinimalExample.java 範例,分別以 LF 和 CRLF 行尾重現此差異。追蹤 formatter 產生不同換行的原因,然後驗證在兩個平台上格式化相同原始碼時,都能得到一致的輸出,且不會多出一個換行符號。

由索引模型根據 Issue 內容生成。

描述

When applying GJF, some code is formatted differently between Unix-style line seperators (LF) and Windows-style line seperators (CRLF). With windows-style an extra linebreak is added, caused by the char difference between LF (\n) and CRLF (\r\n).

I changed the line-separator-style with Intellij (File>File Properties> Line Separators, while having the file opened) and got the following outputs:

Executing java -jar google-java-format-1.10.0-all-deps.jar MinimalExample.java when the file is formatted with Unix line endings:

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import feign.FeignException;

public class MinimalExample {
  void googleJavaFormatWindowsFail() {

    FeignException feignException = mock(FeignException.class);
    when(feignException.contentUTF8()).thenReturn("""
    {
      "errorCode" : 420
    }
    """);
  }
}

Executing java -jar google-java-format-1.10.0-all-deps.jar MinimalExample.java when the file is formatted with Windows line endings:

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import feign.FeignException;

public class MinimalExample {
  void googleJavaFormatWindowsFail() {

    FeignException feignException = mock(FeignException.class);
    when(feignException.contentUTF8())
        .thenReturn("""
    {
      "errorCode" : 420
    }
    """);
  }
}

If two chars are added, Unix-style generates the same line-break:

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import feign.FeignException;

public class MinimalExample {
  void googleJavaFormatWindowsFail() {

    FeignException feignException = mock(FeignException.class);
    when(feignException.contentUTF8())
        .thenReturn("""
    {
      "errorCode" : 42069
    }
    """);
  }
}

Done on GJF 1.10.0, 1,9 and 1.8 with Java 15.0.2
Windows 10 10.0
IntelliJ IDEA 2021.1.1 (Community Edition)

主要語言
Java
星號
6.2k
分支
936
平均合併
6 分鐘
30 天內合併 PR
3

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

google/google-java-format 的其他 Issue

查看 google/google-java-format 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。