diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000..323237b --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1 @@ +style = "blue" diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 343704f..cb74c88 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -36,15 +36,22 @@ jobs: with: files: lcov.info + # https://documenter.juliadocs.org/stable/man/hosting/#GitHub-Actions docs: name: Documentation + # These permissions are needed to: + # - Use deploy the documentation: https://documenter.juliadocs.org/stable/man/hosting/#Permissions + permissions: + contents: write + pull-requests: read + statuses: write runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: julia-actions/setup-julia@v2 with: version: "1" - - name: ainstall dependencies + - name: Install dependencies shell: julia --color=yes --project=docs {0} run: | using Pkg diff --git a/.github/workflows/Format.yml b/.github/workflows/Format.yml new file mode 100644 index 0000000..64ae3bb --- /dev/null +++ b/.github/workflows/Format.yml @@ -0,0 +1,32 @@ +--- +# Ideally would use https://github.com/julia-actions/julia-format in the future +name: Format +on: + pull_request: + types: [opened, synchronize, reopened, ready_for_review] + paths: + - "**/*.jl" + - ".github/workflows/Format.yml" +jobs: + code-style: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: julia-actions/setup-julia@v2 + - uses: julia-actions/cache@v2 + - name: Install JuliaFormatter + shell: julia --color=yes {0} + run: | + import Pkg + Pkg.add(Pkg.PackageSpec(; name="JuliaFormatter", version="1")) + - name: Check formatting + shell: julia --color=yes {0} + run: | + using JuliaFormatter + format(".") || exit(1) + # Add formatting suggestions to non-draft PRs even if when "Check formatting" fails + - uses: reviewdog/action-suggester@a3026c6020837c23b61a79d12db223a00df19e6a # v1.19.0 + if: ${{ !cancelled() && github.event_name == 'pull_request' && github.event.pull_request.draft == false }} + with: + tool_name: JuliaFormatter + filter_mode: nofilter # Post results on all results and not just changed files: https://github.com/reviewdog/reviewdog#filter-mode diff --git a/docs/make.jl b/docs/make.jl index 73c163c..7ead8c5 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -5,10 +5,8 @@ const IS_CI = get(ENV, "CI", nothing) == "true" makedocs(; modules=[MultilineStrings], - format=Documenter.HTML(prettyurls=IS_CI), - pages=[ - "Home" => "index.md", - ], + format=Documenter.HTML(; prettyurls=IS_CI), + pages=["Home" => "index.md"], sitename="MultilineStrings.jl", checkdocs=:exports, linkcheck=true, diff --git a/src/MultilineStrings.jl b/src/MultilineStrings.jl index 0f46f2a..72301ef 100644 --- a/src/MultilineStrings.jl +++ b/src/MultilineStrings.jl @@ -20,7 +20,7 @@ to YAML multiline strings (also known as block scalars). or keep all newlines from the end (`:keep`) """ function multiline(str::AbstractString; style=DEFAULT_STYLE, chomp=DEFAULT_CHOMP) - multiline(str, style, chomp) + return multiline(str, style, chomp) end """ @@ -173,7 +173,12 @@ function _process_indicators(indicators::AbstractString) '\0', '\0' end - if style_char != '\0' && chomp_char != '\0' && isletter(style_char) ⊻ isletter(chomp_char) + mixed_indicators = ( + style_char != '\0' && + chomp_char != '\0' && + isletter(style_char) ⊻ isletter(chomp_char) + ) + if mixed_indicators throw(ArgumentError("Can't mix YAML style block indicators with letter indicators")) end diff --git a/src/indent.jl b/src/indent.jl index 01b9aea..fdb3bd6 100644 --- a/src/indent.jl +++ b/src/indent.jl @@ -17,8 +17,8 @@ See also `Base.unindent` and `Base.indentation`. function indent(str::AbstractString, n::Int) n == 0 && return str # Note: this loses the type of the original string - buf = IOBuffer(sizehint=sizeof(str)) - indent_str = ' ' ^ n + buf = IOBuffer(; sizehint=sizeof(str)) + indent_str = ' '^n line_start = firstindex(str) blank_line = true @@ -38,5 +38,5 @@ function indent(str::AbstractString, n::Int) !blank_line && print(buf, indent_str) print(buf, SubString(str, line_start, lastindex(str))) - String(take!(buf)) + return String(take!(buf)) end diff --git a/test/runtests.jl b/test/runtests.jl index c822d37..daa9980 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,7 +6,7 @@ using YAML: YAML function yaml_block(str, block_scalar) yaml = "example: $block_scalar\n$(indent(str, 2))" - YAML.load(yaml)["example"] + return YAML.load(yaml)["example"] end const TEST_STRINGS = [ @@ -51,9 +51,9 @@ end @test multiline(str, :literal, :clip) == expected_lc @test multiline(str, :literal, :strip) == expected_ls - @test multiline(str, style=:literal, chomp=:keep) == expected_lk - @test multiline(str, style=:literal, chomp=:clip) == expected_lc - @test multiline(str, style=:literal, chomp=:strip) == expected_ls + @test multiline(str; style=:literal, chomp=:keep) == expected_lk + @test multiline(str; style=:literal, chomp=:clip) == expected_lc + @test multiline(str; style=:literal, chomp=:strip) == expected_ls @test multiline(str, "lk") == expected_lk @test multiline(str, "lc") == expected_lc @@ -68,9 +68,9 @@ end @test multiline(str, :folded, :clip) == expected_fc @test multiline(str, :folded, :strip) == expected_fs - @test multiline(str, style=:folded, chomp=:keep) == expected_fk - @test multiline(str, style=:folded, chomp=:clip) == expected_fc - @test multiline(str, style=:folded, chomp=:strip) == expected_fs + @test multiline(str; style=:folded, chomp=:keep) == expected_fk + @test multiline(str; style=:folded, chomp=:clip) == expected_fc + @test multiline(str; style=:folded, chomp=:strip) == expected_fs @test multiline(str, "fk") == expected_fk @test multiline(str, "fc") == expected_fc @@ -81,8 +81,8 @@ end end @testset "default chomp" begin - @test multiline(str, style=:literal) == expected_ls - @test multiline(str, style=:folded) == expected_fs + @test multiline(str; style=:literal) == expected_ls + @test multiline(str; style=:folded) == expected_fs @test multiline(str, "l") == expected_ls @test multiline(str, "f") == expected_fs @@ -92,9 +92,9 @@ end end @testset "default style" begin - @test multiline(str, chomp=:keep) == expected_fk - @test multiline(str, chomp=:clip) == expected_fc - @test multiline(str, chomp=:strip) == expected_fs + @test multiline(str; chomp=:keep) == expected_fk + @test multiline(str; chomp=:clip) == expected_fc + @test multiline(str; chomp=:strip) == expected_fs @test multiline(str, "k") == expected_fk @test multiline(str, "c") == expected_fc @@ -131,7 +131,8 @@ end # The quoting in these examples can result in exceptions being raised during parsing # if handled incorrectly. @test interpolate("\"\\n\"") == "\"\\n\"" - @test interpolate("\$(join([\"a\", \"b\"], \", \"))") == Expr(:string, :(join(["a", "b"], ", "))) + @test interpolate("\$(join([\"a\", \"b\"], \", \"))") == + Expr(:string, :(join(["a", "b"], ", "))) end @testset "@m_str" begin