]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/swift.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / swift.vim
1 " Author: Dan Loman <https://github.com/namolnad>
2 " Description: Functions for integrating with Swift tools
3
4 " Find the nearest dir containing a Package.swift file and assume it is the root of the Swift project.
5 function! ale#swift#FindProjectRoot(buffer) abort
6     let l:swift_config = ale#path#FindNearestFile(a:buffer, 'Package.swift')
7
8     if !empty(l:swift_config)
9         return fnamemodify(l:swift_config, ':h')
10     endif
11
12     return ''
13 endfunction
14
15 " Support Apple Swift Format {{{1
16
17 call ale#Set('swift_appleswiftformat_executable', 'swift-format')
18 call ale#Set('swift_appleswiftformat_use_swiftpm', 0)
19
20 " Return the executable depending on whether or not to use Swift Package Manager.
21 "
22 " If not asked to use Swift Package Manager (use_swiftpm = 0), the returned
23 " value is the global executable, else the returned value is 'swift' because
24 " the final command line will be `swift run swift-format ...`.
25 "
26 " Failure is expected if use_swiftpm is `1` but no Package.swift can be located.
27 function! ale#swift#GetAppleSwiftFormatExecutable(buffer) abort
28     if !ale#Var(a:buffer, 'swift_appleswiftformat_use_swiftpm')
29         return ale#Var(a:buffer, 'swift_appleswiftformat_executable')
30     endif
31
32     if ale#path#FindNearestFile(a:buffer, 'Package.swift') is# ''
33         " If there is no Package.swift file, we don't use swift-format even if it exists,
34         " so we return '' to indicate failure.
35         return ''
36     endif
37
38     return 'swift'
39 endfunction
40
41 " Return the command depending on whether or not to use Swift Package Manager.
42 "
43 " If asked to use Swift Package Manager (use_swiftpm = 1), the command
44 " arguments are prefixed with 'swift run'.
45 "
46 " In either case, the configuration file is located and added to the command.
47 function! ale#swift#GetAppleSwiftFormatCommand(buffer) abort
48     let l:executable = ale#swift#GetAppleSwiftFormatExecutable(a:buffer)
49     let l:command_args = ''
50
51     if ale#Var(a:buffer, 'swift_appleswiftformat_use_swiftpm')
52         let l:command_args = ' ' . 'run swift-format'
53     endif
54
55     return ale#Escape(l:executable) . l:command_args
56 endfunction
57
58 " Locate the nearest '.swift-format' configuration file, and return the
59 " arguments, else return an empty string.
60 function! ale#swift#GetAppleSwiftFormatConfigArgs(buffer) abort
61     let l:config_filepath = ale#path#FindNearestFile(a:buffer, '.swift-format')
62
63     if l:config_filepath isnot# ''
64         return '--configuration' . ' ' . l:config_filepath
65     endif
66
67     return ''
68 endfunction
69
70 " }}}