From 864d0861e47568ca5e69c9cba82af655730ae0d7 Mon Sep 17 00:00:00 2001 From: Dan Loman Date: Mon, 8 Apr 2019 08:25:30 -0700 Subject: [PATCH] [lsp] Add Swift / SourceKit-LSP support (#2420) * [linter] Add LSP support for Swift via sourcekit-lsp --- ale_linters/swift/sourcekitlsp.vim | 13 ++++++++++++ autoload/ale/swift.vim | 13 ++++++++++++ ...swift_sourcekitlsp_command_callbacks.vader | 21 +++++++++++++++++++ .../src/folder/dummy.swift | 0 .../swift-package-project/Package.swift | 0 .../src/folder/dummy.swift | 0 test/test_swift_find_project_root.vader | 18 ++++++++++++++++ 7 files changed, 65 insertions(+) create mode 100644 ale_linters/swift/sourcekitlsp.vim create mode 100644 autoload/ale/swift.vim create mode 100644 test/command_callback/test_swift_sourcekitlsp_command_callbacks.vader create mode 100644 test/swift-test-files/non-swift-package-project/src/folder/dummy.swift create mode 100644 test/swift-test-files/swift-package-project/Package.swift create mode 100644 test/swift-test-files/swift-package-project/src/folder/dummy.swift create mode 100644 test/test_swift_find_project_root.vader diff --git a/ale_linters/swift/sourcekitlsp.vim b/ale_linters/swift/sourcekitlsp.vim new file mode 100644 index 00000000..560893bf --- /dev/null +++ b/ale_linters/swift/sourcekitlsp.vim @@ -0,0 +1,13 @@ +" Author: Dan Loman +" Description: Support for sourcekit-lsp https://github.com/apple/sourcekit-lsp + +call ale#Set('sourcekit_lsp_executable', 'sourcekit-lsp') + +call ale#linter#Define('swift', { +\ 'name': 'sourcekitlsp', +\ 'lsp': 'stdio', +\ 'executable': {b -> ale#Var(b, 'sourcekit_lsp_executable')}, +\ 'command': '%e', +\ 'project_root': function('ale#swift#FindProjectRoot'), +\ 'language': 'swift', +\}) diff --git a/autoload/ale/swift.vim b/autoload/ale/swift.vim new file mode 100644 index 00000000..b31b8dc5 --- /dev/null +++ b/autoload/ale/swift.vim @@ -0,0 +1,13 @@ +" Author: Dan Loman +" Description: Functions for integrating with Swift tools + +" Find the nearest dir containing a Package.swift file and assume it is the root of the Swift project. +function! ale#swift#FindProjectRoot(buffer) abort + let l:swift_config = ale#path#FindNearestFile(a:buffer, 'Package.swift') + + if !empty(l:swift_config) + return fnamemodify(l:swift_config, ':h') + endif + + return '' +endfunction diff --git a/test/command_callback/test_swift_sourcekitlsp_command_callbacks.vader b/test/command_callback/test_swift_sourcekitlsp_command_callbacks.vader new file mode 100644 index 00000000..4611c6de --- /dev/null +++ b/test/command_callback/test_swift_sourcekitlsp_command_callbacks.vader @@ -0,0 +1,21 @@ +Before: + call ale#assert#SetUpLinterTest('swift', 'sourcekitlsp') + +After: + call ale#assert#TearDownLinterTest() + +Execute(The default executable path should be correct): + call ale#test#SetFilename('../swift-test-files/swift-package-project/src/folder/dummy.swift') + + AssertLinter 'sourcekit-lsp', ale#Escape('sourcekit-lsp') + +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_sourcekit_lsp_executable = '/path/to/custom/sourcekitlsp' + + AssertLinter '/path/to/custom/sourcekitlsp', + \ ale#Escape('/path/to/custom/sourcekitlsp') + +Execute(The language should be correct): + AssertLSPLanguage 'swift' diff --git a/test/swift-test-files/non-swift-package-project/src/folder/dummy.swift b/test/swift-test-files/non-swift-package-project/src/folder/dummy.swift new file mode 100644 index 00000000..e69de29b diff --git a/test/swift-test-files/swift-package-project/Package.swift b/test/swift-test-files/swift-package-project/Package.swift new file mode 100644 index 00000000..e69de29b diff --git a/test/swift-test-files/swift-package-project/src/folder/dummy.swift b/test/swift-test-files/swift-package-project/src/folder/dummy.swift new file mode 100644 index 00000000..e69de29b diff --git a/test/test_swift_find_project_root.vader b/test/test_swift_find_project_root.vader new file mode 100644 index 00000000..7cb1cc29 --- /dev/null +++ b/test/test_swift_find_project_root.vader @@ -0,0 +1,18 @@ +Before: + call ale#test#SetDirectory('/testplugin/test') + +After: + call ale#test#RestoreDirectory() + +Execute(Detect root of Swift project with Package.swift correctly): + call ale#test#SetFilename('swift-test-files/swift-package-project/src/folder/dummy.swift') + AssertEqual + \ ale#path#Simplify(g:dir . '/swift-test-files/swift-package-project'), + \ ale#swift#FindProjectRoot(bufnr('')) + +Execute(Detect no root in case of non-Package.swift project): + call ale#test#SetFilename('swift-test-files/non-swift-package-project/src/folder/dummy.swift') + AssertEqual + \ '', + \ ale#swift#FindProjectRoot(bufnr('')) +