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

Option to selectively disable sorting for an entry in an array or whole array #43

Closed
paddyroddy opened this issue Feb 15, 2023 · 6 comments · Fixed by #52
Closed

Option to selectively disable sorting for an entry in an array or whole array #43

paddyroddy opened this issue Feb 15, 2023 · 6 comments · Fixed by #52
Labels
enhancement New feature or request

Comments

@paddyroddy
Copy link

It would be great to have the ability to selectively disable sorting for an array (via a comment or a flag in the config). I think the sorting feature is great, but I can only solve this by disabling it for all files, like in #37.

[tool.tomlsort]
all = false
sort_inline_arrays = false

For example, with Coverage for the following to work src must appear before the .tox line, but sorting will change it.

[tool.coverage]
paths.source = [
    "src",
    ".tox*/*/lib/python*/site-packages",
]
@KyleKing
Copy link

KyleKing commented Feb 17, 2023

I like the idea of a comment and I'm planning on working on #42. Using a configuration item seems like it would be hard to specify what the rule applies to, while a comment is very clear and a well established pattern for Python tools (flake8, mypy, etc.).

What do you think of using # toml-sort: pin for the comment?

With the comment, you could write:

[tool.coverage]
paths.source = [
    "src",  # toml-sort: pin
    ".tox*/*/lib/python*/site-packages",
]

And in my case:

[tool.poetry.dependencies]
python = "^3.11"  # toml-sort: pin
antigravity = "^0.1"

The relative order of pins to eachother would be preserved and they would always be moved to the top:

[tool.coverage]
paths.source = [
    ".tox*/*/lib/python*/site-packages",
    "src",  # toml-sort: pin
	"another",  # toml-sort: pin
]

Where another is pinned after src, so that after sorting, this would be the end result:

[tool.coverage]
paths.source = [
    "src",  # toml-sort: pin
	"another",  # toml-sort: pin
    ".tox*/*/lib/python*/site-packages",
]

vs. without the comments:

[tool.coverage]
paths.source = [
    ".tox*/*/lib/python*/site-packages",
	"another",
    "src",
]

To selectively ignore an entire array, maybe we could add another type of comment code:

[tool.coverage]
paths.source = [  # toml-sort: no-sort
    ".tox*/*/lib/python*/site-packages",
    "src",
	"another",
]

Any opinions on this approach? Is this the desired behavior?

@jonathangreen
Copy link
Contributor

jonathangreen commented Feb 17, 2023

I've thought about doing something like this by allowing config overrides either per key or per table.

Something like this might work for config file syntax:

[tool.tomlsort.overrides]
tool.coverage.sort_inline_arrays = false

Where sort_inline_arrays could be any of the configuration options currently available.

I think that would provide a lot of flexibility when specific tables need different sort options.

@paddyroddy
Copy link
Author

I like the idea of a comment and I'm planning on working on #42. Using a configuration item seems like it would be hard to specify what the rule applies to, while a comment is very clear and a well established pattern for Python tools (flake8, mypy, etc.).

What do you think of using # toml-sort: pin for the comment?

With the comment, you could write:

[tool.coverage]
paths.source = [
    "src",  # toml-sort: pin
    ".tox*/*/lib/python*/site-packages",
]

And in my case:

[tool.poetry.dependencies]
python = "^3.11"  # toml-sort: pin
antigravity = "^0.1"

The relative order of pins to eachother would be preserved and they would always be moved to the top:

[tool.coverage]
paths.source = [
    ".tox*/*/lib/python*/site-packages",
    "src",  # toml-sort: pin
	"another",  # toml-sort: pin
]

Where another is pinned after src, so that after sorting, this would be the end result:

[tool.coverage]
paths.source = [
    "src",  # toml-sort: pin
	"another",  # toml-sort: pin
    ".tox*/*/lib/python*/site-packages",
]

vs. without the comments:

[tool.coverage]
paths.source = [
    ".tox*/*/lib/python*/site-packages",
	"another",
    "src",
]

To selectively ignore an entire array, maybe we could add another type of comment code:

[tool.coverage]
paths.source = [  # toml-sort: no-sort
    ".tox*/*/lib/python*/site-packages",
    "src",
	"another",
]

Any opinions on this approach? Is this the desired behavior?

This looks like a great solution!

@paddyroddy
Copy link
Author

paddyroddy commented Feb 17, 2023

I've thought about doing something like this by allowing config overrides either per key or per table.

Something like this might work for config file syntax:

[tool.tomlsort.overrides]
tool.coverage.sort_inline_arrays = false

Where sort_inline_arrays could be any of the configuration options currently available.

I think that would provide a lot of flexibility when specific tables need different sort options.

That would be really cool, but I sense a bit harder to implement than the comment option? I guess in my case I'd really be looking for tool.coverage.paths.source.sort_inline_arrays = false but that might be even harder to implement.

@KyleKing
Copy link

KyleKing commented Feb 17, 2023

I almost think we might want both options:

  • Config: one source of truth for how files should be sorted, but requires a configuration file because specifying by CLI would be cumbersome
  • Comments: easy to specify per file, but repetitive, especially for defaults like tool.poetry.dependencies.python and will stop working if the array is moved inline

Maybe the new logic in toml-sort could:

  1. Scan the file for comments
  2. Merge the comment overrides with the config overrides
  3. Sort
  4. Apply the overrides

Maybe we could borrow the concept of known_first_party from isort with sort_first and expand the sort_inline_arrays to be a more general no_sort. If so, the configuration file could look something like:

[tool.tomlsort.overrides]
no_sort = ["tool.coverage.paths.source"]

[tool.tomlsort.overrides.sort_first]
tool.poetry.dependencies = ["python"]

The benefit of a more general no_sort is that it could also apply to tables. For example, if you didn't want the poetry dependencies to be sorted at all (maybe you already use poetry-sort plugin)

[tool.tomlsort.overrides]
no_sort = ["tool.poetry.dependencies"]

And with sort_first, you could also specify multiple ordered values:

[tool.tomlsort.sort_first]
tool.coverage.paths.source = ["src", "another"]

@paddyroddy
Copy link
Author

My particular use case can be solved by:

[tool.tomlsort]
all = true
overrides."tool.coverage.paths.source".inline_arrays = false

😁 cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants