Merge py cli and js cli js-beautify definitions.

This commit is contained in:
Chiel ten Brinke 2017-06-23 09:07:06 +02:00
parent 31c2de2398
commit 889665717c
2 changed files with 43 additions and 48 deletions

View File

@ -140,14 +140,10 @@ if !exists('g:formatdef_jsbeautify_javascript')
elseif filereadable(expand('~/.jsbeautifyrc'))
let g:formatdef_jsbeautify_javascript = '"js-beautify"'
else
let g:formatdef_jsbeautify_javascript = '"js-beautify -X -f - -".(&expandtab ? "s ".shiftwidth() : "t").(&textwidth ? " -w ".&textwidth : "")'
let g:formatdef_jsbeautify_javascript = '"js-beautify -X -".(&expandtab ? "s ".shiftwidth() : "t").(&textwidth ? " -w ".&textwidth : "")'
endif
endif
if !exists('g:formatdef_pyjsbeautify_javascript')
let g:formatdef_pyjsbeautify_javascript = '"js-beautify -X -".(&expandtab ? "s ".shiftwidth() : "t").(&textwidth ? " -w ".&textwidth : "")." -"'
endif
if !exists('g:formatdef_jscs')
let g:formatdef_jscs = '"jscs -x"'
endif
@ -164,50 +160,49 @@ endif
" corresponding config is found they are used, otherwiese the formatter fails.
" No windows support at the moment.
if !exists('g:formatdef_eslint_local')
function! g:BuildESLintLocalCmd()
let l:path = fnamemodify(expand('%'), ':p')
let verbose = &verbose || g:autoformat_verbosemode == 1
if has('win32')
return "(>&2 echo 'ESLint Local not supported on win32')"
endif
" find formatter & config file
let l:prog = findfile('node_modules/.bin/eslint', l:path.";")
let l:cfg = findfile('.eslintrc.js', l:path.";")
if empty(l:cfg)
let l:cfg = findfile('.eslintrc.yaml', l:path.";")
endif
if empty(l:cfg)
let l:cfg = findfile('.eslintrc.yml', l:path.";")
endif
if empty(l:cfg)
let l:cfg = findfile('.eslintrc.json', l:path.";")
endif
if empty(l:cfg)
let l:cfg = findfile('.eslintrc', l:path.";")
endif
if (empty(l:cfg) || empty(l:prog))
if verbose
return "(>&2 echo 'No local ESLint program and/or config found')"
endif
return
endif
function! g:BuildESLintLocalCmd()
let l:path = fnamemodify(expand('%'), ':p')
let verbose = &verbose || g:autoformat_verbosemode == 1
if has('win32')
return "(>&2 echo 'ESLint Local not supported on win32')"
endif
" find formatter & config file
let l:prog = findfile('node_modules/.bin/eslint', l:path.";")
let l:cfg = findfile('.eslintrc.js', l:path.";")
if empty(l:cfg)
let l:cfg = findfile('.eslintrc.yaml', l:path.";")
endif
if empty(l:cfg)
let l:cfg = findfile('.eslintrc.yml', l:path.";")
endif
if empty(l:cfg)
let l:cfg = findfile('.eslintrc.json', l:path.";")
endif
if empty(l:cfg)
let l:cfg = findfile('.eslintrc', l:path.";")
endif
if (empty(l:cfg) || empty(l:prog))
if verbose
return "(>&2 echo 'No local ESLint program and/or config found')"
endif
return
endif
" This formatter uses a temporary file as ESLint has not option to print
" the formatted source to stdout without modifieing the file.
let l:eslint_js_tmp_file = fnameescape(tempname().".js")
let content = getline('1', '$')
call writefile(content, l:eslint_js_tmp_file)
return l:prog." -c ".l:cfg." --fix ".l:eslint_js_tmp_file." 1> /dev/null; exit_code=$?
\ cat ".l:eslint_js_tmp_file."; rm -f ".l:eslint_js_tmp_file."; exit $exit_code"
endfunction
let g:formatdef_eslint_local = "g:BuildESLintLocalCmd()"
" This formatter uses a temporary file as ESLint has not option to print
" the formatted source to stdout without modifieing the file.
let l:eslint_js_tmp_file = fnameescape(tempname().".js")
let content = getline('1', '$')
call writefile(content, l:eslint_js_tmp_file)
return l:prog." -c ".l:cfg." --fix ".l:eslint_js_tmp_file." 1> /dev/null; exit_code=$?
\ cat ".l:eslint_js_tmp_file."; rm -f ".l:eslint_js_tmp_file."; exit $exit_code"
endfunction
let g:formatdef_eslint_local = "g:BuildESLintLocalCmd()"
endif
if !exists('g:formatters_javascript')
let g:formatters_javascript = [
\ 'eslint_local',
\ 'eslint_local',
\ 'jsbeautify_javascript',
\ 'pyjsbeautify_javascript',
\ 'jscs',
\ 'standard_javascript',
\ 'xo_javascript'
@ -221,18 +216,14 @@ if !exists('g:formatdef_jsbeautify_json')
elseif filereadable(expand('~/.jsbeautifyrc'))
let g:formatdef_jsbeautify_json = '"js-beautify"'
else
let g:formatdef_jsbeautify_json = '"js-beautify -f - -".(&expandtab ? "s ".shiftwidth() : "t")'
let g:formatdef_jsbeautify_json = '"js-beautify -".(&expandtab ? "s ".shiftwidth() : "t")'
endif
endif
if !exists('g:formatdef_pyjsbeautify_json')
let g:formatdef_pyjsbeautify_json = '"js-beautify -".(&expandtab ? "s ".shiftwidth() : "t")." -"'
endif
if !exists('g:formatters_json')
let g:formatters_json = [
\ 'jsbeautify_json',
\ 'pyjsbeautify_json',
\ ]
endif

4
samples/test.json Normal file
View File

@ -0,0 +1,4 @@
{
"test": "test",
"test": [1, 2, 3]
}