-
-
Notifications
You must be signed in to change notification settings - Fork 42
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
/* comments */ should not be reformatted #179
Comments
In the wild, you can see this all over Nixpkgs test such as https://github.com/NixOS/nixpkgs/blob/master/nixos/tests/switch-test.nix#L8 |
Fwiw, nvim-treesitter now supports that: nvim-treesitter/nvim-treesitter#6418 (comment) |
or fenced vim hints.
That's not released, yet. |
We discussed this in the team meeting today:
Conclusion: We generally think that it would be fine to preserve |
This issue has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/formatting-team-meeting-2024-04-16/43533/1 |
Looks like this is the relevant rendering code: Lines 77 to 91 in c67a7b6
And here is the corresponding parsing code: Lines 83 to 88 in c67a7b6
Maybe there should be an additional Having never touched haskell, I doubt I'm up to the task. Hopefully pointing to the relevant code will inspire someone more knowledgeable to submit a PR. |
The issue is less with the parsing, that's a bit fiddly but no more than a chore. The big open question is, what to do in the renderer with such comments when they are followed by significant amounts of code which overflow the line length limit. The rendering engine currently ignores all line length calculations involving comments, however this strongly depends on the assumption that comment tokens are always strictly followed by a line break. |
Thanks for clarifying!
Ideally, inserting a line break before the comment would be enough to not overflow, but that's the best case scenario... I think it's fair to say, that if someone has written an inline block comment on the same line as a long string (for example), it was intentional. Maybe in that scenario it is just accepted that the line will overflow? If the long content to the right of the inline block comment is not a string, then I think it is ok to reformat that as usual, such that it is wrapped over multiple lines. In summary, yes - you're right; this is more complex that it first appears!
That sounds like a bigger issue. Perhaps "inline" comments can be a distinct type from other comments and therefore included in line length calculations? |
So one thing that one could do would be to special case these comments in the parser but only if they are followed by |
While it'd be nice to have support for normal strings too, I agree that is a reasonable approach that'd work for most usage. I do sometimes do things like this in my config. {
# This snippet is so short it feels strange to use an indented string:
someLuaOption = /* lua */ "function() print('hello, world!') end";
} But some support is better than no support, and it can always be iterated on in the future. |
Just to propose one additional use case for {
/* <-- Toggling a # line comment at the beginning of this line toggles the whole block
services.foo = {
enable = true;
bar = "baz";
};
# */
} For this kind of case I think it would be valuable to avoid splitting the final {
# /*
<-- Toggling a # line comment at the beginning of this line toggles the whole block
services.foo = {
enable = true;
bar = "baz";
};
#
*/
} I use this in my configs to quickly toggle on/off large sections of the config. I'm not sure how popular a practice it is in general, but it seems to be moderately common from a cursory search: |
I don't think this issue is proposing that block comments not be formatted; instead this issue is about allowing short and/or inline Given nixfmt is deterministic, it'd probably have to be based on how long the comment content is and whether or not there is trailing code after the comment? Your issue about block comments being formatted messing up your workflow should probably be tracked in a separate ticket and may be best implemented as a configurable cli flag. |
@ian-h-chamberlain commenting out code blocks with /* has the advantage that it is low on diff noise, however it has the big disadvantage that it breaks as soon as that block contains another nested /* comment. |
I opened #225 to track separately, maybe it would also make sense to update the title of this issue to be more specifically about short/inline |
Btw here's the code that handles the conversion from Lines 131 to 169 in e819b2d
|
This issue has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/enforcing-nix-formatting-in-nixpkgs/49506/8 |
For reference, we had a discussion about this on Matrix |
I want to work on this issues please assign me this issue. |
Does that mean we just need to drop that code and we are good to go? |
Description
/* */
style comments should not be reformatted. They have additional use cases such as tree-sitter picking up the the syntax to highlight before scripts inside multiline strings.Small example input
Expected output
(now tree-sitter in my editor highlights this string to Lua which really helps me read/write code in this block now that it’s not just syntax highlighted as a string)
Actual output
The text was updated successfully, but these errors were encountered: