From 43456e196f5c6b6f5e4ca9ed96f11aa201c41334 Mon Sep 17 00:00:00 2001 From: juan Date: Thu, 9 Mar 2023 16:26:45 +0800 Subject: [PATCH] Add proper support for snippets, and tab width --- lua/core.lua | 5 +- lua/plugins.lua | 27 ++++---- lua/plugins/cmp.lua | 143 ++++++++++++++++++++++++------------------ lua/plugins/test.java | 0 4 files changed, 99 insertions(+), 76 deletions(-) create mode 100644 lua/plugins/test.java diff --git a/lua/core.lua b/lua/core.lua index 737da48..ecc2104 100644 --- a/lua/core.lua +++ b/lua/core.lua @@ -3,15 +3,12 @@ local o = vim.o local global = vim.g -- look and feel -vim.opt.shortmess:append({I = true}) -- remove intro message o.termguicolors = true o.ruler = true o.relativenumber = true o.number = true o.showmode = false o.scrolloff = 10 -o.softtabstop = 2 -o.shiftwidth = 2 -- controlling vim.opt.completeopt = {'menu', 'menuone', 'noselect'} @@ -19,7 +16,7 @@ o.ignorecase = true o.smartcase = true o.smartindent = true o.autoindent = true -o.mouse = "a" +o.mouse = "i" -- leader settings global.mapleader = ' ' diff --git a/lua/plugins.lua b/lua/plugins.lua index db6f4c1..f6604a2 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -45,7 +45,11 @@ return require('packer').startup(function(use) } -- completer - use { 'L3MON4D3/LuaSnip', event = 'UIEnter' } + use { + 'L3MON4D3/LuaSnip', + event = 'UIEnter', + requires = { 'rafamadriz/friendly-snippets' } + } use { 'hrsh7th/nvim-cmp', event = 'InsertEnter', @@ -54,22 +58,22 @@ return require('packer').startup(function(use) requires = { -- local { 'hrsh7th/cmp-cmdline', after = 'nvim-cmp' }, - { 'hrsh7th/cmp-buffer', after = 'nvim-cmp' }, - { 'hrsh7th/cmp-path', after = 'nvim-cmp' }, { - 'petertriho/cmp-git', - after = 'nvim-cmp', - config = function() require("cmp_git").setup() end - }, { 'delphinus/cmp-ctags', after = 'nvim-cmp' }, -- end + { 'hrsh7th/cmp-buffer', after = 'nvim-cmp' }, + { 'hrsh7th/cmp-path', after = 'nvim-cmp' }, { + 'petertriho/cmp-git', + after = 'nvim-cmp', + config = function() require("cmp_git").setup() end + }, { 'delphinus/cmp-ctags', after = 'nvim-cmp' }, -- end -- lsp { 'hrsh7th/cmp-nvim-lsp-document-symbol', after = 'nvim-cmp' }, -- TS - { 'ray-x/cmp-treesitter', after = 'nvim-cmp' }, -- end + { 'ray-x/cmp-treesitter', after = 'nvim-cmp' }, -- end -- Snip - { 'saadparwaiz1/cmp_luasnip', after = { 'nvim-cmp', 'LuaSnip' } } + { 'saadparwaiz1/cmp_luasnip', after = { 'nvim-cmp', 'LuaSnip' } } } } -- formating and editing - use { "williamboman/mason.nvim", event = 'BufEnter'} + use { "williamboman/mason.nvim", event = 'BufEnter' } use { 'windwp/nvim-autopairs', after = 'nvim-cmp', @@ -146,7 +150,8 @@ return require('packer').startup(function(use) require 'treesitter-context'.setup { enable = true, -- Enable this plugin (Can be enabled/disabled later via commands) max_lines = 0, -- How many lines the window should span. Values <= 0 mean no limit. - patterns = { -- Match patterns for TS nodes. These get wrapped to match at word boundaries. + patterns = { + -- Match patterns for TS nodes. These get wrapped to match at word boundaries. -- For all filetypes -- Note that setting an entry here replaces all other patterns for this entry. -- By setting the 'default' entry below, you can control which nodes you want to diff --git a/lua/plugins/cmp.lua b/lua/plugins/cmp.lua index e0a9906..f20a3ab 100644 --- a/lua/plugins/cmp.lua +++ b/lua/plugins/cmp.lua @@ -1,73 +1,94 @@ -local cmp = require 'cmp' -local luasnip = require("luasnip") +require("luasnip.loaders.from_vscode").lazy_load() local has_words_before = function() - 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 + 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) - -- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. - require('luasnip').lsp_expand(args.body) -- For `luasnip` users. - -- require('snippy').expand_snippet(args.body) -- For `snippy` users. - -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. - end - }, - window = { - -- completion = cmp.config.window.bordered(), - -- documentation = cmp.config.window.bordered(), - }, - mapping = cmp.mapping.preset.insert({ - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.complete(), - [''] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({select = true}), - [""] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.select_next_item() - elseif luasnip.expand_or_jumpable() then - luasnip.expand_or_jump() - else - fallback() - end - end, {"i", "s"}), - - [""] = 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"}) - }), - sources = cmp.config.sources({{name = 'nvim_lsp'}}, {{name = 'treesitter'}}, - {{name = 'luasnip'}}, - {{name = 'buffer'}, {name = 'path'}}) + -- ... Your other configuration ... + snippet = { + -- REQUIRED - you must specify a snippet engine + expand = function(args) + -- vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users. + require('luasnip').lsp_expand(args.body) -- For `luasnip` users. + -- require('snippy').expand_snippet(args.body) -- For `snippy` users. + -- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users. + end, + }, + mapping = { + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + -- You could replace the expand_or_jumpable() calls with expand_or_locally_jumpable() + -- they way you will only jump inside the snippet region + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif has_words_before() then + cmp.complete() + else + fallback() + end + end, { "i", "s" }), + [""] = 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" }), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete(), + [''] = cmp.mapping.abort(), + [''] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items + -- ... Your other mappings ... + }, + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + { name = "treesitter" }, + { name = "buffer" }, + { name = "luasnip" }, -- For luasnip users. + { name = "path" }, + { name = "git" }, + { name = "ctags" }, + }) + -- ... Your other configuration ... }) --- Use buffer source for `/` (if you enabled `native_menu`, this won't work anymore). -cmp.setup.cmdline('/', { - mapping = cmp.mapping.preset.cmdline(), - sources = {{name = 'nvim_lsp_document_symbol'}, {name = 'buffer'}} -}) - --- Use cmdline & path source for ':' (if you enabled `native_menu`, this won't work anymore). -cmp.setup.cmdline(':', { - mapping = cmp.mapping.preset.cmdline(), - sources = cmp.config.sources({{name = 'path'}}, {{name = 'cmdline'}}) +cmp.setup.cmdline({ '/', '?' }, { + mapping = cmp.mapping.preset.cmdline(), + sources = { + { name = 'buffer' } + } }) -- Set configuration for specific filetype. cmp.setup.filetype('gitcommit', { - sources = cmp.config.sources({{name = 'git'}}, - {{name = 'buffer'}}) + sources = cmp.config.sources({ + { name = 'cmp_git' }, -- You can specify the `cmp_git` source if you were installed it. + }, { + { name = 'buffer' }, + }) +}) + +-- `:` cmdline setup. +cmp.setup.cmdline(':', { + mapping = cmp.mapping.preset.cmdline(), + sources = cmp.config.sources({ + { name = 'path' } + }, { + { + name = 'cmdline', + option = { + ignore_cmds = { 'Man', '!' } + } + } + }) }) diff --git a/lua/plugins/test.java b/lua/plugins/test.java new file mode 100644 index 0000000..e69de29