From 81d4fe89106849bec45adff0c1241c11102f0e4a Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 12 Feb 2026 13:11:03 -0500 Subject: [PATCH 1/3] Add a pass of JuliaFormatter to handle line wrapping --- Project.toml | 2 +- src/ITensorFormatter.jl | 43 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index 6af40c6..3f087a4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ITensorFormatter" uuid = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd" -version = "0.2.1" +version = "0.2.2" authors = ["ITensor developers and contributors"] [workspace] diff --git a/src/ITensorFormatter.jl b/src/ITensorFormatter.jl index fd8af87..38be0b5 100644 --- a/src/ITensorFormatter.jl +++ b/src/ITensorFormatter.jl @@ -10,6 +10,38 @@ using JuliaFormatter: JuliaFormatter using JuliaSyntax: JuliaSyntax, @K_str, SyntaxNode, children, kind, parseall, span using Runic: Runic +# 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, + # Disable semantic transformations (Runic is authoritative) + always_use_return = false, + import_to_using = false, + pipe_to_function_call = false, + short_to_long_function_def = false, + long_to_short_function_def = false, + conditional_to_if = false, + short_circuit_to_if = false, + # Disable whitespace changes (Runic is authoritative) + whitespace_typedefs = false, + whitespace_ops_in_indices = false, + whitespace_in_kwargs = true, + # Disable annotation/structural changes + annotate_untyped_fields_with_any = false, + format_docstrings = false, + remove_extra_newlines = false, + indent_submodule = false, + separate_kwargs_with_semicolon = false, + # Line-wrapping-related options + trailing_comma = true, + trailing_zero = true, + join_lines_based_on_source = true, +) + function is_using_or_import(x) return kind(x) === K"using" || kind(x) === K"import" end @@ -92,9 +124,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) @@ -224,12 +254,15 @@ 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 - pushfirst!(inputfiles, "--inplace") - Runic.main(inputfiles) + # Pass 2: Line wrapping via JuliaFormatter + JuliaFormatter.format(inputfiles; JULIAFORMATTER_OPTIONS...) + # Pass 3: Canonicalize via Runic + Runic.main(["--inplace"; inputfiles]) return 0 end From 835a8702a906ec04cb6ef31a74623a48f097da29 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 12 Feb 2026 13:36:46 -0500 Subject: [PATCH 2/3] Make JuliaFormatter pass more opinionated --- src/ITensorFormatter.jl | 45 ++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/src/ITensorFormatter.jl b/src/ITensorFormatter.jl index 38be0b5..dfd21ff 100644 --- a/src/ITensorFormatter.jl +++ b/src/ITensorFormatter.jl @@ -18,24 +18,28 @@ const JULIAFORMATTER_OPTIONS = ( indent = 4, margin = 92, always_for_in = true, - # Disable semantic transformations (Runic is authoritative) - always_use_return = false, - import_to_using = false, - pipe_to_function_call = false, - short_to_long_function_def = false, - long_to_short_function_def = false, - conditional_to_if = false, + 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 = true, + conditional_to_if = true, short_circuit_to_if = false, - # Disable whitespace changes (Runic is authoritative) - whitespace_typedefs = false, - whitespace_ops_in_indices = false, + # Whitespace options consistent with Runic + whitespace_typedefs = true, + whitespace_ops_in_indices = true, whitespace_in_kwargs = true, - # Disable annotation/structural changes - annotate_untyped_fields_with_any = false, - format_docstrings = false, - remove_extra_newlines = false, - indent_submodule = false, - separate_kwargs_with_semicolon = false, + # 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, @@ -259,9 +263,14 @@ function main(argv) content = organize_import_blocks_file(inputfile) write(inputfile, content) end - # Pass 2: Line wrapping via JuliaFormatter + # Pass 2: Formatting via JuliaFormatter JuliaFormatter.format(inputfiles; JULIAFORMATTER_OPTIONS...) - # Pass 3: Canonicalize via Runic + # 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 + # Pass 4: Canonicalize via Runic Runic.main(["--inplace"; inputfiles]) return 0 end From 38caa16be964ab1f5ebc0d4509138fd8e2da52e6 Mon Sep 17 00:00:00 2001 From: mtfishman Date: Thu, 12 Feb 2026 13:46:26 -0500 Subject: [PATCH 3/3] Format --- docs/make.jl | 6 +++--- docs/make_index.jl | 2 +- docs/make_readme.jl | 2 +- src/ITensorFormatter.jl | 19 +++++++++---------- test/runtests.jl | 11 ++++++++--- test/test_aqua.jl | 2 +- 6 files changed, 23 insertions(+), 19 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index 8f67ff6..921916c 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -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") @@ -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, ) diff --git a/docs/make_index.jl b/docs/make_index.jl index ae4692b..e70dcf8 100644 --- a/docs/make_index.jl +++ b/docs/make_index.jl @@ -1,5 +1,5 @@ -using Literate: Literate using ITensorFormatter: ITensorFormatter +using Literate: Literate function ccq_logo(content) include_ccq_logo = """ diff --git a/docs/make_readme.jl b/docs/make_readme.jl index 610c6da..2959259 100644 --- a/docs/make_readme.jl +++ b/docs/make_readme.jl @@ -1,5 +1,5 @@ -using Literate: Literate using ITensorFormatter: ITensorFormatter +using Literate: Literate function ccq_logo(content) include_ccq_logo = """ diff --git a/src/ITensorFormatter.jl b/src/ITensorFormatter.jl index dfd21ff..cbc8ab5 100644 --- a/src/ITensorFormatter.jl +++ b/src/ITensorFormatter.jl @@ -24,7 +24,7 @@ const JULIAFORMATTER_OPTIONS = ( import_to_using = true, pipe_to_function_call = true, short_to_long_function_def = true, - long_to_short_function_def = true, + long_to_short_function_def = false, conditional_to_if = true, short_circuit_to_if = false, # Whitespace options consistent with Runic @@ -46,9 +46,7 @@ const JULIAFORMATTER_OPTIONS = ( join_lines_based_on_source = true, ) -function is_using_or_import(x) - return kind(x) === K"using" || kind(x) === K"import" -end +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) @@ -172,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 [] ...") 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, """ @@ -201,7 +199,7 @@ function print_help() --version Print ITensorFormatter and julia version information. - """ + """, ) return end @@ -223,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 diff --git a/test/runtests.jl b/test/runtests.jl index 2124a45..f97c7b1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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") diff --git a/test/test_aqua.jl b/test/test_aqua.jl index 4fee2fd..bfa635f 100644 --- a/test/test_aqua.jl +++ b/test/test_aqua.jl @@ -1,5 +1,5 @@ -using ITensorFormatter: ITensorFormatter using Aqua: Aqua +using ITensorFormatter: ITensorFormatter using Test: @testset @testset "Code quality (Aqua.jl)" begin