Skip to content

Commit 728bc6c

Browse files
committed
Add dprint fixer
1 parent dcec4b3 commit 728bc6c

File tree

7 files changed

+80
-0
lines changed

7 files changed

+80
-0
lines changed

autoload/ale/fix/registry.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,11 @@ let s:default_registry = {
491491
\ 'suggested_filetypes': ['lua'],
492492
\ 'description': 'Fix Lua files with luafmt.',
493493
\ },
494+
\ 'dprint': {
495+
\ 'function': 'ale#fixers#dprint#Fix',
496+
\ 'suggested_filetypes': ['javascript', 'typescript', 'json', 'markdown'],
497+
\ 'description': 'Pluggable and configurable code formatting platform',
498+
\ },
494499
\ 'stylua': {
495500
\ 'function': 'ale#fixers#stylua#Fix',
496501
\ 'suggested_filetypes': ['lua'],

autoload/ale/fixers/dprint.vim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
call ale#Set('dprint_executable', 'dprint')
2+
call ale#Set('dprint_executable_override', 0)
3+
call ale#Set('dprint_use_global', get(g:, 'ale_use_global_executables', 0))
4+
call ale#Set('dprint_options', '')
5+
call ale#Set('dprint_config', 'dprint.json')
6+
7+
function! ale#fixers#dprint#Fix(buffer) abort
8+
let l:executable = ale#path#FindExecutable(a:buffer, 'dprint', ['dprint'])
9+
let l:executable_override = ale#Var(a:buffer, 'dprint_executable_override')
10+
11+
if !executable(l:executable) && !l:executable_override
12+
return 0
13+
endif
14+
15+
let l:options = ale#Var(a:buffer, 'dprint_options')
16+
let l:config = ale#path#FindNearestFile(a:buffer, ale#Var(a:buffer, 'dprint_config'))
17+
18+
if !empty(l:config)
19+
let l:options = l:options . ' -c ' . ale#Escape(l:config)
20+
endif
21+
22+
let l:options = l:options . ' --stdin %s'
23+
24+
return {
25+
\ 'command': ale#Escape(l:executable)
26+
\ . ' fmt '
27+
\ . l:options
28+
\}
29+
endfunction

doc/ale-supported-languages-and-tools.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ Notes:
267267
* JavaScript
268268
* `cspell`
269269
* `deno`
270+
* `dprint`
270271
* `eslint`
271272
* `fecs`
272273
* `flow`
@@ -586,6 +587,7 @@ Notes:
586587
* TypeScript
587588
* `cspell`
588589
* `deno`
590+
* `dprint`
589591
* `eslint`
590592
* `fecs`
591593
* `prettier`

supported-tools.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ formatting.
276276
* JavaScript
277277
* [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell)
278278
* [deno](https://deno.land/)
279+
* [dprint](https://dprint.dev/)
279280
* [eslint](http://eslint.org/)
280281
* [fecs](http://fecs.baidu.com/)
281282
* [flow](https://flowtype.org/)
@@ -595,6 +596,7 @@ formatting.
595596
* TypeScript
596597
* [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell)
597598
* [deno](https://deno.land/)
599+
* [dprint](https://dprint.dev/)
598600
* [eslint](http://eslint.org/)
599601
* [fecs](http://fecs.baidu.com/)
600602
* [prettier](https://github.com/prettier/prettier)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Before:
2+
call ale#assert#SetUpFixerTest('typescript', 'dprint')
3+
call ale#test#SetFilename('../test-files/dprint/blank.ts')
4+
let g:ale_dprint_executable_override = 0
5+
let g:ale_dprint_executable = 'dprint'
6+
let g:ale_dprint_config = ''
7+
8+
After:
9+
Restore
10+
call ale#assert#TearDownFixerTest()
11+
12+
Execute(The dprint callback should return 0 for a non-existent executable):
13+
let g:ale_dprint_executable = 'foo'
14+
AssertFixer 0
15+
16+
Execute(The dprint callback should return the correct default values):
17+
let g:ale_dprint_executable_override = 1
18+
AssertFixer {
19+
\ 'command': ale#Escape('dprint')
20+
\ . ' fmt '
21+
\ . ' --stdin %s'
22+
\ }
23+
24+
Execute(The dprint callback should include config):
25+
let g:ale_dprint_executable_override = 1
26+
let g:ale_dprint_config = 'dprint.json'
27+
28+
AssertFixer {
29+
\ 'command': ale#Escape('dprint')
30+
\ . ' fmt '
31+
\ . ' -c ' . ale#Escape('/testplugin/test/test-files/dprint/dprint.json') . ' --stdin %s'
32+
\ }
33+
34+
Execute(The dprint callback should include custom options):
35+
let g:ale_dprint_executable_override = 1
36+
let g:ale_dprint_options = '--verbose'
37+
38+
AssertFixer {
39+
\ 'command': ale#Escape('dprint')
40+
\ . ' fmt '
41+
\ . '--verbose' . ' --stdin %s'
42+
\ }

test/test-files/dprint/blank.ts

Whitespace-only changes.

test/test-files/dprint/dprint.json

Whitespace-only changes.

0 commit comments

Comments
 (0)