Skip to content

Commit 7005937

Browse files
committed
julia coding style tweaks
1 parent af18bf5 commit 7005937

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

bin/julia/format.jl

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
# on stdin and doesn't support line ranges, both of which are nice features for
2121
# the vim-codefmt plugin. The --file-path flag lets this program find
2222
# .JuliaFormatter.toml files to determine code style preferences.
23+
2324
try
24-
using JuliaFormatter
25+
@eval using JuliaFormatter
2526
catch ArgumentError
2627
println(
2728
stderr,
@@ -30,7 +31,7 @@ catch ArgumentError
3031
exit(2)
3132
end
3233
try
33-
using ArgParse
34+
@eval using ArgParse
3435
catch ArgumentError
3536
println(
3637
stderr,
@@ -59,29 +60,32 @@ end
5960

6061
"Entry point to run format.jl. argv is the command line arguments."
6162
function main(argv::Vector{<:AbstractString})
62-
if length(argv) > 0 && argv[1] == "--check-install"
63-
exit(0) # Already successfully imported the necessary modules
64-
end
6563
s = ArgParseSettings(
6664
"$(basename(PROGRAM_FILE)): format all or part of Julia code read from stdin",
6765
autofix_names=true
6866
)
6967
@add_arg_table! s begin
7068
#! format: off
7169
"--file_path"
72-
help = "file path of the code (default: current working directory)"
73-
metavar = "path/to/file.jl"
70+
help = "file path of the code (default: current working directory)"
71+
metavar = "path/to/file.jl"
7472
"--lines"
75-
help = "line range(s) to format (1-based)"
76-
arg_type = LineRange
77-
metavar = "first:last"
78-
nargs = '*'
73+
help = "line range(s) to format (1-based)"
74+
arg_type = LineRange
75+
metavar = "first:last"
76+
nargs = '*'
77+
"--check_install"
78+
help = "exit with status 0 if dependencies are installed, 2 otherwise"
79+
action = :store_true
7980
#! format: on
8081
end
8182
args = parse_args(argv, s, as_symbols=true)
83+
if args[:check_install]
84+
exit(0) # if we got this far, module import succeeded
85+
end
8286
file_path = let p = args[:file_path]
83-
isnothing(p) ? joinpath(pwd(), "file-path-not-specified") :
84-
abspath(expanduser(p))
87+
fakefile = "file-path-not-specified"
88+
isnothing(p) ? joinpath(pwd(), fakefile) : abspath(expanduser(p))
8589
end
8690
# Sort line ranges and check for overlap, which would make things complicated
8791
ranges = sort(args[:lines], by=x -> x.first)
@@ -145,7 +149,7 @@ function formatranges(ranges::Vector{LineRange}, opts)
145149
line = lines[lnum]
146150
lnum += 1
147151
# disable existing formatter directives
148-
if (m = match(formatpat, line); !isnothing(m))
152+
if (m = match(formatpat, line)) !== nothing
149153
line = "# disabled:$marker:$line"
150154
requested = m.captures[1] == "on"
151155
end
@@ -165,7 +169,7 @@ function formatranges(ranges::Vector{LineRange}, opts)
165169
# if there's a format:off directive inside the range, respect that;
166170
# if there's a format:on directive inside the range and formatting had
167171
# been off, enable it at this point
168-
if (m = match(formatpat, line); !isnothing(m))
172+
if (m = match(formatpat, line)) !== nothing
169173
line = "# disabled:$marker:$line"
170174
if m.captures[1] == "on" && !requested
171175
requested = true
@@ -186,7 +190,7 @@ function formatranges(ranges::Vector{LineRange}, opts)
186190
while lnum <= length(lines)
187191
line = lines[lnum]
188192
lnum += 1
189-
if (m = match(formatpat, line); !isnothing(m))
193+
if occursin(formatpat, line)
190194
line = "# disabled:$marker:$line"
191195
end
192196
println(text, line)
@@ -210,15 +214,17 @@ function formatranges(ranges::Vector{LineRange}, opts)
210214
last = line
211215
if skipnext
212216
skipnext = false
213-
elseif !isnothing(match(addedpat, line))
217+
elseif occursin(addedpat, line)
214218
skipnext = true
215219
else
216-
if (m = match(disabledpat, line); !isnothing(m))
217-
(line,) = m.captures
220+
if (m = match(disabledpat, line)) !== nothing
221+
line = m.captures[1]
218222
end
219223
println(line)
220224
end
221225
end
222226
end
223227

224-
main(ARGS)
228+
if abspath(PROGRAM_FILE) == @__FILE__
229+
main(ARGS)
230+
end

0 commit comments

Comments
 (0)