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

reorder_keys does not sort if there are comments #675

Open
tv42 opened this issue Sep 13, 2024 · 7 comments
Open

reorder_keys does not sort if there are comments #675

tv42 opened this issue Sep 13, 2024 · 7 comments

Comments

@tv42
Copy link

tv42 commented Sep 13, 2024

$  taplo --version
taplo 0.9.3

$ cat bug.toml
[pleasesort]
z = 2
# test
a = 1

$ cat bug.toml | taplo format --no-auto-config -o reorder_keys=true bug.toml -
[pleasesort]
z = 2
# test
a = 1

$ grep -v '^#' bug.toml | taplo format --no-auto-config -o reorder_keys=true -
[pleasesort]
a = 1
z = 2
@tv42
Copy link
Author

tv42 commented Sep 13, 2024

It seems a blank line will also prevent reorder_keys.

@panekj
Copy link
Collaborator

panekj commented Sep 13, 2024

This is correct behaviour, a comment might refer to specific line so it shouldn't be moved anywhere else

@tv42
Copy link
Author

tv42 commented Sep 13, 2024

You could choose to do what a lot of code refactoring does: assume a full-line comment describes an immediately-following non-comment line.

@panekj
Copy link
Collaborator

panekj commented Sep 13, 2024

But that is a wrong assumption because that comment doesn't have to refer to next line

for example:

[workspace.dependencies]
ahash              = { version = "0.8.3" }
anyhow             = { version = "1.0.53" }
arc-swap           = { version = "1.5.0" }
async-trait        = { version = "0.1.52" }
either             = { version = "1.6.1" }
futures            = { version = "0.3.5" }
# getrandom          = { version = "0.2", default-features = false }
glob               = { version = "0.3" }

In this case, the comment doesn't relate to next line or previous line, it's standalone

but in below case, comment is referring directly to next line:

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ansi_term      = { version = "0.12" }
async-ctrlc    = { version = "1.2.0", features = ["stream"], optional = true }
atty           = { version = "0.2.14" }
lsp-async-stub = { path = "../lsp-async-stub", features = ["tokio-tcp", "tokio-stdio"] }
# `prettydiff` is also a CLI that pulls in `clap` by default
prettydiff = { version = "0.6.1", default-features = false }
tokio      = { workspace = true, features = ["sync", "fs", "time", "io-std", "rt-multi-thread", "parking_lot"] }

(generally comments should be on the same line as the line they refer to)

@tv42
Copy link
Author

tv42 commented Sep 13, 2024

Yet, it is the assumption made all over the place in code formatters. If you want to write a comment that not about the following line, you put a blank line after it.

@tv42
Copy link
Author

tv42 commented Sep 13, 2024

Even in the world you are advocating for where comments cannot be tied to uncommented lines, commenting out a line silently breaking reordering sounds unpleasant :-(

@tv42
Copy link
Author

tv42 commented Sep 13, 2024

Here's another reason full line comments seem superior to me -- this would be hard to express legibly with an inline comment.

[[rule]]
include = ["deny.toml"]
# WAITING https://github.com/tamasfe/taplo/issues/608
#keys = ["graph.targets", "licenses.allow", "licenses.exceptions.allow"]
keys = ["graph", "licenses"]
formatting = { reorder_arrays = true }

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

No branches or pull requests

2 participants