Skip to content

Commit c3bbab5

Browse files
committed
Add dprint fixer
1 parent dcec4b3 commit c3bbab5

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
call ale#Set('dprint_executable', 'dprint')
2+
call ale#Set('dprint_options', '')
3+
call ale#Set('dprint_config', '')
4+
5+
function! ale#fixers#dprint#GetExecutable(buffer) abort
6+
return ale#Var(a:buffer, 'dprint_executable')
7+
endfunction
8+
9+
function! ale#fixers#dprint#GetConfig(buffer) abort
10+
let l:project_config = ale#Var(a:buffer, 'dprint_config')
11+
12+
if !empty(l:project_config)
13+
return l:project_config
14+
endif
15+
16+
let l:possible_project_configs = [
17+
\ '.dprint.json',
18+
\ 'dprint.json'
19+
\]
20+
21+
for l:possible_config in l:possible_project_configs
22+
let l:found_config = ale#path#FindNearestFile(a:buffer, l:possible_config)
23+
24+
if !empty(l:found_config)
25+
return l:found_config
26+
endif
27+
endfor
28+
29+
return ''
30+
endfunction
31+
32+
function! ale#fixers#dprint#Fix(buffer) abort
33+
let l:executable = ale#fixers#dprint#GetExecutable(a:buffer)
34+
35+
if !executable(l:executable)
36+
return 0
37+
endif
38+
39+
let l:options = ale#Var(a:buffer, 'dprint_options')
40+
let l:config = ale#fixers#dprint#GetConfig(a:buffer)
41+
42+
if !empty(l:config)
43+
let l:options = l:options . ' -c ' . ale#Escape(l:config)
44+
endif
45+
46+
let l:options = l:options . ' --stdin %s'
47+
48+
return {
49+
\ 'command': ale#Escape(l:executable) . ' fmt ' . l:options,
50+
\}
51+
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Before:
2+
call ale#assert#SetUpFixerTest('typescript', 'dprint')
3+
4+
After:
5+
Restore
6+
call ale#assert#TearDownFixerTest()
7+
8+
Execute(The dprint callback should return the correct default values):
9+
AssertFixer
10+
\ {
11+
\ 'read_temporary_file': 1,
12+
\ 'command':
13+
\ ale#Escape('dprint')
14+
\ . ' ' . ale#Escape('')
15+
\ . ' %t',
16+
\ }
17+
18+
Execute(The dprint callback should include custom options):
19+
let g:ale_typescript_dprint_options = '-c dprint.json'
20+
21+
AssertFixer
22+
\ {
23+
\ 'read_temporary_file': 1,
24+
\ 'command':
25+
\ ale#Escape('dprint')
26+
\ . ' ' . ale#Escape('-c dprint.json')
27+
\ . ' %t',
28+
\ }

0 commit comments

Comments
 (0)