dense-analysis / ale

Check syntax in Vim/Neovim asynchronously and fix files, with Language Server Protocol (LSP) support
BSD 2-Clause "Simplified" License
13.54k stars 1.43k forks source link

Add JSON fixer using 'python -m json.tool' #4314

Open Konstantin-Glukhov opened 2 years ago

Konstantin-Glukhov commented 2 years ago

Implement something similar to the following code:

ftplugin/json.vim:

let b:ale_fixers = ['json_python_fixer] let b:ale_json_python_fixer_options = '--indent 2'

autoload/ale/fixers/json_python_fixer.vim:

" Description: Fix JSON files with "python -m json.tool"
call ale#Set('json_python_fixer_options', '')

function! ale#fixers#json_python_fixer#Fix(buffer) abort
  let l:command = 'python'

  if !executable(l:command)
    echo 'ALEFix: ' . l:command . ' executable not found'
    return {'command': 'REM'}
  endif

  let l:command .= ' -m json.tool'

  let l:options = ale#Var(a:buffer, 'json_python_fixer_options')
  if len(l:options)
    let l:command .= ' ' . l:options
  endif

  return { 'command': l:command . ' -'}
endfunction
idbrii commented 17 hours ago

A big advantage of json.tool over alternatives like jsbeautify is that you can sort the keys:

let g:ale_json_python_fixer_options = '--sort-keys'