|
| 1 | +The built-in JuliaFormatter formatter knows how to format Julia files. If you |
| 2 | +aren't familiar with basic codefmt usage yet, see main.vroom first. |
| 3 | + |
| 4 | +We'll set up codefmt and configure the vroom environment, then jump into some |
| 5 | +examples. |
| 6 | + |
| 7 | + :source $VROOMDIR/setupvroom.vim |
| 8 | + |
| 9 | + :let g:repeat_calls = [] |
| 10 | + :function FakeRepeat(...)<CR> |
| 11 | + | call add(g:repeat_calls, a:000)<CR> |
| 12 | + :endfunction |
| 13 | + :call maktaba#test#Override('repeat#set', 'FakeRepeat') |
| 14 | + |
| 15 | + :call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) |
| 16 | + |
| 17 | + |
| 18 | +The JuliaFormatter formatter uses the bin/julia/format.jl script which is |
| 19 | +bundled with codefmt. That script will return an error if Julia or the |
| 20 | +JuliaFormatter package are not installed. |
| 21 | + |
| 22 | + % module Foo bar(x) = x ? "yes" : "no" end |
| 23 | + :FormatCode JuliaFormatter |
| 24 | + ! .*/bin/julia/format.jl .* |
| 25 | + $ module Foo { |
| 26 | + $ function bar(x) |
| 27 | + $ if x |
| 28 | + $ "yes" |
| 29 | + $ else |
| 30 | + $ "no" |
| 31 | + $ end |
| 32 | + $ end |
| 33 | + $ end |
| 34 | + |
| 35 | +The name or path of the format.jl script can be configured via the |
| 36 | +julia_format_executable flag if the bundled format.jl doesn't work. |
| 37 | + |
| 38 | + :Glaive codefmt julia_format_executable='/path/to/myscript' |
| 39 | + :FormatCode JuliaFormatter |
| 40 | + ! /path/to/myscript .* |
| 41 | + $ module Foo |
| 42 | + $ function bar(x) |
| 43 | + $ if x |
| 44 | + $ "yes" |
| 45 | + $ else |
| 46 | + $ "no" |
| 47 | + $ end |
| 48 | + $ end |
| 49 | + $ end |
| 50 | + :let g:format_jl = maktaba#path#Join([expand("$VROOMDIR:h:h"), 'bin', 'julia', 'format.jl']) |
| 51 | + :Glaive codefmt julia_format_executable=`g:format_jl` |
| 52 | + |
| 53 | +It can format specific line ranges of code using :FormatLines. |
| 54 | + |
| 55 | + @clear |
| 56 | + % module Foo<CR> |
| 57 | + |function bar(x)<CR> |
| 58 | + |print(x ? "yes" : "no")<CR> |
| 59 | + |print(<CR> |
| 60 | + |x &&<CR> |
| 61 | + |!x ?<CR> |
| 62 | + |"impossible" :<CR> |
| 63 | + |"ok")<CR> |
| 64 | + |end<CR> |
| 65 | + |end |
| 66 | + |
| 67 | + :4,8FormatLines JuliaFormatter |
| 68 | + ! .*/bin/julia/format.jl .*--lines 4:8.* |
| 69 | + $ module Foo { |
| 70 | + $ function bar(x) |
| 71 | + $ print(x ? "yes" : "no") |
| 72 | + $ print(if x && !x |
| 73 | + $ "impossible" |
| 74 | + $ else |
| 75 | + $ "ok" |
| 76 | + $ end) |
| 77 | + $ end |
| 78 | + $ end |
0 commit comments