Skip to content

Commit 8eb4803

Browse files
Add pymarkdown fixer (dense-analysis#5045)
1 parent 0360a73 commit 8eb4803

File tree

5 files changed

+141
-7
lines changed

5 files changed

+141
-7
lines changed

autoload/ale/fix/registry.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ let s:default_registry = {
457457
\ 'suggested_filetypes': ['markdown'],
458458
\ 'description': 'Fix markdown files with pandoc.',
459459
\ },
460+
\ 'pymarkdown': {
461+
\ 'function': 'ale#fixers#pymarkdown#Fix',
462+
\ 'suggested_filetypes': ['markdown'],
463+
\ 'description': 'Fix markdown files with pymarkdown.',
464+
\ },
460465
\ 'shfmt': {
461466
\ 'function': 'ale#fixers#shfmt#Fix',
462467
\ 'suggested_filetypes': ['sh'],

autoload/ale/fixers/pymarkdown.vim

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
scriptencoding utf-8
2+
" Author: Adrian Vollmer <[email protected]>
3+
" Description: Fix markdown files with pymarkdown.
4+
5+
call ale#Set('markdown_pymarkdown_executable', 'pymarkdown')
6+
call ale#Set('markdown_pymarkdown_options', '')
7+
call ale#Set('markdown_pymarkdown_use_global', get(g:, 'ale_use_global_executables', 0))
8+
call ale#Set('markdown_pymarkdown_auto_pipenv', 0)
9+
call ale#Set('markdown_pymarkdown_auto_poetry', 0)
10+
call ale#Set('markdown_pymarkdown_auto_uv', 0)
11+
12+
function! ale#fixers#pymarkdown#GetExecutable(buffer) abort
13+
if (ale#Var(a:buffer, 'python_auto_pipenv') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_pipenv'))
14+
\ && ale#python#PipenvPresent(a:buffer)
15+
return 'pipenv'
16+
endif
17+
18+
if (ale#Var(a:buffer, 'python_auto_poetry') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_poetry'))
19+
\ && ale#python#PoetryPresent(a:buffer)
20+
return 'poetry'
21+
endif
22+
23+
if (ale#Var(a:buffer, 'python_auto_uv') || ale#Var(a:buffer, 'markdown_pymarkdown_auto_uv'))
24+
\ && ale#python#UvPresent(a:buffer)
25+
return 'uv'
26+
endif
27+
28+
return ale#python#FindExecutable(a:buffer, 'markdown_pymarkdown', ['pymarkdown'])
29+
endfunction
30+
31+
function! ale#fixers#pymarkdown#Fix(buffer) abort
32+
let l:executable = ale#fixers#pymarkdown#GetExecutable(a:buffer)
33+
let l:options = ale#Var(a:buffer, 'markdown_pymarkdown_options')
34+
35+
let l:exec_args = l:executable =~? 'pipenv\|poetry\|uv$'
36+
\ ? ' run pymarkdown'
37+
\ : ''
38+
39+
return {
40+
\ 'command': ale#Escape(l:executable) . l:exec_args
41+
\ . ' fix'
42+
\ . (!empty(l:options) ? ' ' . l:options : '')
43+
\ . ' %t',
44+
\ 'read_temporary_file': 1,
45+
\}
46+
endfunction

doc/ale-markdown.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,11 @@ See |ale-javascript-prettier| for information about the available options.
111111
===============================================================================
112112
pymarkdown *ale-markdown-pymarkdown*
113113

114+
pymarkdown can be used both as a linter and a fixer for Markdown files.
115+
114116
*ale-options.markdown_pymarkdown_executable*
115117
*g:ale_markdown_pymarkdown_executable*
116-
*b:ale_markdown_pymarkdown_executable*
118+
*b:ale_markdown_pymarkdown_executable*
117119
markdown_pymarkdown_executable
118120
g:ale_markdown_pymarkdown_executable
119121
Type: |String|
@@ -126,7 +128,7 @@ g:ale_markdown_pymarkdown_executable
126128

127129
*ale-options.markdown_pymarkdown_options*
128130
*g:ale_markdown_pymarkdown_options*
129-
*b:ale_markdown_pymarkdown_options*
131+
*b:ale_markdown_pymarkdown_options*
130132
markdown_pymarkdown_options
131133
g:ale_markdown_pymarkdown_options
132134
Type: |String|
@@ -137,7 +139,7 @@ g:ale_markdown_pymarkdown_options
137139

138140
*ale-options.markdown_pymarkdown_use_global*
139141
*g:ale_markdown_pymarkdown_use_global*
140-
*b:ale_markdown_pymarkdown_use_global*
142+
*b:ale_markdown_pymarkdown_use_global*
141143
markdown_pymarkdown_use_global
142144
g:ale_markdown_pymarkdown_use_global
143145
Type: |Number|
@@ -147,7 +149,7 @@ g:ale_markdown_pymarkdown_use_global
147149

148150
*ale-options.markdown_pymarkdown_auto_pipenv*
149151
*g:ale_markdown_pymarkdown_auto_pipenv*
150-
*b:ale_markdown_pymarkdown_auto_pipenv*
152+
*b:ale_markdown_pymarkdown_auto_pipenv*
151153
markdown_pymarkdown_auto_pipenv
152154
g:ale_markdown_pymarkdown_auto_pipenv
153155
Type: |Number|
@@ -158,7 +160,7 @@ g:ale_markdown_pymarkdown_auto_pipenv
158160

159161
*ale-options.markdown_pymarkdown_auto_poetry*
160162
*g:ale_markdown_pymarkdown_auto_poetry*
161-
*b:ale_markdown_pymarkdown_auto_poetry*
163+
*b:ale_markdown_pymarkdown_auto_poetry*
162164
markdown_pymarkdown_auto_poetry
163165
g:ale_markdown_pymarkdown_auto_poetry
164166
Type: |Number|
@@ -169,7 +171,7 @@ g:ale_markdown_pymarkdown_auto_poetry
169171

170172
*ale-options.markdown_pymarkdown_auto_uv*
171173
*g:ale_markdown_pymarkdown_auto_uv*
172-
*b:ale_markdown_pymarkdown_auto_uv*
174+
*b:ale_markdown_pymarkdown_auto_uv*
173175
markdown_pymarkdown_auto_uv
174176
g:ale_markdown_pymarkdown_auto_uv
175177
Type: |Number|

supported-tools.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ formatting.
421421
* [pandoc](https://pandoc.org)
422422
* [prettier](https://github.com/prettier/prettier)
423423
* [proselint](http://proselint.com/)
424-
* [pymarkdown](https://github.com/jackdewinter/pymarkdown)
424+
* [pymarkdown](https://github.com/jackdewinter/pymarkdown) :floppy_disk:
425425
* [redpen](http://redpen.cc/)
426426
* [remark-lint](https://github.com/wooorm/remark-lint)
427427
* [textlint](https://textlint.github.io/)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
Before:
2+
Save g:ale_markdown_pymarkdown_executable
3+
Save g:ale_markdown_pymarkdown_options
4+
Save g:ale_markdown_pymarkdown_auto_pipenv
5+
Save g:ale_markdown_pymarkdown_auto_poetry
6+
Save g:ale_markdown_pymarkdown_auto_uv
7+
8+
After:
9+
Restore
10+
11+
Execute(The pymarkdown callback should return 'pymarkdown' as default command):
12+
AssertEqual
13+
\ {
14+
\ 'command': ale#Escape('pymarkdown') . ' fix %t',
15+
\ 'read_temporary_file': 1,
16+
\ },
17+
\ ale#fixers#pymarkdown#Fix(bufnr(''))
18+
19+
Execute(The pymarkdown executable and options should be configurable):
20+
let g:ale_markdown_pymarkdown_executable = 'foobar'
21+
let g:ale_markdown_pymarkdown_options = '--some-option'
22+
23+
AssertEqual
24+
\ {
25+
\ 'command': ale#Escape('foobar') . ' fix --some-option %t',
26+
\ 'read_temporary_file': 1,
27+
\ },
28+
\ ale#fixers#pymarkdown#Fix(bufnr(''))
29+
30+
Execute(Setting executable to 'pipenv' appends 'run pymarkdown'):
31+
let g:ale_markdown_pymarkdown_executable = 'path/to/pipenv'
32+
33+
AssertEqual
34+
\ {
35+
\ 'command': ale#Escape('path/to/pipenv') . ' run pymarkdown fix %t',
36+
\ 'read_temporary_file': 1,
37+
\ },
38+
\ ale#fixers#pymarkdown#Fix(bufnr(''))
39+
40+
Execute(Pipenv is detected when markdown_pymarkdown_auto_pipenv is set):
41+
let g:ale_markdown_pymarkdown_auto_pipenv = 1
42+
call ale#test#SetFilename('../test-files/python/pipenv/whatever.py')
43+
44+
AssertEqual
45+
\ {
46+
\ 'command': ale#Escape('pipenv') . ' run pymarkdown fix %t',
47+
\ 'read_temporary_file': 1,
48+
\ },
49+
\ ale#fixers#pymarkdown#Fix(bufnr(''))
50+
51+
Execute(Setting executable to 'poetry' appends 'run pymarkdown'):
52+
let g:ale_markdown_pymarkdown_executable = 'path/to/poetry'
53+
54+
AssertEqual
55+
\ {
56+
\ 'command': ale#Escape('path/to/poetry') . ' run pymarkdown fix %t',
57+
\ 'read_temporary_file': 1,
58+
\ },
59+
\ ale#fixers#pymarkdown#Fix(bufnr(''))
60+
61+
Execute(Poetry is detected when markdown_pymarkdown_auto_poetry is set):
62+
let g:ale_markdown_pymarkdown_auto_poetry = 1
63+
call ale#test#SetFilename('../test-files/python/poetry/whatever.py')
64+
65+
AssertEqual
66+
\ {
67+
\ 'command': ale#Escape('poetry') . ' run pymarkdown fix %t',
68+
\ 'read_temporary_file': 1,
69+
\ },
70+
\ ale#fixers#pymarkdown#Fix(bufnr(''))
71+
72+
Execute(uv is detected when markdown_pymarkdown_auto_uv is set):
73+
let g:ale_markdown_pymarkdown_auto_uv = 1
74+
call ale#test#SetFilename('../test-files/python/uv/whatever.py')
75+
76+
AssertEqual
77+
\ {
78+
\ 'command': ale#Escape('uv') . ' run pymarkdown fix %t',
79+
\ 'read_temporary_file': 1,
80+
\ },
81+
\ ale#fixers#pymarkdown#Fix(bufnr(''))

0 commit comments

Comments
 (0)