Skip to content

Commit 4b435eb

Browse files
authored
Add swift autoformatting using swift-format (#193)
1 parent f393d51 commit 4b435eb

File tree

6 files changed

+116
-0
lines changed

6 files changed

+116
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ helpfiles in the `doc/` directory. The helpfiles are also available via
3131
* Shell (shfmt)
3232
* [Vue](http://vuejs.org) (prettier)
3333
* Nix (nixpkgs-fmt)
34+
* Swift ([swift-format](https://github.com/apple/swift-format))
3435

3536
# Commands
3637

@@ -95,6 +96,7 @@ augroup autoformat_settings
9596
" Alternative: autocmd FileType python AutoFormatBuffer autopep8
9697
autocmd FileType rust AutoFormatBuffer rustfmt
9798
autocmd FileType vue AutoFormatBuffer prettier
99+
autocmd FileType swift AutoFormatBuffer swift-format
98100
augroup END
99101
```
100102

autoload/codefmt/swiftformat.vim

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
" Copyright 2022 Google Inc. All rights reserved.
2+
"
3+
" Licensed under the Apache License, Version 2.0 (the "License");
4+
" you may not use this file except in compliance with the License.
5+
" You may obtain a copy of the License at
6+
"
7+
" http://www.apache.org/licenses/LICENSE-2.0
8+
"
9+
" Unless required by applicable law or agreed to in writing, software
10+
" distributed under the License is distributed on an "AS IS" BASIS,
11+
" WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
" See the License for the specific language governing permissions and
13+
" limitations under the License.
14+
15+
let s:plugin = maktaba#plugin#Get('codefmt')
16+
17+
18+
""
19+
" @private
20+
" Formatter: swift-format
21+
function! codefmt#swiftformat#GetFormatter() abort
22+
let l:formatter = {
23+
\ 'name': 'swift-format',
24+
\ 'setup_instructions': 'Install swift-format from ' .
25+
\ '(https://github.com/apple/swift-format).'}
26+
27+
function l:formatter.IsAvailable() abort
28+
return executable(s:plugin.Flag('swift_format_executable'))
29+
endfunction
30+
31+
function l:formatter.AppliesToBuffer() abort
32+
return &filetype is# 'swift'
33+
endfunction
34+
35+
""
36+
" Reformat the current buffer with swift-format or the binary named in
37+
" @flag(swift_format_executable)
38+
"
39+
" We implement Format(), and not FormatRange{,s}(), because swift-format doesn't
40+
" provide a hook for formatting a range
41+
42+
function l:formatter.Format() abort
43+
let l:executable = s:plugin.Flag('swift_format_executable')
44+
45+
call codefmt#formatterhelpers#Format([
46+
\ l:executable])
47+
endfunction
48+
49+
return l:formatter
50+
endfunction

doc/codefmt.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ The current list of defaults by filetype is:
4848
* python: autopep8, black, yapf
4949
* rust: rustfmt
5050
* sh: shfmt
51+
* swift: swift-format
5152

5253
==============================================================================
5354
CONFIGURATION *codefmt-config*
@@ -189,6 +190,11 @@ Default: 'luaformatterfiveone' `
189190
The path to the cljstyle executable.
190191
Default: 'cljstyle' `
191192

193+
*codefmt:swift_format_executable*
194+
The path to the swift-format executable.
195+
Default: 'swift-format' `
196+
197+
192198
*codefmt:plugin[autocmds]*
193199
Configures whether plugin/autocmds.vim should be loaded.
194200
Default: 1 `

instant/flags.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ call s:plugin.Flag('shfmt_executable', 'shfmt')
143143
" takes no args and returns a list with command line arguments.
144144
call s:plugin.Flag('prettier_options', [])
145145

146+
""
147+
" The path to the swift-format executable.
148+
call s:plugin.Flag('swift_format_executable', 'swift-format')
149+
146150
""
147151
" @private
148152
function s:LookupPrettierExecutable() abort

plugin/register.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
" * python: autopep8, black, yapf
4343
" * rust: rustfmt
4444
" * sh: shfmt
45+
" * swift: swift-format
4546

4647

4748
let [s:plugin, s:enter] = maktaba#plugin#Enter(expand('<sfile>:p'))
@@ -75,3 +76,4 @@ call s:registry.AddExtension(codefmt#black#GetFormatter())
7576
call s:registry.AddExtension(codefmt#yapf#GetFormatter())
7677
call s:registry.AddExtension(codefmt#rustfmt#GetFormatter())
7778
call s:registry.AddExtension(codefmt#shfmt#GetFormatter())
79+
call s:registry.AddExtension(codefmt#swiftformat#GetFormatter())

vroom/swiftformat.vroom

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
The built-in swift-format formatter knows how to format swift code.
2+
If you aren't familiar with basic codefmt usage yet, see main.vroom first.
3+
4+
We'll set up codefmt and configure the vroom environment, then jump into some
5+
examples.
6+
7+
:source $VROOMDIR/setupvroom.vim
8+
9+
:let g:repeat_calls = []
10+
:function FakeRepeat(...)<CR>
11+
| call add(g:repeat_calls, a:000)<CR>
12+
:endfunction
13+
:call maktaba#test#Override('repeat#set', 'FakeRepeat')
14+
15+
:call codefmt#SetWhetherToPerformIsAvailableChecksForTesting(0)
16+
17+
18+
The swift-format formatter expects the swift-format executable to be installed on your
19+
system.
20+
21+
% f()
22+
:FormatCode swift-format
23+
! swift-format .*
24+
$ f()
25+
26+
You can format any buffer with swift-format specifying the formatter explicitly.
27+
28+
@clear
29+
% func f(a: String,b:Int)->String{
30+
| return "a" }
31+
:FormatCode swift-format
32+
! swift-format .*2>.*
33+
$ func f(a: String, b: Int) -> String {
34+
$ return "a"
35+
$ }
36+
func f(a: String, b: Int) -> String {
37+
return "a"
38+
}
39+
@end
40+
41+
The swift filetype will use the swift-format formatter by default.
42+
43+
@clear
44+
% f()
45+
46+
:set filetype=swift
47+
:FormatCode
48+
! swift-format .*
49+
$ f()
50+
51+
:set filetype=
52+

0 commit comments

Comments
 (0)