Skip to content

Commit ff5d347

Browse files
authored
Add a pass of JuliaFormatter to handle line wrapping and other options (#3)
1 parent 8a6804c commit ff5d347

7 files changed

Lines changed: 70 additions & 24 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ITensorFormatter"
22
uuid = "b6bf39f1-c9d3-4bad-aad8-593d802f65fd"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
authors = ["ITensor developers <support@itensor.org> and contributors"]
55

66
[workspace]

docs/make.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using ITensorFormatter: ITensorFormatter
21
using Documenter: Documenter, DocMeta, deploydocs, makedocs
2+
using ITensorFormatter: ITensorFormatter
33

44
DocMeta.setdocmeta!(
5-
ITensorFormatter, :DocTestSetup, :(using ITensorFormatter); recursive = true
5+
ITensorFormatter, :DocTestSetup, :(using ITensorFormatter); recursive = true,
66
)
77

88
include("make_index.jl")
@@ -20,5 +20,5 @@ makedocs(;
2020
)
2121

2222
deploydocs(;
23-
repo = "github.com/ITensor/ITensorFormatter.jl", devbranch = "main", push_preview = true
23+
repo = "github.com/ITensor/ITensorFormatter.jl", devbranch = "main", push_preview = true,
2424
)

docs/make_index.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Literate: Literate
21
using ITensorFormatter: ITensorFormatter
2+
using Literate: Literate
33

44
function ccq_logo(content)
55
include_ccq_logo = """

docs/make_readme.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using Literate: Literate
21
using ITensorFormatter: ITensorFormatter
2+
using Literate: Literate
33

44
function ccq_logo(content)
55
include_ccq_logo = """

src/ITensorFormatter.jl

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,43 @@ using JuliaFormatter: JuliaFormatter
1010
using JuliaSyntax: JuliaSyntax, @K_str, SyntaxNode, children, kind, parseall, span
1111
using Runic: Runic
1212

13-
function is_using_or_import(x)
14-
return kind(x) === K"using" || kind(x) === K"import"
15-
end
13+
# JuliaFormatter options chosen to be compatible with Runic.
14+
# JuliaFormatter handles line wrapping (which Runic doesn't do),
15+
# then Runic runs last to canonicalize everything else.
16+
const JULIAFORMATTER_OPTIONS = (
17+
style = JuliaFormatter.DefaultStyle(),
18+
indent = 4,
19+
margin = 92,
20+
always_for_in = true,
21+
for_in_replacement = "in",
22+
# Semantic transformations consistent with Runic
23+
always_use_return = true,
24+
import_to_using = true,
25+
pipe_to_function_call = true,
26+
short_to_long_function_def = true,
27+
long_to_short_function_def = false,
28+
conditional_to_if = true,
29+
short_circuit_to_if = false,
30+
# Whitespace options consistent with Runic
31+
whitespace_typedefs = true,
32+
whitespace_ops_in_indices = true,
33+
whitespace_in_kwargs = true,
34+
# Annotation/structural changes
35+
annotate_untyped_fields_with_any = true,
36+
format_docstrings = true,
37+
remove_extra_newlines = true,
38+
indent_submodule = true,
39+
separate_kwargs_with_semicolon = true,
40+
surround_whereop_typeparameters = true,
41+
disallow_single_arg_nesting = true,
42+
normalize_line_endings = "unix",
43+
# Line-wrapping-related options
44+
trailing_comma = true,
45+
trailing_zero = true,
46+
join_lines_based_on_source = true,
47+
)
48+
49+
is_using_or_import(x) = kind(x) === K"using" || kind(x) === K"import"
1650

1751
function find_using_or_import(x)
1852
if is_using_or_import(x)
@@ -92,9 +126,7 @@ function organize_import_block(siblings, node_text)
92126
join(io, sort!(import_lines), "\n")
93127
length(import_lines) > 0 && length(using_lines) > 0 && print(io, "\n")
94128
join(io, sort!(using_lines), "\n")
95-
str_to_fmt = String(take!(io))
96-
97-
return JuliaFormatter.format_text(str_to_fmt; join_lines_based_on_source = true)
129+
return String(take!(io))
98130
end
99131

100132
function organize_import_blocks(input)
@@ -138,23 +170,23 @@ const ITENSORFORMATTER_VERSION = pkgversion(@__MODULE__)
138170
# Print a typical cli program help message
139171
function print_help()
140172
io = stdout
141-
printstyled(io, "NAME", bold = true)
173+
printstyled(io, "NAME"; bold = true)
142174
println(io)
143175
println(io, " ITensorFormatter.main - format Julia source code")
144176
println(io)
145-
printstyled(io, "SYNOPSIS", bold = true)
177+
printstyled(io, "SYNOPSIS"; bold = true)
146178
println(io)
147179
println(io, " julia -m ITensorFormatter [<options>] <path>...")
148180
println(io)
149-
printstyled(io, "DESCRIPTION", bold = true)
181+
printstyled(io, "DESCRIPTION"; bold = true)
150182
println(io)
151183
println(
152184
io, """
153185
`ITensorFormatter.main` (typically invoked as `julia -m ITensorFormatter`)
154186
formats Julia source code using the ITensorFormatter.jl formatter.
155-
"""
187+
""",
156188
)
157-
printstyled(io, "OPTIONS", bold = true)
189+
printstyled(io, "OPTIONS"; bold = true)
158190
println(io)
159191
println(
160192
io, """
@@ -167,7 +199,7 @@ function print_help()
167199
168200
--version
169201
Print ITensorFormatter and julia version information.
170-
"""
202+
""",
171203
)
172204
return
173205
end
@@ -189,6 +221,7 @@ organizes using/import statements by merging adjacent blocks, sorting modules an
189221
and line-wrapping. Accepts file paths and directories as arguments.
190222
191223
# Examples
224+
192225
```julia-repl
193226
julia> using ITensorFormatter: ITensorFormatter
194227
@@ -224,12 +257,20 @@ function main(argv)
224257
end
225258
end
226259
isempty(inputfiles) && return 0
260+
# Pass 1: Organize import/using blocks
261+
for inputfile in inputfiles
262+
content = organize_import_blocks_file(inputfile)
263+
write(inputfile, content)
264+
end
265+
# Pass 2: Formatting via JuliaFormatter
266+
JuliaFormatter.format(inputfiles; JULIAFORMATTER_OPTIONS...)
267+
# Pass 3: Re-organize imports (fix up any changes from JuliaFormatter, e.g. import_to_using)
227268
for inputfile in inputfiles
228269
content = organize_import_blocks_file(inputfile)
229270
write(inputfile, content)
230271
end
231-
pushfirst!(inputfiles, "--inplace")
232-
Runic.main(inputfiles)
272+
# Pass 4: Canonicalize via Runic
273+
Runic.main(["--inplace"; inputfiles])
233274
return 0
234275
end
235276

test/runtests.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ const GROUP = uppercase(
1616
end,
1717
)
1818

19-
"match files of the form `test_*.jl`, but exclude `*setup*.jl`"
19+
"""
20+
match files of the form `test_*.jl`, but exclude `*setup*.jl`
21+
"""
2022
function istestfile(path)
2123
fn = basename(path)
22-
return endswith(fn, ".jl") && startswith(basename(fn), "test_") && !contains(fn, "setup")
24+
return endswith(fn, ".jl") && startswith(basename(fn), "test_") &&
25+
!contains(fn, "setup")
2326
end
24-
"match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`"
27+
"""
28+
match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`
29+
"""
2530
function isexamplefile(path)
2631
fn = basename(path)
2732
return endswith(fn, ".jl") && !endswith(fn, "_notest.jl") && !contains(fn, "setup")

test/test_aqua.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using ITensorFormatter: ITensorFormatter
21
using Aqua: Aqua
2+
using ITensorFormatter: ITensorFormatter
33
using Test: @testset
44

55
@testset "Code quality (Aqua.jl)" begin

0 commit comments

Comments
 (0)