From f2a21c960aba61d4ab0a0b50051b29ab8c893249 Mon Sep 17 00:00:00 2001 From: w0rp Date: Wed, 8 Feb 2023 01:14:47 +0000 Subject: [PATCH] Add support for lua-language-server --- ale_linters/lua/lua_language_server.vim | 15 ++++++++++++ autoload/ale/lua.vim | 28 ++++++++++++++++++++++ doc/ale-lua.txt | 24 +++++++++++++++++++ doc/ale-supported-languages-and-tools.txt | 1 + doc/ale.txt | 1 + supported-tools.md | 1 + test/linter/test_lua_language_server.vader | 16 +++++++++++++ 7 files changed, 86 insertions(+) create mode 100644 ale_linters/lua/lua_language_server.vim create mode 100644 autoload/ale/lua.vim create mode 100644 test/linter/test_lua_language_server.vader diff --git a/ale_linters/lua/lua_language_server.vim b/ale_linters/lua/lua_language_server.vim new file mode 100644 index 00000000..ebddd4b5 --- /dev/null +++ b/ale_linters/lua/lua_language_server.vim @@ -0,0 +1,15 @@ +" Author: w0rp +" Description: lua-language-server integration (https://github.com/LuaLS/lua-language-server) + +call ale#Set('lua_language_server_executable', 'lua-language-server') +call ale#Set('lua_language_server_config', {}) + +call ale#linter#Define('lua', { +\ 'name': 'lua_language_server', +\ 'aliases': ['lua-language-server'], +\ 'lsp': 'stdio', +\ 'executable': {b -> ale#Var(b, 'lua_language_server_executable')}, +\ 'command': '%e', +\ 'project_root': function('ale#lua#FindProjectRoot'), +\ 'lsp_config': {b -> ale#Var(b, 'lua_language_server_config')}, +\}) diff --git a/autoload/ale/lua.vim b/autoload/ale/lua.vim new file mode 100644 index 00000000..cda81aac --- /dev/null +++ b/autoload/ale/lua.vim @@ -0,0 +1,28 @@ +" Author: w0rp +" Description: Functions for integrating with Lua linters. + +" Find project root for a Lua language server. +function! ale#lua#FindProjectRoot(buffer) abort + let l:possible_project_roots = [ + \ '.git', + \ bufname(a:buffer), + \] + + for l:possible_root in l:possible_project_roots + let l:project_root = ale#path#FindNearestFile(a:buffer, l:possible_root) + + if empty(l:project_root) + let l:project_root = ale#path#FindNearestDirectory(a:buffer, l:possible_root) + endif + + if !empty(l:project_root) + " dir:p expands to /full/path/to/dir/ whereas + " file:p expands to /full/path/to/file (no trailing slash) + " Appending '/' ensures that :h:h removes the path's last segment + " regardless of whether it is a directory or not. + return fnamemodify(l:project_root . '/', ':p:h:h') + endif + endfor + + return '' +endfunction diff --git a/doc/ale-lua.txt b/doc/ale-lua.txt index 7ee60d0e..62d67324 100644 --- a/doc/ale-lua.txt +++ b/doc/ale-lua.txt @@ -27,6 +27,30 @@ g:ale_lua_lua_format_options *g:ale_lua_lua_format_options* This variable can be set to pass additional options to lua-format. +=============================================================================== +lua-language-server *ale-lua-lua-language-server* + *ale-lua-language-server* + +g:ale_lua_language_server_executable *g:ale_lua_language_server_executable* + *b:ale_lua_language_server_executable* + Type: |String| + Default: `'lua-language-server'` + + This variable can be changed to set the path to lua-language-server. + + If you have compiled the language server yourself in `/some/path`, the path + will be `'/some/path/bin/lua-language-server'`. + + +g:ale_lua_lua_language_server_config *g:ale_lua_lua_language_server_config* + *b:ale_lua_lua_language_server_config* + Type: |Dictionary| + Default: `{}` + + Dictionary containing configuration settings that will be passed to the + language server. + + =============================================================================== luac *ale-lua-luac* diff --git a/doc/ale-supported-languages-and-tools.txt b/doc/ale-supported-languages-and-tools.txt index 705eef41..b84fbdae 100644 --- a/doc/ale-supported-languages-and-tools.txt +++ b/doc/ale-supported-languages-and-tools.txt @@ -341,6 +341,7 @@ Notes: * Lua * `cspell` * `lua-format` + * `lua-language-server` * `luac` * `luacheck` * `luafmt` diff --git a/doc/ale.txt b/doc/ale.txt index afaf098c..1ed74ec5 100644 --- a/doc/ale.txt +++ b/doc/ale.txt @@ -3068,6 +3068,7 @@ documented in additional help files. lua.....................................|ale-lua-options| cspell................................|ale-lua-cspell| lua-format............................|ale-lua-lua-format| + lua-language-server...................|ale-lua-lua-language-server| luac..................................|ale-lua-luac| luacheck..............................|ale-lua-luacheck| luafmt................................|ale-lua-luafmt| diff --git a/supported-tools.md b/supported-tools.md index a3008b5c..408bbce1 100644 --- a/supported-tools.md +++ b/supported-tools.md @@ -350,6 +350,7 @@ formatting. * Lua * [cspell](https://github.com/streetsidesoftware/cspell/tree/main/packages/cspell) * [lua-format](https://github.com/Koihik/LuaFormatter) + * [lua-language-server](https://github.com/LuaLS/lua-language-server) * [luac](https://www.lua.org/manual/5.1/luac.html) * [luacheck](https://github.com/mpeterv/luacheck) * [luafmt](https://github.com/trixnz/lua-fmt) diff --git a/test/linter/test_lua_language_server.vader b/test/linter/test_lua_language_server.vader new file mode 100644 index 00000000..c4529835 --- /dev/null +++ b/test/linter/test_lua_language_server.vader @@ -0,0 +1,16 @@ +Before: + call ale#assert#SetUpLinterTest('lua', 'lua_language_server') + +After: + call ale#assert#TearDownLinterTest() + +Execute(The default lua-language-server settings should be correct): + AssertLinter 'lua-language-server', ale#Escape('lua-language-server') + AssertLSPConfig {} + +Execute(lua-language-server should be configurable): + let b:ale_lua_language_server_executable = 'billy' + let b:ale_lua_language_server_config = {'x': 'y'} + + AssertLinter 'billy', ale#Escape('billy') + AssertLSPConfig {'x': 'y'}