On Fri, 8 Apr 2022 12:17:37 GMT, Tejesh R <d...@openjdk.java.net> wrote:
>> getText function returned extra endOfLine when appended. The reason was in >> `EditorEditorKit` class, `write(Writer out, Document doc, int pos, int len)` >> method, where translation happens from buffer to Out(Writer Object) if >> endOfLine is other than '\n' ( which is '\r\n' in windows). In order to >> write each line till End of line, the string till '\n' is written including >> '\r' and again endOfLine is written which results in extra Carriage Return. >> To solve this issue, a Condition is added which checks if previous character >> to '\n' is '\r', if true then whole string except Carriage Return ('\r') is >> written, else whole string till before '\n' is written. > > Tejesh R has updated the pull request incrementally with one additional > commit since the last revision: > > Updated based on Review Comments The [`DefaultEditorKit`](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/text/DefaultEditorKit.html) class defines handling of new lines: _“…while the document is in memory, the "\n" character is used to define a newline…”_. [The comment inside its `read` method](https://github.com/openjdk/jdk/blob/096bca4a9c5e8ac2668dd965df92153ea1d80add/src/java.desktop/share/classes/javax/swing/text/DefaultEditorKit.java#L212) explains `\r` is converted to `\n` upon reading. As such, the behaviour described in [JDK-8180276](https://bugs.openjdk.java.net/browse/JDK-8180276) is not a bug. The mistake is that the user uses `System.lineSeparator()` to add a line break to the document model. Since it's the model, `\n` is to be used, which is _system-independent_. When document is written to disk, line breaks are converted to system-dependent according to the value of [`EndOfLineStringProperty`](https://docs.oracle.com/en/java/javase/17/docs/api/java.desktop/javax/swing/text/DefaultEditorKit.html#EndOfLineStringProperty) property of the document. If we handle `\r` before `\n` when writing out a document, we may get another bug report where `\r` not followed by `\n` results in a line break too. This is why I consider the current behaviour correct. The questions by @prsadhuk and the resulted discussion have led me to this conclusion. The bug should be closed as *Not an Issue* with the explanation that `\n` should always be used to add line break in a `Document` model, `System.lineSeparator()` should not be used. Does it sound reasonable? ------------- PR: https://git.openjdk.java.net/jdk/pull/8122