pyscripter / SynEdit

SynEdit is a syntax highlighting edit control, not based on the Windows common controls.
26 stars 11 forks source link

Unify LineBreak and FileFormat properties of TSynEditStringList #49

Closed pyscripter closed 1 year ago

pyscripter commented 1 year ago

Discussed in https://github.com/pyscripter/SynEdit/discussions/48

Originally posted by **pyscripter** December 9, 2022 There are currently two properties of TSynEditStringList that relate to the line endings of saved files: - FileFormat: TSynEditFileFormat = (sffDos, sffUnix, sffMac, sffUnicode) - LineBreak inherited from TStrings (introduced in Delphi Berlin 10.1) The base class TStrings uses LineBreak only in: - saving files (through GetTextStr) - reading files (SetTextStr) - Text property getter, setter GetTextStr, SetTextStr. Currently TSynEditStringList uses instead FileFormat without setting LineBreak: - SetTextStr is overwritten and detects line endings in the provided text, setting FIleFormat appropriately - GetTextStr is inherited and uses LineBreak except when saving, in which case the FileFormat is taken into account - In all other cases SynEdit uses SLineBreak (SelText, SelLength). In a few places it is assumed that SLineBreak = `#13#10` (e.g. CharIndexToRowCol, RowColToCharIndex). It is confusing to have two properties that relate to Line Endings. Also, I think it is more consistent for TSynEditStringList to inherit behavior from TStrings. Since LineBreak has been availalbe since Delphi Berlin, and this repo is not compatible with versions before that, I propose that we drop FileFormat and use instead LineBreak: - LineBreak is set when reading files or setting the Text property (SetTextStr). TStrings does not do that. - LineBreak is used when reading the Text property. This is inherited behavior. However, and since LineBreak is currently not changed, this is a breaking change. Currently Text always contains SLineBreaks. - In all other cases SynEdit should use SLineBreak. An advantage is that users can programmatically set LineBreak without casting to TSynEditStringList. I want to add that an analogous change was made earlier, so that TSynEditStringList now uses the Encoding inherited property instead of a custom property in SynUnicode.pas. Since Encoding is a read-only property of TStrings (no idea why!), a SetEncoding method was added to TSynEditStringList. The properties that affect how a file is saved are: Encoding, WriteBOM, LineBreak and TrailingLineBreak. Encoding, WriteBOM and LineBreak are detected when reading a file. This is so that reading and later saving a file, preserves the original format. With TrailingLineBreak set to False, reading and then writing to any file will keep the file identical to its original. @MShark67,@vincentparrett,@JaFi-cz What do you think?
JaFi-cz commented 1 year ago

I agree. But TrailingLinebreag doesn't work correctly now. It removes last linebreak in some cases. I will try to find example.

pyscripter commented 1 year ago

Please try again. There was an error in the previous commit. The behavior of TrailingLineSpaces has not been affected I think.