google / keep-sorted

keep-sorted is a language-agnostic formatter that sorts lines between two markers in a larger file.
Apache License 2.0
132 stars 15 forks source link

keep-sorted's handling of unmatched directives is a little awkward #50

Open JeffFaer opened 1 week ago

JeffFaer commented 1 week ago

https://github.com/google/keep-sorted/blob/bac77d0c1cffd305d3bc17666616deacfc7fac4c/keepsorted/keep_sorted.go#L143-L154

If we find a keep-sorted start or keep-sorted end line that don't have a corresponding keep-sorted end or keep-sorted start line, we recommend that it be deleted. That's certainly one way to solve the problem, but the other way to solve it would be to add the required directive somewhere else in the file.

Internal users have often run afoul of this especially with code templates that want the result to use keep-sorted, but the code template itself is using keep-sorted incorrectly.

The main problem they run into is that running keep-sorted in fix mode eagerly removes these lines without any prompting, which can be pretty frustrating. I think we should have some better behavior here so that the tool is a little less annoying to use

Some ideas: