simrat39 / rust-tools.nvim

Tools for better development in rust using neovim's builtin lsp
MIT License
2.17k stars 158 forks source link

LSP not working in some autocomplete contexts #220

Closed Caylub closed 2 years ago

Caylub commented 2 years ago

Expected Behavior

Typing . after a variable should resolve methods from LSP into my autocomplete plugin.

Current Behavior

LSP works as expected when declaring imports, without delays or any other strange behavior.

When attempting to use the LSP to populate the autocomplete in other contexts, for instance in the case of trying to populate the autocomplete menu with resolved methods of a type, it doesn't work.

Ex: let x: str = "SomeString". doesn't show anything in the autocomplete menu.

Possible Solution

I'm not sure what's going on since it works as expected in one context and not at all in the other.

Steps to Reproduce

  1. using neovim 0.8.0-dev+151-gd30621064, install simrat39/rust-tools.nvim
  2. create a rust project with cargo new ...
  3. open the main.rs
  4. configure cmp.nvim
    
    local cmp_status_ok, cmp = pcall(require, "cmp")
    if not cmp_status_ok then
    return
    end

local snip_status_ok, luasnip = pcall(require, "luasnip") if not snip_status_ok then return end

require("luasnip/loaders/from_vscode").lazy_load()

local check_backspace = function() local col = vim.fn.col "." - 1 return col == 0 or vim.fn.getline("."):sub(col, col):match "%s" end

local kind_icons = { Text = "", Method = "m", Function = "", Constructor = "", Field = "", Variable = "", Class = "", Interface = "", Module = "", Property = "", Unit = "", Value = "", Enum = "", Keyword = "", Snippet = "", Color = "", File = "", Reference = "", Folder = "", EnumMember = "", Constant = "", Struct = "", Event = "", Operator = "", TypeParameter = "", }

cmp.setup { snippet = { expand = function(args) luasnip.lsp_expand(args.body) -- For luasnip users. end, }, mapping = { [""] = cmp.mapping.select_prev_item(), [""] = cmp.mapping.select_next_item(), [""] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }), [""] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }), [""] = cmp.config.disable, [""] = cmp.mapping { i = cmp.mapping.abort(), c = cmp.mapping.close(), }, [""] = cmp.mapping.confirm { select = true }, [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_next_item() elseif luasnip.expandable() then luasnip.expand() elseif luasnip.expand_or_jumpable() then luasnip.expand_or_jump() elseif check_backspace() then fallback() else fallback() end end, { "i", "s", }), [""] = cmp.mapping(function(fallback) if cmp.visible() then cmp.select_prev_item() elseif luasnip.jumpable(-1) then luasnip.jump(-1) else fallback() end end, { "i", "s", }), }, formatting = { fields = { "kind", "abbr", "menu" }, format = function(entry, vim_item) vim_item.kind = string.format("%s", kind_icons[vim_item.kind]) vim_item.menu = ({ nvim_lsp = "[LS]", luasnip = "[Snippet]", buffer = "[Buffer]", path = "[Path]", })[entry.source.name] return vim_item end, }, sources = { { name = "nvim_lsp" }, { name = "luasnip" }, { name = "buffer" }, { name = "path" }, }, confirm_opts = { behavior = cmp.ConfirmBehavior.Replace, select = false, }, window = { documentation = { border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, }, }, view = { ghost_text = false, native_menu = false, }, }


5. verify that rust-tools has installed the language server and that it's running
6. attempt to use the running LSP to populate autocomplete, and see that it works.
7. attempt to use LSP to populate autocomplete when defining a variable, and see that it does not work.

## Context (Environment)
* neovim `0.8.0-dev+151-gd30621064`
* `rust-tools` (commit id: `11dcd67`) 
*   -- cmp plugins
  use "hrsh7th/nvim-cmp"
  use "hrsh7th/cmp-buffer"
  use "hrsh7th/cmp-path"
  use "hrsh7th/cmp-cmdline"
  use "hrsh7th/cmp-nvim-lsp"
  use "saadparwaiz1/cmp_luasnip"

## Video Snippet Of Issue
![LSP_Autocomplete_Not_Working_Correctly](https://user-images.githubusercontent.com/109186779/178626981-bcab6fd3-f9f7-44b9-ba9b-77927ff9ea59.gif)
simrat39 commented 2 years ago

image Interesting, seems to work for me, could be something with your cmp config.

Thanks for the detailed report, I'll have a look and see whats up

simrat39 commented 2 years ago

image Ok here's my config, should fix the issue for you, I should probably add this to the wiki

Caylub commented 2 years ago

Thanks for the reply! Would you mind sending me your neovim config?

simrat39 commented 2 years ago

https://github.com/simrat39/dotfiles/blob/master/nvim/.config/nvim/lua/sim_config/rust-tools.lua#L4