diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim index 25e7ef6e1..e9621dc96 100644 --- a/autoload/ale/fix/registry.vim +++ b/autoload/ale/fix/registry.vim @@ -462,6 +462,11 @@ let s:default_registry = { \ 'suggested_filetypes': ['markdown'], \ 'description': 'Fix markdown files with pandoc.', \ }, +\ 'mdformat': { +\ 'function': 'ale#fixers#mdformat#Fix', +\ 'suggested_filetypes': ['markdown'], +\ 'description': 'Fix Markdown files with mdformat.', +\ }, \ 'pymarkdown': { \ 'function': 'ale#fixers#pymarkdown#Fix', \ 'suggested_filetypes': ['markdown'], diff --git a/autoload/ale/fixers/mdformat.vim b/autoload/ale/fixers/mdformat.vim new file mode 100644 index 000000000..2f8af8f52 --- /dev/null +++ b/autoload/ale/fixers/mdformat.vim @@ -0,0 +1,43 @@ +" Author: Horacio +" Description: Fix Markdown files with mdformat. + +call ale#Set('markdown_mdformat_executable', 'mdformat') +call ale#Set('markdown_mdformat_options', '') +call ale#Set('markdown_mdformat_use_global', get(g:, 'ale_use_global_executables', 0)) +call ale#Set('markdown_mdformat_auto_pipenv', 0) +call ale#Set('markdown_mdformat_auto_poetry', 0) +call ale#Set('markdown_mdformat_auto_uv', 0) + +function! ale#fixers#mdformat#GetExecutable(buffer) abort + if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'markdown_mdformat_auto_pipenv')) + \ && ale#python#PipenvPresent(a:buffer) + return 'pipenv' + endif + + if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'markdown_mdformat_auto_poetry')) + \ && ale#python#PoetryPresent(a:buffer) + return 'poetry' + endif + + if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'markdown_mdformat_auto_uv')) + \ && ale#python#UvPresent(a:buffer) + return 'uv' + endif + + return ale#python#FindExecutable(a:buffer, 'markdown_mdformat', ['mdformat']) +endfunction + +function! ale#fixers#mdformat#Fix(buffer) abort + let l:executable = ale#fixers#mdformat#GetExecutable(a:buffer) + let l:command = ale#Escape(l:executable) + + if l:executable =~? '\(pipenv\|poetry\|uv\)$' + let l:command .= ' run mdformat' + endif + + return { + \ 'command': l:command + \ . ale#Pad(ale#Var(a:buffer, 'markdown_mdformat_options')) + \ . ' -', + \} +endfunction diff --git a/doc/ale-markdown.txt b/doc/ale-markdown.txt index ef3659be1..5076a9433 100644 --- a/doc/ale-markdown.txt +++ b/doc/ale-markdown.txt @@ -82,6 +82,77 @@ g:ale_markdown_marksman_executable Override the invoked `marksman` binary. +=============================================================================== +mdformat *ale-markdown-mdformat* + + *ale-options.markdown_mdformat_executable* + *g:ale_markdown_mdformat_executable* + *b:ale_markdown_mdformat_executable* +markdown_mdformat_executable +g:ale_markdown_mdformat_executable + Type: |String| + Default: `'mdformat'` + + See |ale-integrations-local-executables| + + Set this to `'pipenv'` to invoke `pipenv run mdformat`. + Set this to `'poetry'` to invoke `poetry run mdformat`. + Set this to `'uv'` to invoke `uv run mdformat`. + + *ale-options.markdown_mdformat_options* + *g:ale_markdown_mdformat_options* + *b:ale_markdown_mdformat_options* +markdown_mdformat_options +g:ale_markdown_mdformat_options + Type: |String| + Default: `''` + + This variable can be changed to add command-line arguments to mdformat. + + *ale-options.markdown_mdformat_use_global* + *g:ale_markdown_mdformat_use_global* + *b:ale_markdown_mdformat_use_global* +markdown_mdformat_use_global +g:ale_markdown_mdformat_use_global + Type: |Number| + Default: `get(g:, 'ale_use_global_executables', 0)` + + See |ale-integrations-local-executables| + + *ale-options.markdown_mdformat_auto_pipenv* + *g:ale_markdown_mdformat_auto_pipenv* + *b:ale_markdown_mdformat_auto_pipenv* +markdown_mdformat_auto_pipenv +g:ale_markdown_mdformat_auto_pipenv + Type: |Number| + Default: `0` + + Detect whether the file is inside a pipenv, and set the executable to + `pipenv` if true. This is overridden by a manually-set executable. + + *ale-options.markdown_mdformat_auto_poetry* + *g:ale_markdown_mdformat_auto_poetry* + *b:ale_markdown_mdformat_auto_poetry* +markdown_mdformat_auto_poetry +g:ale_markdown_mdformat_auto_poetry + Type: |Number| + Default: `0` + + Detect whether the file is inside a Poetry project, and set the executable + to `poetry` if true. This is overridden by a manually-set executable. + + *ale-options.markdown_mdformat_auto_uv* + *g:ale_markdown_mdformat_auto_uv* + *b:ale_markdown_mdformat_auto_uv* +markdown_mdformat_auto_uv +g:ale_markdown_mdformat_auto_uv + Type: |Number| + Default: `0` + + Detect whether the file is inside a uv project, and set the executable to + `uv` if true. This is overridden by a manually-set executable. + + =============================================================================== mdl *ale-markdown-mdl* diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 353d2b58c..7fd2d1631 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -436,6 +436,7 @@ Notes: * `languagetool`!! * `markdownlint`!! * `marksman` + * `mdformat` * `mdl` * `pandoc` * `prettier` diff --git a/doc/ale.txt b/doc/ale.txt index 0c55eab59..cb12cf0cf 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -3779,6 +3779,7 @@ documented in additional help files. harper................................|ale-markdown-harper| markdownlint..........................|ale-markdown-markdownlint| marksman..............................|ale-markdown-marksman| + mdformat..............................|ale-markdown-mdformat| mdl...................................|ale-markdown-mdl| pandoc................................|ale-markdown-pandoc| prettier..............................|ale-markdown-prettier| diff --git a/supported-tools.md b/supported-tools.md index 2085be38c..ac954aa7f 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -446,6 +446,7 @@ formatting. * [languagetool](https://languagetool.org/) :floppy_disk: :speech_balloon: * [markdownlint](https://github.com/DavidAnson/markdownlint) :floppy_disk: * [marksman](https://github.com/artempyanykh/marksman) :speech_balloon: + * [mdformat](https://github.com/hukkin/mdformat) * [mdl](https://github.com/mivok/markdownlint) * [pandoc](https://pandoc.org) * [prettier](https://github.com/prettier/prettier) diff --git a/test/fixers/test_mdformat_fixer_callback.vader b/test/fixers/test_mdformat_fixer_callback.vader new file mode 100644 index 000000000..7b00591f5 --- /dev/null +++ b/test/fixers/test_mdformat_fixer_callback.vader @@ -0,0 +1,46 @@ +Before: + call ale#assert#SetUpFixerTest('markdown', 'mdformat') + +After: + call ale#assert#TearDownFixerTest() + +Execute(The mdformat callback should return the default command): + AssertFixer + \ { + \ 'command': ale#Escape('mdformat') . ' -', + \ } + +Execute(The mdformat executable and options should be configurable): + let g:ale_markdown_mdformat_executable = 'foobar' + let g:ale_markdown_mdformat_options = '--wrap 80' + + AssertFixer + \ { + \ 'command': ale#Escape('foobar') . ' --wrap 80 -', + \ } + +Execute(Setting executable to pipenv should append run mdformat): + let g:ale_markdown_mdformat_executable = 'path/to/pipenv' + + AssertFixer + \ { + \ 'command': ale#Escape('path/to/pipenv') . ' run mdformat -', + \ } + +Execute(Poetry should be detected when auto_poetry is set): + let g:ale_markdown_mdformat_auto_poetry = 1 + call ale#test#SetFilename('../test-files/python/poetry/whatever.py') + + AssertFixer + \ { + \ 'command': ale#Escape('poetry') . ' run mdformat -', + \ } + +Execute(uv should be detected when auto_uv is set): + let g:ale_markdown_mdformat_auto_uv = 1 + call ale#test#SetFilename('../test-files/python/uv/whatever.py') + + AssertFixer + \ { + \ 'command': ale#Escape('uv') . ' run mdformat -', + \ }