Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Konstantin-Glukhov opened this issue Sep 20, 2022 · 1 comment · May be fixed by #4847
Open

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

Konstantin-Glukhov opened this issue Sep 20, 2022 · 1 comment · May be fixed by #4847
Labels
new tool Support for new linters, fixers, etc.

Comments

@Konstantin-Glukhov
Copy link

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
@Konstantin-Glukhov Konstantin-Glukhov added the new tool Support for new linters, fixers, etc. label Sep 20, 2022
@idbrii
Copy link
Contributor

idbrii commented Oct 11, 2024

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'

idbrii added a commit to idbrii/vim-ale that referenced this issue Oct 15, 2024
Resolves dense-analysis#4314.

Add a fixer that's built into python for json formatting. Include a
couple arguments in docs to make these features more discoverable.

Uses stdin-based fixing so you don't need to save the file to fix.
idbrii added a commit to idbrii/vim-ale that referenced this issue Oct 17, 2024
Resolves dense-analysis#4314.

Add a fixer that's built into python for json formatting. Include a
couple arguments in docs to make these features more discoverable.

Uses stdin-based fixing so you don't need to save the file to fix.
idbrii added a commit to idbrii/vim-ale that referenced this issue Oct 17, 2024
Resolves dense-analysis#4314.

Add a fixer that's built into python for json formatting. Include a
couple arguments in docs to make these features more discoverable.

Uses stdin-based fixing so you don't need to save the file to fix.
@idbrii idbrii linked a pull request Oct 22, 2024 that will close this issue
idbrii added a commit to idbrii/vim-ale that referenced this issue Nov 5, 2024
Resolves dense-analysis#4314.

Add a fixer that's built into python for json formatting. Include a
couple arguments in docs to make these features more discoverable.

Uses stdin-based fixing so you don't need to save the file to fix.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new tool Support for new linters, fixers, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants