Skip to content

Commit 9cf7cb2

Browse files
authored
Add Julia formatting workflow (#15)
* Fix CI typo * Set permissions for documentation * Enable self-test * Fix formatting * Set filter mode
1 parent 01bd778 commit 9cf7cb2

File tree

7 files changed

+67
-23
lines changed

7 files changed

+67
-23
lines changed

.JuliaFormatter.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style = "blue"

.github/workflows/CI.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,22 @@ jobs:
3636
with:
3737
files: lcov.info
3838

39+
# https://documenter.juliadocs.org/stable/man/hosting/#GitHub-Actions
3940
docs:
4041
name: Documentation
42+
# These permissions are needed to:
43+
# - Use deploy the documentation: https://documenter.juliadocs.org/stable/man/hosting/#Permissions
44+
permissions:
45+
contents: write
46+
pull-requests: read
47+
statuses: write
4148
runs-on: ubuntu-latest
4249
steps:
4350
- uses: actions/checkout@v4
4451
- uses: julia-actions/setup-julia@v2
4552
with:
4653
version: "1"
47-
- name: ainstall dependencies
54+
- name: Install dependencies
4855
shell: julia --color=yes --project=docs {0}
4956
run: |
5057
using Pkg

.github/workflows/Format.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
# Ideally would use https://github.com/julia-actions/julia-format in the future
3+
name: Format
4+
on:
5+
pull_request:
6+
types: [opened, synchronize, reopened, ready_for_review]
7+
paths:
8+
- "**/*.jl"
9+
- ".github/workflows/Format.yml"
10+
jobs:
11+
code-style:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: julia-actions/setup-julia@v2
16+
- uses: julia-actions/cache@v2
17+
- name: Install JuliaFormatter
18+
shell: julia --color=yes {0}
19+
run: |
20+
import Pkg
21+
Pkg.add(Pkg.PackageSpec(; name="JuliaFormatter", version="1"))
22+
- name: Check formatting
23+
shell: julia --color=yes {0}
24+
run: |
25+
using JuliaFormatter
26+
format(".") || exit(1)
27+
# Add formatting suggestions to non-draft PRs even if when "Check formatting" fails
28+
- uses: reviewdog/action-suggester@a3026c6020837c23b61a79d12db223a00df19e6a # v1.19.0
29+
if: ${{ !cancelled() && github.event_name == 'pull_request' && github.event.pull_request.draft == false }}
30+
with:
31+
tool_name: JuliaFormatter
32+
filter_mode: nofilter # Post results on all results and not just changed files: https://github.com/reviewdog/reviewdog#filter-mode

docs/make.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ const IS_CI = get(ENV, "CI", nothing) == "true"
55

66
makedocs(;
77
modules=[MultilineStrings],
8-
format=Documenter.HTML(prettyurls=IS_CI),
9-
pages=[
10-
"Home" => "index.md",
11-
],
8+
format=Documenter.HTML(; prettyurls=IS_CI),
9+
pages=["Home" => "index.md"],
1210
sitename="MultilineStrings.jl",
1311
checkdocs=:exports,
1412
linkcheck=true,

src/MultilineStrings.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ to YAML multiline strings (also known as block scalars).
2020
or keep all newlines from the end (`:keep`)
2121
"""
2222
function multiline(str::AbstractString; style=DEFAULT_STYLE, chomp=DEFAULT_CHOMP)
23-
multiline(str, style, chomp)
23+
return multiline(str, style, chomp)
2424
end
2525

2626
"""
@@ -173,7 +173,12 @@ function _process_indicators(indicators::AbstractString)
173173
'\0', '\0'
174174
end
175175

176-
if style_char != '\0' && chomp_char != '\0' && isletter(style_char) isletter(chomp_char)
176+
mixed_indicators = (
177+
style_char != '\0' &&
178+
chomp_char != '\0' &&
179+
isletter(style_char) isletter(chomp_char)
180+
)
181+
if mixed_indicators
177182
throw(ArgumentError("Can't mix YAML style block indicators with letter indicators"))
178183
end
179184

src/indent.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ See also `Base.unindent` and `Base.indentation`.
1717
function indent(str::AbstractString, n::Int)
1818
n == 0 && return str
1919
# Note: this loses the type of the original string
20-
buf = IOBuffer(sizehint=sizeof(str))
21-
indent_str = ' ' ^ n
20+
buf = IOBuffer(; sizehint=sizeof(str))
21+
indent_str = ' '^n
2222

2323
line_start = firstindex(str)
2424
blank_line = true
@@ -38,5 +38,5 @@ function indent(str::AbstractString, n::Int)
3838
!blank_line && print(buf, indent_str)
3939
print(buf, SubString(str, line_start, lastindex(str)))
4040

41-
String(take!(buf))
41+
return String(take!(buf))
4242
end

test/runtests.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using YAML: YAML
66

77
function yaml_block(str, block_scalar)
88
yaml = "example: $block_scalar\n$(indent(str, 2))"
9-
YAML.load(yaml)["example"]
9+
return YAML.load(yaml)["example"]
1010
end
1111

1212
const TEST_STRINGS = [
@@ -51,9 +51,9 @@ end
5151
@test multiline(str, :literal, :clip) == expected_lc
5252
@test multiline(str, :literal, :strip) == expected_ls
5353

54-
@test multiline(str, style=:literal, chomp=:keep) == expected_lk
55-
@test multiline(str, style=:literal, chomp=:clip) == expected_lc
56-
@test multiline(str, style=:literal, chomp=:strip) == expected_ls
54+
@test multiline(str; style=:literal, chomp=:keep) == expected_lk
55+
@test multiline(str; style=:literal, chomp=:clip) == expected_lc
56+
@test multiline(str; style=:literal, chomp=:strip) == expected_ls
5757

5858
@test multiline(str, "lk") == expected_lk
5959
@test multiline(str, "lc") == expected_lc
@@ -68,9 +68,9 @@ end
6868
@test multiline(str, :folded, :clip) == expected_fc
6969
@test multiline(str, :folded, :strip) == expected_fs
7070

71-
@test multiline(str, style=:folded, chomp=:keep) == expected_fk
72-
@test multiline(str, style=:folded, chomp=:clip) == expected_fc
73-
@test multiline(str, style=:folded, chomp=:strip) == expected_fs
71+
@test multiline(str; style=:folded, chomp=:keep) == expected_fk
72+
@test multiline(str; style=:folded, chomp=:clip) == expected_fc
73+
@test multiline(str; style=:folded, chomp=:strip) == expected_fs
7474

7575
@test multiline(str, "fk") == expected_fk
7676
@test multiline(str, "fc") == expected_fc
@@ -81,8 +81,8 @@ end
8181
end
8282

8383
@testset "default chomp" begin
84-
@test multiline(str, style=:literal) == expected_ls
85-
@test multiline(str, style=:folded) == expected_fs
84+
@test multiline(str; style=:literal) == expected_ls
85+
@test multiline(str; style=:folded) == expected_fs
8686

8787
@test multiline(str, "l") == expected_ls
8888
@test multiline(str, "f") == expected_fs
@@ -92,9 +92,9 @@ end
9292
end
9393

9494
@testset "default style" begin
95-
@test multiline(str, chomp=:keep) == expected_fk
96-
@test multiline(str, chomp=:clip) == expected_fc
97-
@test multiline(str, chomp=:strip) == expected_fs
95+
@test multiline(str; chomp=:keep) == expected_fk
96+
@test multiline(str; chomp=:clip) == expected_fc
97+
@test multiline(str; chomp=:strip) == expected_fs
9898

9999
@test multiline(str, "k") == expected_fk
100100
@test multiline(str, "c") == expected_fc
@@ -131,7 +131,8 @@ end
131131
# The quoting in these examples can result in exceptions being raised during parsing
132132
# if handled incorrectly.
133133
@test interpolate("\"\\n\"") == "\"\\n\""
134-
@test interpolate("\$(join([\"a\", \"b\"], \", \"))") == Expr(:string, :(join(["a", "b"], ", ")))
134+
@test interpolate("\$(join([\"a\", \"b\"], \", \"))") ==
135+
Expr(:string, :(join(["a", "b"], ", ")))
135136
end
136137

137138
@testset "@m_str" begin

0 commit comments

Comments
 (0)