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

Single-line "format: off" trailing comment #843

Open
kenahoo opened this issue Jun 11, 2024 · 3 comments
Open

Single-line "format: off" trailing comment #843

kenahoo opened this issue Jun 11, 2024 · 3 comments

Comments

@kenahoo
Copy link

kenahoo commented Jun 11, 2024

Has the idea of disabling formatting for a single line, using a special trailing comment, been discussed before? I searched the tracker but I couldn't find one.

My motivating example - a codebase had the following:

                my_variable = ismissing(row.VarValue) ? get_default_val(row.OtherVal, distance) : row.VarValue,

The formatter wanted to change it to this:

                my_variable = if ismissing(row.VarValue)
                    get_default_val(row.OtherVal, distance)
                else
                    row.VarValue
                end,

which I found less desirable. I wanted to stick something like #! nofmt or similar at the end of the line, but apparently no such directive exists, so I'm doing this:

                #! format: off
                my_variable = ismissing(row.VarValue) ? get_default_val(row.OtherVal, distance) : row.VarValue,
                #! format: on

But I don't love having 3 lines where 1 would do, and having 2/3 of the lines just there to avoid this issue. Would love to know your thoughts.

Thanks.

(BTW, I'm probably going to change this existing code to @coalesce row.VarValue get_default_val(row.OtherVal, distance), but I'm not sure everybody likes that and it's a little beside the main point.)

@domluna
Copy link
Owner

domluna commented Jun 11, 2024

I think this is a cool idea. #215 (comment) is interesting as well. The flags (indent and margin) would require making edits in a few files though since we would want that specific part to be formatted according to the new indent and margin.

I'm in the middle of moving parts to JuliaSyntax which makes things like this easier since the tree has full knowledge of comments, semicolons, string literals and other things.

@domluna
Copy link
Owner

domluna commented Jun 11, 2024

But I think my stance on indents/margin being variable is that it is a bad idea. But maybe the concept of disabling format for a single line could be done with an inline comment so:

                my_variable = ismissing(row.VarValue) ? get_default_val(row.OtherVal, distance) : row.VarValue, # format: off

which would be treated as the 3 line version. Pretty sure that requires only editing document.jl to capture it.

@kenahoo
Copy link
Author

kenahoo commented Jun 13, 2024

Cool. I'll watch this space. =)

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