20
20
# on stdin and doesn't support line ranges, both of which are nice features for
21
21
# the vim-codefmt plugin. The --file-path flag lets this program find
22
22
# .JuliaFormatter.toml files to determine code style preferences.
23
+
23
24
try
24
- using JuliaFormatter
25
+ @eval using JuliaFormatter
25
26
catch ArgumentError
26
27
println (
27
28
stderr ,
@@ -30,7 +31,7 @@ catch ArgumentError
30
31
exit (2 )
31
32
end
32
33
try
33
- using ArgParse
34
+ @eval using ArgParse
34
35
catch ArgumentError
35
36
println (
36
37
stderr ,
59
60
60
61
" Entry point to run format.jl. argv is the command line arguments."
61
62
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
65
63
s = ArgParseSettings (
66
64
" $(basename (PROGRAM_FILE )) : format all or part of Julia code read from stdin" ,
67
65
autofix_names= true
68
66
)
69
67
@add_arg_table! s begin
70
68
# ! format: off
71
69
" --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"
74
72
" --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
79
80
# ! format: on
80
81
end
81
82
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
82
86
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))
85
89
end
86
90
# Sort line ranges and check for overlap, which would make things complicated
87
91
ranges = sort (args[:lines ], by= x -> x. first)
@@ -145,7 +149,7 @@ function formatranges(ranges::Vector{LineRange}, opts)
145
149
line = lines[lnum]
146
150
lnum += 1
147
151
# disable existing formatter directives
148
- if (m = match (formatpat, line); ! isnothing (m))
152
+ if (m = match (formatpat, line)) != = nothing
149
153
line = " # disabled:$marker :$line "
150
154
requested = m. captures[1 ] == " on"
151
155
end
@@ -165,7 +169,7 @@ function formatranges(ranges::Vector{LineRange}, opts)
165
169
# if there's a format:off directive inside the range, respect that;
166
170
# if there's a format:on directive inside the range and formatting had
167
171
# been off, enable it at this point
168
- if (m = match (formatpat, line); ! isnothing (m))
172
+ if (m = match (formatpat, line)) != = nothing
169
173
line = " # disabled:$marker :$line "
170
174
if m. captures[1 ] == " on" && ! requested
171
175
requested = true
@@ -186,7 +190,7 @@ function formatranges(ranges::Vector{LineRange}, opts)
186
190
while lnum <= length (lines)
187
191
line = lines[lnum]
188
192
lnum += 1
189
- if (m = match ( formatpat, line); ! isnothing (m) )
193
+ if occursin ( formatpat, line)
190
194
line = " # disabled:$marker :$line "
191
195
end
192
196
println (text, line)
@@ -210,15 +214,17 @@ function formatranges(ranges::Vector{LineRange}, opts)
210
214
last = line
211
215
if skipnext
212
216
skipnext = false
213
- elseif ! isnothing ( match ( addedpat, line) )
217
+ elseif occursin ( addedpat, line)
214
218
skipnext = true
215
219
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 ]
218
222
end
219
223
println (line)
220
224
end
221
225
end
222
226
end
223
227
224
- main (ARGS )
228
+ if abspath (PROGRAM_FILE ) == @__FILE__
229
+ main (ARGS )
230
+ end
0 commit comments