Add proper support for snippets, and tab width

This commit is contained in:
juan 2023-03-09 16:26:45 +08:00
parent 0ce6e0e6ed
commit 43456e196f
Signed by: juan
GPG Key ID: 5C1E5093C74F1DC7
4 changed files with 99 additions and 76 deletions

View File

@ -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 = ' '

View File

@ -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',
@ -69,7 +73,7 @@ return require('packer').startup(function(use)
}
-- 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

View File

@ -1,15 +1,16 @@
local cmp = require 'cmp'
local luasnip = require("luasnip")
require("luasnip.loaders.from_vscode").lazy_load()
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
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({
-- ... Your other configuration ...
snippet = {
-- REQUIRED - you must specify a snippet engine
expand = function(args)
@ -17,28 +18,22 @@ cmp.setup({
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
end,
},
window = {
-- completion = cmp.config.window.bordered(),
-- documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = cmp.mapping.confirm({select = true}),
mapping = {
["<Tab>"] = 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"}),
end, { "i", "s" }),
["<S-Tab>"] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item()
@ -47,27 +42,53 @@ cmp.setup({
else
fallback()
end
end, {"i", "s"})
}),
sources = cmp.config.sources({{name = 'nvim_lsp'}}, {{name = 'treesitter'}},
{{name = 'luasnip'}},
{{name = 'buffer'}, {name = 'path'}})
end, { "i", "s" }),
['<C-b>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete(),
['<C-e>'] = cmp.mapping.abort(),
['<CR>'] = 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('/', {
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'}})
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', '!' }
}
}
})
})

0
lua/plugins/test.java Normal file
View File