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

Weird replace behaviour with gopls #706

Closed
2 tasks done
itaranto opened this issue Jan 5, 2022 · 6 comments
Closed
2 tasks done

Weird replace behaviour with gopls #706

itaranto opened this issue Jan 5, 2022 · 6 comments
Labels
bug Something isn't working

Comments

@itaranto
Copy link

itaranto commented Jan 5, 2022

FAQ

  • I have checked the FAQ and it didn't resolve my problem.

Issues

  • I have checked existing issues and there are no open or closed issues with the same problem.

Neovim Version

NVIM v0.6.1

Minimal reproducible config

-- Set completeopt to have a better completion experience
vim.opt.completeopt = 'menuone,noselect'

-- nvim-cmp setup
local has_words_before = function()
  local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match('%s') == nil
end

local luasnip = require 'luasnip'
local cmp = require('cmp')

cmp.setup {
  mapping = {
    ['<C-p>'] = cmp.mapping.select_prev_item(),
    ['<C-n>'] = cmp.mapping.select_next_item(),
    ['<C-d>'] = cmp.mapping.scroll_docs(4),
    ['<C-u>'] = cmp.mapping.scroll_docs(-4),
    ['<CR>'] = cmp.mapping.confirm {
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    },
    ['<Tab>'] = cmp.mapping(function(fallback)
      if cmp.visible() then
        cmp.select_next_item()
      elseif luasnip.expand_or_jumpable() then
        luasnip.expand_or_jump()
      elseif has_words_before() then
        cmp.complete()
      else
        fallback()
      end
    end, { 'i', 's' }),
    ['<S-Tab>'] = 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' }),
  },
  snippet = {
    expand = function(args)
      require('luasnip').lsp_expand(args.body)
    end,
  },
  sources = {
    { name = 'nvim_lsp' },
    { name = 'nvim_lua' },
    { name = 'luasnip' },
    {
      name = 'buffer',
      option = {
        get_bufnrs = function()
          local bufs = {}
          for _, win in ipairs(vim.api.nvim_list_wins()) do
            bufs[vim.api.nvim_win_get_buf(win)] = true
          end
          return vim.tbl_keys(bufs)
        end
      },
    },
    { name = 'calc' },
    { name = 'path' },
    { name = 'spell' },
  },
  formatting = {
      format = function(entry, vim_item)
        vim_item.menu = ({
          nvim_lsp = '[LSP]',
          buffer = '[Buffer]',
          calc = '[Calc]',
          path = '[Path]',
          spell = '[Spell]',
        })[entry.source.name]
        return vim_item
      end,
  },
}

Description

After executing "Confirm" inside a Go struct the field gets replaced:

recording.mp4

The same when calling a function:

recording.mp4

Steps to reproduce

See description.

Expected behavior

See description.

Actual behavior

See description.

Additional context

No response

@itaranto itaranto added the bug Something isn't working label Jan 5, 2022
@hrsh7th
Copy link
Owner

hrsh7th commented Jan 6, 2022

First of all, The gopls replaces the text by itself.

But I think this issue caused by your configuration.

    ['<CR>'] = cmp.mapping.confirm {
      behavior = cmp.ConfirmBehavior.Replace,
      select = true,
    },

↓↓↓

    ['<CR>'] = cmp.mapping.confirm {
      behavior = cmp.ConfirmBehavior.Insert,
      select = true,
    },

@hrsh7th hrsh7th closed this as completed Jan 6, 2022
@itaranto
Copy link
Author

itaranto commented Jan 6, 2022

behavior = cmp.ConfirmBehavior.Insert,

Setting to cmp.ConfirmBehavior.Insert doesn't change anything, I should have explained that in the description, sorry.

I have a simpler example for you:

  • Confirm completion before a funcion parameter
  • The next parameter name gets replaced
  • If I undo the changes with u the 2nd parameter gets restored
recording.mp4

Please @hrsh7th don't close this issue.

@hrsh7th
Copy link
Owner

hrsh7th commented Jan 6, 2022

Please @hrsh7th don't close this issue.

I will close when such an issue comes up. The problem explains that word replacement is the problem, but the sample specifies ConfirmBehavior.Replace.

Anyway, I'll check your screencast. (I guess it's LSP server's problem).

@hrsh7th
Copy link
Owner

hrsh7th commented Jan 6, 2022

The word replacement was specified by the gopls itself. If you think this is still issue, you should report it to gopls

Kapture.2022-01-06.at.23.36.05.mp4

.

@itaranto
Copy link
Author

itaranto commented Jan 6, 2022

I will close when such an issue comes up. The problem explains that word replacement is the problem, but the sample specifies ConfirmBehavior.Replace.

Yes, but I also tried with ConfirmBehavior.Insert with the same result.

The word replacement was specified by the gopls itself. If you think this is still issue, you should report it to gopls

Makes sense, so it's a gopls issue then...

@ajitid
Copy link

ajitid commented Feb 18, 2022

Yep it’s a gopls issue. I’d suggest you to track this golang/go#40871

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants