From 54ab506d6b1aaf003528e4e98ac99ad7add55e3b Mon Sep 17 00:00:00 2001 From: Adrian Vollmer Date: Tue, 9 Sep 2025 13:58:17 +0200 Subject: [PATCH] Add pymarkdown fixer --- autoload/ale/fix/registry.vim | 5 ++ autoload/ale/fixers/pymarkdown.vim | 46 +++++++++++ doc/ale-markdown.txt | 14 ++-- supported-tools.md | 2 +- .../test_pymarkdown_fixer_callback.vader | 81 +++++++++++++++++++ 5 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 autoload/ale/fixers/pymarkdown.vim create mode 100644 test/fixers/test_pymarkdown_fixer_callback.vader diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 8288b1a9d8..fd5c4009c1 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -452,6 +452,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['markdown'], \ 'description': 'Fix markdown files with pandoc.', \ }, +\ 'pymarkdown': { +\ 'function': 'ale#fixers#pymarkdown#Fix', +\ 'suggested_filetypes': ['markdown'], +\ 'description': 'Fix markdown files with pymarkdown.', +\ }, \ 'shfmt': { \ 'function': 'ale#fixers#shfmt#Fix', \ 'suggested_filetypes': ['sh'], diff --git a/autoload/ale/fixers/pymarkdown.vim b/autoload/ale/fixers/pymarkdown.vim new file mode 100644 index 0000000000..0ff782a953 --- /dev/null +++ b/autoload/ale/fixers/pymarkdown.vim @@ -0,0 +1,46 @@ +scriptencoding utf-8 +" Author: Adrian Vollmer +" Description: Fix markdown files with pymarkdown. + +call ale#Set('markdown_pymarkdown_executable', 'pymarkdown') +call ale#Set('markdown_pymarkdown_options', '') +call ale#Set('markdown_pymarkdown_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('markdown_pymarkdown_auto_pipenv', 0) +call ale#Set('markdown_pymarkdown_auto_poetry', 0) +call ale#Set('markdown_pymarkdown_auto_uv', 0) + +function! ale#fixers#pymarkdown#GetExecutable(buffer) abort + if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_pipenv')) + \ && ale#python#PipenvPresent(a:buffer) + return 'pipenv' + endif + + if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_poetry')) + \ && ale#python#PoetryPresent(a:buffer) + return 'poetry' + endif + + if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_uv')) + \ && ale#python#UvPresent(a:buffer) + return 'uv' + endif + + return ale#python#FindExecutable(a:buffer, 'markdown_pymarkdown', ['pymarkdown']) +endfunction + +function! ale#fixers#pymarkdown#Fix(buffer) abort + let l:executable = ale#fixers#pymarkdown#GetExecutable(a:buffer) + let l:options = ale#Var(a:buffer, 'markdown_pymarkdown_options') + + let l:exec_args = l:executable =~? 'pipenv\|poetry\|uv$' + \ ? ' run pymarkdown' + \ : '' + + return { + \ 'command': ale#Escape(l:executable) . l:exec_args + \ . ' fix' + \ . (!empty(l:options) ? ' ' . l:options : '') + \ . ' %t', + \ 'read_temporary_file': 1, + \} +endfunction diff --git a/doc/ale-markdown.txt b/doc/ale-markdown.txt index a2b23e334f..a309f1387f 100644 --- a/doc/ale-markdown.txt +++ b/doc/ale-markdown.txt @@ -111,9 +111,11 @@ See |ale-javascript-prettier| for information about the available options. =============================================================================== pymarkdown *ale-markdown-pymarkdown* +pymarkdown can be used both as a linter and a fixer for Markdown files. + *ale-options.markdown_pymarkdown_executable* *g:ale_markdown_pymarkdown_executable* - *b:ale_markdown_pymarkdown_executable* + *b:ale_markdown_pymarkdown_executable* markdown_pymarkdown_executable g:ale_markdown_pymarkdown_executable Type: |String| @@ -126,7 +128,7 @@ g:ale_markdown_pymarkdown_executable *ale-options.markdown_pymarkdown_options* *g:ale_markdown_pymarkdown_options* - *b:ale_markdown_pymarkdown_options* + *b:ale_markdown_pymarkdown_options* markdown_pymarkdown_options g:ale_markdown_pymarkdown_options Type: |String| @@ -137,7 +139,7 @@ g:ale_markdown_pymarkdown_options *ale-options.markdown_pymarkdown_use_global* *g:ale_markdown_pymarkdown_use_global* - *b:ale_markdown_pymarkdown_use_global* + *b:ale_markdown_pymarkdown_use_global* markdown_pymarkdown_use_global g:ale_markdown_pymarkdown_use_global Type: |Number| @@ -147,7 +149,7 @@ g:ale_markdown_pymarkdown_use_global *ale-options.markdown_pymarkdown_auto_pipenv* *g:ale_markdown_pymarkdown_auto_pipenv* - *b:ale_markdown_pymarkdown_auto_pipenv* + *b:ale_markdown_pymarkdown_auto_pipenv* markdown_pymarkdown_auto_pipenv g:ale_markdown_pymarkdown_auto_pipenv Type: |Number| @@ -158,7 +160,7 @@ g:ale_markdown_pymarkdown_auto_pipenv *ale-options.markdown_pymarkdown_auto_poetry* *g:ale_markdown_pymarkdown_auto_poetry* - *b:ale_markdown_pymarkdown_auto_poetry* + *b:ale_markdown_pymarkdown_auto_poetry* markdown_pymarkdown_auto_poetry g:ale_markdown_pymarkdown_auto_poetry Type: |Number| @@ -169,7 +171,7 @@ g:ale_markdown_pymarkdown_auto_poetry *ale-options.markdown_pymarkdown_auto_uv* *g:ale_markdown_pymarkdown_auto_uv* - *b:ale_markdown_pymarkdown_auto_uv* + *b:ale_markdown_pymarkdown_auto_uv* markdown_pymarkdown_auto_uv g:ale_markdown_pymarkdown_auto_uv Type: |Number| diff --git a/supported-tools.md b/supported-tools.md index a89b9230c5..a50f09e2f2 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -418,7 +418,7 @@ formatting. * [pandoc](https://pandoc.org) * [prettier](https://github.com/prettier/prettier) * [proselint](http://proselint.com/) - * [pymarkdown](https://github.com/jackdewinter/pymarkdown) + * [pymarkdown](https://github.com/jackdewinter/pymarkdown) :floppy_disk: * [redpen](http://redpen.cc/) * [remark-lint](https://github.com/wooorm/remark-lint) * [textlint](https://textlint.github.io/) diff --git a/test/fixers/test_pymarkdown_fixer_callback.vader b/test/fixers/test_pymarkdown_fixer_callback.vader new file mode 100644 index 0000000000..41d9968d7d --- /dev/null +++ b/test/fixers/test_pymarkdown_fixer_callback.vader @@ -0,0 +1,81 @@ +Before: + Save g:ale_markdown_pymarkdown_executable + Save g:ale_markdown_pymarkdown_options + Save g:ale_markdown_pymarkdown_auto_pipenv + Save g:ale_markdown_pymarkdown_auto_poetry + Save g:ale_markdown_pymarkdown_auto_uv + +After: + Restore + +Execute(The pymarkdown callback should return 'pymarkdown' as default command): + AssertEqual + \ { + \ 'command': ale#Escape('pymarkdown') . ' fix %t', + \ 'read_temporary_file': 1, + \ }, + \ ale#fixers#pymarkdown#Fix(bufnr('')) + +Execute(The pymarkdown executable and options should be configurable): + let g:ale_markdown_pymarkdown_executable = 'foobar' + let g:ale_markdown_pymarkdown_options = '--some-option' + + AssertEqual + \ { + \ 'command': ale#Escape('foobar') . ' fix --some-option %t', + \ 'read_temporary_file': 1, + \ }, + \ ale#fixers#pymarkdown#Fix(bufnr('')) + +Execute(Setting executable to 'pipenv' appends 'run pymarkdown'): + let g:ale_markdown_pymarkdown_executable = 'path/to/pipenv' + + AssertEqual + \ { + \ 'command': ale#Escape('path/to/pipenv') . ' run pymarkdown fix %t', + \ 'read_temporary_file': 1, + \ }, + \ ale#fixers#pymarkdown#Fix(bufnr('')) + +Execute(Pipenv is detected when markdown_pymarkdown_auto_pipenv is set): + let g:ale_markdown_pymarkdown_auto_pipenv = 1 + call ale#test#SetFilename('../test-files/python/pipenv/whatever.py') + + AssertEqual + \ { + \ 'command': ale#Escape('pipenv') . ' run pymarkdown fix %t', + \ 'read_temporary_file': 1, + \ }, + \ ale#fixers#pymarkdown#Fix(bufnr('')) + +Execute(Setting executable to 'poetry' appends 'run pymarkdown'): + let g:ale_markdown_pymarkdown_executable = 'path/to/poetry' + + AssertEqual + \ { + \ 'command': ale#Escape('path/to/poetry') . ' run pymarkdown fix %t', + \ 'read_temporary_file': 1, + \ }, + \ ale#fixers#pymarkdown#Fix(bufnr('')) + +Execute(Poetry is detected when markdown_pymarkdown_auto_poetry is set): + let g:ale_markdown_pymarkdown_auto_poetry = 1 + call ale#test#SetFilename('../test-files/python/poetry/whatever.py') + + AssertEqual + \ { + \ 'command': ale#Escape('poetry') . ' run pymarkdown fix %t', + \ 'read_temporary_file': 1, + \ }, + \ ale#fixers#pymarkdown#Fix(bufnr('')) + +Execute(uv is detected when markdown_pymarkdown_auto_uv is set): + let g:ale_markdown_pymarkdown_auto_uv = 1 + call ale#test#SetFilename('../test-files/python/uv/whatever.py') + + AssertEqual + \ { + \ 'command': ale#Escape('uv') . ' run pymarkdown fix %t', + \ 'read_temporary_file': 1, + \ }, + \ ale#fixers#pymarkdown#Fix(bufnr('')) \ No newline at end of file