Add support for `dart format` fixer (#3764)

This commit is contained in:
ghsang 2021-07-16 16:20:47 +09:00 committed by GitHub
parent d8f4e8b708
commit d6dbb5398e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 0 deletions

View File

@ -381,6 +381,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['dart'],
\ 'description': 'Fix Dart files with dartfmt.',
\ },
\ 'dart-format': {
\ 'function': 'ale#fixers#dart_format#Fix',
\ 'suggested_filetypes': ['dart'],
\ 'description': 'Fix Dart files with dart format.',
\ },
\ 'xmllint': {
\ 'function': 'ale#fixers#xmllint#Fix',
\ 'suggested_filetypes': ['xml'],

View File

@ -0,0 +1,18 @@
" Author: ghsang <gwonhyuksang@gmail.com>
" Description: Integration of dart format with ALE.
call ale#Set('dart_format_executable', 'dart')
call ale#Set('dart_format_options', '')
function! ale#fixers#dart_format#Fix(buffer) abort
let l:executable = ale#Var(a:buffer, 'dart_format_executable')
let l:options = ale#Var(a:buffer, 'dart_format_options')
return {
\ 'command': ale#Escape(l:executable)
\ . ' format'
\ . (empty(l:options) ? '' : ' ' . l:options)
\ . ' %t',
\ 'read_temporary_file': 1,
\}
endfunction

View File

@ -58,6 +58,37 @@ g:ale_dart_dartanalyzer_executable *g:ale_dart_dartanalyzer_executable*
This variable can be set to change the path to dartanalyzer.
===============================================================================
dart-format *ale-dart-format*
Installation
-------------------------------------------------------------------------------
Installing Dart should probably ensure that `dart` is in your `$PATH`.
In case it is not, try to set the executable option to its absolute path. : >
" Set the executable path for dart to the absolute path to it.
let g:ale_dart_format_executable = '/usr/lib/dart/bin/dart'
>
Options
-------------------------------------------------------------------------------
g:ale_dart_format_executable *g:ale_dart_format_executable*
*b:ale_dart_format_executable*
Type: |String|
Default: `'dart'`
This variable can be set to specify an absolute path to the
format executable (or to specify an alternate executable).
g:ale_dart_format_options *g:ale_dart_format_options*
*b:ale_dart_format_options*
Type: |String|
Default: `''`
This variable can be set to pass additional options to the dart format fixer.
===============================================================================
dartfmt *ale-dart-dartfmt*

View File

@ -124,6 +124,7 @@ Notes:
* `dafny`!!
* Dart
* `analysis_server`
* `dart-format`!!
* `dartanalyzer`!!
* `dartfmt`!!
* `language_server`

View File

@ -2699,6 +2699,7 @@ documented in additional help files.
dart....................................|ale-dart-options|
analysis_server.......................|ale-dart-analysis_server|
dartanalyzer..........................|ale-dart-dartanalyzer|
dart-format...........................|ale-dart-format|
dartfmt...............................|ale-dart-dartfmt|
desktop.................................|ale-desktop-options|
desktop-file-validate.................|ale-desktop-desktop-file-validate|

View File

@ -133,6 +133,7 @@ formatting.
* [dafny](https://rise4fun.com/Dafny) :floppy_disk:
* Dart
* [analysis_server](https://github.com/dart-lang/sdk/tree/master/pkg/analysis_server)
* [dart-format](https://github.com/dart-lang/sdk/tree/master/utils/dartfmt)
* [dartanalyzer](https://github.com/dart-lang/sdk/tree/master/pkg/analyzer_cli) :floppy_disk:
* [dartfmt](https://github.com/dart-lang/sdk/tree/master/utils/dartfmt)
* [language_server](https://github.com/natebosch/dart_language_server)

View File

@ -0,0 +1,40 @@
Before:
Save g:ale_dart_format_executable
Save g:ale_dart_format_options
" Use an invalid global executable, so we don't match it.
let g:ale_dart_format_executable = 'xxxinvalid'
let g:ale_dart_format_options = ''
call ale#test#SetDirectory('/testplugin/test/fixers')
After:
Restore
call ale#test#RestoreDirectory()
Execute(The dart format callback should return the correct default values):
call ale#test#SetFilename('../test-files/dart/testfile.dart')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape('xxxinvalid')
\ . ' format'
\ . ' %t',
\ },
\ ale#fixers#dart_format#Fix(bufnr(''))
Execute(The dart format callback should include custom dart format options):
let g:ale_dart_format_options = "-l 80"
call ale#test#SetFilename('../test-files/dart/testfile.dart')
AssertEqual
\ {
\ 'read_temporary_file': 1,
\ 'command': ale#Escape('xxxinvalid')
\ . ' format'
\ . ' ' . g:ale_dart_format_options
\ . ' %t',
\ },
\ ale#fixers#dart_format#Fix(bufnr(''))