From 6a0e6be5c7d4e869d238e347590b690080f7a722 Mon Sep 17 00:00:00 2001 From: ShatteredMINT Date: Mon, 30 Sep 2024 05:17:31 +0200 Subject: [PATCH] completion setup --- init.lua | 2 ++ lua/config/init.lua | 1 + lua/config/lazy.lua | 22 ++++++++++++ lua/config/nvim-cmp.lua | 79 +++++++++++++++++++++++++++++++++++++++++ lua/config/theme.lua | 6 ++++ lua/plugins/init.lua | 1 + lua/plugins/lazy.lua | 13 +++++++ plugin/keymap.lua | 1 + 8 files changed, 125 insertions(+) create mode 100644 lua/config/init.lua create mode 100644 lua/config/lazy.lua create mode 100644 lua/config/nvim-cmp.lua create mode 100644 lua/config/theme.lua create mode 100644 lua/plugins/init.lua create mode 100644 lua/plugins/lazy.lua diff --git a/init.lua b/init.lua index e69de29..ac18b62 100644 --- a/init.lua +++ b/init.lua @@ -0,0 +1,2 @@ +require("plugins") +require("config") diff --git a/lua/config/init.lua b/lua/config/init.lua new file mode 100644 index 0000000..55b8979 --- /dev/null +++ b/lua/config/init.lua @@ -0,0 +1 @@ +require("config.lazy") diff --git a/lua/config/lazy.lua b/lua/config/lazy.lua new file mode 100644 index 0000000..940433b --- /dev/null +++ b/lua/config/lazy.lua @@ -0,0 +1,22 @@ +require("lazy").setup({ + "navarasu/onedark.nvim", + + -- Auto-completion engine + { + "hrsh7th/nvim-cmp", + dependencies = { + "hrsh7th/cmp-nvim-lsp", -- lsp auto-completion + "hrsh7th/cmp-buffer", -- buffer auto-completion + "hrsh7th/cmp-path", -- path auto-completion + "hrsh7th/cmp-cmdline", -- cmdline auto-completion + }, + config = function() + require("config.nvim-cmp") + end, + }, + -- Code snippet engine + { + "L3MON4D3/LuaSnip", + version = "v2.*", + }, +}) diff --git a/lua/config/nvim-cmp.lua b/lua/config/nvim-cmp.lua new file mode 100644 index 0000000..6dfec5a --- /dev/null +++ b/lua/config/nvim-cmp.lua @@ -0,0 +1,79 @@ +local has_words_before = function() + unpack = unpack or table.unpack + local line, col = unpack(vim.api.nvim_win_get_cursor(0)) + return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil +end + +local luasnip = require("luasnip") +local cmp = require("cmp") + +cmp.setup({ + snippet = { + -- REQUIRED - you must specify a snippet engine + expand = function(args) + require('luasnip').lsp_expand(args.body) -- For `luasnip` users. + end, + }, + mapping = cmp.mapping.preset.insert({ + -- Use to scroll the docs + ['K'] = cmp.mapping.scroll_docs( -4), + ['J'] = cmp.mapping.scroll_docs(4), + -- Use to switch in items + ['k'] = cmp.mapping.select_prev_item(), + ['j'] = cmp.mapping.select_next_item(), + -- Use (Enter) to confirm selection + -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + [''] = cmp.mapping.confirm({ select = true }), + + -- A super tab + -- sourc: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip + [""] = cmp.mapping(function(fallback) + -- Hint: if the completion menu is visible select next one + if cmp.visible() then + cmp.select_next_item() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), -- i - insert mode; s - select mode + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable( -1) then + luasnip.jump( -1) + else + fallback() + end + end, { "i", "s" }), + }), + + -- Let's configure the item's appearance + -- source: https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance + formatting = { + -- Set order from left to right + -- kind: single letter indicating the type of completion + -- abbr: abbreviation of "word"; when not empty it is used in the menu instead of "word" + -- menu: extra text for the popup menu, displayed after "word" or "abbr" + fields = { 'abbr', 'menu' }, + + -- customize the appearance of the completion menu + format = function(entry, vim_item) + vim_item.menu = ({ + nvim_lsp = '[Lsp]', + luasnip = '[Luasnip]', + buffer = '[File]', + path = '[Path]', + })[entry.source.name] + return vim_item + end, + }, + + -- Set source precedence + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, -- For nvim-lsp + { name = 'luasnip' }, -- For luasnip user + { name = 'buffer' }, -- For buffer word completion + { name = 'path' }, -- For path completion + }) +}) diff --git a/lua/config/theme.lua b/lua/config/theme.lua new file mode 100644 index 0000000..f4b89ac --- /dev/null +++ b/lua/config/theme.lua @@ -0,0 +1,6 @@ +local onedark = require("onedark") + +onedark.setup{ + style = "darker" +} +onedark.load() diff --git a/lua/plugins/init.lua b/lua/plugins/init.lua new file mode 100644 index 0000000..b5a7f84 --- /dev/null +++ b/lua/plugins/init.lua @@ -0,0 +1 @@ +require("lua.plugins.lazy") diff --git a/lua/plugins/lazy.lua b/lua/plugins/lazy.lua new file mode 100644 index 0000000..14d91f8 --- /dev/null +++ b/lua/plugins/lazy.lua @@ -0,0 +1,13 @@ +local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" +if not (vim.uv or vim.loop).fs_stat(lazypath) then + vim.fn.system({ + "git", + "clone", + "--filter=blob:none", + "https://github.com/folke/lazy.nvim.git", + "--branch=stable", -- latest stable release + lazypath, + }) +end +vim.opt.rtp:prepend(lazypath) + diff --git a/plugin/keymap.lua b/plugin/keymap.lua index 6f8b237..631707d 100644 --- a/plugin/keymap.lua +++ b/plugin/keymap.lua @@ -29,6 +29,7 @@ map('n', 'wc', ':close', opts) -- tab management map('n', '', ':tabnext', opts) map('n', 'x', ':tabclose', opts) +map('n', 'n', ':$tabnew', opts) map('n', 's', ':$tab split', opts)