|
| 1 | +The built-in js-beautify formatter knows how to format javascript, json, html |
| 2 | +and css code. |
| 3 | + |
| 4 | +We'll set up codefmt and configure the vroom environment, then jump into some |
| 5 | +examples. If you haven't seen the setup code below yet, read through main.vroom |
| 6 | +briefly. |
| 7 | + |
| 8 | + :set nocompatible |
| 9 | + :let g:repo = fnamemodify($VROOMFILE, ':p:h:h') |
| 10 | + :execute 'source' g:repo . '/bootstrap.vim' |
| 11 | + :call maktaba#syscall#SetUsableShellRegex('\v<shell\.vroomfaker$') |
| 12 | + |
| 13 | + :let g:repeat_calls = [] |
| 14 | + :function FakeRepeat(...)<CR> |
| 15 | + | call add(g:repeat_calls, a:000)<CR> |
| 16 | + :endfunction |
| 17 | + :call maktaba#test#Override('repeat#set', 'FakeRepeat') |
| 18 | + |
| 19 | + :call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0) |
| 20 | + |
| 21 | + |
| 22 | +The js-beautify formatter expects the js-beautify executable to be installed |
| 23 | +on your system. |
| 24 | + |
| 25 | + % f() |
| 26 | + :FormatCode js-beautify |
| 27 | + ! js-beautify .* |
| 28 | + $ f() |
| 29 | + |
| 30 | +The name or path of the js-beautify executable can be configured via the |
| 31 | +js_beautify_executable flag if the default of "js-beautify" doesn't work. |
| 32 | + |
| 33 | + :Glaive codefmt js_beautify_executable='myjsb' |
| 34 | + :FormatCode js-beautify |
| 35 | + ! myjsb .* |
| 36 | + $ f() |
| 37 | + :Glaive codefmt js_beautify_executable='js-beautify' |
| 38 | + |
| 39 | + |
| 40 | +You can format any buffer with js-beautify specifying the formatter explicitly. |
| 41 | + |
| 42 | + @clear |
| 43 | + % for (i = 0; i < max; i++) {<CR> |
| 44 | + |a=1234 + 5678 + i; b = 1234*5678+i; c=1234/2+i;} |
| 45 | + |
| 46 | + :FormatCode js-beautify |
| 47 | + ! js-beautify .*2>.* |
| 48 | + $ for (i = 0; i < max; i++) { |
| 49 | + $ a = 1234 + 5678 + i; |
| 50 | + $ b = 1234 * 5678 + i; |
| 51 | + $ c = 1234 / 2 + i; |
| 52 | + $ } |
| 53 | + for (i = 0; i < max; i++) { |
| 54 | + a = 1234 + 5678 + i; |
| 55 | + b = 1234 * 5678 + i; |
| 56 | + c = 1234 / 2 + i; |
| 57 | + } |
| 58 | + @end |
| 59 | + |
| 60 | +# @TODO: fix default formatter for javascript |
| 61 | +Several filetypes will use the js-beautify formatter by default: |
| 62 | +javascript, json, html and css. |
| 63 | + |
| 64 | + @clear |
| 65 | + % f(); |
| 66 | + |
| 67 | + :set filetype=javascript |
| 68 | + :FormatCode |
| 69 | + ! clang-format .* |
| 70 | + $ f(); |
| 71 | + |
| 72 | + :set filetype=json |
| 73 | + :FormatCode |
| 74 | + ! js-beautify .* |
| 75 | + $ f(); |
| 76 | + |
| 77 | + :set filetype=html |
| 78 | + :FormatCode |
| 79 | + ! js-beautify .* |
| 80 | + $ f(); |
| 81 | + |
| 82 | + :set filetype=css |
| 83 | + :FormatCode |
| 84 | + ! js-beautify .* |
| 85 | + $ f(); |
| 86 | + |
| 87 | + :set filetype= |
| 88 | + |
| 89 | + |
| 90 | +It can format specific line ranges of code using :FormatLines. |
| 91 | + |
| 92 | + @clear |
| 93 | + % for (i = 0; i < max; i++) {<CR> |
| 94 | + |a=1234 + 5678 + i; b = 1234*5678+i;<CR> |
| 95 | + |c=1234/2+i;} |
| 96 | + |
| 97 | + :2,2FormatLines js-beautify |
| 98 | + ! js-beautify .*2>.* |
| 99 | + $ a = 1234 + 5678 + i; |
| 100 | + $ b = 1234 * 5678 + i; |
| 101 | + for (i = 0; i < max; i++) { |
| 102 | + a = 1234 + 5678 + i; |
| 103 | + b = 1234 * 5678 + i; |
| 104 | + c=1234/2+i;} |
| 105 | + @end |
| 106 | + |
| 107 | +NOTE: the js-beautify formatter does not natively support range formatting, |
| 108 | +so there are certain limitations like not being able to format misaligned |
| 109 | +braces. |
0 commit comments