neovim / nvim-lspconfig

Quickstart configs for Nvim LSP
Apache License 2.0
10.46k stars 2.06k forks source link

Alternative project local settings #1874

Closed henry-hsieh closed 2 years ago

henry-hsieh commented 2 years ago

Language server

No response

Requested feature

I'm aware of there is a Wiki page for project local settings. It uses set exrc that Neovim will execute the .nvimrc or .exrc of the current directory. However, the set exrc is not secure. Moreover, the option is not usable at latest release version of Neovim (0.7.0).

Coc uses .vim/coc-settings.json as local configuration file instead of using script. I thought that other LSP clients use configuration files, too. It's more secure by parsing configuration file.

Other clients which have this feature

coc.nvim

uggedal commented 2 years ago

For per project level virtualenv paths with pylsp I did the following:

local jedi_env = function(env)
  return {
    pylsp = {
      plugins = {
        jedi = {
          environment = env,
        },
      },
    },
  }
end

lspconfig.pylsp.setup({
  on_init = function(client)
    local path = client.workspace_folders[1].name

    if path == '/home/foo/src/project1' then
      client.config.settings = jedi_env('/home/foo/.local/pipx/venvs/pyinfra')
    elseif path == '/home/foo/src/project2' then
      client.config.settings = jedi_env('./venv')
    end

    client.notify('workspace/didChangeConfiguration')
    return true
  end,
})
henry-hsieh commented 2 years ago

I recently realized that Neovim has a built-in function for converting JSON to Lua table. All you need to do is parsing setting files at root directory and assign to LSP settings.

The example is in my pull request of svlangserver. Check the on_init function at line 38.

henry-hsieh commented 2 years ago

For per project level virtualenv paths with pylsp I did the following:

local jedi_env = function(env)
  return {
    pylsp = {
      plugins = {
        jedi = {
          environment = env,
        },
      },
    },
  }
end

lspconfig.pylsp.setup({
  on_init = function(client)
    local path = client.workspace_folders[1].name

    if path == '/home/foo/src/project1' then
      client.config.settings = jedi_env('/home/foo/.local/pipx/venvs/pyinfra')
    elseif path == '/home/foo/src/project2' then
      client.config.settings = jedi_env('./venv')
    end

    client.notify('workspace/didChangeConfiguration')
    return true
  end,
})

I updated the project local settings in Wiki page. I think that your method should be default method for project local settings because replacement of exrc is detecting path in personal vimrc.

nlsp-settings.nvim provides project local settings likes coc.nvim for lspconfig. Therefore, my custom json loader can be replaced by it.