summaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
authorbosr <bosr@users.noreply.github.com>2021-04-07 12:34:34 +0200
committerGitHub <noreply@github.com>2021-04-07 19:34:34 +0900
commitf0887d3e6178482255f11aa378124aef3699245f (patch)
treece9b0300de08ab7220ab63ed4e857da876367f9f /autoload
parent06f57ca9733aab6e6b67015917fdfd4bf1c70c48 (diff)
downloadale-f0887d3e6178482255f11aa378124aef3699245f.zip
apple-swift-format: linter and fixer with config swiftpm support (#3671)
Diffstat (limited to 'autoload')
-rw-r--r--autoload/ale/fix/registry.vim5
-rw-r--r--autoload/ale/fixers/appleswiftformat.vim16
-rw-r--r--autoload/ale/swift.vim57
3 files changed, 78 insertions, 0 deletions
diff --git a/autoload/ale/fix/registry.vim b/autoload/ale/fix/registry.vim
index 0cf866dc..c26d4da3 100644
--- a/autoload/ale/fix/registry.vim
+++ b/autoload/ale/fix/registry.vim
@@ -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'],
diff --git a/autoload/ale/fixers/appleswiftformat.vim b/autoload/ale/fixers/appleswiftformat.vim
new file mode 100644
index 00000000..ca27e82c
--- /dev/null
+++ b/autoload/ale/fixers/appleswiftformat.vim
@@ -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
diff --git a/autoload/ale/swift.vim b/autoload/ale/swift.vim
index b31b8dc5..3232d42a 100644
--- a/autoload/ale/swift.vim
+++ b/autoload/ale/swift.vim
@@ -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
+
+" }}}