From 81b5ea969f26c520c4da9248d5e6d9b6a32b709c Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Thu, 9 Sep 2021 20:48:47 +0200 Subject: Change compe to cmp package --- .config/nvim/lua/core/autopairs.lua | 52 ++++++---- .config/nvim/lua/core/builtins/init.lua | 2 +- .config/nvim/lua/core/cmp.lua | 123 +++++++++++++++++++++++ .config/nvim/lua/core/compe.lua | 145 --------------------------- .config/nvim/lua/core/lualine/components.lua | 3 +- 5 files changed, 156 insertions(+), 169 deletions(-) create mode 100644 .config/nvim/lua/core/cmp.lua delete mode 100644 .config/nvim/lua/core/compe.lua (limited to '.config/nvim/lua/core') diff --git a/.config/nvim/lua/core/autopairs.lua b/.config/nvim/lua/core/autopairs.lua index f8bdfd1..8ea3094 100644 --- a/.config/nvim/lua/core/autopairs.lua +++ b/.config/nvim/lua/core/autopairs.lua @@ -20,36 +20,44 @@ function M.config() end M.setup = function() - -- skip it, if you use another global object - _G.MUtils = {} local autopairs = require "nvim-autopairs" local Rule = require "nvim-autopairs.rule" - - vim.g.completion_confirm_key = "" - MUtils.completion_confirm = function() - if vim.fn.pumvisible() ~= 0 then - if vim.fn.complete_info()["selected"] ~= -1 then - return vim.fn["compe#confirm"](autopairs.esc "") - else - return autopairs.esc "" - end - else - return autopairs.autopairs_cr() - end - end - - if package.loaded["compe"] then - require("nvim-autopairs.completion.compe").setup { - map_cr = options.builtin.autopairs.map_cr, - map_complete = options.builtin.autopairs.map_complete, - } - end + local cond = require "nvim-autopairs.conds" autopairs.setup { check_ts = options.builtin.autopairs.check_ts, ts_config = options.builtin.autopairs.ts_config, } + -- vim.g.completion_confirm_key = "" + + autopairs.add_rule(Rule("$$", "$$", "tex")) + autopairs.add_rules { + Rule("$", "$", { "tex", "latex" }) -- don't add a pair if the next character is % + :with_pair(cond.not_after_regex_check "%%") -- don't add a pair if the previous character is xxx + :with_pair(cond.not_before_regex_check("xxx", 3)) -- don't move right when repeat character + :with_move(cond.none()) -- don't delete if the next character is xx + :with_del(cond.not_after_regex_check "xx") -- disable add newline when press + :with_cr(cond.none()), + } + autopairs.add_rules { + Rule("$$", "$$", "tex"):with_pair(function(opts) + print(vim.inspect(opts)) + if opts.line == "aa $$" then + -- don't add pair on that line + return false + end + end), + } + + if package.loaded["cmp"] then + require("nvim-autopairs.completion.cmp").setup { + map_cr = true, -- map on insert mode + map_complete = true, -- it will auto insert `(` after select function or method item + auto_select = true, -- automatically select the first item + } + end + require("nvim-treesitter.configs").setup { autopairs = { enable = true } } local ts_conds = require "nvim-autopairs.ts-conds" diff --git a/.config/nvim/lua/core/builtins/init.lua b/.config/nvim/lua/core/builtins/init.lua index 32f96af..dc9b5ff 100644 --- a/.config/nvim/lua/core/builtins/init.lua +++ b/.config/nvim/lua/core/builtins/init.lua @@ -4,7 +4,7 @@ local builtins = { "keymappings", "core.which-key", "core.gitsigns", - "core.compe", + "core.cmp", "core.dashboard", "core.dap", "core.terminal", diff --git a/.config/nvim/lua/core/cmp.lua b/.config/nvim/lua/core/cmp.lua new file mode 100644 index 0000000..50f7058 --- /dev/null +++ b/.config/nvim/lua/core/cmp.lua @@ -0,0 +1,123 @@ +local M = {} + +local check_backspace = function() + local col = vim.fn.col "." - 1 + return col == 0 or vim.fn.getline("."):sub(col, col):match "%s" +end + +local function T(str) + return vim.api.nvim_replace_termcodes(str, true, true, true) +end + +local is_emmet_active = function() + local clients = vim.lsp.buf_get_clients() + + for _, client in pairs(clients) do + if client.name == "emmet_ls" then + return true + end + end + return false +end + +M.config = function() + local status_cmp_ok, cmp = pcall(require, "cmp") + if not status_cmp_ok then + return + end + local status_luasnip_ok, luasnip = pcall(require, "luasnip") + if not status_luasnip_ok then + return + end + options.builtin.cmp = { + formatting = { + format = function(entry, vim_item) + local icons = require("lsp.kind").icons + vim_item.kind = icons[vim_item.kind] + vim_item.menu = ({ + nvim_lsp = "(LSP)", + emoji = "(Emoji)", + path = "(Path)", + calc = "(Calc)", + cmp_tabnine = "(Tabnine)", + vsnip = "(Snippet)", + luasnip = "(Snippet)", + buffer = "(Buffer)", + })[entry.source.name] + vim_item.dup = ({ + buffer = 1, + path = 1, + nvim_lsp = 0, + })[entry.source.name] or 0 + return vim_item + end, + }, + snippet = { + expand = function(args) + require("luasnip").lsp_expand(args.body) + end, + }, + documentation = { + border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, + }, + sources = { + { name = "nvim_lsp" }, + { name = "path" }, + { name = "luasnip" }, + { name = "cmp_tabnine" }, + { name = "nvim_lua" }, + { name = "buffer" }, + { name = "calc" }, + { name = "emoji" }, + { name = "treesitter" }, + { name = "crates" }, + }, + mapping = { + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + -- TODO: potentially fix emmet nonsense + [""] = cmp.mapping(function() + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(T "", "n") + elseif luasnip.expand_or_jumpable() then + vim.fn.feedkeys(T "luasnip-expand-or-jump", "") + elseif check_backspace() then + vim.fn.feedkeys(T "", "n") + elseif is_emmet_active() then + return vim.fn["cmp#complete"]() + else + vim.fn.feedkeys(T "", "n") + end + end, { + "i", + "s", + }), + [""] = cmp.mapping(function(fallback) + if vim.fn.pumvisible() == 1 then + vim.fn.feedkeys(T "", "n") + elseif luasnip.jumpable(-1) then + vim.fn.feedkeys(T "luasnip-jump-prev", "") + else + fallback() + end + end, { + "i", + "s", + }), + + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.close(), + [""] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, + }, + } +end + +M.setup = function() + require("luasnip/loaders/from_vscode").lazy_load() + require("cmp").setup(options.builtin.cmp) +end + +return M diff --git a/.config/nvim/lua/core/compe.lua b/.config/nvim/lua/core/compe.lua deleted file mode 100644 index 19e49e0..0000000 --- a/.config/nvim/lua/core/compe.lua +++ /dev/null @@ -1,145 +0,0 @@ -local M = {} - -M.config = function() - options.builtin.compe = { - active = true, - on_config_done = nil, - autocomplete = true, - debug = false, - min_length = 1, - preselect = "enable", - throttle_time = 80, - source_timeout = 200, - incomplete_delay = 400, - max_abbr_width = 100, - max_kind_width = 100, - max_menu_width = 100, - documentation = { - border = "single", - winhighlight = "NormalFloat:CompeDocumentation,FloatBorder:CompeDocumentationBorder", - max_width = 120, - min_width = 60, - max_height = math.floor(vim.o.lines * 0.3), - min_height = 1, - }, - -- documentation = true, - - source = { - path = { kind = "  (Path)" }, - buffer = { kind = "  (Buffer)" }, - calc = { kind = "  (Calc)" }, - vsnip = { kind = "  (Snippet)" }, - nvim_lsp = { kind = "  (LSP)" }, - nvim_lua = false, - spell = { kind = "  (Spell)" }, - tags = false, - vim_dadbod_completion = false, - snippets_nvim = false, - ultisnips = false, - treesitter = false, - emoji = { kind = " ﲃ (Emoji)", filetypes = { "markdown", "text" } }, - -- for emoji press : (idk if that in compe tho) - }, - - keymap = { - values = { - insert_mode = { - -- [""] = { 'pumvisible() ? "" : ""', { silent = true, noremap = true, expr = true } }, - -- [""] = { 'pumvisible() ? "" : ""', { silent = true, noremap = true, expr = true } }, - [""] = { - "compe#complete()", - { silent = true, noremap = true, expr = true }, - }, - [""] = { - "compe#close('')", - { silent = true, noremap = true, expr = true }, - }, - [""] = { - "compe#scroll({ 'delta': +4 })", - { silent = true, noremap = true, expr = true }, - }, - [""] = { - "compe#scroll({ 'delta': -4 })", - { silent = true, noremap = true, expr = true }, - }, - }, - }, - opts = { - insert_mode = { noremap = true, silent = true, expr = true }, - }, - }, - } -end - -M.setup = function() - vim.g.vsnip_snippet_dir = options.vsnip_dir - - local compe = require "compe" - - compe.setup(options.builtin.compe) - - 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 - - local is_emmet_active = function() - local clients = vim.lsp.buf_get_clients() - - for _, client in pairs(clients) do - if client.name == "emmet_ls" then - return true - end - end - return false - 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#jumpable", { 1 }) == 1 then - return t "(vsnip-jump-next)" - elseif check_back_space() then - return t "" - elseif is_emmet_active() then - return vim.fn["compe#complete"]() - else - return t "" - 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 - - local keymap = require "keymappings" - keymap.load(options.builtin.compe.keymap.values, options.builtin.compe.keymap.opts) - - 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 }) - - if options.builtin.compe.on_config_done then - options.builtin.compe.on_config_done(compe) - end -end - -return M diff --git a/.config/nvim/lua/core/lualine/components.lua b/.config/nvim/lua/core/lualine/components.lua index adae368..6af34fa 100644 --- a/.config/nvim/lua/core/lualine/components.lua +++ b/.config/nvim/lua/core/lualine/components.lua @@ -85,7 +85,8 @@ return { }, treesitter = { function() - if next(vim.treesitter.highlighter.active) then + local b = vim.api.nvim_get_current_buf() + if next(vim.treesitter.highlighter.active[b]) then return "  " end return "" -- cgit v1.2.3-70-g09d2