diff options
author | w0rp <w0rp@users.noreply.github.com> | 2020-08-17 23:37:23 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-17 23:37:23 +0100 |
commit | bc6304bdb0361b3236222cf857427a924c9e2c4d (patch) | |
tree | 05eeb3779c913608850f58597480ded3a03d2f78 | |
parent | ac56574b55a11a8a921bb5169dd4dd3a7348aeeb (diff) | |
parent | 2548763d04568823c4ef240f7f115adbb1dcfca2 (diff) | |
download | ale-bc6304bdb0361b3236222cf857427a924c9e2c4d.zip |
Merge pull request #3007 from klaaspieter/swift-format
Add Apple's swift-format as a linter
-rw-r--r-- | ale_linters/swift/swiftformat.vim | 62 | ||||
-rw-r--r-- | doc/ale-supported-languages-and-tools.txt | 1 | ||||
-rw-r--r-- | supported-tools.md | 1 | ||||
-rw-r--r-- | test/command_callback/test_swift_swiftformat_command_callbacks.vader | 25 | ||||
-rw-r--r-- | test/handler/test_swiftformat_handler.vader | 28 |
5 files changed, 117 insertions, 0 deletions
diff --git a/ale_linters/swift/swiftformat.vim b/ale_linters/swift/swiftformat.vim new file mode 100644 index 00000000..2504511a --- /dev/null +++ b/ale_linters/swift/swiftformat.vim @@ -0,0 +1,62 @@ +" Author: Klaas Pieter Annema <https://github.com/klaaspieter> +" Description: Support for swift-format https://github.com/apple/swift-format + +let s:default_executable = 'swift-format' +call ale#Set('swift_swiftformat_executable', s:default_executable) + +function! ale_linters#swift#swiftformat#UseSwift(buffer) abort + let l:swift_config = ale#path#FindNearestFile(a:buffer, 'Package.swift') + let l:executable = ale#Var(a:buffer, 'swift_swiftformat_executable') + + return !empty(l:swift_config) && l:executable is# s:default_executable +endfunction + +function! ale_linters#swift#swiftformat#GetExecutable(buffer) abort + if ale_linters#swift#swiftformat#UseSwift(a:buffer) + return 'swift' + endif + + return ale#Var(a:buffer, 'swift_swiftformat_executable') +endfunction + +function! ale_linters#swift#swiftformat#GetCommand(buffer) abort + let l:executable = ale_linters#swift#swiftformat#GetExecutable(a:buffer) + let l:args = '--mode lint %t' + + if ale_linters#swift#swiftformat#UseSwift(a:buffer) + let l:args = 'run swift-format' . ' ' . l:args + endif + + return ale#Escape(l:executable) . ' ' . l:args +endfunction + +function! ale_linters#swift#swiftformat#Handle(buffer, lines) abort + " Matches 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': 'swift-format', +\ 'executable': function('ale_linters#swift#swiftformat#GetExecutable'), +\ 'command': function('ale_linters#swift#swiftformat#GetCommand'), +\ 'output_stream': 'stderr', +\ 'language': 'swift', +\ 'callback': 'ale_linters#swift#swiftformat#Handle' +\}) diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 2dc05287..b401176b 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -453,6 +453,7 @@ Notes: * Swift * `sourcekit-lsp` * `swiftformat` + * `swift-format` * `swiftlint` * Tcl * `nagelfar`!! diff --git a/supported-tools.md b/supported-tools.md index 7fa6ec4f..f91c0fa8 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -462,6 +462,7 @@ formatting. * Swift * [sourcekit-lsp](https://github.com/apple/sourcekit-lsp) * [swiftformat](https://github.com/nicklockwood/SwiftFormat) + * [swift-format](https://github.com/apple/swift-format) * [swiftlint](https://github.com/realm/SwiftLint) * Tcl * [nagelfar](http://nagelfar.sourceforge.net) :floppy_disk: diff --git a/test/command_callback/test_swift_swiftformat_command_callbacks.vader b/test/command_callback/test_swift_swiftformat_command_callbacks.vader new file mode 100644 index 00000000..7be20bf7 --- /dev/null +++ b/test/command_callback/test_swift_swiftformat_command_callbacks.vader @@ -0,0 +1,25 @@ +Before: + call ale#assert#SetUpLinterTest('swift', 'swiftformat') + +After: + call ale#assert#TearDownLinterTest() + +Execute(Should use default command when not in a swift package): + call ale#test#SetFilename('../swift-test-files/non-swift-package-project/src/folder/dummy.swift') + + AssertLinter 'swift-format', + \ ale#Escape('swift-format') . ' --mode lint %t' + +Execute(Should use swift run when in a swift package): + call ale#test#SetFilename('../swift-test-files/swift-package-project/src/folder/dummy.swift') + + AssertLinter 'swift', + \ ale#Escape('swift') . ' run swift-format --mode lint %t' + +Execute(Should let users configure a global executable and override local paths): + call ale#test#SetFilename('../swift-test-files/swift-package-project/src/folder/dummy.swift') + + let g:ale_swift_swiftformat_executable = '/path/to/custom/swift-format' + + AssertLinter '/path/to/custom/swift-format', + \ ale#Escape('/path/to/custom/swift-format') . ' --mode lint %t' diff --git a/test/handler/test_swiftformat_handler.vader b/test/handler/test_swiftformat_handler.vader new file mode 100644 index 00000000..3dcc4f1a --- /dev/null +++ b/test/handler/test_swiftformat_handler.vader @@ -0,0 +1,28 @@ +Before: + runtime ale_linters/swift/swiftformat.vim + +After: + call ale#linter#Reset() + +Execute(The swiftformat handler should parse lines correctly): + AssertEqual + \ [ + \ { + \ 'lnum': 4, + \ 'col': 21, + \ 'type': 'W', + \ 'code': 'DoNotUseSemicolons', + \ 'text': 'remove '';'' and move the next statement to the new line', + \ }, + \ { + \ 'lnum': 3, + \ 'col': 12, + \ 'type': 'W', + \ 'code': 'Spacing', + \ 'text': 'remove 1 space' + \ }, + \ ], + \ ale_linters#swift#swiftformat#Handle(bufnr(''), [ + \ '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', + \ ]) |