Skip to content

Conversation

@drubery
Copy link

@drubery drubery commented Oct 29, 2025

This addresses #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.

@JeffFaer
Copy link
Collaborator

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

// StickyPrefixes tells us about other types of lines that should behave as sticky comments.
StickyPrefixes map[string]bool `key:"sticky_prefixes"`
// GroupDelimiterRegexes tells us if a line is allowed to end a group.
GroupDelimiterRegexes []RegexOption `key:"group_delimiter_regexes"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if someone provides a replacement template with this option? Is that even valid?

I wonder if it should actually be separate from the ByRegexpOption

},
},
{
name: "GroupDelimiter_BlankLine",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a golden test or two for this new option?

This addresses google#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
Copy link
Author

drubery commented Nov 13, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants