diff options
Diffstat (limited to 'lua/lspconfig/util.lua')
| -rw-r--r-- | lua/lspconfig/util.lua | 127 |
1 files changed, 72 insertions, 55 deletions
diff --git a/lua/lspconfig/util.lua b/lua/lspconfig/util.lua index c4c21c22..625d3170 100644 --- a/lua/lspconfig/util.lua +++ b/lua/lspconfig/util.lua @@ -8,16 +8,16 @@ local fn = vim.fn local M = {} M.default_config = { - log_level = lsp.protocol.MessageType.Warning; - message_level = lsp.protocol.MessageType.Warning; - settings = vim.empty_dict(); - init_options = vim.empty_dict(); - handlers = {}; + log_level = lsp.protocol.MessageType.Warning, + message_level = lsp.protocol.MessageType.Warning, + settings = vim.empty_dict(), + init_options = vim.empty_dict(), + handlers = {}, } function M.validate_bufnr(bufnr) validate { - bufnr = { bufnr, 'n' } + bufnr = { bufnr, "n" }, } return bufnr == 0 and api.nvim_get_current_buf() or bufnr end @@ -48,19 +48,21 @@ end function M.create_module_commands(module_name, commands) for command_name, def in pairs(commands) do - local parts = {"command!"} + 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 + 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'lspconfig'[%q].commands[%q][1](<f-args>)", module_name, command_name)) + table.insert( + parts, + string.format("lua require'lspconfig'[%q].commands[%q][1](<f-args>)", module_name, command_name) + ) api.nvim_command(table.concat(parts, " ")) end end @@ -75,8 +77,8 @@ function M.has_bins(...) end M.script_path = function() - local str = debug.getinfo(2, "S").source:sub(2) - return str:match("(.*[/\\])") + local str = debug.getinfo(2, "S").source:sub(2) + return str:match "(.*[/\\])" end -- Some path utilities @@ -87,20 +89,20 @@ M.path = (function() end local function is_dir(filename) - return exists(filename) == 'directory' + return exists(filename) == "directory" end local function is_file(filename) - return exists(filename) == 'file' + return exists(filename) == "file" end - local is_windows = uv.os_uname().version:match("Windows") + local is_windows = uv.os_uname().version:match "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:$") + return path:match "^%a:$" end else is_fs_root = function(path) @@ -110,16 +112,16 @@ M.path = (function() local function is_absolute(filename) if is_windows then - return filename:match("^%a:") or filename:match("^\\\\") + return filename:match "^%a:" or filename:match "^\\\\" else - return filename:match("^/") + return filename:match "^/" end end local dirname do - local strip_dir_pat = path_sep.."([^"..path_sep.."]+)$" - local strip_sep_pat = path_sep.."$" + local strip_dir_pat = path_sep .. "([^" .. path_sep .. "]+)$" + local strip_sep_pat = path_sep .. "$" dirname = function(path) if not path or #path == 0 then return @@ -133,9 +135,7 @@ M.path = (function() end local function path_join(...) - local result = - table.concat( - vim.tbl_flatten {...}, path_sep):gsub(path_sep.."+", path_sep) + local result = table.concat(vim.tbl_flatten { ... }, path_sep):gsub(path_sep .. "+", path_sep) return result end @@ -146,7 +146,9 @@ M.path = (function() -- 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 not dir then + return + end -- If we can't ascend further, then stop looking. if cb(dir, path) then return dir, path @@ -161,42 +163,45 @@ M.path = (function() 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 + if not v then + return + end + if is_fs_root(v) then + return + end return dirname(v), path end return it, path, path end local function is_descendant(root, path) - if (not path) then - return false; + if not path then + return false end local function cb(dir, _) - return dir == root; + return dir == root end - local dir, _ = traverse_parents(path, cb); + local dir, _ = traverse_parents(path, cb) - return dir == root; + return dir == root end return { - is_dir = is_dir; - is_file = is_file; - is_absolute = is_absolute; - exists = exists; - sep = path_sep; - dirname = dirname; - join = path_join; - traverse_parents = traverse_parents; - iterate_parents = iterate_parents; - is_descendant = is_descendant; + is_dir = is_dir, + is_file = is_file, + is_absolute = is_absolute, + exists = exists, + sep = path_sep, + dirname = dirname, + join = path_join, + traverse_parents = traverse_parents, + iterate_parents = iterate_parents, + is_descendant = is_descendant, } 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) @@ -204,8 +209,12 @@ function M.server_per_root_dir_manager(_make_config) local manager = {} function manager.add(root_dir) - if not root_dir then return end - if not M.path.is_dir(root_dir) then return end + if not root_dir then + return + end + if not M.path.is_dir(root_dir) then + return + end -- Check if we have a client alredy or start and store it. local client_id = clients[root_dir] @@ -213,10 +222,15 @@ function M.server_per_root_dir_manager(_make_config) local new_config = _make_config(root_dir) --TODO:mjlbach -- these prints only show up with nvim_error_writeln() if not new_config.cmd then - print(string.format("Error, cmd not defined for [%q].".. - "You must manually define a cmd for the default config for this server." - .."See server documentation.", new_config.name)) - return + print( + string.format( + "Error, cmd not defined for [%q]." + .. "You must manually define a cmd for the default config for this server." + .. "See server documentation.", + new_config.name + ) + ) + return elseif vim.fn.executable(new_config.cmd[1]) == 0 then vim.notify(string.format("cmd [%q] is not executable.", new_config.cmd[1]), vim.log.levels.Error) return @@ -245,15 +259,19 @@ function M.server_per_root_dir_manager(_make_config) end function M.search_ancestors(startpath, func) - validate { func = {func, 'f'} } - if func(startpath) then return startpath end + validate { func = { func, "f" } } + if func(startpath) then + return startpath + end for path in M.path.iterate_parents(startpath) do - if func(path) then return path end + if func(path) then + return path + end end end function M.root_pattern(...) - local patterns = vim.tbl_flatten {...} + local patterns = vim.tbl_flatten { ... } local function matcher(path) for _, pattern in ipairs(patterns) do for _, p in ipairs(vim.fn.glob(M.path.join(path, pattern), true, true)) do @@ -289,6 +307,5 @@ function M.find_package_json_ancestor(startpath) end) end - return M -- vim:et ts=2 sw=2 |
