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

fix(completion): stop completion when completion is exhausted #28558

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

famiu
Copy link
Member

@famiu famiu commented Apr 28, 2024

Problem: Currently, completion is not stopped when completion is exhausted (no matching completion items), which also means CompleteDone is not called either. Furthermore, CompleteDone is called when pressing a special character (Backspace, Space, Esc) instead.

Solution: Correctly stop completion and trigger CompleteDone when completion is exhausted.

@github-actions github-actions bot added the completion Nvim built-in (omni)completion label Apr 28, 2024
@famiu famiu marked this pull request as ready for review April 28, 2024 21:31
@zeertzjq
Copy link
Member

No tests?

Problem: Currently, completion is not stopped when completion is exhausted (no matching completion items), which also means `CompleteDone` is not called either. Furthermore, `CompleteDone` is called when pressing a special character (Backspace, Space, Esc) instead.

Solution: Correctly stop completion and trigger `CompleteDone` when completion is exhausted.
@famiu
Copy link
Member Author

famiu commented Apr 28, 2024

No tests?

Added a regression test

@famiu famiu requested a review from zeertzjq April 28, 2024 21:54
Copy link
Member

@zeertzjq zeertzjq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is correct. After this change, typing backspace after completion is exhausted can no longer resume completion. For example:

  1. Run :set completeopt+=noinsert
  2. Type i<C-X><C-V>
  3. Type a
  4. Type a
  5. Type <BS>

Without this PR the <BS> resumes completion. With this PR it doesn't.

@famiu
Copy link
Member Author

famiu commented Apr 28, 2024

I don't think this change is correct. After this change, typing backspace after completion is exhausted can no longer resume completion. For example:

  1. Run :set completeopt+=noinsert
  2. Type i<C-X><C-V>
  3. Type a
  4. Type a
  5. Type <BS>

Without this PR the <BS> resumes completion. With this PR it doesn't.

@mfussenegger thoughts?

@clason
Copy link
Member

clason commented Apr 29, 2024

I guess we now know why exhaustion does not trigger CompleteDone...

@mfussenegger
Copy link
Member

mfussenegger commented Apr 29, 2024

Why does <C-X><C-V> have this re-trigger on <BS> behavior, but complete() doesn't?

local fp = io.open("/tmp/nvim.log", "a+")

vim.o.completeopt = "menuone,noinsert"
vim.api.nvim_create_autocmd("CompleteDone", {
  callback = function()
    fp:write("called\n")
    fp:flush()
  end
})

vim.keymap.set("i", "<C-s>", function()
  vim.fn.complete(vim.fn.col("."), {"abcd", "abdc", "acbd"})
end)

With a<C-s>e<BS> this doesn't trigger again.
Could we make this change exclusive to complete() to not break <C-X><C-V>?

@famiu
Copy link
Member Author

famiu commented Apr 29, 2024

Why does <C-X><C-V> have this re-trigger on <BS> behavior, but complete() doesn't?

local fp = io.open("/tmp/nvim.log", "a+")

vim.o.completeopt = "menuone,noinsert"
vim.api.nvim_create_autocmd("CompleteDone", {
  callback = function()
    fp:write("called\n")
    fp:flush()
  end
})

vim.keymap.set("i", "<C-s>", function()
  vim.fn.complete(vim.fn.col("."), {"abcd", "abdc", "acbd"})
end)

With a<C-s>e<BS> this doesn't trigger again. Could we make this change exclusive to complete() to not break <C-X><C-V>?

I think it might be better to implement the <BS> retrigger logic instead, but let's leave the PR open for now if we decide that that's not worth doing

@famiu
Copy link
Member Author

famiu commented Apr 30, 2024

Why does <C-X><C-V> have this re-trigger on <BS> behavior, but complete() doesn't?

local fp = io.open("/tmp/nvim.log", "a+")

vim.o.completeopt = "menuone,noinsert"
vim.api.nvim_create_autocmd("CompleteDone", {
  callback = function()
    fp:write("called\n")
    fp:flush()
  end
})

vim.keymap.set("i", "<C-s>", function()
  vim.fn.complete(vim.fn.col("."), {"abcd", "abdc", "acbd"})
end)

With a<C-s>e<BS> this doesn't trigger again. Could we make this change exclusive to complete() to not break <C-X><C-V>?

@mfussenegger
Seems like things like completefunc and omnifunc can return a refresh key alongside the list of matches, which is how they can be refreshed (see |complete-functions|). There seems to be no equivalent to it for complete().

@justinmk Would it make sense to add something like nvim_complete() that takes in a Lua function which returns a list of matches, and allows additional options (like refresh, for example)?

@justinmk
Copy link
Member

justinmk commented Apr 30, 2024

Would it make sense to add something like nvim_complete() that takes in a Lua function which returns a list of matches, and allows additional options (like refresh, for example)?

why do we need a new function, could it be a new arg on the existing complete() ? or perhaps nvim_complete_set() should be revisited, can it be extended or generalized to also cover this purpose?

@famiu
Copy link
Member Author

famiu commented Apr 30, 2024

Would it make sense to add something like nvim_complete() that takes in a Lua function which returns a list of matches, and allows additional options (like refresh, for example)?

why do we need a new function, could it be a new arg on the existing complete() ? or perhaps nvim_complete_set() should be revisited, can it be extended or generalized to also cover this purpose?

A new arg on complete might work, yes, but the reason I suggested a new API is that a completion provider might want its completion items list to be regenerated when refreshing completion, so taking in a callback instead of a list of items would be useful in that case complete() will always use completion items from the initial list, which means updating that list isn't possible. We could refresh completion based on the initial list, but I think something that can take a callback would be much more robust and useful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completion Nvim built-in (omni)completion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants