Skip to content

Commit 0de6460

Browse files
authored
Merge branch 'google:master' into master
2 parents 010da55 + 406686d commit 0de6460

File tree

10 files changed

+250
-0
lines changed

10 files changed

+250
-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/buildifier.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,15 @@ function! codefmt#buildifier#GetFormatter() abort
3939
" @flag(buildifier)
4040
" @throws ShellError
4141
function l:formatter.Format() abort
42+
let l:lint_flag = s:plugin.Flag('buildifier_lint_mode')
4243
let l:cmd = [ s:plugin.Flag('buildifier_executable') ]
44+
if !empty(l:lint_flag)
45+
let l:cmd += ["--lint=" . l:lint_flag]
46+
endif
47+
let l:warnings_flag = s:plugin.Flag('buildifier_warnings')
48+
if !empty(l:warnings_flag)
49+
let l:cmd += ["--warnings=" . l:warnings_flag]
50+
endif
4351
let l:fname = expand('%:p')
4452
if !empty(l:fname)
4553
let l:cmd += ['-path', l:fname]

autoload/codefmt/clangformat.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ function! codefmt#clangformat#GetFormatter() abort
129129
""
130130
" Reformat buffer with clang-format, only targeting [ranges] if given.
131131
function l:formatter.FormatRanges(ranges) abort
132+
if line('$') == 1 && getline(1) == ''
133+
" If the buffer is completely empty, there's nothing to do
134+
return
135+
endif
136+
132137
let l:Style_value = s:plugin.Flag('clang_format_style')
133138
if type(l:Style_value) is# type('')
134139
let l:style = l:Style_value

autoload/codefmt/isort.vim

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
" Copyright 2020 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+
16+
let s:plugin = maktaba#plugin#Get('codefmt')
17+
18+
19+
""
20+
" @private
21+
" Formatter: isort
22+
function! codefmt#isort#GetFormatter() abort
23+
let l:formatter = {
24+
\ 'name': 'isort',
25+
\ 'setup_instructions': 'Install isort ' .
26+
\ '(https://pypi.python.org/pypi/isort/).'}
27+
28+
function l:formatter.IsAvailable() abort
29+
return executable(s:plugin.Flag('isort_executable'))
30+
endfunction
31+
32+
function l:formatter.AppliesToBuffer() abort
33+
return &filetype is# 'python'
34+
endfunction
35+
36+
""
37+
" Reformat the current buffer with isort or the binary named in
38+
" @flag(isort_executable)
39+
"
40+
" We implement Format(), and not FormatRange{,s}(), because isort doesn't
41+
" provide a hook for formatting a range
42+
function l:formatter.Format() abort
43+
let l:executable = s:plugin.Flag('isort_executable')
44+
45+
call codefmt#formatterhelpers#Format([
46+
\ l:executable,
47+
\ '-'])
48+
endfunction
49+
50+
return l:formatter
51+
endfunction

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: 37 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*
@@ -93,6 +94,10 @@ Default: 'yapf' `
9394
The path to the black executable.
9495
Default: 'black' `
9596

97+
*codefmt:isort_executable*
98+
The path to the isort executable.
99+
Default: 'isort' `
100+
96101
*codefmt:gn_executable*
97102
The path to the gn executable.
98103
Default: 'gn' `
@@ -101,6 +106,34 @@ Default: 'gn' `
101106
The path to the buildifier executable.
102107
Default: 'buildifier' `
103108

109+
*codefmt:buildifier_lint_mode*
110+
The lint_mode for buildifier. passed to buildifier --lint parameter.
111+
112+
Options:
113+
"" (empty): Use default from buildifier.
114+
"Off": Do not fix issues.
115+
"Fix": Fix issues automatically during formatting.
116+
"Warn": Format only if there are no issues; if there are issues, it will cause
117+
an error and do no formatting.
118+
Default: '' `
119+
120+
*codefmt:buildifier_warnings*
121+
The warnings options passed to buildifier to modify the defaults. Whatever is
122+
specified is added to the commandline after "--warnings=". For example, if you
123+
add this to your config:
124+
>
125+
Glaive codefmt buildifier_warnings='-module-docstring,+unsorted-dict-items'
126+
<
127+
Then buildifier will omit the "module-docstring" warning, but add
128+
"unsorted-dict-items" (which is ignored by default). This works also in
129+
fix-mode, in which case dictionary items will be resorted upon buffer save.
130+
131+
Options:
132+
"" (empty): Use default warnings from buildifier.
133+
"-some-warning": Remove 'some-warning' from the warning set.
134+
"+some-warning": Add 'some-warning' to the warning set.
135+
Default: '' `
136+
104137
*codefmt:google_java_executable*
105138
The path to the google-java executable. Generally, this should have the form:
106139
`java -jar /path/to/google-java`
@@ -134,6 +167,10 @@ Command line arguments to feed prettier. Either a list or callable that takes
134167
no args and returns a list with command line arguments.
135168
Default: [] `
136169

170+
*codefmt:swift_format_executable*
171+
The path to the swift-format executable.
172+
Default: 'swift-format' `
173+
137174
*codefmt:prettier_executable*
138175
The path to the prettier executable. String, list, or callable that takes no
139176
args and returns a string or a list. The default uses npx if available, so

instant/flags.vim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ call s:plugin.Flag('yapf_executable', 'yapf')
8888
" The path to the black executable.
8989
call s:plugin.Flag('black_executable', 'black')
9090

91+
""
92+
" The path to the isort executable.
93+
call s:plugin.Flag('isort_executable', 'isort')
94+
9195
""
9296
" The path to the gn executable.
9397
call s:plugin.Flag('gn_executable', 'gn')
@@ -96,6 +100,33 @@ call s:plugin.Flag('gn_executable', 'gn')
96100
" The path to the buildifier executable.
97101
call s:plugin.Flag('buildifier_executable', 'buildifier')
98102

103+
""
104+
" The lint_mode for buildifier. passed to buildifier --lint parameter.
105+
"
106+
" Options:
107+
" - "" (empty): Use default from buildifier.
108+
" - "Off": Do not fix issues.
109+
" - "Fix": Fix issues automatically during formatting.
110+
" - "Warn": Format only if there are no issues; if there are issues, it will
111+
" cause an error and do no formatting.
112+
call s:plugin.Flag('buildifier_lint_mode', '')
113+
114+
""
115+
" The warnings options passed to buildifier to modify the defaults. Whatever is
116+
" specified is added to the commandline after "--warnings=". For example, if you
117+
" add this to your config: >
118+
" Glaive codefmt buildifier_warnings='-module-docstring,+unsorted-dict-items'
119+
" <
120+
" Then buildifier will omit the "module-docstring" warning, but add
121+
" "unsorted-dict-items" (which is ignored by default). This works also in
122+
" fix-mode, in which case dictionary items will be resorted upon buffer save.
123+
"
124+
" Options:
125+
" - "" (empty): Use default warnings from buildifier.
126+
" - "-some-warning": Remove 'some-warning' from the warning set.
127+
" - "+some-warning": Add 'some-warning' to the warning set.
128+
call s:plugin.Flag('buildifier_warnings', '')
129+
99130
""
100131
" The path to the google-java executable. Generally, this should have the
101132
" form:
@@ -131,6 +162,10 @@ call s:plugin.Flag('shfmt_executable', 'shfmt')
131162
" takes no args and returns a list with command line arguments.
132163
call s:plugin.Flag('prettier_options', [])
133164

165+
""
166+
" The path to the swift-format executable.
167+
call s:plugin.Flag('swift_format_executable', 'swift-format')
168+
134169
""
135170
" @private
136171
function s:LookupPrettierExecutable() abort

plugin/register.vim

Lines changed: 3 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'))
@@ -70,7 +71,9 @@ call s:registry.AddExtension(codefmt#ktfmt#GetFormatter())
7071
call s:registry.AddExtension(codefmt#luaformatterfiveone#GetFormatter())
7172
call s:registry.AddExtension(codefmt#nixpkgs_fmt#GetFormatter())
7273
call s:registry.AddExtension(codefmt#autopep8#GetFormatter())
74+
call s:registry.AddExtension(codefmt#isort#GetFormatter())
7375
call s:registry.AddExtension(codefmt#black#GetFormatter())
7476
call s:registry.AddExtension(codefmt#yapf#GetFormatter())
7577
call s:registry.AddExtension(codefmt#rustfmt#GetFormatter())
7678
call s:registry.AddExtension(codefmt#shfmt#GetFormatter())
79+
call s:registry.AddExtension(codefmt#swiftformat#GetFormatter())

vroom/clangformat.vroom

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,10 @@ current buffer.
181181

182182
The -assume-filename arg passed above is also related, used by the clang-format
183183
tool to detect any custom style rules particular to the current file.
184+
185+
186+
Bug #102. When the buffer is empty, clang-format does _not_ return a cursor
187+
position
188+
189+
@clear
190+
:FormatCode clang-format

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)