From 102b46ca4a99522ffd8066f1fac9ba4fe8759710 Mon Sep 17 00:00:00 2001 From: christianchiarulli Date: Tue, 27 Apr 2021 01:17:29 -0400 Subject: [PATCH] improve completion --- lua/lv-autopairs/init.lua | 158 ++++++++++++++++++++++---------------- lua/lv-compe/init.lua | 96 +++++++++++++++++------ lua/plugins.lua | 1 + 3 files changed, 163 insertions(+), 92 deletions(-) diff --git a/lua/lv-autopairs/init.lua b/lua/lv-autopairs/init.lua index 5d0adf85..c3f6a6e0 100644 --- a/lua/lv-autopairs/init.lua +++ b/lua/lv-autopairs/init.lua @@ -1,77 +1,99 @@ -require('nvim-autopairs').setup() +-- require('nvim-autopairs').setup() +-- local npairs = require('nvim-autopairs') + +-- local function imap(lhs, rhs, opts) +-- local options = {noremap = false} +-- if opts then options = vim.tbl_extend('force', options, opts) end +-- vim.api.nvim_set_keymap('i', lhs, rhs, options) +-- end + +-- _G.MUtils = {} + +-- -- TEST +-- vim.g.completion_confirm_key = "" +-- MUtils.completion_confirm = function() +-- if vim.fn.pumvisible() ~= 0 then +-- if vim.fn.complete_info()["selected"] ~= -1 then +-- vim.fn["compe#confirm"]() +-- -- return npairs.esc("") +-- return npairs.esc("") +-- else +-- vim.defer_fn(function() +-- vim.fn["compe#confirm"]("") +-- end, 20) +-- return npairs.esc("") +-- end +-- else +-- return npairs.check_break_line_char() +-- end +-- end +-- -- TEST + +-- MUtils.completion_confirm = function() +-- if vim.fn.pumvisible() ~= 0 then +-- if vim.fn.complete_info()["selected"] ~= -1 then +-- vim.fn["compe#confirm"]() +-- return npairs.esc("") +-- else +-- vim.api.nvim_select_popupmenu_item(0, false, false, {}) +-- vim.fn["compe#confirm"]() +-- return npairs.esc("") +-- end +-- else +-- return npairs.check_break_line_char() +-- end +-- end + +-- MUtils.tab = function() +-- if vim.fn.pumvisible() ~= 0 then +-- return npairs.esc("") +-- else +-- if vim.fn["vsnip#available"](1) ~= 0 then +-- vim.fn.feedkeys(string.format('%c%c%c(vsnip-expand-or-jump)', 0x80, 253, 83)) +-- return npairs.esc("") +-- else +-- return npairs.esc("") +-- end +-- end +-- end + +-- MUtils.s_tab = function() +-- if vim.fn.pumvisible() ~= 0 then +-- return npairs.esc("") +-- else +-- if vim.fn["vsnip#jumpable"](-1) ~= 0 then +-- vim.fn.feedkeys(string.format('%c%c%c(vsnip-jump-prev)', 0x80, 253, 83)) +-- return npairs.esc("") +-- else +-- return npairs.esc("") +-- end +-- end +-- end + +-- -- Autocompletion and snippets +-- vim.api.nvim_set_keymap('i', '', 'v:lua.MUtils.completion_confirm()', {expr = true, noremap = true}) +-- -- imap("", "v:lua.MUtils.completion_confirm()", {expr = true, noremap = true}) +-- imap("", "v:lua.MUtils.tab()", {expr = true, noremap = true}) +-- imap("", "v:lua.MUtils.s_tab()", {expr = true, noremap = true}) + +local remap = vim.api.nvim_set_keymap local npairs = require('nvim-autopairs') -local function imap(lhs, rhs, opts) - local options = {noremap = false} - if opts then options = vim.tbl_extend('force', options, opts) end - vim.api.nvim_set_keymap('i', lhs, rhs, options) -end +-- skip it, if you use another global object +_G.MUtils= {} -_G.MUtils = {} - --- TEST vim.g.completion_confirm_key = "" -MUtils.completion_confirm = function() - if vim.fn.pumvisible() ~= 0 then - if vim.fn.complete_info()["selected"] ~= -1 then - vim.fn["compe#confirm"]() - -- return npairs.esc("") - return npairs.esc("") - else - vim.defer_fn(function() - vim.fn["compe#confirm"]("") - end, 20) - return npairs.esc("") - end +MUtils.completion_confirm=function() + if vim.fn.pumvisible() ~= 0 then + if vim.fn.complete_info()["selected"] ~= -1 then + return vim.fn["compe#confirm"](npairs.esc("")) else - return npairs.check_break_line_char() - end -end --- TEST - -MUtils.completion_confirm = function() - if vim.fn.pumvisible() ~= 0 then - if vim.fn.complete_info()["selected"] ~= -1 then - vim.fn["compe#confirm"]() - return npairs.esc("") - else - vim.api.nvim_select_popupmenu_item(0, false, false, {}) - vim.fn["compe#confirm"]() - return npairs.esc("") - end - else - return npairs.check_break_line_char() + return npairs.esc("") end + else + return npairs.autopairs_cr() + end end -MUtils.tab = function() - if vim.fn.pumvisible() ~= 0 then - return npairs.esc("") - else - if vim.fn["vsnip#available"](1) ~= 0 then - vim.fn.feedkeys(string.format('%c%c%c(vsnip-expand-or-jump)', 0x80, 253, 83)) - return npairs.esc("") - else - return npairs.esc("") - end - end -end -MUtils.s_tab = function() - if vim.fn.pumvisible() ~= 0 then - return npairs.esc("") - else - if vim.fn["vsnip#jumpable"](-1) ~= 0 then - vim.fn.feedkeys(string.format('%c%c%c(vsnip-jump-prev)', 0x80, 253, 83)) - return npairs.esc("") - else - return npairs.esc("") - end - end -end - --- Autocompletion and snippets -vim.api.nvim_set_keymap('i', '', 'v:lua.MUtils.completion_confirm()', {expr = true, noremap = true}) --- imap("", "v:lua.MUtils.completion_confirm()", {expr = true, noremap = true}) -imap("", "v:lua.MUtils.tab()", {expr = true, noremap = true}) -imap("", "v:lua.MUtils.s_tab()", {expr = true, noremap = true}) +remap('i' , '','v:lua.MUtils.completion_confirm()', {expr = true , noremap = true}) diff --git a/lua/lv-compe/init.lua b/lua/lv-compe/init.lua index 61270960..d3865058 100644 --- a/lua/lv-compe/init.lua +++ b/lua/lv-compe/init.lua @@ -15,20 +15,20 @@ require'compe'.setup { documentation = true, source = { - path = {kind = "  "}, - buffer = {kind = "  "}, - calc = {kind = "  "}, - vsnip = {kind = "  "}, - nvim_lsp = {kind = "  "}, + path = {kind = "  (Path)"}, + buffer = {kind = "  (Buffer)"}, + calc = {kind = "  (Calc)"}, + vsnip = {kind = "  (Snippet)"}, + nvim_lsp = {kind = "  (LSP)"}, -- nvim_lua = {kind = "  "}, nvim_lua = false, - spell = {kind = "  "}, + spell = {kind = "  (Spell)"}, tags = false, vim_dadbod_completion = true, -- snippets_nvim = {kind = "  "}, -- ultisnips = {kind = "  "}, -- treesitter = {kind = "  "}, - emoji = {kind = " ﲃ ", filetypes={"markdown", "text"}} + emoji = {kind = " ﲃ (Emoji)", filetypes={"markdown", "text"}} -- for emoji press : (idk if that in compe tho) } } @@ -59,8 +59,56 @@ require'compe'.setup { --  --  +-- local t = function(str) +-- return vim.api.nvim_replace_termcodes(str, true, true, true) +-- end + +-- local check_back_space = function() +-- local col = vim.fn.col('.') - 1 +-- if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then +-- return true +-- else +-- return false +-- end +-- end + +-- -- Use (s-)tab to: +-- --- move to prev/next item in completion menuone +-- --- jump to prev/next snippet's placeholder +-- _G.tab_complete = function() +-- if vim.fn.pumvisible() == 1 then +-- return t "" +-- elseif vim.fn.call("vsnip#available", {1}) == 1 then +-- return t "(vsnip-expand-or-jump)" +-- elseif check_back_space() then +-- return t "" +-- else +-- return vim.fn['compe#complete']() +-- end +-- end +-- _G.s_tab_complete = function() +-- if vim.fn.pumvisible() == 1 then +-- return t "" +-- elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then +-- return t "(vsnip-jump-prev)" +-- else +-- return t "" +-- end +-- end + +-- vim.api.nvim_set_keymap("i", "", "v:lua.tab_complete()", {expr = true}) +-- vim.api.nvim_set_keymap("s", "", "v:lua.tab_complete()", {expr = true}) +-- vim.api.nvim_set_keymap("i", "", "v:lua.s_tab_complete()", {expr = true}) +-- vim.api.nvim_set_keymap("s", "", "v:lua.s_tab_complete()", {expr = true}) + + + + + + + local t = function(str) - return vim.api.nvim_replace_termcodes(str, true, true, true) + return vim.api.nvim_replace_termcodes(str, true, true, true) end local check_back_space = function() @@ -76,24 +124,24 @@ end --- move to prev/next item in completion menuone --- jump to prev/next snippet's placeholder _G.tab_complete = function() - if vim.fn.pumvisible() == 1 then - return t "" - elseif vim.fn.call("vsnip#available", {1}) == 1 then - return t "(vsnip-expand-or-jump)" - elseif check_back_space() then - return t "" - else - return vim.fn['compe#complete']() - end + if vim.fn.pumvisible() == 1 then + return t "" + elseif vim.fn.call("vsnip#available", {1}) == 1 then + return t "(vsnip-expand-or-jump)" + elseif check_back_space() then + return t "" + else + return vim.fn['compe#complete']() + end end _G.s_tab_complete = function() - if vim.fn.pumvisible() == 1 then - return t "" - elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then - return t "(vsnip-jump-prev)" - else - return t "" - end + if vim.fn.pumvisible() == 1 then + return t "" + elseif vim.fn.call("vsnip#jumpable", {-1}) == 1 then + return t "(vsnip-jump-prev)" + else + return t "" + end end vim.api.nvim_set_keymap("i", "", "v:lua.tab_complete()", {expr = true}) diff --git a/lua/plugins.lua b/lua/plugins.lua index da33339c..967a9d1d 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -83,6 +83,7 @@ return require("packer").startup( require_plugin("nvim-lspconfig") require_plugin("lspsaga.nvim") require_plugin("nvim-lspinstall") + require_plugin("friendly-snippets") require_plugin("popup.nvim") require_plugin("plenary.nvim") require_plugin("telescope.nvim")