Skip to content

Commit

Permalink
Relax toggle comments to allow more "pragmas" (#88)
Browse files Browse the repository at this point in the history
This patch relaxes the toggle comments `# runic: (off|on)` such that the
comment may contain more than just the toggle comment. This is useful
when combining with other "pragmas" such as e.g. Literate.jl line
filters. For example, the following now also toggles formatting:

```julia
\# runic: off     #src
not = formatted   #src
\# runic: on      #src
```
  • Loading branch information
fredrikekre authored Oct 31, 2024
1 parent 45c471b commit e395af1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
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

0 comments on commit e395af1

Please sign in to comment.