Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Support sorting only opted-in blocks within a file #9

Open
emosenkis opened this issue Feb 12, 2025 · 4 comments
Open

Comments

@emosenkis
Copy link

When writing code, it's fairly common to have blocks that should be sorted. Something like:

_CURRENCY_CODES = [  # Please keep sorted
  "AED",
  # NOTE: Keep this for historical reasons
  "AFN",
  ...
  "ZMW",
  "ZWL",
]

It would be great to be able to take advantage of --comment-prefix and I haven't found any other tools that can do this. Example:

_CURRENCY_CODES = [
  # BEGIN SORT
  "AED",
  # NOTE: Keep this for historical reasons
  "AFN",
  ...
  "ZMW",
  "ZWL",
  # END SORT
]

Then I would run it like:

omegasort --comment-prefix '#' --block-start 'BEGIN SORT' --block-end 'END SORT' currencies.py

Bonus points for allowing to specify a prefix or suffix that needs to appear on all lines but the last, e.g. for languages that don't allow a trailing , or constructs like type unions where you can't have a trailing |:

SomeType = (
  # BEGIN SORT
  A |
  B |
  C
  # END SORT
)

Note that it would be important to support either prefix or suffix to ensure conformance with code formatters, which might require this to be written as:

SomeType = (
  # BEGIN SORT
  A
  | B
  | C
  # END SORT
)
@autarch
Copy link
Member

autarch commented Feb 12, 2025

Thanks for the suggestions. I think it'd be good to make a separate issue for the two suggestions, since they're not really related.

I think the block thing wouldn't be too hard to do.

I wonder if instead of accepting an arbitrary start and end comment we could just add some sort of --delimited-blocks-only flag. There's not that many comment syntaxes out there, at least in popular use. So it would look for a regex sort of like this:

/\s*(?:\#|\/\/|\/*|;)\s*OMEGASORT-BEGIN(?:\s*\*\/)?/

And the same regex for OMEGASORT-END.

This would be simpler to implement and simpler to document.

Do you there's a lot of value in letting users specify there own block delimiter text?

@emosenkis
Copy link
Author

I don't think it needs to be fully custom but I'd prefer it to be generic rather than specifying "omegasort". I'd like it to be case insensitive so people can follow their own style preference.

I think it should take the comment prefix into account if that's going to be part of the regex.

These are all nice to haves, though. I'd be thrilled to have something that covers this use case no matter what the details are.

@emosenkis
Copy link
Author

Are you interested in receiving a PR for this? I'm not sure but I might have time to work on it.

@autarch
Copy link
Member

autarch commented Feb 13, 2025

Sure, that'd be great. But just for the blocks piece. I'd prefer to consider the other part separately.

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

No branches or pull requests

2 participants