diff --git a/README.md b/README.md index 52b9553..58e6dba 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,28 @@ require'lspconfig'.omnisharp.setup { } ``` +#### Mount Additional Volumes + +You can mount additional volumes to let the lsp server access caches or other directories. + +```lua +require'lspconfig'.rust_analyzer.setup { + cmd = require'lspcontainers'.command( + 'rust_analyzer', + { + extra_volumes = { + '/home/[UserName]/.cargo:/root/.cargo:ro', + ... + } + } + ), + ... +} +``` + +The property `extra_volumes` recieves a list of strings formated for the `--volume` option of Docker. +[See reference](https://docs.docker.com/engine/reference/run/#volume-shared-filesystems). + ### Podman Support If you are using podman instead of docker it is sufficient to just specify "podman" as `container_runtime`: diff --git a/lua/lspcontainers/init.lua b/lua/lspcontainers/init.lua index 0fae8e5..abfd336 100644 --- a/lua/lspcontainers/init.lua +++ b/lua/lspcontainers/init.lua @@ -25,9 +25,9 @@ local supported_languages = { local user = user_id..":"..group_id if runtime == "docker" then - network = "bridge" + network = "bridge" elseif runtime == "podman" then - network = "slirp4netns" + network = "slirp4netns" end return { @@ -61,7 +61,7 @@ local supported_languages = { solargraph = { image = "docker.io/lspcontainers/solargraph" }, sumneko_lua = { image = "docker.io/lspcontainers/lua-language-server" }, svelte = { image = "docker.io/lspcontainers/svelte-language-server" }, - tailwindcss= { image = "docker.io/lspcontainers/tailwindcss-language-server" }, + tailwindcss = { image = "docker.io/lspcontainers/tailwindcss-language-server" }, terraformls = { image = "docker.io/lspcontainers/terraform-ls" }, tsserver = { image = "docker.io/lspcontainers/typescript-language-server" }, vuels = { image = "docker.io/lspcontainers/vue-language-server" }, @@ -69,39 +69,45 @@ local supported_languages = { } -- default command to run the lsp container -local default_cmd = function (runtime, workdir, image, network, docker_volume) +local default_cmd = function(runtime, workdir, image, network, docker_volume, extra_volumes) if vim.loop.os_uname().sysname == "Windows_NT" then workdir = Dos2UnixSafePath(workdir) end - local mnt_volume - if docker_volume ~= nil then - mnt_volume ="--volume="..docker_volume..":"..workdir..":z" - else - mnt_volume = "--volume="..workdir..":"..workdir..":z" - end - - return { + local cmd = { runtime, "container", "run", "--interactive", "--rm", - "--network="..network, - "--workdir="..workdir, - mnt_volume, - image + "--network=" .. network, + "--workdir=" .. workdir, } + + if docker_volume ~= nil then + table.insert(cmd, "--volume="..docker_volume..":"..workdir..":z") + else + table.insert(cmd, "--volume="..workdir..":"..workdir..":z") + end + + for _, v in pairs(extra_volumes) do + table.insert(cmd, "--volume="..v) + end + + table.insert(cmd, image) + + return cmd end local function command(server, user_opts) -- Start out with the default values: - local opts = { + local opts = { container_runtime = "docker", root_dir = vim.fn.getcwd(), cmd_builder = default_cmd, network = "none", docker_volume = nil, + extra_volumes = {}, } -- If the LSP is known, it override the defaults: @@ -119,7 +125,14 @@ local function command(server, user_opts) return 1 end - return opts.cmd_builder(opts.container_runtime, opts.root_dir, opts.image, opts.network, opts.docker_volume) + return opts.cmd_builder( + opts.container_runtime, + opts.root_dir, + opts.image, + opts.network, + opts.docker_volume, + opts.extra_volumes + ) end Dos2UnixSafePath = function(workdir)