keep-sorted icon indicating copy to clipboard operation
keep-sorted copied to clipboard

Adding user-defined group delimiters

Open drubery opened this issue 2 months ago • 2 comments

This addresses https://github.com/google/keep-sorted/issues/97. The discussion there suggested trying to override line breaks, but this turns out to be difficult. The options are not specified until after we've split into lines, and lots of places use line numbers for fixes.

Instead, we allow customization of how lines are associated into groups. In order to support the TOML's use case (end groups on blank line) and SQL's use case (end groups if the line ends in a semicolon), we allow a new list of regexes, group_delimiter_regexes that determine if the line should end a group.

There's a subtlety to precedence with sticky_prefixes. Since lines starting with sticky_prefixes should associate to a group below them, the first line with a sticky prefix can create a new group, despite what the We allow sticky_prefixes to take precedence.

drubery avatar Oct 29 '25 17:10 drubery

The discussion there suggested trying to override line breaks, but this turns out to be difficult. The options are not specified until after we've split into lines

Yeah, I hadn't fully considered how we split into lines earlier in the flow than we parse options for a block. That certainly complicates that suggestion a bit. I wonder how bad it would be to pass the full file into newBlocks and have it just search for start and end directives instead of needing the file to be split up into lines ahead of time

block could have another field that specifies what delimiter was used to create its lines, and that would mean that lines might not actually by true lines if the delimiter is something other than the newline

There's a subtlety to precedence with sticky_prefixes. Since lines starting with sticky_prefixes should associate to a group below them, the first line with a sticky prefix can create a new group, despite what the We allow sticky_prefixes to take precedence.

Could you also add some goldens that capture that subtlety? Especially cases where one of the group_delimiter_regexes matches the sticky_prefixed line

JeffFaer avatar Nov 13 '25 04:11 JeffFaer

I wonder how bad it would be to pass the full file into newBlocks and have it just search for start and end directives instead of needing the file to be split up into lines ahead of time

I had initially shied away from that because we keep a lot of "line numbers" around. Some of those are just internal bookkeeping, but some of those are shown to the user, so those ones need to be real line numbers. But I've just pushed a version with a golden that suggests it might be necessary. My original use case of grouping by double-newline doesn't automatically fix very well because we must put a double-newline at the end of the block. That feels kind of unnatural, and maybe I just need to do the work.

I'll try to separate out the two notions of line number and use a line delimiter in the block options instead.

drubery avatar Nov 13 '25 12:11 drubery