|
| 1 | + |
| 2 | +The built-in mixformat formatter knows how to format Elixir code. |
| 3 | +If you aren't familiar with basic codefmt usage yet, see main.vroom first. |
| 4 | + |
| 5 | +We'll set up codefmt and configure the vroom environment, then jump into some |
| 6 | +examples. |
| 7 | + |
| 8 | + :source $VROOMDIR/setupvroom.vim |
| 9 | + |
| 10 | + :let g:repeat_calls = [] |
| 11 | + :function FakeRepeat(...)<CR> |
| 12 | + | call add(g:repeat_calls, a:000)<CR> |
| 13 | + :endfunction |
| 14 | + :call maktaba#test#Override('repeat#set', 'FakeRepeat') |
| 15 | + |
| 16 | + :call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) |
| 17 | + |
| 18 | + |
| 19 | +The mixformat formatter expects the mix executable to be installed on your |
| 20 | +system. |
| 21 | + |
| 22 | + % IO.puts("Hello world") |
| 23 | + :FormatCode mixformat |
| 24 | + ! cd .* && mix format .* - 2>.* |
| 25 | + $ IO.puts("Hello world") |
| 26 | + |
| 27 | +The name or path of the mixformat executable can be configured via the |
| 28 | +mix_executable flag if the default of "mix" doesn't work. |
| 29 | + |
| 30 | + :Glaive codefmt mix_executable='someothermix' |
| 31 | + :FormatCode mixformat |
| 32 | + ! cd .* && someothermix format .* - 2>.* |
| 33 | + $ IO.puts("Hello world") |
| 34 | + :Glaive codefmt mix_executable='mix' |
| 35 | + |
| 36 | + |
| 37 | +You can format any buffer with mixformat specifying the formatter explicitly. |
| 38 | + |
| 39 | + @clear |
| 40 | + % def foo() do<CR> |
| 41 | + |IO.puts("Hello"); IO.puts("World");<CR> |
| 42 | + |end |
| 43 | + |
| 44 | + :FormatCode mixformat |
| 45 | + ! cd .* && mix format .* - 2>.* |
| 46 | + $ def foo() do |
| 47 | + $ IO.puts("Hello") |
| 48 | + $ IO.puts("World") |
| 49 | + $ end |
| 50 | + def foo() do |
| 51 | + IO.puts("Hello") |
| 52 | + IO.puts("World") |
| 53 | + end |
| 54 | + @end |
| 55 | + |
| 56 | +The elixir, eelixer, and heex filetypes will use the mixformat formatter |
| 57 | +by default. |
| 58 | + |
| 59 | + @clear |
| 60 | + % IO.puts("Hello world") |
| 61 | + |
| 62 | + :set filetype=elixir |
| 63 | + :FormatCode |
| 64 | + ! cd .* && mix format .* - 2>.* |
| 65 | + $ IO.puts("Hello world") |
| 66 | + |
| 67 | + :set filetype=eelixir |
| 68 | + :FormatCode |
| 69 | + ! cd .* && mix format .* - 2>.* |
| 70 | + $ IO.puts("Hello world") |
| 71 | + |
| 72 | + :set filetype=heex |
| 73 | + :FormatCode |
| 74 | + ! cd .* && mix format .* - 2>.* |
| 75 | + $ IO.puts("Hello world") |
| 76 | + |
| 77 | + :set filetype= |
| 78 | + |
| 79 | +It can format specific line ranges of code using :FormatLines. |
| 80 | + |
| 81 | + @clear |
| 82 | + % defmodule Foo do<CR> |
| 83 | + |def bar(list) do<CR> |
| 84 | + |[head | tail] = list; IO.puts(head)<CR> |
| 85 | + |end<CR> |
| 86 | + |end |
| 87 | + |
| 88 | + :2,4FormatLines mixformat |
| 89 | + ! cd .* && mix format .* - 2>.* |
| 90 | + $ def bar(list) do |
| 91 | + $ [head | tail] = list |
| 92 | + $ IO.puts(head) |
| 93 | + $ end |
| 94 | + defmodule Foo do |
| 95 | + def bar(list) do |
| 96 | + [head | tail] = list |
| 97 | + IO.puts(head) |
| 98 | + end |
| 99 | + end |
| 100 | + @end |
| 101 | + |
| 102 | +NOTE: the mix formatter does not natively support range formatting, so there |
| 103 | +are certain limitations like misaligning indentation levels. |
0 commit comments