Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apple-swift-format: linter and fixer with config swiftpm support (#3671)
- Loading branch information
Showing
14 changed files
with
265 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
" Authors: Klaas Pieter Annema <https://github.com/klaaspieter>, bosr <bosr@bosr.cc> | ||
" Description: Support for swift-format https://github.com/apple/swift-format | ||
|
||
function! ale_linters#swift#appleswiftformat#GetLinterCommand(buffer) abort | ||
let l:command_args = ale#swift#GetAppleSwiftFormatCommand(a:buffer) . ' lint %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 l:command_args | ||
endfunction | ||
|
||
function! ale_linters#swift#appleswiftformat#Handle(buffer, lines) abort | ||
" Matches the typical output of swift-format, that is lines of the following pattern: | ||
" | ||
" Sources/main.swift:4:21: warning: [DoNotUseSemicolons] remove ';' and move the next statement to the new line | ||
" Sources/main.swift:3:12: warning: [Spacing] remove 1 space | ||
let l:pattern = '\v^.*:(\d+):(\d+): (\S+): \[(\S+)\] (.*)$' | ||
let l:output = [] | ||
|
||
for l:match in ale#util#GetMatches(a:lines, l:pattern) | ||
call add(l:output, { | ||
\ 'lnum': l:match[1] + 0, | ||
\ 'col': l:match[2] + 0, | ||
\ 'type': l:match[3] is# 'error' ? 'E' : 'W', | ||
\ 'code': l:match[4], | ||
\ 'text': l:match[5], | ||
\}) | ||
endfor | ||
|
||
return l:output | ||
endfunction | ||
|
||
call ale#linter#Define('swift', { | ||
\ 'name': 'apple-swift-format', | ||
\ 'executable': function('ale#swift#GetAppleSwiftFormatExecutable'), | ||
\ 'command': function('ale_linters#swift#appleswiftformat#GetLinterCommand'), | ||
\ 'output_stream': 'stderr', | ||
\ 'language': 'swift', | ||
\ 'callback': 'ale_linters#swift#appleswiftformat#Handle' | ||
\}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Before: | ||
call ale#assert#SetUpFixerTest('swift', 'apple-swift-format') | ||
|
||
After: | ||
call ale#assert#TearDownFixerTest() | ||
|
||
Execute(The swiftformat callback should return the correct default values): | ||
call ale#test#SetFilename('../test-files/swift/dummy.swift') | ||
let g:ale_swift_appleswiftformat_executable = 'xxxinvalid' | ||
let g:ale_swift_appleswiftformat_use_swiftpm = 0 | ||
|
||
AssertEqual | ||
\ { | ||
\ 'read_temporary_file': 1, | ||
\ 'command': ale#Escape(g:ale_swift_appleswiftformat_executable) | ||
\ . ' format --in-place %t', | ||
\ }, | ||
\ ale#fixers#appleswiftformat#Fix(bufnr('')) | ||
|
||
Execute(The swiftformat callback should return the correct default values and located configuration): | ||
call ale#test#SetDirectory('/testplugin/test/test-files/swift/swift-package-project-with-config') | ||
call ale#test#SetFilename('src/folder/dummy.swift') | ||
|
||
let g:ale_swift_appleswiftformat_executable = 'xxxinvalid' | ||
let g:ale_swift_appleswiftformat_use_swiftpm = 0 | ||
|
||
AssertEqual | ||
\ { | ||
\ 'read_temporary_file': 1, | ||
\ 'command': ale#Escape(g:ale_swift_appleswiftformat_executable) | ||
\ . ' format --in-place %t --configuration ' . glob(g:dir . '/.swift-format'), | ||
\ }, | ||
\ ale#fixers#appleswiftformat#Fix(bufnr('')) | ||
|
||
call ale#test#RestoreDirectory() | ||
|
||
Execute(The swiftformat callback should use swiftpm is use_swiftpm is set to 1): | ||
call ale#test#SetFilename('../test-files/swift/swift-package-project/src/folder/dummy.swift') | ||
let g:ale_swift_appleswiftformat_use_swiftpm = 1 | ||
|
||
AssertEqual | ||
\ { | ||
\ 'read_temporary_file': 1, | ||
\ 'command': ale#Escape('swift') | ||
\ . ' run swift-format format --in-place %t', | ||
\ }, | ||
\ ale#fixers#appleswiftformat#Fix(bufnr('')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Before: | ||
call ale#assert#SetUpLinterTest('swift', 'appleswiftformat') | ||
|
||
After: | ||
call ale#assert#TearDownLinterTest() | ||
|
||
Execute(Should use default command when use_swiftpm is not set): | ||
call ale#test#SetFilename('../test-files/swift/non-swift-package-project/src/folder/dummy.swift') | ||
|
||
let g:ale_swift_appleswiftformat_executable = 'swift-format' | ||
let g:ale_swift_appleswiftformat_use_swiftpm = 0 | ||
|
||
AssertLinter 'swift-format', ale#Escape('swift-format') . ' lint %t' | ||
|
||
Execute(Should use default command and available configuration when use_swiftpm is not set): | ||
call ale#test#SetDirectory('/testplugin/test/test-files/swift/swift-package-project-with-config') | ||
call ale#test#SetFilename('src/folder/dummy.swift') | ||
|
||
let g:ale_swift_appleswiftformat_executable = 'swift-format' | ||
let g:ale_swift_appleswiftformat_use_swiftpm = 0 | ||
|
||
AssertLinter 'swift-format', | ||
\ ale#Escape('swift-format') . ' lint %t ' . '--configuration ' | ||
\ . glob(g:dir . '/.swift-format') | ||
|
||
call ale#test#RestoreDirectory() | ||
|
||
Execute(Should use swift run when use_swiftpm is set to 1): | ||
call ale#test#SetFilename('../test-files/swift/swift-package-project/src/folder/dummy.swift') | ||
|
||
let g:ale_swift_appleswiftformat_use_swiftpm = 1 | ||
|
||
AssertLinter 'swift', ale#Escape('swift') . ' run swift-format lint %t' | ||
|
||
Execute(Should use the provided global executable): | ||
call ale#test#SetFilename('../test-files/swift/swift-package-project/src/folder/dummy.swift') | ||
|
||
let g:ale_swift_appleswiftformat_executable = '/path/to/custom/swift-format' | ||
let g:ale_swift_appleswiftformat_use_swiftpm = 0 | ||
|
||
AssertLinter '/path/to/custom/swift-format', | ||
\ ale#Escape('/path/to/custom/swift-format') . ' lint %t' |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.