From b5fce4efabcb80ec3d0e9cd8ef0f056d480feb37 Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Fri, 11 Oct 2024 14:02:08 -0700 Subject: [PATCH] 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. --- autoload/ale/fix/registry.vim | 5 ++++ autoload/ale/fixers/json_pytool.vim | 20 ++++++++++++++ doc/ale-json.txt | 32 +++++++++++++++++++++++ doc/ale-supported-languages-and-tools.txt | 1 + supported-tools.md | 1 + 5 files changed, 59 insertions(+) create mode 100644 autoload/ale/fixers/json_pytool.vim diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 88468371..3a07227f 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -446,6 +446,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'], diff --git a/autoload/ale/fixers/json_pytool.vim b/autoload/ale/fixers/json_pytool.vim new file mode 100644 index 00000000..17aeeea1 --- /dev/null +++ b/autoload/ale/fixers/json_pytool.vim @@ -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 diff --git a/doc/ale-json.txt b/doc/ale-json.txt index 7b240373..02ac5379 100644 --- a/doc/ale-json.txt +++ b/doc/ale-json.txt @@ -77,6 +77,38 @@ g:ale_json_fixjson_use_global *g:ale_json_fixjson_use_global* See |ale-integrations-local-executables| +=============================================================================== +python's json.tool *ale-json-pytool* + +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* diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index b0689781..b5e2a654 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -317,6 +317,7 @@ Notes: * `eslint` * `fixjson` * `jq` + * `json.tool` * `jsonlint` * `prettier` * `spectral` diff --git a/supported-tools.md b/supported-tools.md index 68635d99..d1973b02 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -326,6 +326,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)