Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ITensorFormatter"
uuid = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd"
version = "0.2.1"
version = "0.2.2"
authors = ["ITensor developers <support@itensor.org> and contributors"]

[workspace]
Expand Down
6 changes: 3 additions & 3 deletions docs/make.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using ITensorFormatter: ITensorFormatter
using Documenter: Documenter, DocMeta, deploydocs, makedocs
using ITensorFormatter: ITensorFormatter

DocMeta.setdocmeta!(
ITensorFormatter, :DocTestSetup, :(using ITensorFormatter); recursive = true
ITensorFormatter, :DocTestSetup, :(using ITensorFormatter); recursive = true,
)

include("make_index.jl")
Expand All @@ -20,5 +20,5 @@ makedocs(;
)

deploydocs(;
repo = "github.com/ITensor/ITensorFormatter.jl", devbranch = "main", push_preview = true
repo = "github.com/ITensor/ITensorFormatter.jl", devbranch = "main", push_preview = true,
)
2 changes: 1 addition & 1 deletion docs/make_index.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Literate: Literate
using ITensorFormatter: ITensorFormatter
using Literate: Literate

function ccq_logo(content)
include_ccq_logo = """
Expand Down
2 changes: 1 addition & 1 deletion docs/make_readme.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Literate: Literate
using ITensorFormatter: ITensorFormatter
using Literate: Literate

function ccq_logo(content)
include_ccq_logo = """
Expand Down
69 changes: 55 additions & 14 deletions src/ITensorFormatter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,43 @@ using JuliaFormatter: JuliaFormatter
using JuliaSyntax: JuliaSyntax, @K_str, SyntaxNode, children, kind, parseall, span
using Runic: Runic

function is_using_or_import(x)
return kind(x) === K"using" || kind(x) === K"import"
end
# JuliaFormatter options chosen to be compatible with Runic.
# JuliaFormatter handles line wrapping (which Runic doesn't do),
# then Runic runs last to canonicalize everything else.
const JULIAFORMATTER_OPTIONS = (
style = JuliaFormatter.DefaultStyle(),
indent = 4,
margin = 92,
always_for_in = true,
for_in_replacement = "in",
# Semantic transformations consistent with Runic
always_use_return = true,
import_to_using = true,
pipe_to_function_call = true,
short_to_long_function_def = true,
long_to_short_function_def = false,
conditional_to_if = true,
short_circuit_to_if = false,
# Whitespace options consistent with Runic
whitespace_typedefs = true,
whitespace_ops_in_indices = true,
whitespace_in_kwargs = true,
# Annotation/structural changes
annotate_untyped_fields_with_any = true,
format_docstrings = true,
remove_extra_newlines = true,
indent_submodule = true,
separate_kwargs_with_semicolon = true,
surround_whereop_typeparameters = true,
disallow_single_arg_nesting = true,
normalize_line_endings = "unix",
# Line-wrapping-related options
trailing_comma = true,
trailing_zero = true,
join_lines_based_on_source = true,
)

is_using_or_import(x) = kind(x) === K"using" || kind(x) === K"import"

function find_using_or_import(x)
if is_using_or_import(x)
Expand Down Expand Up @@ -92,9 +126,7 @@ function organize_import_block(siblings, node_text)
join(io, sort!(import_lines), "\n")
length(import_lines) > 0 && length(using_lines) > 0 && print(io, "\n")
join(io, sort!(using_lines), "\n")
str_to_fmt = String(take!(io))

return JuliaFormatter.format_text(str_to_fmt; join_lines_based_on_source = true)
return String(take!(io))
end

function organize_import_blocks(input)
Expand Down Expand Up @@ -138,23 +170,23 @@ const ITENSORFORMATTER_VERSION = pkgversion(@__MODULE__)
# Print a typical cli program help message
function print_help()
io = stdout
printstyled(io, "NAME", bold = true)
printstyled(io, "NAME"; bold = true)
println(io)
println(io, " ITensorFormatter.main - format Julia source code")
println(io)
printstyled(io, "SYNOPSIS", bold = true)
printstyled(io, "SYNOPSIS"; bold = true)
println(io)
println(io, " julia -m ITensorFormatter [<options>] <path>...")
println(io)
printstyled(io, "DESCRIPTION", bold = true)
printstyled(io, "DESCRIPTION"; bold = true)
println(io)
println(
io, """
`ITensorFormatter.main` (typically invoked as `julia -m ITensorFormatter`)
formats Julia source code using the ITensorFormatter.jl formatter.
"""
""",
)
printstyled(io, "OPTIONS", bold = true)
printstyled(io, "OPTIONS"; bold = true)
println(io)
println(
io, """
Expand All @@ -167,7 +199,7 @@ function print_help()

--version
Print ITensorFormatter and julia version information.
"""
""",
)
return
end
Expand All @@ -189,6 +221,7 @@ organizes using/import statements by merging adjacent blocks, sorting modules an
and line-wrapping. Accepts file paths and directories as arguments.

# Examples

```julia-repl
julia> using ITensorFormatter: ITensorFormatter

Expand Down Expand Up @@ -224,12 +257,20 @@ function main(argv)
end
end
isempty(inputfiles) && return 0
# Pass 1: Organize import/using blocks
for inputfile in inputfiles
content = organize_import_blocks_file(inputfile)
write(inputfile, content)
end
# Pass 2: Formatting via JuliaFormatter
JuliaFormatter.format(inputfiles; JULIAFORMATTER_OPTIONS...)
# Pass 3: Re-organize imports (fix up any changes from JuliaFormatter, e.g. import_to_using)
for inputfile in inputfiles
content = organize_import_blocks_file(inputfile)
write(inputfile, content)
end
pushfirst!(inputfiles, "--inplace")
Runic.main(inputfiles)
# Pass 4: Canonicalize via Runic
Runic.main(["--inplace"; inputfiles])
return 0
end

Expand Down
11 changes: 8 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ const GROUP = uppercase(
end,
)

"match files of the form `test_*.jl`, but exclude `*setup*.jl`"
"""
match files of the form `test_*.jl`, but exclude `*setup*.jl`
"""
function istestfile(path)
fn = basename(path)
return endswith(fn, ".jl") && startswith(basename(fn), "test_") && !contains(fn, "setup")
return endswith(fn, ".jl") && startswith(basename(fn), "test_") &&
!contains(fn, "setup")
end
"match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`"
"""
match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`
"""
function isexamplefile(path)
fn = basename(path)
return endswith(fn, ".jl") && !endswith(fn, "_notest.jl") && !contains(fn, "setup")
Expand Down
2 changes: 1 addition & 1 deletion test/test_aqua.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ITensorFormatter: ITensorFormatter
using Aqua: Aqua
using ITensorFormatter: ITensorFormatter
using Test: @testset

@testset "Code quality (Aqua.jl)" begin
Expand Down
Loading