Skip to content

Commit 0048478

Browse files
committed
Add fixer for json: python's json.tool
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 e5d6d94 commit 0048478

File tree

5 files changed

+59
-0
lines changed

5 files changed

+59
-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

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

8585

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

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`

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)