From 1a4b08539bf44ef84516d26760093734e0257340 Mon Sep 17 00:00:00 2001 From: Ben Falconer Date: Wed, 6 Jun 2018 16:58:49 +0100 Subject: [PATCH 1/3] Allow initialization options to be passed to language servers --- autoload/ale/linter.vim | 24 ++++++++++++++++++++ autoload/ale/lsp.vim | 13 ++++++----- autoload/ale/lsp/message.vim | 3 ++- doc/ale.txt | 15 +++++++++++++ test/lsp/test_lsp_client_messages.vader | 3 ++- test/test_linter_defintion_processing.vader | 25 +++++++++++++++++++++ 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/autoload/ale/linter.vim b/autoload/ale/linter.vim index c791ba4..5eb2fd8 100644 --- a/autoload/ale/linter.vim +++ b/autoload/ale/linter.vim @@ -227,6 +227,21 @@ function! ale#linter#PreProcess(linter) abort throw '`completion_filter` must be a callback' endif endif + + if has_key(a:linter, 'initialization_options_callback') + if has_key(a:linter, 'initialization_options') + throw 'Only one of `initialization_options` or ' + \ . '`initialization_options_callback` should be set' + endif + + let l:obj.initialization_options_callback = a:linter.initialization_options_callback + + if !s:IsCallback(l:obj.initialization_options_callback) + throw '`initialization_options_callback` must be a callback if defined' + endif + elseif has_key(a:linter, 'initialization_options') + let l:obj.initialization_options = a:linter.initialization_options + endif endif let l:obj.output_stream = get(a:linter, 'output_stream', 'stdout') @@ -451,12 +466,20 @@ function! ale#linter#StartLSP(buffer, linter, callback) abort return {} endif + let l:initialization_options = {} + if has_key(a:linter, 'initialization_options_callback') + let l:initialization_options = ale#util#GetFunction(a:linter.initialization_options_callback)(a:buffer) + elseif has_key(a:linter, 'initialization_options') + let l:initialization_options = a:linter.initialization_options + endif + if a:linter.lsp is# 'socket' let l:address = ale#linter#GetAddress(a:buffer, a:linter) let l:conn_id = ale#lsp#ConnectToAddress( \ l:address, \ l:root, \ a:callback, + \ l:initialization_options, \) else let l:executable = ale#linter#GetExecutable(a:buffer, a:linter) @@ -474,6 +497,7 @@ function! ale#linter#StartLSP(buffer, linter, callback) abort \ l:command, \ l:root, \ a:callback, + \ l:initialization_options, \) endif diff --git a/autoload/ale/lsp.vim b/autoload/ale/lsp.vim index c1d04db..df4f16d 100644 --- a/autoload/ale/lsp.vim +++ b/autoload/ale/lsp.vim @@ -6,7 +6,7 @@ let s:connections = [] let g:ale_lsp_next_message_id = 1 -function! s:NewConnection() abort +function! s:NewConnection(initialization_options) abort " id: The job ID as a Number, or the server address as a string. " data: The message data received so far. " executable: An executable only set for program connections. @@ -18,6 +18,7 @@ function! s:NewConnection() abort \ 'projects': {}, \ 'open_documents': [], \ 'callback_list': [], + \ 'initialization_options': a:initialization_options, \} call add(s:connections, l:conn) @@ -271,7 +272,7 @@ endfunction " " The job ID will be returned for for the program if it ran, otherwise " 0 will be returned. -function! ale#lsp#StartProgram(executable, command, project_root, callback) abort +function! ale#lsp#StartProgram(executable, command, project_root, callback, initialization_options) abort if !executable(a:executable) return 0 endif @@ -279,7 +280,7 @@ function! ale#lsp#StartProgram(executable, command, project_root, callback) abor let l:conn = s:FindConnection('executable', a:executable) " Get the current connection or a new one. - let l:conn = !empty(l:conn) ? l:conn : s:NewConnection() + let l:conn = !empty(l:conn) ? l:conn : s:NewConnection(a:initialization_options) let l:conn.executable = a:executable if !has_key(l:conn, 'id') || !ale#job#IsRunning(l:conn.id) @@ -305,10 +306,10 @@ function! ale#lsp#StartProgram(executable, command, project_root, callback) abor endfunction " Connect to an address and set up a callback for handling responses. -function! ale#lsp#ConnectToAddress(address, project_root, callback) abort +function! ale#lsp#ConnectToAddress(address, project_root, callback, initialization_options) abort let l:conn = s:FindConnection('id', a:address) " Get the current connection or a new one. - let l:conn = !empty(l:conn) ? l:conn : s:NewConnection() + let l:conn = !empty(l:conn) ? l:conn : s:NewConnection(a:initialization_options) if !has_key(l:conn, 'channel') || ch_status(l:conn.channel) isnot# 'open' let l:conn.channnel = ch_open(a:address, { @@ -383,7 +384,7 @@ function! ale#lsp#Send(conn_id, message, ...) abort " Only send the init message once. if !l:project.init_request_id let [l:init_id, l:init_data] = ale#lsp#CreateMessageData( - \ ale#lsp#message#Initialize(l:project_root), + \ ale#lsp#message#Initialize(l:project_root, l:conn.initialization_options), \) let l:project.init_request_id = l:init_id diff --git a/autoload/ale/lsp/message.vim b/autoload/ale/lsp/message.vim index 5637fa2..1c0499d 100644 --- a/autoload/ale/lsp/message.vim +++ b/autoload/ale/lsp/message.vim @@ -24,12 +24,13 @@ function! ale#lsp#message#GetNextVersionID() abort return l:id endfunction -function! ale#lsp#message#Initialize(root_path) abort +function! ale#lsp#message#Initialize(root_path, initialization_options) abort " TODO: Define needed capabilities. return [0, 'initialize', { \ 'processId': getpid(), \ 'rootPath': a:root_path, \ 'capabilities': {}, + \ 'initializationOptions': a:initialization_options, \}] endfunction diff --git a/doc/ale.txt b/doc/ale.txt index 66a81f3..0808e2a 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -2331,6 +2331,10 @@ ale#linter#Define(filetype, linter) *ale#linter#Define()* An optional `completion_filter` callback may be defined for filtering completion results. + An optional `initialization_options` or + `initialization_options_callback` may be defined to + pass initialization options to the LSP. + `project_root_callback` A |String| or |Funcref| for a callback function accepting a buffer number. A |String| should be returned representing the path to the project for the @@ -2372,6 +2376,17 @@ ale#linter#Define(filetype, linter) *ale#linter#Define()* setting can make it easier to guess the linter name by offering a few alternatives. + `initialization_options` A |Dictionary| of initialization options for LSPs. + This will be fed (as JSON) to the LSP in the + initialize command. + + `initialization_options_callback` + A |String| or |Funcref| for a callback function + accepting a buffer number. A |Dictionary| should be + returned for initialization options to pass the LSP. + This can be used in place of `initialization_options` + when more complicated processing is needed. + Only one of `command`, `command_callback`, or `command_chain` should be specified. `command_callback` is generally recommended when a command string needs to be generated dynamically, or any global options are used. diff --git a/test/lsp/test_lsp_client_messages.vader b/test/lsp/test_lsp_client_messages.vader index 89a29c8..e7eda33 100644 --- a/test/lsp/test_lsp_client_messages.vader +++ b/test/lsp/test_lsp_client_messages.vader @@ -16,9 +16,10 @@ Execute(ale#lsp#message#Initialize() should return correct messages): \ 'processId': getpid(), \ 'rootPath': '/foo/bar', \ 'capabilities': {}, + \ 'initializationOptions': {'foo': 'bar'}, \ } \ ], - \ ale#lsp#message#Initialize('/foo/bar') + \ ale#lsp#message#Initialize('/foo/bar', {'foo': 'bar'}) Execute(ale#lsp#message#Initialized() should return correct messages): AssertEqual [1, 'initialized'], ale#lsp#message#Initialized() diff --git a/test/test_linter_defintion_processing.vader b/test/test_linter_defintion_processing.vader index 653587b..48a4a39 100644 --- a/test/test_linter_defintion_processing.vader +++ b/test/test_linter_defintion_processing.vader @@ -465,3 +465,28 @@ Execute(PreProcess should complain about address_callback for non-LSP linters): AssertThrows call ale#linter#PreProcess(g:linter) AssertEqual '`address_callback` cannot be used when lsp != ''socket''', g:vader_exception + +Execute(PreProcess should complain about using initialization_options and initialization_options_callback together): + let g:linter = { + \ 'name': 'x', + \ 'lsp': 'socket', + \ 'address_callback': 'X', + \ 'language': 'x', + \ 'project_root_callback': 'x', + \ 'initialization_options': 'x', + \ 'initialization_options_callback': 'x', + \} + + AssertThrows call ale#linter#PreProcess(g:linter) + AssertEqual 'Only one of `initialization_options` or `initialization_options_callback` should be set', g:vader_exception + +Execute (PreProcess should throw when initialization_options_callback is not a callback): + AssertThrows call ale#linter#PreProcess({ + \ 'name': 'foo', + \ 'lsp': 'socket', + \ 'address_callback': 'X', + \ 'language': 'x', + \ 'project_root_callback': 'x', + \ 'initialization_options_callback': {}, + \}) + AssertEqual '`initialization_options_callback` must be a callback if defined', g:vader_exception From 641c0c797ba5dc4adac199fca4015d833a7e1f61 Mon Sep 17 00:00:00 2001 From: Ben Falconer Date: Wed, 6 Jun 2018 16:59:44 +0100 Subject: [PATCH 2/3] Pass rootUri to LSPs in addition to rootPath --- autoload/ale/lsp/message.vim | 2 ++ test/lsp/test_lsp_client_messages.vader | 1 + 2 files changed, 3 insertions(+) diff --git a/autoload/ale/lsp/message.vim b/autoload/ale/lsp/message.vim index 1c0499d..9e05156 100644 --- a/autoload/ale/lsp/message.vim +++ b/autoload/ale/lsp/message.vim @@ -26,11 +26,13 @@ endfunction function! ale#lsp#message#Initialize(root_path, initialization_options) abort " TODO: Define needed capabilities. + " NOTE: rootPath is deprecated in favour of rootUri return [0, 'initialize', { \ 'processId': getpid(), \ 'rootPath': a:root_path, \ 'capabilities': {}, \ 'initializationOptions': a:initialization_options, + \ 'rootUri': ale#path#ToURI(a:root_path), \}] endfunction diff --git a/test/lsp/test_lsp_client_messages.vader b/test/lsp/test_lsp_client_messages.vader index e7eda33..dc28c2e 100644 --- a/test/lsp/test_lsp_client_messages.vader +++ b/test/lsp/test_lsp_client_messages.vader @@ -17,6 +17,7 @@ Execute(ale#lsp#message#Initialize() should return correct messages): \ 'rootPath': '/foo/bar', \ 'capabilities': {}, \ 'initializationOptions': {'foo': 'bar'}, + \ 'rootUri': 'file:///foo/bar', \ } \ ], \ ale#lsp#message#Initialize('/foo/bar', {'foo': 'bar'}) From 20db9ab7197b6341644fcddc87f82c5731852ea8 Mon Sep 17 00:00:00 2001 From: Ben Falconer Date: Wed, 6 Jun 2018 17:03:15 +0100 Subject: [PATCH 3/3] Add the cquery LSP #1475 #1594 --- README.md | 7 +-- ale_linters/cpp/cquery.vim | 34 +++++++++++++ doc/ale-cpp.txt | 20 ++++++++ doc/ale.txt | 3 +- .../test_cpp_cquery_command_callbacks.vader | 48 +++++++++++++++++++ 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 ale_linters/cpp/cquery.vim create mode 100644 test/command_callback/test_cpp_cquery_command_callbacks.vader diff --git a/README.md b/README.md index c42e83f..90ec9d4 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ formatting. | Bash | shell [-n flag](https://www.gnu.org/software/bash/manual/bash.html#index-set), [shellcheck](https://www.shellcheck.net/), [shfmt](https://github.com/mvdan/sh) | | Bourne Shell | shell [-n flag](http://linux.die.net/man/1/sh), [shellcheck](https://www.shellcheck.net/), [shfmt](https://github.com/mvdan/sh) | | C | [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint), [clang](http://clang.llvm.org/), [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [clang-format](https://clang.llvm.org/docs/ClangFormat.html), [flawfinder](https://www.dwheeler.com/flawfinder/), [gcc](https://gcc.gnu.org/) | -| C++ (filetype cpp) | [clang](http://clang.llvm.org/), [clangcheck](http://clang.llvm.org/docs/ClangCheck.html) !!, [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [clang-format](https://clang.llvm.org/docs/ClangFormat.html), [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint) !!, [flawfinder](https://www.dwheeler.com/flawfinder/), [gcc](https://gcc.gnu.org/) | +| C++ (filetype cpp) | [clang](http://clang.llvm.org/), [clangcheck](http://clang.llvm.org/docs/ClangCheck.html) !!, [clangtidy](http://clang.llvm.org/extra/clang-tidy/) !!, [clang-format](https://clang.llvm.org/docs/ClangFormat.html), [cppcheck](http://cppcheck.sourceforge.net), [cpplint](https://github.com/google/styleguide/tree/gh-pages/cpplint) !!, [cquery](https://github.com/cquery-project/cquery), [flawfinder](https://www.dwheeler.com/flawfinder/), [gcc](https://gcc.gnu.org/) | | CUDA | [nvcc](http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html) | | C# | [mcs](http://www.mono-project.com/docs/about-mono/languages/csharp/) see:`help ale-cs-mcs` for details, [mcsc](http://www.mono-project.com/docs/about-mono/languages/csharp/) !! see:`help ale-cs-mcsc` for details and configuration| | Chef | [foodcritic](http://www.foodcritic.io/) | @@ -749,8 +749,9 @@ ALE cannot easily detect which compiler flags to use. Some tools and build configurations can generate [compile_commands.json](https://clang.llvm.org/docs/JSONCompilationDatabase.html) -files. The `cppcheck`, `clangcheck` and `clangtidy` linters can read these -files for automatically determining the appropriate compiler flags to use. +files. The `cppcheck`, `clangcheck`, `clangtidy` and `cquery` linters can read +these files for automatically determining the appropriate compiler flags to +use. For linting with compilers like `gcc` and `clang`, and with other tools, you will need to tell ALE which compiler flags to use yourself. You can use diff --git a/ale_linters/cpp/cquery.vim b/ale_linters/cpp/cquery.vim new file mode 100644 index 0000000..dbaed52 --- /dev/null +++ b/ale_linters/cpp/cquery.vim @@ -0,0 +1,34 @@ +" Author: Ben Falconer +" Description: A language server for C++ + +call ale#Set('cpp_cquery_executable', 'cquery') +call ale#Set('cpp_cquery_cache_directory', expand('~/.cache/cquery')) + +function! ale_linters#cpp#cquery#GetProjectRoot(buffer) abort + let l:project_root = ale#path#FindNearestFile(a:buffer, 'compile_commands.json') + + return !empty(l:project_root) ? fnamemodify(l:project_root, ':h:h') : '' +endfunction + +function! ale_linters#cpp#cquery#GetExecutable(buffer) abort + return ale#Var(a:buffer, 'cpp_cquery_executable') +endfunction + +function! ale_linters#cpp#cquery#GetCommand(buffer) abort + let l:executable = ale_linters#cpp#cquery#GetExecutable(a:buffer) + return ale#Escape(l:executable) +endfunction + +function! ale_linters#cpp#cquery#GetInitializationOptions(buffer) abort + return {'cacheDirectory': ale#Var(a:buffer, 'cpp_cquery_cache_directory')} +endfunction + +call ale#linter#Define('cpp', { +\ 'name': 'cquery', +\ 'lsp': 'stdio', +\ 'executable_callback': 'ale_linters#cpp#cquery#GetExecutable', +\ 'command_callback': 'ale_linters#cpp#cquery#GetCommand', +\ 'project_root_callback': 'ale_linters#cpp#cquery#GetProjectRoot', +\ 'initialization_options_callback': 'ale_linters#cpp#cquery#GetInitializationOptions', +\ 'language': 'cpp', +\}) diff --git a/doc/ale-cpp.txt b/doc/ale-cpp.txt index 05e5479..8bd111e 100644 --- a/doc/ale-cpp.txt +++ b/doc/ale-cpp.txt @@ -156,6 +156,26 @@ g:ale_cpp_cpplint_options *g:ale_cpp_cpplint_options* This variable can be changed to modify flags given to cpplint. +=============================================================================== +cquery *ale-cpp-cquery* + +g:ale_cpp_cquery_executable *g:ale_cpp_cquery_executable* + *b:ale_cpp_cquery_executable* + Type: |String| + Default: `'cquery'` + + This variable can be changed to use a different executable for cquery. + + +g:ale_cpp_cquery_cache_directory *g:ale_cpp_cquery_cache_directory* + *b:ale_cpp_cquery_cache_directory* + Type: |String| + Default: `'~/.cache/cquery'` + + This variable can be changed to decide which directory cquery uses for its + cache. + + =============================================================================== flawfinder *ale-cpp-flawfinder* diff --git a/doc/ale.txt b/doc/ale.txt index 0808e2a..0531589 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -44,6 +44,7 @@ CONTENTS *ale-contents* clangtidy...........................|ale-cpp-clangtidy| cppcheck............................|ale-cpp-cppcheck| cpplint.............................|ale-cpp-cpplint| + cquery..............................|ale-cpp-cquery| flawfinder..........................|ale-cpp-flawfinder| gcc.................................|ale-cpp-gcc| c#....................................|ale-cs-options| @@ -319,7 +320,7 @@ Notes: * Bash: `shell` (-n flag), `shellcheck`, `shfmt` * Bourne Shell: `shell` (-n flag), `shellcheck`, `shfmt` * C: `cppcheck`, `cpplint`!!, `clang`, `clangtidy`!!, `clang-format`, `flawfinder`, `gcc` -* C++ (filetype cpp): `clang`, `clangcheck`!!, `clangtidy`!!, `clang-format`, `cppcheck`, `cpplint`!!, `flawfinder`, `gcc` +* C++ (filetype cpp): `clang`, `clangcheck`!!, `clangtidy`!!, `clang-format`, `cppcheck`, `cpplint`!!, `cquery`, `flawfinder`, `gcc` * CUDA: `nvcc`!! * C#: `mcs`, `mcsc`!! * Chef: `foodcritic` diff --git a/test/command_callback/test_cpp_cquery_command_callbacks.vader b/test/command_callback/test_cpp_cquery_command_callbacks.vader new file mode 100644 index 0000000..89a3e22 --- /dev/null +++ b/test/command_callback/test_cpp_cquery_command_callbacks.vader @@ -0,0 +1,48 @@ +" Author: Ben Falconer +" Description: A language server for C++ + +Before: + Save g:ale_cpp_cquery_executable + Save g:ale_cpp_cquery_cache_directory + + unlet! g:ale_cpp_cquery_executable + unlet! b:ale_cpp_cquery_executable + unlet! g:ale_cpp_cquery_cache_directory + unlet! b:ale_cpp_cquery_cache_directory + + runtime ale_linters/cpp/cquery.vim + +After: + Restore + unlet! b:ale_cpp_cquery_executable + unlet! b:ale_cpp_cquery_cache_directory + call ale#linter#Reset() + +Execute(The executable should be configurable): + AssertEqual 'cquery', ale_linters#cpp#cquery#GetExecutable(bufnr('')) + + let b:ale_cpp_cquery_executable = 'foobar' + + AssertEqual 'foobar', ale_linters#cpp#cquery#GetExecutable(bufnr('')) + +Execute(The executable should be used in the command): + AssertEqual + \ ale#Escape('cquery'), + \ ale_linters#cpp#cquery#GetCommand(bufnr('')) + + let b:ale_cpp_cquery_executable = 'foobar' + + AssertEqual + \ ale#Escape('foobar'), + \ ale_linters#cpp#cquery#GetCommand(bufnr('')) + +Execute(The cache directory should be configurable): + AssertEqual + \ {'cacheDirectory': expand('$HOME/.cache/cquery')}, + \ ale_linters#cpp#cquery#GetInitializationOptions(bufnr('')) + + let b:ale_cpp_cquery_cache_directory = '/foo/bar' + + AssertEqual + \ {'cacheDirectory': '/foo/bar'}, + \ ale_linters#cpp#cquery#GetInitializationOptions(bufnr(''))