Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pyright languageserver #303

Merged
merged 6 commits into from
Nov 16, 2020
Merged

Add pyright languageserver #303

merged 6 commits into from
Nov 16, 2020

Conversation

mjlbach
Copy link
Contributor

@mjlbach mjlbach commented Jul 14, 2020

Closes #299
Depends on neovim/neovim#12638

lua/nvim_lsp/pyright.lua Outdated Show resolved Hide resolved
@mjlbach
Copy link
Contributor Author

mjlbach commented Jul 14, 2020

There are currently some issues discovering python module completion, still working through it. It works intermittently for some modules now, and there's a difference using the new pyright-langserver cli option vs manually building and running the language server directly...

@polarmutex
Copy link

@mjlbach what are the current issues you are seeing, and is there something I can help fix or look into?

@mjlbach
Copy link
Contributor Author

mjlbach commented Jul 31, 2020

Hi @bryall, there's not any issues, but pyright won't work without implementing some additional features in neovim core (namely workspaceDidChange). See: neovim/neovim#12638

That PR works, it's just a bit messy.

@mjlbach
Copy link
Contributor Author

mjlbach commented Jul 31, 2020

It's also important to note that the Microsoft Python Language server in C# will soon be no longer maintained.

microsoft/python-language-server#2096 (comment)

@polarmutex
Copy link

i know, I am planning on switching to this LSP soon, and want to see if I can provide support to help get this in good shape. I will take a look at the issue

@mjlbach
Copy link
Contributor Author

mjlbach commented Jul 31, 2020

If you try out the above PR and the one from neovim core, let me know how it works :)

@mjlbach
Copy link
Contributor Author

mjlbach commented Sep 9, 2020

@justinmk I temporarily added a workaround (cc: @bryall) until the neovim/neovim#12638 is merged, so I believe this can be merged now.

@Seirdy
Copy link

Seirdy commented Sep 9, 2020 via email

@mjlbach
Copy link
Contributor Author

mjlbach commented Sep 9, 2020

@justinmk I temporarily added a workaround (cc: @bryall) until the #299 is merged, so I believe this can be merged now.
Dym until [neovim/neovim#12638](neovim/neovim#12638) is merged? Rohan Kumar

Yes, fixed

@qbedard
Copy link

qbedard commented Nov 10, 2020

Well hot dang. Can we merge?

@mjlbach
Copy link
Contributor Author

mjlbach commented Nov 10, 2020

FYI for those wanting to use this and waiting for this to be merged, you can add what this PR does to your init.vim.

:lua << EOF
  local nvim_lsp = require('nvim_lsp')
  -- vim.lsp.set_log_level("debug")

  local on_attach = function(_, bufnr)
    vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
    require'diagnostic'.on_attach()
    require'completion'.on_attach()

    -- Mappings.
    local opts = { noremap=true, silent=true }
    vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<Cmd>lua vim.lsp.buf.hover()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>wl', '<cmd>lua vim.lsp.buf.list_workspace_folders()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', '<leader>e', '<cmd>lua vim.lsp.util.show_line_diagnostics()<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', ']d', '<cmd>lua vim.api.nvim_command("NextDiagnosticCycle")<CR>', opts)
    vim.api.nvim_buf_set_keymap(bufnr, 'n', '[d', '<cmd>lua vim.api.nvim_command("PrevDiagnosticCycle")<CR>', opts)
  end

  local configs = require('nvim_lsp/configs')
  --if not configs.pyright then
  configs.pyright = {
    default_config = {
      cmd = {"pyright-langserver", "--stdio"};
      filetypes = {"python"};
      root_dir = nvim_lsp.util.root_pattern(".git", "setup.py",  "setup.cfg", "pyproject.toml", "requirements.txt");
      settings = {
        analysis = { autoSearchPaths= true; };
        pyright = { useLibraryCodeForTypes = true; };
      };
      -- The following before_init function can be removed once https://github.com/neovim/neovim/pull/12638 is merged
      before_init = function(initialize_params)
        initialize_params['workspaceFolders'] = {{
          name = 'workspace',
          uri = initialize_params['rootUri']
        }}
      end
      };
  }
  --end

  local servers = {'gopls', 'rust_analyzer', 'sumneko_lua', 'tsserver', 'vimls', 'jsonls', 'html', 'ghcide', 'rnix', 'ocamllsp', 'pyright'}
  for _, lsp in ipairs(servers) do
    nvim_lsp[lsp].setup {
      on_attach = on_attach,
    }
  end
EOF

@mjlbach
Copy link
Contributor Author

mjlbach commented Nov 16, 2020

@h-michael Can we merge this?

@h-michael
Copy link
Contributor

h-michael commented Nov 16, 2020

@mjlbach Could you chane module name?
#348

@mjlbach
Copy link
Contributor Author

mjlbach commented Nov 16, 2020

From pyright?

@mjlbach
Copy link
Contributor Author

mjlbach commented Nov 16, 2020

Oh, I see. Yep, I'll fix

@h-michael
Copy link
Contributor

@mjlbach See this PR #348

@mjlbach
Copy link
Contributor Author

mjlbach commented Nov 16, 2020

Done, I can squash commits if necessary

@h-michael h-michael merged commit df9cd18 into neovim:master Nov 16, 2020
@h-michael
Copy link
Contributor

@mjlbach Thanks

@kamalmarhubi
Copy link

@mjlbach excited to try this. does it still need neovim/neovim#12638 in order to work? if so, 🤞 that gets merged soon!

@mjlbach
Copy link
Contributor Author

mjlbach commented Nov 16, 2020

@kamalmarhubi Nope, I added a temporary fix to bypass the need for that PR, I'll remove the fix once that PR is merged.

@ellisonleao
Copy link

really dumb question @mjlbach :

is pyright supposed to work as an alternative to pyls now?

@RonanMacF
Copy link

really dumb question @mjlbach :

is pyright supposed to work as an alternative to pyls now?

Fom my understanding this should currently be the best python language server out there.

@RonanMacF
Copy link

Anyone manage to get this setup using the code pasted by OP above? I have the pyright installed and can see that I have the pyright object selected but when I try run setup i'm running into attempt to call field 'setup' (a nil value)

@mjlbach
Copy link
Contributor Author

mjlbach commented Nov 16, 2020

This was merged, did you try just using the current plugin? And I use the snippet I posted

@RonanMacF
Copy link

I am actually using your nix neovim-nightly-master which hasn't updated yet but was eager to try it out :) Yep copy + paste just changed local servers = { 'pyright' } and removed the diagnostic line. I'll wait for build to update and try it again though

@mjlbach
Copy link
Contributor Author

mjlbach commented Nov 16, 2020

Ah that might be out of date, you may need to try replace nvim_lsp with lspconfig in the snippet

@mjlbach
Copy link
Contributor Author

mjlbach commented Jan 18, 2021

Pyright (as seen in the quickstart guide), works fine. No workarounds needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add pyright support
9 participants