supermaven-inc / supermaven-nvim

The official Neovim plugin for Supermaven
https://supermaven.com/
MIT License
279 stars 16 forks source link

Doesn't work with built-in nvim completion #59

Open flexagoon opened 3 weeks ago

flexagoon commented 3 weeks ago

When you set up built-in nvim lsp completion like this:

vim.lsp.completion.enable(true, 1, 0, { autotrigger = true })

In your LspAttach, and then try to type something, the completion conflicts with Supermaven

image

Autocompletion inserts the text preview into the buffer, but supermaven writes its ghost text on top of that, so the completion preview isn't visible.

Here's what it looks like with supermaven disabled:

image

AlejandroSuero commented 3 weeks ago

@flexagoon What is the behaviour of vim.lsp.completion.enable?

Using nvim v0.10.0 I only seem to get help for vim.lsp.buf.completion(context) and using nvim-lspconfig and nvim-cmp I don't seem to get a conflict when completing from LSP.

flexagoon commented 3 weeks ago

@AlejandroSuero it's not in 0.10, it was added only recently. I'm using 0.11 nightly.

flexagoon commented 3 weeks ago

https://github.com/neovim/neovim/pull/27339

Here's the PR that added this functionality

@MariaSolOs do you know what this may be related to?

flexagoon commented 3 weeks ago

@AlejandroSuero I think the popup menu that's used here is the regular completion popup, you can trigger the same thing by using omnicompletion (<C-x><C-o>), the issue will still be there

AlejandroSuero commented 3 weeks ago

@flexagoon I am not sure if the PR maybe conflicting with the default keymaps.accept_suggestion = "<Tab>" that supermaven sets. At a first glance it doesn't seem to do anything more than that which can could be the cause of conflict. Maybe @MariaSolOs has more insight on that.

If you haven't other keymaps using <C-y>, test this out to see if it resolves it:

require("supermaven-nvim").setup({
  accept_suggestion = "<C-y>",
})

Right now supermaven uses this when accepting a completion:

https://github.com/supermaven-inc/supermaven-nvim/blob/ef3bd1a6b6f722857f2f88d929dd4ac875655611/lua/supermaven-nvim/completion_preview.lua#L140-L171

Maybe vim.lsp.util.apply_text_edits is another cause of the conflict.

flexagoon commented 3 weeks ago

@AlejandroSuero this isn't about key bindings, the conflict happens before anything is pressed, when the popup window opens. The completion preview is overriden by supermaven completion

hboon commented 3 weeks ago

@flexagoon I compared the 2 screenshots and the only difference is, in the 1st one, there's ghost text inserted by Supermaven. Looking at 2nd screenshot, it doesn't seem to have hidden anything?

flexagoon commented 3 weeks ago

@hboon supermaven ghost text hides the Handle() text inserted by completion

AlejandroSuero commented 3 weeks ago

@hboon @flexagoon

Screenshot 2024-06-10 at 09 09 27

When using it with supermaven and vim.lsp.completion it doesn't detect the text provided by vim.lsp.completion as "text" I would say, seems like it is not "inserting" it.

When using supermaven and nvim-cmp with cmp.SelectBehaviour.Insert it inserts the text and doesn't fail.

https://github.com/supermaven-inc/supermaven-nvim/assets/71392160/a586c6e9-24e8-44ab-a5c1-d751a2a9bd70

[!NOTE] When you see colored text in completion is nvim-cmp at work, otherwise is vim.lsp.completion

From what I see and testing it manually, it seems like when selecting an option in vim.lsp.completion it doesn't really "insert" the text as nvim-cmp does, therefore supermaven it will keep showing ghost text from the last inserted character. With nvim-cmp as it seems to insert it fully it detects it and keeps completing after nvim-cmp inserted text.

AlejandroSuero commented 3 weeks ago

If accepting the text provided by vim.lsp.completion with <C-y>, supermaven works as expected.

hboon commented 3 weeks ago

@hboon supermaven ghost text hides the Handle() text inserted by completion

@flexagoon ah, I see what you mean now. Thanks.

AlejandroSuero commented 3 weeks ago

I think nvim-cmp use vim.lsp.util.apply_text_edits when inserting the text I don't know if vim.lsp.completion does it in any form like this or how exactly it does it.

AlejandroSuero commented 3 weeks ago

In nvim v0.10.0 it happens as well as it does in v0.11, in my version and config I haven't configured vim.lsp.completion in any way so just triggering like @flexagoon said, with <C-x><C-o> and cycling through them with <C-n> or <C-p>, if not accepting with <C-y> it will overlay the suggestion on top of vim.lsp.completion text.

AlejandroSuero commented 3 weeks ago

One more update on this nvim-cmp seems to be triggering InsertCharPre event but vim.lsp.completion does not, this may be the cause for which it doesn't detect the changes.

LOGS:

-- using nvim-cmp
event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}
event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}
event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}
event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}
event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}
event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}
event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}
event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}
-- using vim.lsp.completion

event {
  buf = 11,
  event = "InsertCharPre",
  file = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua",
  group = 18,
  id = 43,
  match = "/Users/aome/dev/nvim_plugins/supermaven-nvim/lua/supermaven-nvim/api.lua"
}

[!NOTE] The first registered event is me writting r to start completion for require.

AlejandroSuero commented 3 weeks ago

I added a little change with the events mentioned in https://github.com/neovim/neovim/issues/25714#issuecomment-1959108102 and here is the result:

https://github.com/supermaven-inc/supermaven-nvim/assets/71392160/d9add2b4-75ed-4784-bae7-ce9c7ad4916c

@hboon @flexagoon if you have some thoughts on how this looks to start a PR about it, would be helpful.

I added the events on this:

https://github.com/supermaven-inc/supermaven-nvim/blob/ef3bd1a6b6f722857f2f88d929dd4ac875655611/lua/supermaven-nvim/document_listener.lua#L12-L22

hboon commented 3 weeks ago

I figure it has to be tested on 0.11.x to be sure though

AlejandroSuero commented 3 weeks ago

@hboon it behaves the same as shown in the previous comment both in stable (v0.10.0) and nightly (v0.11.x)

vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI", "TextChangedP", "CompleteChanged", "CompleteDone" } ...)

I tested it without each one separately and with all together, with the same result.

Seems like nvim-cmp triggers TextChanged{I,P} and CompleteDone when accepting, and vim.lsp.completion uses TextChangedP and Complete{Changed,Done}. For this @MariaSolOS could have more insight I guess.

AlejandroSuero commented 3 weeks ago

And something weird happens if you trigger vim.lsp.completion with <C-x><C-o> and start writing, the suggestion disappears.

MariaSolOs commented 3 weeks ago

Sorry but I’m honestly unsure as to what may be the source of the conflict here.