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

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

Open
flexagoon opened this issue Jun 9, 2024 · 19 comments
Open

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

flexagoon opened this issue Jun 9, 2024 · 19 comments

Comments

@flexagoon
Copy link

flexagoon commented Jun 9, 2024

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
Copy link
Contributor

@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
Copy link
Author

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

@flexagoon
Copy link
Author

neovim/neovim#27339

Here's the PR that added this functionality

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

@flexagoon
Copy link
Author

@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
Copy link
Contributor

@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:

function CompletionPreview.on_accept_suggestion(is_partial)
local accept_completion = CompletionPreview:accept_completion_text(is_partial)
if accept_completion ~= nil and accept_completion.is_active then
local completion_text = accept_completion.completion_text
local prior_delete = accept_completion.prior_delete
local cursor = vim.api.nvim_win_get_cursor(0)
local range = {
start = {
line = cursor[1] - 1,
character = math.max(cursor[2] - prior_delete, 0),
},
["end"] = {
line = cursor[1] - 1,
character = vim.fn.col("$"),
},
}
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Space><Left><Del>", true, false, true), "n", false)
vim.lsp.util.apply_text_edits(
{ { range = range, newText = completion_text } },
vim.api.nvim_get_current_buf(),
"utf-16"
)
local lines = u.line_count(completion_text)
local last_line = u.get_last_line(completion_text)
local new_cursor_pos = { cursor[1] + lines, cursor[2] + #last_line + 1 }
vim.api.nvim_win_set_cursor(0, new_cursor_pos)
else
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Tab>", true, false, true), "n", true)
end
end

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

@flexagoon
Copy link
Author

@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
Copy link

hboon commented Jun 10, 2024

@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
Copy link
Author

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

@AlejandroSuero
Copy link
Contributor

AlejandroSuero commented Jun 10, 2024

@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.

completion-with-supermaven.mp4

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
Copy link
Contributor

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

@hboon
Copy link

hboon commented Jun 10, 2024

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

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

@AlejandroSuero
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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:

  • nvim-cmp:
-- 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"
}
  • vim.lsp.completion
-- 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
Copy link
Contributor

I added a little change with the events mentioned in neovim/neovim#25714 (comment) and here is the result:

nvim-builtin-completion-changed-events.mp4

@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:

vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
group = M.augroup,
callback = function(event)
local file_name = event["file"]
local buffer = event["buf"]
if not file_name or not buffer then
return
end
binary:on_update(buffer, file_name, "text_changed")
end,
})

@hboon
Copy link

hboon commented Jun 10, 2024

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

@AlejandroSuero
Copy link
Contributor

@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
Copy link
Contributor

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

@MariaSolOs
Copy link

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

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

No branches or pull requests

4 participants