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

Ignore indentation effect of a specific block #889

Open
MilesCranmer opened this issue Dec 11, 2024 · 9 comments
Open

Ignore indentation effect of a specific block #889

MilesCranmer opened this issue Dec 11, 2024 · 9 comments

Comments

@MilesCranmer
Copy link
Contributor

Sometimes in my code I would like to use a macro on a block of code without indenting it. For example, I want to go from

const CONST = 1
function foo(x)
    x + CONST
end

to this:

@mymacro begin

const CONST = 1
function foo(x)
    x + CONST
end

end

This could be something like DispatchDoctor.jl @stable or Base.@assume_effects, which can operate on entire code blocks.

Now, if I have the formatter turned on, this will turn into

@mymacro begin

    const CONST = 1
    function foo(x)
        x + CONST
    end

end

This is of course valid, but I would like to disable it for the code block. I want the @mymacro begin to just sit at the top level without affecting the rest of the code. (It's kind of like how module A ... end doesn't indent everything in the contents.)

The only workaround at the moment is to use #! format: off for the entire code block. This works but it prevents me from using JuliaFormatter.jl for internal code.

Thus, @domluna, I was wondering if there could be a special command to ignore the increased indentation for a single block? Happy to help add it if you can point me to where to edit.

@MilesCranmer
Copy link
Contributor Author

I should also mention I tried the first thing I thought of:

#! format: off
@mymacro begin
#! format: on

...

#! format: off
end
#! format: on

but this sadly did not work. (This might be the right API for this, because at the time it felt the most intuitive to me.)

@domluna
Copy link
Owner

domluna commented Dec 12, 2024

I should also mention I tried the first thing I thought of:

#! format: off
@mymacro begin
#! format: on

...

#! format: off
end
#! format: on

but this sadly did not work. (This might be the right API for this, because at the time it felt the most intuitive to me.)

this should work, not sure why it doesn't.

essentially all this happens here:

https://github.com/domluna/JuliaFormatter.jl/blob/master/src/print.jl#L1-L79

@MilesCranmer
Copy link
Contributor Author

MilesCranmer commented Dec 12, 2024

Here's a MWE:

julia> using JuliaFormatter

julia> format_text("\n#! format: off\n@mymacro begin\n#! format: on\nfoo() = nothing\n#! format: off\nend\n#! format: on")
"\n#! format: off\n@mymacro begin\n#! format: on\n    foo() = nothing\n#! format: off\nend\n#! format: on"

You can see the extra spaces added before the foo()

(inline version of this:

#! format: off
@mymacro begin
#! format: on
foo() = nothing
#! format: off
end
#! format: on

@domluna
Copy link
Owner

domluna commented Dec 12, 2024

unless I'm missing something this part

#! format: on
foo() = nothing
#! format: off

will be formatted and it is. Hmmm perhaps the confusion is the scope of the format on/off rule. It's based on the lines and that's it. so the initial format off in this case would just ignore doing anything to

@mymacro begin

@MilesCranmer
Copy link
Contributor Author

Oh I think I got confused. So, in this case, how could I turn off indentation for a specific code block, without disabling formatting within said block?

@domluna
Copy link
Owner

domluna commented Dec 13, 2024 via email

@MilesCranmer
Copy link
Contributor Author

I see, thanks. Could I request it as a new feature? I want to be able to remove a single indentation for a @mymacro begin block.

@domluna
Copy link
Owner

domluna commented Dec 13, 2024

I'd be fine with it as a new feature but I probably won't get around to implementing this any time soon.

@MilesCranmer
Copy link
Contributor Author

In case I or someone else reading this thread in the future wants to try adding it, where should we start?

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