feat: added workspace support

This commit is contained in:
Folke Lemaitre 2021-07-14 21:07:08 +02:00
parent cf3b028043
commit 681c0fc46c
No known key found for this signature in database
GPG Key ID: 707FE6FEB82F7984
4 changed files with 132 additions and 82 deletions

8
.nvim.settings.json Normal file
View File

@ -0,0 +1,8 @@
{
"lua-dev": {
"library": { "plugins": ["workspace.nvim"] }
},
"lspconfig": {
"sumneko_lua": { "settings": {} }
}
}

View File

@ -2,6 +2,7 @@ local M = {}
--- @class LuaApiOptions
M.defaults = {
config_name = "sumneko_lua", -- name of the lspconfig config
library = {
vimruntime = true, -- runtime path
types = true, -- full signature, docs and completion of vim.api, vim.treesitter, vim.lsp and others

View File

@ -3,8 +3,49 @@ local M = {}
function M.setup(opts)
local config = require("lua-dev.config")
config.setup(opts)
M.setup_workspace()
local ret = require("lua-dev.sumneko").setup()
return vim.tbl_deep_extend("force", {}, ret, config.options.lspconfig or {})
end
function M.setup_workspace()
local ok, workspace = pcall(require, "workspace")
if not ok then
return
end
local config = require("lua-dev.config")
local sumneko = require("lua-dev.sumneko")
workspace.register({
name = "lua-dev",
get_schema = function()
return require("workspace.schema").to_schema({ ["lua-dev"] = config.defaults })
end,
---@param ws Workspace
on_workspace_settings = function(ws)
local local_settings = ws.local_settings
-- merge global lua-dev options with workspace options
local opts = ws.settings:get("lua-dev", { defaults = config.options })
-- enable lua-dev if:
-- * this is a lua file part of your nvim config
-- * this is a workspace that has a lua folder (so most likely a plugin)
-- * the local workspace settings have lua-dev.enabled = true
local is_vim_config = ws:has_file(sumneko.config_path())
local enabled = is_vim_config or ws:has_file(ws.root_dir .. "/lua") or local_settings:get("lua-dev.enabled")
if enabled then
-- enable plugins by default only for nvim configs
if not is_vim_config and not local_settings:get("lua-dev.library.plugins") then
opts.library.plugins = false
end
local settings = sumneko.get(opts).setup().settings
ws.settings:merge(settings, "lspconfig." .. opts.config_name .. ".settings")
end
end,
})
end
return M

View File

@ -1,106 +1,106 @@
local Config = require("lua-dev.config")
local M = {}
local S = {}
function M.library()
local ret = {}
function S.get(opts)
local M = {}
if Config.options.library.types then
ret[M.types()] = true
end
function M.library()
local ret = {}
local function add(lib, filter)
for _, p in pairs(vim.fn.expand(lib .. "/lua", false, true)) do
p = vim.loop.fs_realpath(p)
if p then
-- p = vim.fn.fnamemodify(p, ":h")
local skip = false
if type(filter) == "table" then
local name = vim.fn.fnamemodify(p, ":t")
skip = true
for _, f in pairs(filter) do
if name == f then
skip = false
break
if opts.library.types then
ret[M.types()] = true
end
local function add(lib, filter)
for _, p in pairs(vim.fn.expand(lib .. "/lua", false, true)) do
p = vim.loop.fs_realpath(p)
if p then
-- p = vim.fn.fnamemodify(p, ":h")
local skip = false
if type(filter) == "table" then
local name = vim.fn.fnamemodify(p, ":h:t")
skip = true
for _, f in pairs(filter) do
if name == f then
skip = false
break
end
end
end
end
if not skip then
ret[p] = true
if not skip then
ret[p] = true
end
end
end
end
end
if Config.options.library.vimruntime then
add("$VIMRUNTIME")
end
if Config.options.library.plugins then
for _, site in pairs(vim.split(vim.o.packpath, ",")) do
add(site .. "/pack/*/opt/*", Config.options.library.plugins)
add(site .. "/pack/*/start/*", Config.options.library.plugins)
if opts.library.vimruntime then
add("$VIMRUNTIME")
end
if opts.library.plugins then
for _, site in pairs(vim.split(vim.o.packpath, ",")) do
add(site .. "/pack/*/opt/*", opts.library.plugins)
add(site .. "/pack/*/start/*", opts.library.plugins)
end
end
return ret
end
return ret
end
function M.path()
local path = {} --vim.split(package.path, ";")
table.insert(path, "lua/?.lua")
table.insert(path, "lua/?/init.lua")
for lib, _ in pairs(M.library()) do
function M.path()
local path = {} --vim.split(package.path, ";")
table.insert(path, "lua/?.lua")
table.insert(path, "lua/?/init.lua")
-- for lib, _ in pairs(M.library()) do
-- table.insert(path, lib .. "/?.lua")
-- table.insert(path, lib .. "/?/init.lua")
-- end
return path
end
return path
end
function M.config_path()
return vim.loop.fs_realpath(vim.fn.stdpath("config"))
end
function M.config_path()
return vim.loop.fs_realpath(vim.fn.stdpath("config"))
end
function M.types()
local f = debug.getinfo(1, "S").source:sub(2)
return vim.loop.fs_realpath(vim.fn.fnamemodify(f, ":h:h:h") .. "/types")
end
function M.types()
local f = debug.getinfo(1, "S").source:sub(2)
return vim.loop.fs_realpath(vim.fn.fnamemodify(f, ":h:h:h") .. "/types")
end
function M.setup()
local path = M.path()
return {
on_new_config = function(config, root_dir)
local lib = vim.tbl_deep_extend("force", {}, config.settings.Lua.workspace.library)
lib[vim.loop.fs_realpath(root_dir) .. "/lua"] = nil
lib[vim.loop.fs_realpath(root_dir)] = nil
config.settings.Lua.workspace.library = lib
return config
end,
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
-- Setup your lua path
path = path,
function M.setup()
local path = M.path()
return {
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
-- Setup your lua path
path = path,
},
completion = { callSnippet = "Replace" },
-- diagnostics = {
-- -- Get the language server to recognize the `vim` global
-- globals = { "vim" },
-- },
-- hint = { enable = true },
workspace = {
-- Make the server aware of Neovim runtime files
library = M.library(),
maxPreload = 1000,
preloadFileSize = 150,
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = { enable = false },
},
completion = { callSnippet = "Replace" },
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = { "vim" },
},
-- hint = { enable = true },
workspace = {
-- Make the server aware of Neovim runtime files
library = M.library(),
maxPreload = 1000,
preloadFileSize = 150,
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = { enable = false },
},
},
}
}
end
return M
end
return M
S = vim.tbl_deep_extend("force", S, S.get(Config.options))
return S