apple-swift-format: linter and fixer with config swiftpm support (#3671)

This commit is contained in:
bosr
2021-04-07 12:34:34 +02:00
committed by GitHub
parent 06f57ca973
commit f0887d3e61
14 changed files with 265 additions and 92 deletions

View File

@@ -191,6 +191,11 @@ let s:default_registry = {
\ 'suggested_filetypes': ['swift'],
\ 'description': 'Apply SwiftFormat to a file.',
\ },
\ 'apple-swift-format': {
\ 'function': 'ale#fixers#appleswiftformat#Fix',
\ 'suggested_filetypes': ['swift'],
\ 'description': 'Apply apple/swift-format to a file.',
\ },
\ 'phpcbf': {
\ 'function': 'ale#fixers#phpcbf#Fix',
\ 'suggested_filetypes': ['php'],

View File

@@ -0,0 +1,16 @@
" Author: (bosr) <bosr@bosr.cc>
" Description: Integration of apple/swift-format formatter with ALE.
function! ale#fixers#appleswiftformat#Fix(buffer) abort
let l:command_args = ale#swift#GetAppleSwiftFormatCommand(a:buffer) . ' format --in-place %t'
let l:config_args = ale#swift#GetAppleSwiftFormatConfigArgs(a:buffer)
if l:config_args isnot# ''
let l:command_args = l:command_args . ' ' . l:config_args
endif
return {
\ 'read_temporary_file': 1,
\ 'command': l:command_args,
\}
endfunction

View File

@@ -11,3 +11,60 @@ function! ale#swift#FindProjectRoot(buffer) abort
return ''
endfunction
" Support Apple Swift Format {{{1
call ale#Set('swift_appleswiftformat_executable', 'swift-format')
call ale#Set('swift_appleswiftformat_use_swiftpm', 0)
" Return the executable depending on whether or not to use Swift Package Manager.
"
" If not asked to use Swift Package Manager (use_swiftpm = 0), the returned
" value is the global executable, else the returned value is 'swift' because
" the final command line will be `swift run swift-format ...`.
"
" Failure is expected if use_swiftpm is `1` but no Package.swift can be located.
function! ale#swift#GetAppleSwiftFormatExecutable(buffer) abort
if !ale#Var(a:buffer, 'swift_appleswiftformat_use_swiftpm')
return ale#Var(a:buffer, 'swift_appleswiftformat_executable')
endif
if ale#path#FindNearestFile(a:buffer, 'Package.swift') is# ''
" If there is no Package.swift file, we don't use swift-format even if it exists,
" so we return '' to indicate failure.
return ''
endif
return 'swift'
endfunction
" Return the command depending on whether or not to use Swift Package Manager.
"
" If asked to use Swift Package Manager (use_swiftpm = 1), the command
" arguments are prefixed with 'swift run'.
"
" In either case, the configuration file is located and added to the command.
function! ale#swift#GetAppleSwiftFormatCommand(buffer) abort
let l:executable = ale#swift#GetAppleSwiftFormatExecutable(a:buffer)
let l:command_args = ''
if ale#Var(a:buffer, 'swift_appleswiftformat_use_swiftpm')
let l:command_args = ' ' . 'run swift-format'
endif
return ale#Escape(l:executable) . l:command_args
endfunction
" Locate the nearest '.swift-format' configuration file, and return the
" arguments, else return an empty string.
function! ale#swift#GetAppleSwiftFormatConfigArgs(buffer) abort
let l:config_filepath = ale#path#FindNearestFile(a:buffer, '.swift-format')
if l:config_filepath isnot# ''
return '--configuration' . ' ' . l:config_filepath
endif
return ''
endfunction
" }}}