@@ -10,9 +10,43 @@ using JuliaFormatter: JuliaFormatter
1010using JuliaSyntax: JuliaSyntax, @K_str , SyntaxNode, children, kind, parseall, span
1111using 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
1751function 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))
98130end
99131
100132function organize_import_blocks (input)
@@ -138,23 +170,23 @@ const ITENSORFORMATTER_VERSION = pkgversion(@__MODULE__)
138170# Print a typical cli program help message
139171function 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
173205end
@@ -189,6 +221,7 @@ organizes using/import statements by merging adjacent blocks, sorting modules an
189221and line-wrapping. Accepts file paths and directories as arguments.
190222
191223# Examples
224+
192225```julia-repl
193226julia> 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
234275end
235276
0 commit comments