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 empty-line completion #105

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

litoj
Copy link

@litoj litoj commented Apr 18, 2024

There are more fixes to be done, but I will leave those just in my fork to not clutter the diff so much.

This is also not the prettiest fix, but it works. Though I do intend (when I have the time) to make it cleaner and simplify the prefix issue.

After digging into these empty line issues a little bit more, I managed to fully fix it. Here are the conclusions I've come to when figuring out what's most likely going on:

  1. no suggestions when on a line without any indentation
    • no idea what indentation had to do with it (maybe less processing time because there was no current line context)
    • callback with results was called before the completion request method finished itself (calling back with an empty table)
    • -> completion was overriden by an empty table
    • ie. race condition
  2. no suggestions on indented lines after a couple re-completions
    • still not sure why any came up but then stopped showing up
    • get_keyword_pattern() returned . -> it requied any character
    • cmp seems to consider empty lines as fitting the pattern, but not indented ones
    • returning empty string didn't help, but cmp_tabnine didn't have this issue and it also turned out that it didn't have this method either
  3. still no suggestions on indented lines
    • cmp seems to prohibit changing the text before the cursor, where line indentation is
    • simmilar issue as (2.) - indented parts of lines are considered non-existent by cmp
    • after removing the indentation from all suggestions and ofsetting them by the indentation amount, all works fine
  4. comparators do nothing
    • comparators receive a different structure, where is a lot more about the completion, than just the text itself
    • the copilot atribute was in a deeper nested table, so the comparator was always accessing nil

@litoj litoj mentioned this pull request Apr 18, 2024
@devshoe
Copy link

devshoe commented Apr 18, 2024

I am using your master branch (couldn't open an issue there), and my expectation was that I would be able to see completions on a new line. I may be doing something wrong. Hope you can help

Here is my config for cmp.

`M.cmp = function()
local cmp = require("cmp")
local luasnip = require("luasnip")
local lspkind = require("lspkind")

    luasnip.config.setup(M.luasnip)

    cmp.setup({
            experimental = {
                    ghost_text = true,
            },
            sorting = {
                    priority_weight = 10,
                    comparators = {

                            require("cmp_copilot.comparators").prioritize,
                            -- Below is the default comparitor list and order for nvim-cmp
                            cmp.config.compare.offset,
                            -- cmp.config.compare.scopes, --this is commented in nvim-cmp too
                            cmp.config.compare.exact,
                            cmp.config.compare.score,
                            cmp.config.compare.recently_used,
                            cmp.config.compare.locality,
                            cmp.config.compare.kind,
                            cmp.config.compare.sort_text,
                            cmp.config.compare.length,
                            cmp.config.compare.order,
                    },
            },
            ---@diagnostic disable-next-line: missing-fields
            formatting = {
                    format = lspkind.cmp_format({
                            mode = "symbol",
                            max_width = 50,
                            symbol_map = { Copilot = "" },
                    }),
            },
            snippet = {
                    expand = function(args)
                            luasnip.lsp_expand(args.body)
                    end,
            },

            completion = {
                    keyword_length = 0,

                    --keyword_pattern = "",
                    -- completeopt = "menu,menuone,noinsert,noselect",
            },
            mapping = require("mappings").cmp(),
            sources = {
                    { name = "copilot", group_index = 2 },
                    { name = "nvim_lsp", group_index = 2 },
                    { name = "luasnip", group_index = 2 },
                    { name = "path", group_index = 2 },
                    { name = "vim-dadbod-completion", group_index = 2 },
            },
    })`

Plugins:

return {
        {
                "JosefLitos/cmp-copilot",
                opts = {},
                config = function()
                        require("cmp_copilot").setup()
                end,
        },

        {
                "zbirenbaum/copilot.lua",
                cmd = "Copilot",
                config = function()
                        require("copilot").setup({
                                suggestion = { enabled = false },
                                panel = { enabled = false },
                        })
                end,
        }}

@litoj
Copy link
Author

litoj commented Apr 18, 2024

I forgot to allow issues at the fork - now fixed, thanks.

Can you not get completions from copilot at all, or just not automatically? I got used to having to manually ask/complete() for them, but I agree it should give suggestions on its own. I don't think I can make it work with no text written though - it triggers only when any of trigger chars are pressed.

Unfortunately keyword length seems to only limit how many characters must be pressed before the completion shows in the menu, not when the completion triggers.

Only trigfer char usable for this would be probably the space key, but I will try if messing with isIncomplete helps.

@devshoe
Copy link

devshoe commented Apr 18, 2024

It is working, just not with tab or space. It appears pressing . is what triggers the autocompletions

@litoj
Copy link
Author

litoj commented Apr 18, 2024

I've set isIncomplete to always be true, that way cmp keeps sending requests for results. Copilot seems to detect when the text changes and only then refresh the results, so it doesn't seem to drain the cpu in any way. Without that the completion wouldn't refresh when the user types any text.

The main thing you were probably looking for was adding Space and Tab to the trigger chars - gets triggered immediately when there is any indentation on the new line.

I'm afraid without indentation this is impossible to do (outside seeing an old completion list because of another cmp bug).

@y9c
Copy link

y9c commented Apr 25, 2024

Hi @JosefLitos, thank you for the fix. I have some trouble in using this fork version. After hit the enter key to input the completion text, there is always the markdown code chunk marks around the code. One example:

When enter
image

The output become:

image

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

Successfully merging this pull request may close these issues.

3 participants