quangnguyen30192 / cmp-nvim-ultisnips

nvim-cmp source for ultisnips
Apache License 2.0
145 stars 19 forks source link

Mapping for TAB people #4

Closed JoseConseco closed 3 years ago

JoseConseco commented 3 years ago

Tab will cycle forward through autocomplete item (unless we expanded snippet - then it will jump to snippet tag stops) ctrl+space - will confirm placing selected item or expand selected snippet

    mapping = {
        ['<C-p>'] = cmp.mapping.select_prev_item(),
        ['<C-n>'] = cmp.mapping.select_next_item(),
        ['<S-Tab>'] = cmp.mapping(function(fallback)
            if vim.fn["UltiSnips#CanJumpBackwards"]() == 1 then
                return vim.fn.feedkeys(t("<C-R>=UltiSnips#JumpBackwards()<CR>"))
            elseif vim.fn.pumvisible() == 1 then
                vim.fn.feedkeys(t("<C-p>"), "n")
            elseif check_back_space() then
                vim.fn.feedkeys(t("<S-tab>"), "n")
            else
                fallback()
            end
        end, { "i", "s", }),
        ['<Tab>'] = cmp.mapping(function(fallback)
            if vim.fn["UltiSnips#CanJumpForwards"]() == 1 then
                return vim.fn.feedkeys(t("<C-R>=UltiSnips#JumpForwards()<CR>"))
            elseif vim.fn.pumvisible() == 1 then
                vim.fn.feedkeys(t("<C-n>"), "n")
            elseif check_back_space() then
                vim.fn.feedkeys(t("<tab>"), "n")
            else
                fallback()
            end
        end, { "i", "s", }),
        ['<C-d>'] = cmp.mapping.scroll_docs(-4),
        ['<C-f>'] = cmp.mapping.scroll_docs(4),
        ['<C-e>'] = cmp.mapping.close(),
        ['<CR>'] = cmp.mapping.confirm({
            behavior = cmp.ConfirmBehavior.Insert,
            select = true,
        }),
        -- ['<C-Space>'] = cmp.mapping.complete(),
        ['<C-Space>'] = cmp.mapping(function(fallback)
            if vim.fn.pumvisible() == 1 then
                if vim.fn["UltiSnips#CanExpandSnippet"]() == 1 then
                    return vim.fn.feedkeys(t("<C-R>=UltiSnips#ExpandSnippet()<CR>"))
                end

                vim.fn.feedkeys(t("<C-n>"), "n")
            elseif check_back_space() then
                vim.fn.feedkeys(t("<cr>"), "n")
            else
                fallback()
            end
        end, { "i", "s", }),
    },   

I just thought I will put it in here for preferring 'tab' btw. dont ask what check_back_space does. I just copied it form other examples.

quangnguyen30192 commented 3 years ago

Hey, thanks. That's is a nice suggestion. I updated the README also improve a bit from your suggestion.

NilsIrl commented 3 years ago

Why is feedkeys used instead of for example just vim.fn["UltiSnips#JumpForwards"]()?

quangnguyen30192 commented 3 years ago

because UltiSnips#JumpForwards() is a function, not an expression key mapping. Just try you will get an error.