From 6fc016ad052725e0d6a474e80f817cb1853e98e2 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sat, 26 Jan 2019 04:45:12 +0000 Subject: Add additional ways to detect LSP project root Currently, we detect the linter root based on a variety of techniques. However, these techniques are not foolproof. For example, clangd works fine for many things without a compile_commands.json file, and Go projects may be built outside of the GOPATH to take advantage of Go 1.11's automatic module support. Add global and buffer-specific variables to allow the user to specify the root, either as a string or a funcref. Make the funcrefs accept the buffer number as an argument to make sure that they can function easily in an asynchronous environment. We define the global variable in the main plugin, since the LSP linter code is not loaded unless required, and we want the variable to be able to be read correctly by :ALEInfo regardless. --- autoload/ale/assert.vim | 13 +++++++++++++ autoload/ale/debugging.vim | 1 + autoload/ale/lsp_linter.vim | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) (limited to 'autoload') diff --git a/autoload/ale/assert.vim b/autoload/ale/assert.vim index ed08ed09..7139f05c 100644 --- a/autoload/ale/assert.vim +++ b/autoload/ale/assert.vim @@ -109,6 +109,14 @@ function! ale#assert#LSPProject(expected_root) abort AssertEqual a:expected_root, l:root endfunction +function! ale#assert#LSPProjectFull(expected_root) abort + let l:buffer = bufnr('') + let l:linter = s:GetLinter() + let l:root = ale#lsp_linter#FindProjectRoot(l:buffer, l:linter) + + AssertEqual a:expected_root, l:root +endfunction + function! ale#assert#LSPAddress(expected_address) abort let l:buffer = bufnr('') let l:linter = s:GetLinter() @@ -158,6 +166,7 @@ function! ale#assert#SetUpLinterTest(filetype, name) abort command! -nargs=+ AssertLSPConfig :call ale#assert#LSPConfig() command! -nargs=+ AssertLSPLanguage :call ale#assert#LSPLanguage() command! -nargs=+ AssertLSPProject :call ale#assert#LSPProject() + command! -nargs=+ AssertLSPProjectFull :call ale#assert#LSPProjectFull() command! -nargs=+ AssertLSPAddress :call ale#assert#LSPAddress() endfunction @@ -193,6 +202,10 @@ function! ale#assert#TearDownLinterTest() abort delcommand AssertLSPProject endif + if exists(':AssertLSPProjectFull') + delcommand AssertLSPProjectFull + endif + if exists(':AssertLSPAddress') delcommand AssertLSPAddress endif diff --git a/autoload/ale/debugging.vim b/autoload/ale/debugging.vim index 3aed38fe..e4bf5e7e 100644 --- a/autoload/ale/debugging.vim +++ b/autoload/ale/debugging.vim @@ -31,6 +31,7 @@ let s:global_variable_list = [ \ 'ale_list_vertical', \ 'ale_list_window_size', \ 'ale_loclist_msg_format', +\ 'ale_lsp_root', \ 'ale_max_buffer_history_size', \ 'ale_max_signs', \ 'ale_maximum_file_size', diff --git a/autoload/ale/lsp_linter.vim b/autoload/ale/lsp_linter.vim index d92dae7e..2c31ea09 100644 --- a/autoload/ale/lsp_linter.vim +++ b/autoload/ale/lsp_linter.vim @@ -152,12 +152,45 @@ function! ale#lsp_linter#GetConfig(buffer, linter) abort return l:config endfunction +function! ale#lsp_linter#FindProjectRoot(buffer, linter) abort + let l:buffer_ale_root = getbufvar(a:buffer, 'ale_lsp_root', {}) + + if type(l:buffer_ale_root) is v:t_string + return l:buffer_ale_root + endif + + " Try to get a buffer-local setting for the root + if has_key(l:buffer_ale_root, a:linter.name) + let l:Root = l:buffer_ale_root[a:linter.name] + + if type(l:Root) is v:t_func + return l:Root(a:buffer) + else + return l:Root + endif + endif + + " Try to get a global setting for the root + if has_key(g:ale_lsp_root, a:linter.name) + let l:Root = g:ale_lsp_root[a:linter.name] + + if type(l:Root) is v:t_func + return l:Root(a:buffer) + else + return l:Root + endif + endif + + " Fall back to the linter-specific configuration + return ale#util#GetFunction(a:linter.project_root_callback)(a:buffer) +endfunction + " Given a buffer, an LSP linter, start up an LSP linter and get ready to " receive messages for the document. function! ale#lsp_linter#StartLSP(buffer, linter) abort let l:command = '' let l:address = '' - let l:root = ale#util#GetFunction(a:linter.project_root_callback)(a:buffer) + let l:root = ale#lsp_linter#FindProjectRoot(a:buffer, a:linter) if empty(l:root) && a:linter.lsp isnot# 'tsserver' " If there's no project root, then we can't check files with LSP, -- cgit v1.2.3