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 fixer for json: python's json.tool #4847

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions autoload/ale/fix/registry.vim
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['json'],
\ 'description': 'Fix JSON files with jq.',
\ },
\ 'json_pytool': {
\ 'function': 'ale#fixers#json_pytool#Fix',
\ 'suggested_filetypes': ['json'],
\ 'description': "Fix JSON files with python's built-in json.tool module.",
\ },
\ 'protolint': {
\ 'function': 'ale#fixers#protolint#Fix',
\ 'suggested_filetypes': ['proto'],
Expand Down
20 changes: 20 additions & 0 deletions autoload/ale/fixers/json_pytool.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
" Author: idbrii
" Description: json formatter as ALE fixer using python's json.tool

call ale#Set('json_pytool_executable', 'python')
call ale#Set('json_pytool_options', '')
call ale#Set('json_pytool_use_global', get(g:, 'ale_use_global_executables', 0))

function! ale#fixers#json_pytool#GetExecutable(buffer) abort
return ale#path#FindExecutable(a:buffer, 'json_pytool', ['python'])
endfunction

function! ale#fixers#json_pytool#Fix(buffer) abort
let l:executable = ale#Escape(ale#fixers#json_pytool#GetExecutable(a:buffer))
let l:opts = ale#Var(a:buffer, 'json_pytool_options')
let l:command = printf('%s -m json.tool %s -', l:executable, l:opts)

return {
\ 'command': l:command
\ }
endfunction
34 changes: 34 additions & 0 deletions doc/ale-json.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,40 @@ g:ale_json_fixjson_use_global *g:ale_json_fixjson_use_global*
See |ale-integrations-local-executables|


===============================================================================
pytool *ale-json-pytool*

Use python's json.tool module to reformat json.

g:ale_json_pytool_executable *g:ale_json_pytool_executable*
*b:ale_json_pytool_executable*

Type: |String|
Default: `'python'`

The python executable that run to use its json.tool module. This fixer
requires python 3, which includes the json module.

g:ale_json_pytool_options *g:ale_json_pytool_options*
*b:ale_json_pytool_options*

Type: |String|
Default: `''`

These options are passed to the json.tool module. Example: >
let g:ale_json_pytool_options = '--sort-keys --indent 2'
< See docs for all options:
https://docs.python.org/3/library/json.html#module-json.tool

g:ale_json_pytool_use_global *g:ale_json_pytool_use_global*
*b:ale_json_pytool_use_global*

Type: |Number|
Default: `get(g:, 'ale_use_global_executables', 0)`

See |ale-integrations-local-executables|


===============================================================================
jsonlint *ale-json-jsonlint*

Expand Down
1 change: 1 addition & 0 deletions doc/ale-supported-languages-and-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ Notes:
* `eslint`
* `fixjson`
* `jq`
* `json.tool`
* `jsonlint`
* `prettier`
* `spectral`
Expand Down
1 change: 1 addition & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3159,6 +3159,7 @@ documented in additional help files.
dprint................................|ale-json-dprint|
eslint................................|ale-json-eslint|
fixjson...............................|ale-json-fixjson|
pytool................................|ale-json-pytool|
jsonlint..............................|ale-json-jsonlint|
jq....................................|ale-json-jq|
prettier..............................|ale-json-prettier|
Expand Down
1 change: 1 addition & 0 deletions supported-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ formatting.
* [eslint](http://eslint.org/) :warning:
* [fixjson](https://github.com/rhysd/fixjson)
* [jq](https://stedolan.github.io/jq/) :warning:
* [json.tool](https://docs.python.org/3/library/json.html#module-json.tool) :warning:
* [jsonlint](https://github.com/zaach/jsonlint)
* [prettier](https://github.com/prettier/prettier)
* [spectral](https://github.com/stoplightio/spectral)
Expand Down