Skip to content

Commit d38a3f7

Browse files
authored
Add fixer for json: python's json.tool (#4847)
Resolves #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.
1 parent 699c0db commit d38a3f7

6 files changed

+62
-0
lines changed

autoload/ale/fix/registry.vim

+5
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,11 @@ let s:default_registry = {
477477
\ 'suggested_filetypes': ['json'],
478478
\ 'description': 'Fix JSON files with jq.',
479479
\ },
480+
\ 'json_pytool': {
481+
\ 'function': 'ale#fixers#json_pytool#Fix',
482+
\ 'suggested_filetypes': ['json'],
483+
\ 'description': "Fix JSON files with python's built-in json.tool module.",
484+
\ },
480485
\ 'protolint': {
481486
\ 'function': 'ale#fixers#protolint#Fix',
482487
\ 'suggested_filetypes': ['proto'],

autoload/ale/fixers/json_pytool.vim

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
" Author: idbrii
2+
" Description: json formatter as ALE fixer using python's json.tool
3+
4+
call ale#Set('json_pytool_executable', 'python')
5+
call ale#Set('json_pytool_options', '')
6+
call ale#Set('json_pytool_use_global', get(g:, 'ale_use_global_executables', 0))
7+
8+
function! ale#fixers#json_pytool#GetExecutable(buffer) abort
9+
return ale#path#FindExecutable(a:buffer, 'json_pytool', ['python'])
10+
endfunction
11+
12+
function! ale#fixers#json_pytool#Fix(buffer) abort
13+
let l:executable = ale#Escape(ale#fixers#json_pytool#GetExecutable(a:buffer))
14+
let l:opts = ale#Var(a:buffer, 'json_pytool_options')
15+
let l:command = printf('%s -m json.tool %s -', l:executable, l:opts)
16+
17+
return {
18+
\ 'command': l:command
19+
\ }
20+
endfunction

doc/ale-json.txt

+34
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,40 @@ g:ale_json_fixjson_use_global *g:ale_json_fixjson_use_global*
8383
See |ale-integrations-local-executables|
8484

8585

86+
===============================================================================
87+
pytool *ale-json-pytool*
88+
89+
Use python's json.tool module to reformat json.
90+
91+
g:ale_json_pytool_executable *g:ale_json_pytool_executable*
92+
*b:ale_json_pytool_executable*
93+
94+
Type: |String|
95+
Default: `'python'`
96+
97+
The python executable that run to use its json.tool module. This fixer
98+
requires python 3, which includes the json module.
99+
100+
g:ale_json_pytool_options *g:ale_json_pytool_options*
101+
*b:ale_json_pytool_options*
102+
103+
Type: |String|
104+
Default: `''`
105+
106+
These options are passed to the json.tool module. Example: >
107+
let g:ale_json_pytool_options = '--sort-keys --indent 2'
108+
< See docs for all options:
109+
https://docs.python.org/3/library/json.html#module-json.tool
110+
111+
g:ale_json_pytool_use_global *g:ale_json_pytool_use_global*
112+
*b:ale_json_pytool_use_global*
113+
114+
Type: |Number|
115+
Default: `get(g:, 'ale_use_global_executables', 0)`
116+
117+
See |ale-integrations-local-executables|
118+
119+
86120
===============================================================================
87121
jsonlint *ale-json-jsonlint*
88122

doc/ale-supported-languages-and-tools.txt

+1
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ Notes:
329329
* `eslint`
330330
* `fixjson`
331331
* `jq`
332+
* `json.tool`
332333
* `jsonlint`
333334
* `prettier`
334335
* `spectral`

doc/ale.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3159,6 +3159,7 @@ documented in additional help files.
31593159
dprint................................|ale-json-dprint|
31603160
eslint................................|ale-json-eslint|
31613161
fixjson...............................|ale-json-fixjson|
3162+
pytool................................|ale-json-pytool|
31623163
jsonlint..............................|ale-json-jsonlint|
31633164
jq....................................|ale-json-jq|
31643165
prettier..............................|ale-json-prettier|

supported-tools.md

+1
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ formatting.
338338
* [eslint](http://eslint.org/) :warning:
339339
* [fixjson](https://github.com/rhysd/fixjson)
340340
* [jq](https://stedolan.github.io/jq/) :warning:
341+
* [json.tool](https://docs.python.org/3/library/json.html#module-json.tool) :warning:
341342
* [jsonlint](https://github.com/zaach/jsonlint)
342343
* [prettier](https://github.com/prettier/prettier)
343344
* [spectral](https://github.com/stoplightio/spectral)

0 commit comments

Comments
 (0)