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

Relax toggle comments to allow more "pragmas" #88

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,16 @@ Runic.

The source comments `# runic: off` and `# runic: on` will toggle the formatting off and on,
respectively. The comments must be on their own line, they must be on the same level in the
syntax tree, and they must come in pairs.
syntax tree, and they must come in pairs. An exception to the pairing rule is made at top
level where a `# runic: off` comment will disable formatting for the remainder of the file.
This is so that a full file can be excluded from formatting without having to add a
`# runic: on` comment at the end of the file.

> [!NOTE]
> Note that it is enough that a comment contain the substring `# runic: off` or
> `# runic: on` so that they can be combined with other "pragmas" such as e.g.
> [Literate.jl line filters](https://fredrikekre.github.io/Literate.jl/v2/fileformat/#Filtering-lines)
> like `#src`.

> [!NOTE]
> For compatibility with [JuliaFormatter](/~https://github.com/domluna/JuliaFormatter.jl) the
Expand All @@ -339,11 +348,6 @@ function foo()
end
```

An exception to the pairing rule is made at top level where a `# runic: off` comment will
disable formatting for the remainder of the file. This is so that a full file can be
excluded from formatting without having to add a `# runic: on` comment at the end of the
file.

### Line width limit

No. Use your <kbd>Enter</kbd> key or refactor your code.
Expand Down
2 changes: 1 addition & 1 deletion src/Runic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function check_format_toggle(ctx::Context, node::Node, kid::Node, i::Int)::Union
# Check if the kid is a comment
kind(kid) === K"Comment" || return nothing
# Check the comment content
reg = r"^#(!)? (runic|format): (on|off)$"
reg = r"#(!)? (runic|format): (on|off)"
str = String(read_bytes(ctx, kid))
offmatch = match(reg, str)
offmatch === nothing && return nothing
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,11 @@ end
return
end
"""
# Extra stuff in the toggle comments
@test format_string("1+1\n$off #src\n1+1\n$on #src\n1+1") ==
"1 + 1\n$off #src\n1+1\n$on #src\n1 + 1"
@test format_string("1+1\n#src $off\n1+1\n#src $on\n1+1") ==
"1 + 1\n#src $off\n1+1\n#src $on\n1 + 1"
end
end

Expand Down
Loading