From 9666b63a70e28b1eef084489a44ea4b4bb4ae65a Mon Sep 17 00:00:00 2001 From: Ashkan Kiani Date: Thu, 14 Nov 2019 01:07:09 -0800 Subject: Rename to nvim_lsp and nvim-lsp. --- CONTRIBUTING.md | 4 +- README.md | 62 +++++------ README_preamble.md | 56 +++++----- autoload/common_lsp.vim | 12 -- autoload/nvim_lsp.vim | 12 ++ lua/common_lsp.lua | 16 --- lua/common_lsp/clangd.lua | 40 ------- lua/common_lsp/gopls.lua | 26 ----- lua/common_lsp/skeleton.lua | 162 --------------------------- lua/common_lsp/texlab.lua | 77 ------------- lua/common_lsp/util.lua | 262 -------------------------------------------- lua/nvim_lsp.lua | 16 +++ lua/nvim_lsp/clangd.lua | 40 +++++++ lua/nvim_lsp/gopls.lua | 26 +++++ lua/nvim_lsp/skeleton.lua | 162 +++++++++++++++++++++++++++ lua/nvim_lsp/texlab.lua | 77 +++++++++++++ lua/nvim_lsp/util.lua | 262 ++++++++++++++++++++++++++++++++++++++++++++ scripts/docgen.lua | 6 +- 18 files changed, 659 insertions(+), 659 deletions(-) delete mode 100644 autoload/common_lsp.vim create mode 100644 autoload/nvim_lsp.vim delete mode 100644 lua/common_lsp.lua delete mode 100644 lua/common_lsp/clangd.lua delete mode 100644 lua/common_lsp/gopls.lua delete mode 100644 lua/common_lsp/skeleton.lua delete mode 100644 lua/common_lsp/texlab.lua delete mode 100644 lua/common_lsp/util.lua create mode 100644 lua/nvim_lsp.lua create mode 100644 lua/nvim_lsp/clangd.lua create mode 100644 lua/nvim_lsp/gopls.lua create mode 100644 lua/nvim_lsp/skeleton.lua create mode 100644 lua/nvim_lsp/texlab.lua create mode 100644 lua/nvim_lsp/util.lua diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a5d632a9..407eba7b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -72,7 +72,7 @@ Example: skeleton.texlab.buf_build = buf_build ``` -After you create a skeleton, you have to `require 'common_lsp/SERVER_NAME'` in -`lua/common_lsp.lua`, and that's it. +After you create a skeleton, you have to `require 'nvim_lsp/SERVER_NAME'` in +`lua/nvim_lsp.lua`, and that's it. Generate docs and you're done. diff --git a/README.md b/README.md index acb89fdb..27b768f1 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,9 @@ things as much as you want in addition to the defaults that this provides. **CONTRIBUTIONS ARE WELCOME!** There's a lot of language servers in the world, and not enough time. See -[`lua/common_lsp/texlab.lua`](https://github.com/neovim/nvim-lsp/blob/master/lua/common_lsp/texlab.lua) +[`lua/nvim_lsp/texlab.lua`](https://github.com/neovim/nvim-lsp/blob/master/lua/nvim_lsp/texlab.lua) and -[`lua/common_lsp/skeleton.lua`](https://github.com/neovim/nvim-lsp/blob/master/lua/common_lsp/skeleton.lua) +[`lua/nvim_lsp/skeleton.lua`](https://github.com/neovim/nvim-lsp/blob/master/lua/nvim_lsp/skeleton.lua) for examples and ask me questions in the [Neovim Gitter](https://gitter.im/neovim/neovim) to help me complete configurations for *all the LSPs!* @@ -26,11 +26,11 @@ implemented from [this excellent list compiled by the coc.nvim contributors](https://github.com/neoclide/coc.nvim/wiki/Language-servers) or [this other excellent list from the emacs lsp-mode contributors](https://github.com/emacs-lsp/lsp-mode#supported-languages) -and create a new file under `lua/common_lsp/SERVER_NAME.lua`. +and create a new file under `lua/nvim_lsp/SERVER_NAME.lua`. - For a simple server which should only ever have one instance for the entire -neovim lifetime, I recommend copying `lua/common_lsp/texlab.lua`. +neovim lifetime, I recommend copying `lua/nvim_lsp/texlab.lua`. - For servers which should have a different instance for each project root, I -recommend copying `lua/common_lsp/gopls.lua`. +recommend copying `lua/nvim_lsp/gopls.lua`. ## Progress @@ -55,22 +55,22 @@ In progress: From vim: ```vim -call common_lsp#texlab({}) -call common_lsp#gopls({}) +call nvim_lsp#texlab({}) +call nvim_lsp#gopls({}) " These are still TODO, but will be done. -call common_lsp#clangd({}) -call common_lsp#ccls({}) -call common_lsp#tsserver({}) +call nvim_lsp#clangd({}) +call nvim_lsp#ccls({}) +call nvim_lsp#tsserver({}) " Or using a dynamic name. -call common_lsp#setup("texlab", {}) -call common_lsp#setup("gopls", {}) +call nvim_lsp#setup("texlab", {}) +call nvim_lsp#setup("gopls", {}) ``` From Lua: ```lua -require 'common_lsp'.texlab.setup { +require 'nvim_lsp'.texlab.setup { name = "texlab_fancy"; log_level = vim.lsp.protocol.MessageType.Log; settings = { @@ -82,15 +82,15 @@ require 'common_lsp'.texlab.setup { } } -local common_lsp = require 'common_lsp' +local nvim_lsp = require 'nvim_lsp' -- Customize how to find the root_dir -common_lsp.gopls.setup { - root_dir = common_lsp.util.root_pattern(".git"); +nvim_lsp.gopls.setup { + root_dir = nvim_lsp.util.root_pattern(".git"); } -- Build the current buffer. -require 'common_lsp'.texlab.buf_build(0) +require 'nvim_lsp'.texlab.buf_build(0) ``` ``` @@ -98,12 +98,12 @@ These are functions to set up servers more easily with some server specific defaults and more server specific things like commands or different diagnostics. -Servers may define extra functions on the `common_lsp.SERVER` table, e.g. -`common_lsp.texlab.buf_build({bufnr})`. +Servers may define extra functions on the `nvim_lsp.SERVER` table, e.g. +`nvim_lsp.texlab.buf_build({bufnr})`. The main setup signature will be: -common_lsp.SERVER.setup({config}) +nvim_lsp.SERVER.setup({config}) {config} is the same as |vim.lsp.start_client()|, but with some additions and changes: @@ -121,14 +121,14 @@ common_lsp.SERVER.setup({config}) If nil is returned, the buffer is skipped. - See |common_lsp.util.search_ancestors()| and the functions which use it: - - |common_lsp.util.root_pattern(patterns...)| finds an ancestor which + See |nvim_lsp.util.search_ancestors()| and the functions which use it: + - |nvim_lsp.util.root_pattern(patterns...)| finds an ancestor which - contains one of the files in `patterns...`. This is equivalent to coc.nvim's "rootPatterns" - More specific utilities: - - |common_lsp.util.find_git_root()| - - |common_lsp.util.find_node_modules_root()| - - |common_lsp.util.find_package_json_root()| + - |nvim_lsp.util.find_git_root()| + - |nvim_lsp.util.find_node_modules_root()| + - |nvim_lsp.util.find_package_json_root()| {name} Defaults to the server's name. @@ -174,8 +174,8 @@ as compile_commands.json or, for simpler projects, a compile_flags.txt. -common_lsp.clangd.setup({config}) -common_lsp#setup("clangd", {config}) +nvim_lsp.clangd.setup({config}) +nvim_lsp#setup("clangd", {config}) ``` Default Values: @@ -195,8 +195,8 @@ A completion engine built from scratch for (la)tex. -common_lsp.texlab.setup({config}) -common_lsp#setup("texlab", {config}) +nvim_lsp.texlab.setup({config}) +nvim_lsp#setup("texlab", {config}) ``` Commands: @@ -225,8 +225,8 @@ Google's lsp server for golang. -common_lsp.gopls.setup({config}) -common_lsp#setup("gopls", {config}) +nvim_lsp.gopls.setup({config}) +nvim_lsp#setup("gopls", {config}) ``` Default Values: diff --git a/README_preamble.md b/README_preamble.md index dcd318cb..2f01a844 100644 --- a/README_preamble.md +++ b/README_preamble.md @@ -14,9 +14,9 @@ things as much as you want in addition to the defaults that this provides. **CONTRIBUTIONS ARE WELCOME!** There's a lot of language servers in the world, and not enough time. See -[`lua/common_lsp/texlab.lua`](https://github.com/norcalli/nvim-common-lsp/blob/master/lua/common_lsp/texlab.lua) +[`lua/nvim_lsp/texlab.lua`](https://github.com/neovim/nvim-lsp/blob/master/lua/nvim_lsp/texlab.lua) and -[`lua/common_lsp/skeleton.lua`](https://github.com/norcalli/nvim-common-lsp/blob/master/lua/common_lsp/skeleton.lua) +[`lua/nvim_lsp/skeleton.lua`](https://github.com/neovim/nvim-lsp/blob/master/lua/nvim_lsp/skeleton.lua) for examples and ask me questions in the [Neovim Gitter](https://gitter.im/neovim/neovim) to help me complete configurations for *all the LSPs!* @@ -26,17 +26,17 @@ implemented from [this excellent list compiled by the coc.nvim contributors](https://github.com/neoclide/coc.nvim/wiki/Language-servers) or [this other excellent list from the emacs lsp-mode contributors](https://github.com/emacs-lsp/lsp-mode#supported-languages) -and create a new file under `lua/common_lsp/SERVER_NAME.lua`. +and create a new file under `lua/nvim_lsp/SERVER_NAME.lua`. - For a simple server which should only ever have one instance for the entire -neovim lifetime, I recommend copying `lua/common_lsp/texlab.lua`. +neovim lifetime, I recommend copying `lua/nvim_lsp/texlab.lua`. - For servers which should have a different instance for each project root, I -recommend copying `lua/common_lsp/gopls.lua`. +recommend copying `lua/nvim_lsp/gopls.lua`. ## Progress Implemented: -- [gopls](https://github.com/norcalli/nvim-common-lsp#gopls) (has some errors) -- [texlab](https://github.com/norcalli/nvim-common-lsp#texlab) +- [gopls](https://github.com/neovim/nvim-lsp#gopls) (has some errors) +- [texlab](https://github.com/neovim/nvim-lsp#texlab) Planned servers to implement (by me, but contributions welcome anyway): - [clangd](https://clang.llvm.org/extra/clangd/Installation.html) @@ -49,28 +49,28 @@ In progress: ## Install -`Plug 'norcalli/nvim-common-lsp'` +`Plug 'neovim/nvim-lsp'` ## Use From vim: ```vim -call common_lsp#texlab({}) -call common_lsp#gopls({}) +call nvim_lsp#texlab({}) +call nvim_lsp#gopls({}) " These are still TODO, but will be done. -call common_lsp#clangd({}) -call common_lsp#ccls({}) -call common_lsp#tsserver({}) +call nvim_lsp#clangd({}) +call nvim_lsp#ccls({}) +call nvim_lsp#tsserver({}) " Or using a dynamic name. -call common_lsp#setup("texlab", {}) -call common_lsp#setup("gopls", {}) +call nvim_lsp#setup("texlab", {}) +call nvim_lsp#setup("gopls", {}) ``` From Lua: ```lua -require 'common_lsp'.texlab.setup { +require 'nvim_lsp'.texlab.setup { name = "texlab_fancy"; log_level = vim.lsp.protocol.MessageType.Log; settings = { @@ -82,15 +82,15 @@ require 'common_lsp'.texlab.setup { } } -local common_lsp = require 'common_lsp' +local nvim_lsp = require 'nvim_lsp' -- Customize how to find the root_dir -common_lsp.gopls.setup { - root_dir = common_lsp.util.root_pattern(".git"); +nvim_lsp.gopls.setup { + root_dir = nvim_lsp.util.root_pattern(".git"); } -- Build the current buffer. -require 'common_lsp'.texlab.buf_build(0) +require 'nvim_lsp'.texlab.buf_build(0) ``` ``` @@ -98,12 +98,12 @@ These are functions to set up servers more easily with some server specific defaults and more server specific things like commands or different diagnostics. -Servers may define extra functions on the `common_lsp.SERVER` table, e.g. -`common_lsp.texlab.buf_build({bufnr})`. +Servers may define extra functions on the `nvim_lsp.SERVER` table, e.g. +`nvim_lsp.texlab.buf_build({bufnr})`. The main setup signature will be: -common_lsp.SERVER.setup({config}) +nvim_lsp.SERVER.setup({config}) {config} is the same as |vim.lsp.start_client()|, but with some additions and changes: @@ -121,14 +121,14 @@ common_lsp.SERVER.setup({config}) If nil is returned, the buffer is skipped. - See |common_lsp.util.search_ancestors()| and the functions which use it: - - |common_lsp.util.root_pattern(patterns...)| finds an ancestor which + See |nvim_lsp.util.search_ancestors()| and the functions which use it: + - |nvim_lsp.util.root_pattern(patterns...)| finds an ancestor which - contains one of the files in `patterns...`. This is equivalent to coc.nvim's "rootPatterns" - More specific utilities: - - |common_lsp.util.find_git_root()| - - |common_lsp.util.find_node_modules_root()| - - |common_lsp.util.find_package_json_root()| + - |nvim_lsp.util.find_git_root()| + - |nvim_lsp.util.find_node_modules_root()| + - |nvim_lsp.util.find_package_json_root()| {name} Defaults to the server's name. diff --git a/autoload/common_lsp.vim b/autoload/common_lsp.vim deleted file mode 100644 index 9d3444d3..00000000 --- a/autoload/common_lsp.vim +++ /dev/null @@ -1,12 +0,0 @@ -function! common_lsp#setup(name, config) - return luaeval("require'common_lsp'[_A[1]].setup(_A[2])", [a:name, a:config]) -endfunction - -" function! common_lsp#texlab(config) -" call common_lsp#setup("texlab", a:config) -" endfunction - -" function! common_lsp#gopls(config) -" call common_lsp#setup("gopls", a:config) -" endfunction - diff --git a/autoload/nvim_lsp.vim b/autoload/nvim_lsp.vim new file mode 100644 index 00000000..61bea51b --- /dev/null +++ b/autoload/nvim_lsp.vim @@ -0,0 +1,12 @@ +function! nvim_lsp#setup(name, config) + return luaeval("require'nvim_lsp'[_A[1]].setup(_A[2])", [a:name, a:config]) +endfunction + +" function! nvim_lsp#texlab(config) +" call nvim_lsp#setup("texlab", a:config) +" endfunction + +" function! nvim_lsp#gopls(config) +" call nvim_lsp#setup("gopls", a:config) +" endfunction + diff --git a/lua/common_lsp.lua b/lua/common_lsp.lua deleted file mode 100644 index 4dea8e1e..00000000 --- a/lua/common_lsp.lua +++ /dev/null @@ -1,16 +0,0 @@ -local skeleton = require 'common_lsp/skeleton' -require 'common_lsp/gopls' -require 'common_lsp/texlab' -require 'common_lsp/clangd' - -local M = { - util = require 'common_lsp/util'; -} - -local mt = {} -function mt:__index(k) - return skeleton[k] -end - -return setmetatable(M, mt) --- vim:et ts=2 sw=2 diff --git a/lua/common_lsp/clangd.lua b/lua/common_lsp/clangd.lua deleted file mode 100644 index 465af674..00000000 --- a/lua/common_lsp/clangd.lua +++ /dev/null @@ -1,40 +0,0 @@ -local skeleton = require 'common_lsp/skeleton' -local util = require 'common_lsp/util' -local lsp = vim.lsp - -local default_capabilities = lsp.protocol.make_client_capabilities() -default_capabilities.offsetEncoding = {"utf-8", "utf-16"} - -skeleton.clangd = { - default_config = { - cmd = {"clangd", "--background-index"}; - filetypes = {"c", "cpp", "objc", "objcpp"}; - root_dir = util.root_pattern("compile_commands.json", "compile_flags.txt", ".git"); - log_level = lsp.protocol.MessageType.Warning; - settings = {}; - capabilities = default_capabilities; - on_init = vim.schedule_wrap(function(client, result) - if result.offsetEncoding then - client.offset_encoding = result.offsetEncoding - end - end) - }; - -- commands = {}; - -- on_new_config = function(new_config) end; - -- on_attach = function(client, bufnr) end; - docs = { - description = [[ -https://clang.llvm.org/extra/clangd/Installation.html - -clangd relies on a [JSON compilation database](https://clang.llvm.org/docs/JSONCompilationDatabase.html) specified -as compile_commands.json or, for simpler projects, a compile_flags.txt. -]]; - default_config = { - root_dir = [[root_pattern("compile_commands.json", "compile_flags.txt", ".git")]]; - on_init = [[function to handle changing offsetEncoding]]; - capabilities = [[default capabilities, with offsetEncoding utf-8]]; - }; - }; -} --- vim:et ts=2 sw=2 - diff --git a/lua/common_lsp/gopls.lua b/lua/common_lsp/gopls.lua deleted file mode 100644 index e3061d16..00000000 --- a/lua/common_lsp/gopls.lua +++ /dev/null @@ -1,26 +0,0 @@ -local skeleton = require 'common_lsp/skeleton' -local util = require 'common_lsp/util' -local lsp = vim.lsp - -skeleton.gopls = { - default_config = { - cmd = {"gopls"}; - filetypes = {"go"}; - root_dir = util.root_pattern("go.mod", ".git"); - log_level = lsp.protocol.MessageType.Warning; - settings = {}; - }; - -- on_new_config = function(new_config) end; - -- on_attach = function(client, bufnr) end; - docs = { - description = [[ -https://github.com/golang/tools/tree/master/gopls - -Google's lsp server for golang. -]]; - default_config = { - root_dir = [[root_pattern("go.mod", ".git")]]; - }; - }; -} --- vim:et ts=2 sw=2 diff --git a/lua/common_lsp/skeleton.lua b/lua/common_lsp/skeleton.lua deleted file mode 100644 index 574c4bb8..00000000 --- a/lua/common_lsp/skeleton.lua +++ /dev/null @@ -1,162 +0,0 @@ -local util = require 'common_lsp/util' -local api, validate, lsp = vim.api, vim.validate, vim.lsp -local tbl_extend = vim.tbl_extend - -local skeleton = {} - - -function skeleton.__newindex(t, template_name, template) - validate { - name = {template_name, 's'}; - default_config = {template.default_config, 't'}; - on_new_config = {template.on_new_config, 'f', true}; - on_attach = {template.on_attach, 'f', true}; - commands = {template.commands, 't', true}; - } - if template.commands then - for k, v in pairs(template.commands) do - validate { - ['command.name'] = {k, 's'}; - ['command.fn'] = {v[1], 'f'}; - } - end - end - - local M = {} - - local default_config = tbl_extend("keep", template.default_config, { - log_level = lsp.protocol.MessageType.Warning; - settings = {}; - callbacks = {}; - }) - - -- Force this part. - default_config.name = template_name - - -- The config here is the one which will be instantiated for the new server, - -- which is why this is a function, so that it can refer to the settings - -- object on the server. - local function add_callbacks(config) - config.callbacks["window/logMessage"] = function(err, method, params, client_id) - if params and params.type <= config.log_level then - lsp.builtin_callbacks[method](err, method, params, client_id) - end - end - - config.callbacks["workspace/configuration"] = function(err, method, params, client_id) - if err then error(tostring(err)) end - if not params.items then - return {} - end - - local result = {} - for _, item in ipairs(params.items) do - if item.section then - local value = util.lookup_section(config.settings, item.section) or vim.NIL - table.insert(result, value) - end - end - return result - end - end - - function M.setup(config) - validate { - root_dir = {config.root_dir, 'f', default_config.root_dir ~= nil}; - filetypes = {config.filetype, 't', true}; - on_new_config = {config.on_new_config, 'f', true}; - on_attach = {config.on_attach, 'f', true}; - } - config = tbl_extend("keep", config, default_config) - - local trigger - if config.filetypes then - trigger = "FileType "..table.concat(config.filetypes, ',') - else - trigger = "BufReadPost *" - end - api.nvim_command(string.format( - "autocmd %s lua require'common_lsp'[%q].manager.try_add()" - , trigger - , config.name - )) - - local get_root_dir = config.root_dir - - -- In the case of a reload, close existing things. - if M.manager then - for _, client in ipairs(M.manager.clients()) do - client.stop(true) - end - M.manager = nil - end - local manager = util.server_per_root_dir_manager(function(_root_dir) - local new_config = vim.tbl_extend("keep", {}, config) - -- Deepcopy anything that is >1 level nested. - new_config.settings = vim.deepcopy(new_config.settings) - util.tbl_deep_extend(new_config.settings, default_config.settings) - - new_config.capabilities = new_config.capabilities or lsp.protocol.make_client_capabilities() - util.tbl_deep_extend(new_config.capabilities, { - workspace = { - configuration = true; - } - }) - - add_callbacks(new_config) - if template.on_new_config then - pcall(template.on_new_config, new_config) - end - if config.on_new_config then - pcall(config.on_new_config, new_config) - end - - -- Save the old _on_attach so that we can reference it via the BufEnter. - new_config._on_attach = new_config.on_attach - new_config.on_attach = vim.schedule_wrap(function(client, bufnr) - if bufnr == api.nvim_get_current_buf() then - M._setup_buffer(client.id) - else - api.nvim_command(string.format( - "autocmd BufEnter ++once lua require'common_lsp'[%q]._setup_buffer(%d)" - , template_name - , bufnr - , client.id - )) - end - end) - return new_config - end) - - function manager.try_add() - local root_dir = get_root_dir(api.nvim_buf_get_name(0), api.nvim_get_current_buf()) - print(api.nvim_get_current_buf(), root_dir) - local id = manager.add(root_dir) - lsp.buf_attach_client(0, id) - end - - M.manager = manager - end - - function M._setup_buffer(client_id) - local client = lsp.get_client_by_id(client_id) - if client.config._on_attach then - client.config._on_attach(client) - end - if template.commands then - -- Create the module commands - util.create_module_commands(template_name, M.commands) - end - end - - M.commands = template.commands - M.name = template_name - M.template_config = template - - rawset(t, template_name, M) - - return M -end - -return setmetatable({}, skeleton) --- vim:et ts=2 sw=2 diff --git a/lua/common_lsp/texlab.lua b/lua/common_lsp/texlab.lua deleted file mode 100644 index d0339e1f..00000000 --- a/lua/common_lsp/texlab.lua +++ /dev/null @@ -1,77 +0,0 @@ -local skeleton = require 'common_lsp/skeleton' -local util = require 'common_lsp/util' -local lsp = vim.lsp - -local cwd = vim.loop.cwd() - -local texlab_build_status = vim.tbl_add_reverse_lookup { - Success = 0; - Error = 1; - Failure = 2; - Cancelled = 3; -} - -local function buf_build(bufnr) - bufnr = util.validate_bufnr(bufnr) - local params = { textDocument = { uri = vim.uri_from_bufnr(bufnr) } } - lsp.buf_request(bufnr, 'textDocument/build', params, - function(err, _, result, _) - if err then error(tostring(err)) end - print("Build "..texlab_build_status[result.status]) - end) -end - --- bufnr isn't actually required here, but we need a valid buffer in order to --- be able to find the client for buf_request. --- TODO find a client by looking through buffers for a valid client? -local function build_cancel_all(bufnr) - bufnr = util.validate_bufnr(bufnr) - local params = { token = "texlab-build-*" } - lsp.buf_request(bufnr, 'window/progress/cancel', params, function(err, method, result, client_id) - if err then error(tostring(err)) end - print("Cancel result", vim.inspect(result)) - end) -end - -skeleton.texlab = { - default_config = { - cmd = {"texlab"}; - filetypes = {"tex", "bib"}; - root_dir = function() return cwd end; - log_level = lsp.protocol.MessageType.Warning; - settings = { - latex = { - build = { - args = {"-pdf", "-interaction=nonstopmode", "-synctex=1"}; - executable = "latexmk"; - onSave = false; - }; - }; - }; - }; - commands = { - TexlabBuild = { - function() - buf_build(0) - end; - description = "Build the current buffer"; - }; - -- TexlabCancelAllBuilds = { - -- }; - }; - -- on_new_config = function(new_config) end; - -- on_attach = function(client, bufnr) end; - docs = { - description = [[ -https://texlab.netlify.com/ - -A completion engine built from scratch for (la)tex. -]]; - default_config = { - root_dir = "vim's starting directory"; - }; - }; -} - -skeleton.texlab.buf_build = buf_build --- vim:et ts=2 sw=2 diff --git a/lua/common_lsp/util.lua b/lua/common_lsp/util.lua deleted file mode 100644 index 6cc6db0f..00000000 --- a/lua/common_lsp/util.lua +++ /dev/null @@ -1,262 +0,0 @@ -local validate = vim.validate -local api = vim.api -local lsp = vim.lsp -local uv = vim.loop - -local M = {} - -function M.validate_bufnr(bufnr) - validate { - bufnr = { bufnr, 'n' } - } - return bufnr == 0 and api.nvim_get_current_buf() or bufnr -end - -function M.add_hook_before(fn, new_fn) - if fn then - return function(...) - -- TODO which result? - new_fn(...) - return fn(...) - end - else - return new_fn - end -end - -function M.add_hook_after(fn, new_fn) - if fn then - return function(...) - -- TODO which result? - fn(...) - return new_fn(...) - end - else - return new_fn - end -end - -local function split_lines(s) - return vim.split(s, "\n", true) -end - -function M.tbl_deep_extend(dst, ...) - validate { dst = { dst, 't' } } - for i = 1, select("#", ...) do - local t = select(i, ...) - validate { arg = { t, 't' } } - for k, v in pairs(t) do - if type(v) == 'table' and not vim.tbl_islist(v) then - dst[k] = M.tbl_deep_extend(dst[k] or {}, v) - else - dst[k] = v - end - end - end - return dst -end - -function M.nvim_multiline_command(command) - validate { command = { command, 's' } } - for line in vim.gsplit(command, "\n", true) do - api.nvim_command(line) - end -end - -function M.lookup_section(settings, section) - for part in vim.gsplit(section, '.', true) do - settings = settings[part] - end - return settings -end - -function M.create_module_commands(module_name, commands) - for command_name, def in pairs(commands) do - local parts = {"command!"} - -- Insert attributes. - for k, v in pairs(def) do - if type(k) == 'string' and type(v) == 'boolean' and v then - table.insert(parts, "-"..k) - elseif type(k) == 'number' and type(v) == 'string' and v:match("^%-") then - table.insert(parts, v) - end - end - table.insert(parts, command_name) - -- The command definition. - table.insert(parts, - string.format("lua require'common_lsp'[%q].commands[%q][1]()", module_name, command_name)) - api.nvim_command(table.concat(parts, " ")) - end -end - --- Some path utilities -M.path = (function() - local function exists(filename) - local stat = uv.fs_stat(filename) - return stat and stat.type or false - end - - local function is_dir(filename) - return exists(filename) == 'directory' - end - - local function is_file(filename) - return exists(filename) == 'file' - end - - local is_windows = uv.os_uname().sysname == "Windows" - local path_sep = is_windows and "\\" or "/" - - local is_fs_root - if is_windows then - is_fs_root = function(path) - return path:match("^%a:\\\\$") - end - else - is_fs_root = function(path) - return path == "/" - end - end - - local dirname - do - local strip_dir_pat = path_sep.."([^"..path_sep.."]+)$" - local strip_sep_pat = path_sep.."$" - dirname = function(path) - if not path then return end - local result = path:gsub(strip_sep_pat, ""):gsub(strip_dir_pat, "") - if #result == 0 then - return "/" - end - return result - end - end - - local function path_join(...) - return table.concat(vim.tbl_flatten {...}, path_sep) - end - - -- Traverse the path calling cb along the way. - local function traverse_parents(path, cb) - path = uv.fs_realpath(path) - local dir = path - -- Just in case our algo is buggy, don't infinite loop. - for _ = 1, 100 do - dir = dirname(dir) - if not dir then return end - -- If we can't ascend further, then stop looking. - if cb(dir, path) then - return dir, path - end - if is_fs_root(dir) then - break - end - end - end - - -- Iterate the path until we find the rootdir. - local function iterate_parents(path) - path = uv.fs_realpath(path) - local function it(s, v) - if not v then return end - if is_fs_root(v) then return end - return dirname(v), path - end - return it, path, path - end - - return { - is_dir = is_dir; - is_file = is_file; - exists = exists; - sep = path_sep; - dirname = dirname; - join = path_join; - traverse_parents = traverse_parents; - iterate_parents = iterate_parents; - } -end)() - - --- Returns a function(root_dir), which, when called with a root_dir it hasn't --- seen before, will call make_config(root_dir) and start a new client. -function M.server_per_root_dir_manager(make_config) - local clients = {} - local manager = {} - - function manager.add(root_dir) - if not root_dir then return end - - -- Check if we have a client alredy or start and store it. - local client_id = clients[root_dir] - if not client_id then - local new_config = make_config(root_dir) - new_config.root_dir = root_dir - new_config.on_exit = M.add_hook_before(new_config.on_exit, function() - clients[root_dir] = nil - end) - client_id = lsp.start_client(new_config) - clients[root_dir] = client_id - end - return client_id - end - - function manager.clients() - local res = {} - for _, id in pairs(clients) do - local client = lsp.get_client_by_id(id) - if client then - table.insert(res, client) - end - end - return res - end - - return manager -end - -function M.search_ancestors(startpath, fn) - validate { fn = {fn, 'f'} } - if fn(startpath) then return startpath end - for path in M.path.iterate_parents(startpath) do - if fn(path) then return path end - end -end - -function M.root_pattern(...) - local patterns = vim.tbl_flatten {...} - local function matcher(path) - for _, pattern in ipairs(patterns) do - if M.path.exists(M.path.join(path, pattern)) then - return path - end - end - end - return function(startpath) - return M.search_ancestors(startpath, matcher) - end -end -function M.find_git_ancestor(startpath) - return M.search_ancestors(startpath, function(path) - if M.path.is_dir(M.path.join(path, ".git")) then - return path - end - end) -end -function M.find_node_modules_ancestor(startpath) - return M.search_ancestors(startpath, function(path) - if M.path.is_dir(M.path.join(path, "node_modules")) then - return path - end - end) -end -function M.find_package_json_ancestor(startpath) - return M.search_ancestors(startpath, function(path) - if M.path.is_file(M.path.join(path, "package.json")) then - return path - end - end) -end - -return M --- vim:et ts=2 sw=2 diff --git a/lua/nvim_lsp.lua b/lua/nvim_lsp.lua new file mode 100644 index 00000000..7868e3a4 --- /dev/null +++ b/lua/nvim_lsp.lua @@ -0,0 +1,16 @@ +local skeleton = require 'nvim_lsp/skeleton' +require 'nvim_lsp/gopls' +require 'nvim_lsp/texlab' +require 'nvim_lsp/clangd' + +local M = { + util = require 'nvim_lsp/util'; +} + +local mt = {} +function mt:__index(k) + return skeleton[k] +end + +return setmetatable(M, mt) +-- vim:et ts=2 sw=2 diff --git a/lua/nvim_lsp/clangd.lua b/lua/nvim_lsp/clangd.lua new file mode 100644 index 00000000..b0d4ec25 --- /dev/null +++ b/lua/nvim_lsp/clangd.lua @@ -0,0 +1,40 @@ +local skeleton = require 'nvim_lsp/skeleton' +local util = require 'nvim_lsp/util' +local lsp = vim.lsp + +local default_capabilities = lsp.protocol.make_client_capabilities() +default_capabilities.offsetEncoding = {"utf-8", "utf-16"} + +skeleton.clangd = { + default_config = { + cmd = {"clangd", "--background-index"}; + filetypes = {"c", "cpp", "objc", "objcpp"}; + root_dir = util.root_pattern("compile_commands.json", "compile_flags.txt", ".git"); + log_level = lsp.protocol.MessageType.Warning; + settings = {}; + capabilities = default_capabilities; + on_init = vim.schedule_wrap(function(client, result) + if result.offsetEncoding then + client.offset_encoding = result.offsetEncoding + end + end) + }; + -- commands = {}; + -- on_new_config = function(new_config) end; + -- on_attach = function(client, bufnr) end; + docs = { + description = [[ +https://clang.llvm.org/extra/clangd/Installation.html + +clangd relies on a [JSON compilation database](https://clang.llvm.org/docs/JSONCompilationDatabase.html) specified +as compile_commands.json or, for simpler projects, a compile_flags.txt. +]]; + default_config = { + root_dir = [[root_pattern("compile_commands.json", "compile_flags.txt", ".git")]]; + on_init = [[function to handle changing offsetEncoding]]; + capabilities = [[default capabilities, with offsetEncoding utf-8]]; + }; + }; +} +-- vim:et ts=2 sw=2 + diff --git a/lua/nvim_lsp/gopls.lua b/lua/nvim_lsp/gopls.lua new file mode 100644 index 00000000..86f5e7be --- /dev/null +++ b/lua/nvim_lsp/gopls.lua @@ -0,0 +1,26 @@ +local skeleton = require 'nvim_lsp/skeleton' +local util = require 'nvim_lsp/util' +local lsp = vim.lsp + +skeleton.gopls = { + default_config = { + cmd = {"gopls"}; + filetypes = {"go"}; + root_dir = util.root_pattern("go.mod", ".git"); + log_level = lsp.protocol.MessageType.Warning; + settings = {}; + }; + -- on_new_config = function(new_config) end; + -- on_attach = function(client, bufnr) end; + docs = { + description = [[ +https://github.com/golang/tools/tree/master/gopls + +Google's lsp server for golang. +]]; + default_config = { + root_dir = [[root_pattern("go.mod", ".git")]]; + }; + }; +} +-- vim:et ts=2 sw=2 diff --git a/lua/nvim_lsp/skeleton.lua b/lua/nvim_lsp/skeleton.lua new file mode 100644 index 00000000..9fa6f835 --- /dev/null +++ b/lua/nvim_lsp/skeleton.lua @@ -0,0 +1,162 @@ +local util = require 'nvim_lsp/util' +local api, validate, lsp = vim.api, vim.validate, vim.lsp +local tbl_extend = vim.tbl_extend + +local skeleton = {} + + +function skeleton.__newindex(t, template_name, template) + validate { + name = {template_name, 's'}; + default_config = {template.default_config, 't'}; + on_new_config = {template.on_new_config, 'f', true}; + on_attach = {template.on_attach, 'f', true}; + commands = {template.commands, 't', true}; + } + if template.commands then + for k, v in pairs(template.commands) do + validate { + ['command.name'] = {k, 's'}; + ['command.fn'] = {v[1], 'f'}; + } + end + end + + local M = {} + + local default_config = tbl_extend("keep", template.default_config, { + log_level = lsp.protocol.MessageType.Warning; + settings = {}; + callbacks = {}; + }) + + -- Force this part. + default_config.name = template_name + + -- The config here is the one which will be instantiated for the new server, + -- which is why this is a function, so that it can refer to the settings + -- object on the server. + local function add_callbacks(config) + config.callbacks["window/logMessage"] = function(err, method, params, client_id) + if params and params.type <= config.log_level then + lsp.builtin_callbacks[method](err, method, params, client_id) + end + end + + config.callbacks["workspace/configuration"] = function(err, method, params, client_id) + if err then error(tostring(err)) end + if not params.items then + return {} + end + + local result = {} + for _, item in ipairs(params.items) do + if item.section then + local value = util.lookup_section(config.settings, item.section) or vim.NIL + table.insert(result, value) + end + end + return result + end + end + + function M.setup(config) + validate { + root_dir = {config.root_dir, 'f', default_config.root_dir ~= nil}; + filetypes = {config.filetype, 't', true}; + on_new_config = {config.on_new_config, 'f', true}; + on_attach = {config.on_attach, 'f', true}; + } + config = tbl_extend("keep", config, default_config) + + local trigger + if config.filetypes then + trigger = "FileType "..table.concat(config.filetypes, ',') + else + trigger = "BufReadPost *" + end + api.nvim_command(string.format( + "autocmd %s lua require'nvim_lsp'[%q].manager.try_add()" + , trigger + , config.name + )) + + local get_root_dir = config.root_dir + + -- In the case of a reload, close existing things. + if M.manager then + for _, client in ipairs(M.manager.clients()) do + client.stop(true) + end + M.manager = nil + end + local manager = util.server_per_root_dir_manager(function(_root_dir) + local new_config = vim.tbl_extend("keep", {}, config) + -- Deepcopy anything that is >1 level nested. + new_config.settings = vim.deepcopy(new_config.settings) + util.tbl_deep_extend(new_config.settings, default_config.settings) + + new_config.capabilities = new_config.capabilities or lsp.protocol.make_client_capabilities() + util.tbl_deep_extend(new_config.capabilities, { + workspace = { + configuration = true; + } + }) + + add_callbacks(new_config) + if template.on_new_config then + pcall(template.on_new_config, new_config) + end + if config.on_new_config then + pcall(config.on_new_config, new_config) + end + + -- Save the old _on_attach so that we can reference it via the BufEnter. + new_config._on_attach = new_config.on_attach + new_config.on_attach = vim.schedule_wrap(function(client, bufnr) + if bufnr == api.nvim_get_current_buf() then + M._setup_buffer(client.id) + else + api.nvim_command(string.format( + "autocmd BufEnter ++once lua require'nvim_lsp'[%q]._setup_buffer(%d)" + , template_name + , bufnr + , client.id + )) + end + end) + return new_config + end) + + function manager.try_add() + local root_dir = get_root_dir(api.nvim_buf_get_name(0), api.nvim_get_current_buf()) + print(api.nvim_get_current_buf(), root_dir) + local id = manager.add(root_dir) + lsp.buf_attach_client(0, id) + end + + M.manager = manager + end + + function M._setup_buffer(client_id) + local client = lsp.get_client_by_id(client_id) + if client.config._on_attach then + client.config._on_attach(client) + end + if template.commands then + -- Create the module commands + util.create_module_commands(template_name, M.commands) + end + end + + M.commands = template.commands + M.name = template_name + M.template_config = template + + rawset(t, template_name, M) + + return M +end + +return setmetatable({}, skeleton) +-- vim:et ts=2 sw=2 diff --git a/lua/nvim_lsp/texlab.lua b/lua/nvim_lsp/texlab.lua new file mode 100644 index 00000000..223f214a --- /dev/null +++ b/lua/nvim_lsp/texlab.lua @@ -0,0 +1,77 @@ +local skeleton = require 'nvim_lsp/skeleton' +local util = require 'nvim_lsp/util' +local lsp = vim.lsp + +local cwd = vim.loop.cwd() + +local texlab_build_status = vim.tbl_add_reverse_lookup { + Success = 0; + Error = 1; + Failure = 2; + Cancelled = 3; +} + +local function buf_build(bufnr) + bufnr = util.validate_bufnr(bufnr) + local params = { textDocument = { uri = vim.uri_from_bufnr(bufnr) } } + lsp.buf_request(bufnr, 'textDocument/build', params, + function(err, _, result, _) + if err then error(tostring(err)) end + print("Build "..texlab_build_status[result.status]) + end) +end + +-- bufnr isn't actually required here, but we need a valid buffer in order to +-- be able to find the client for buf_request. +-- TODO find a client by looking through buffers for a valid client? +local function build_cancel_all(bufnr) + bufnr = util.validate_bufnr(bufnr) + local params = { token = "texlab-build-*" } + lsp.buf_request(bufnr, 'window/progress/cancel', params, function(err, method, result, client_id) + if err then error(tostring(err)) end + print("Cancel result", vim.inspect(result)) + end) +end + +skeleton.texlab = { + default_config = { + cmd = {"texlab"}; + filetypes = {"tex", "bib"}; + root_dir = function() return cwd end; + log_level = lsp.protocol.MessageType.Warning; + settings = { + latex = { + build = { + args = {"-pdf", "-interaction=nonstopmode", "-synctex=1"}; + executable = "latexmk"; + onSave = false; + }; + }; + }; + }; + commands = { + TexlabBuild = { + function() + buf_build(0) + end; + description = "Build the current buffer"; + }; + -- TexlabCancelAllBuilds = { + -- }; + }; + -- on_new_config = function(new_config) end; + -- on_attach = function(client, bufnr) end; + docs = { + description = [[ +https://texlab.netlify.com/ + +A completion engine built from scratch for (la)tex. +]]; + default_config = { + root_dir = "vim's starting directory"; + }; + }; +} + +skeleton.texlab.buf_build = buf_build +-- vim:et ts=2 sw=2 diff --git a/lua/nvim_lsp/util.lua b/lua/nvim_lsp/util.lua new file mode 100644 index 00000000..190869e6 --- /dev/null +++ b/lua/nvim_lsp/util.lua @@ -0,0 +1,262 @@ +local validate = vim.validate +local api = vim.api +local lsp = vim.lsp +local uv = vim.loop + +local M = {} + +function M.validate_bufnr(bufnr) + validate { + bufnr = { bufnr, 'n' } + } + return bufnr == 0 and api.nvim_get_current_buf() or bufnr +end + +function M.add_hook_before(fn, new_fn) + if fn then + return function(...) + -- TODO which result? + new_fn(...) + return fn(...) + end + else + return new_fn + end +end + +function M.add_hook_after(fn, new_fn) + if fn then + return function(...) + -- TODO which result? + fn(...) + return new_fn(...) + end + else + return new_fn + end +end + +local function split_lines(s) + return vim.split(s, "\n", true) +end + +function M.tbl_deep_extend(dst, ...) + validate { dst = { dst, 't' } } + for i = 1, select("#", ...) do + local t = select(i, ...) + validate { arg = { t, 't' } } + for k, v in pairs(t) do + if type(v) == 'table' and not vim.tbl_islist(v) then + dst[k] = M.tbl_deep_extend(dst[k] or {}, v) + else + dst[k] = v + end + end + end + return dst +end + +function M.nvim_multiline_command(command) + validate { command = { command, 's' } } + for line in vim.gsplit(command, "\n", true) do + api.nvim_command(line) + end +end + +function M.lookup_section(settings, section) + for part in vim.gsplit(section, '.', true) do + settings = settings[part] + end + return settings +end + +function M.create_module_commands(module_name, commands) + for command_name, def in pairs(commands) do + local parts = {"command!"} + -- Insert attributes. + for k, v in pairs(def) do + if type(k) == 'string' and type(v) == 'boolean' and v then + table.insert(parts, "-"..k) + elseif type(k) == 'number' and type(v) == 'string' and v:match("^%-") then + table.insert(parts, v) + end + end + table.insert(parts, command_name) + -- The command definition. + table.insert(parts, + string.format("lua require'nvim_lsp'[%q].commands[%q][1]()", module_name, command_name)) + api.nvim_command(table.concat(parts, " ")) + end +end + +-- Some path utilities +M.path = (function() + local function exists(filename) + local stat = uv.fs_stat(filename) + return stat and stat.type or false + end + + local function is_dir(filename) + return exists(filename) == 'directory' + end + + local function is_file(filename) + return exists(filename) == 'file' + end + + local is_windows = uv.os_uname().sysname == "Windows" + local path_sep = is_windows and "\\" or "/" + + local is_fs_root + if is_windows then + is_fs_root = function(path) + return path:match("^%a:\\\\$") + end + else + is_fs_root = function(path) + return path == "/" + end + end + + local dirname + do + local strip_dir_pat = path_sep.."([^"..path_sep.."]+)$" + local strip_sep_pat = path_sep.."$" + dirname = function(path) + if not path then return end + local result = path:gsub(strip_sep_pat, ""):gsub(strip_dir_pat, "") + if #result == 0 then + return "/" + end + return result + end + end + + local function path_join(...) + return table.concat(vim.tbl_flatten {...}, path_sep) + end + + -- Traverse the path calling cb along the way. + local function traverse_parents(path, cb) + path = uv.fs_realpath(path) + local dir = path + -- Just in case our algo is buggy, don't infinite loop. + for _ = 1, 100 do + dir = dirname(dir) + if not dir then return end + -- If we can't ascend further, then stop looking. + if cb(dir, path) then + return dir, path + end + if is_fs_root(dir) then + break + end + end + end + + -- Iterate the path until we find the rootdir. + local function iterate_parents(path) + path = uv.fs_realpath(path) + local function it(s, v) + if not v then return end + if is_fs_root(v) then return end + return dirname(v), path + end + return it, path, path + end + + return { + is_dir = is_dir; + is_file = is_file; + exists = exists; + sep = path_sep; + dirname = dirname; + join = path_join; + traverse_parents = traverse_parents; + iterate_parents = iterate_parents; + } +end)() + + +-- Returns a function(root_dir), which, when called with a root_dir it hasn't +-- seen before, will call make_config(root_dir) and start a new client. +function M.server_per_root_dir_manager(make_config) + local clients = {} + local manager = {} + + function manager.add(root_dir) + if not root_dir then return end + + -- Check if we have a client alredy or start and store it. + local client_id = clients[root_dir] + if not client_id then + local new_config = make_config(root_dir) + new_config.root_dir = root_dir + new_config.on_exit = M.add_hook_before(new_config.on_exit, function() + clients[root_dir] = nil + end) + client_id = lsp.start_client(new_config) + clients[root_dir] = client_id + end + return client_id + end + + function manager.clients() + local res = {} + for _, id in pairs(clients) do + local client = lsp.get_client_by_id(id) + if client then + table.insert(res, client) + end + end + return res + end + + return manager +end + +function M.search_ancestors(startpath, fn) + validate { fn = {fn, 'f'} } + if fn(startpath) then return startpath end + for path in M.path.iterate_parents(startpath) do + if fn(path) then return path end + end +end + +function M.root_pattern(...) + local patterns = vim.tbl_flatten {...} + local function matcher(path) + for _, pattern in ipairs(patterns) do + if M.path.exists(M.path.join(path, pattern)) then + return path + end + end + end + return function(startpath) + return M.search_ancestors(startpath, matcher) + end +end +function M.find_git_ancestor(startpath) + return M.search_ancestors(startpath, function(path) + if M.path.is_dir(M.path.join(path, ".git")) then + return path + end + end) +end +function M.find_node_modules_ancestor(startpath) + return M.search_ancestors(startpath, function(path) + if M.path.is_dir(M.path.join(path, "node_modules")) then + return path + end + end) +end +function M.find_package_json_ancestor(startpath) + return M.search_ancestors(startpath, function(path) + if M.path.is_file(M.path.join(path, "package.json")) then + return path + end + end) +end + +return M +-- vim:et ts=2 sw=2 diff --git a/scripts/docgen.lua b/scripts/docgen.lua index 6cd2b1cb..159c7b5e 100644 --- a/scripts/docgen.lua +++ b/scripts/docgen.lua @@ -1,4 +1,4 @@ -local skeleton = require 'common_lsp/skeleton' +local skeleton = require 'nvim_lsp/skeleton' local inspect = vim.inspect local function filter(...) @@ -91,8 +91,8 @@ for k, v in pairs(skeleton) do ## {{template_name}} {{preamble}} -common_lsp.{{template_name}}.setup({config}) -common_lsp#setup("{{template_name}}", {config}) +nvim_lsp.{{template_name}}.setup({config}) +nvim_lsp#setup("{{template_name}}", {config}) ``` {{body}} -- cgit v1.2.3-70-g09d2