Skip to content

Commit 85f6cc6

Browse files
committed
feat: lsp and cmp
1 parent 2092e02 commit 85f6cc6

File tree

7 files changed

+296
-81
lines changed

7 files changed

+296
-81
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
# arui-nvim-config
2-
nvim 相关配置
2+
3+
nvim 相关配置
4+
5+
> 可以参考
6+
> https://martinlwx.github.io/zh-cn/config-neovim-from-scratch/

init.lua

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -16,84 +16,6 @@ vim.opt.rtp:prepend(lazypath)
1616
require("options")
1717
require("keymaps")
1818

19-
-- 通过 lazy 导入插件
20-
require("lazy").setup({
21-
22-
-- 主题
23-
{
24-
"folke/tokyonight.nvim",
25-
lazy = false,
26-
priority = 1000,
27-
opts = {},
28-
},
29-
30-
-- markdown
31-
{
32-
'MeanderingProgrammer/render-markdown.nvim',
33-
dependencies = { 'nvim-treesitter/nvim-treesitter', 'echasnovski/mini.nvim' }, -- if you use the mini.nvim suite
34-
-- dependencies = { 'nvim-treesitter/nvim-treesitter', 'echasnovski/mini.icons' }, -- if you use standalone mini plugins
35-
-- dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-tree/nvim-web-devicons' }, -- if you prefer nvim-web-devicons
36-
---@module 'render-markdown'
37-
---@type render.md.UserConfig
38-
opts = {},
39-
},
40-
41-
-- 文件树
42-
{
43-
"nvim-neo-tree/neo-tree.nvim",
44-
branch = "v3.x",
45-
dependencies = {
46-
"nvim-lua/plenary.nvim",
47-
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
48-
"MunifTanjim/nui.nvim",
49-
-- "3rd/image.nvim", -- Optional image support in preview window: See `# Preview Mode` for more information
50-
},
51-
filesystem = {
52-
filtered_items = {
53-
visible = true, -- 允许显示隐藏文件和目录
54-
hide_dotfiles = false, -- 显示 . 开头的项目
55-
hide_gitignored = false, -- 显示被 .gitignore 忽略的文件
56-
}
57-
}
58-
},
59-
60-
-- 切换窗口
61-
{
62-
's1n7ax/nvim-window-picker',
63-
name = 'window-picker',
64-
event = 'VeryLazy',
65-
version = '2.*',
66-
config = function()
67-
require'window-picker'.setup()
68-
end,
69-
},
70-
71-
-- 允许图片插入到md等标记型文件
72-
{
73-
"HakonHarnes/img-clip.nvim",
74-
event = "VeryLazy",
75-
opts = {
76-
-- add options here
77-
-- or leave it empty to use the default settings
78-
},
79-
keys = {
80-
-- suggested keymap
81-
{ "<leader>p", "<cmd>PasteImage<cr>", desc = "Paste image from system clipboard" },
82-
},
83-
},
84-
85-
-- codeium
86-
{
87-
"Exafunction/codeium.nvim",
88-
dependencies = {
89-
"nvim-lua/plenary.nvim",
90-
"hrsh7th/nvim-cmp",
91-
},
92-
config = function()
93-
require("codeium").setup({
94-
})
95-
end
96-
},
97-
98-
})
19+
-- 插件配置
20+
require("plugins")
9921

lazy-lock.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
{
2+
"LuaSnip": { "branch": "master", "commit": "03c8e67eb7293c404845b3982db895d59c0d1538" },
3+
"cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" },
4+
"cmp-cmdline": { "branch": "main", "commit": "d250c63aa13ead745e3a40f61fdd3470efde3923" },
5+
"cmp-nvim-lsp": { "branch": "main", "commit": "99290b3ec1322070bcfb9e846450a46f6efa50f0" },
6+
"cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" },
27
"codeium.nvim": { "branch": "main", "commit": "ebed4f7cc8a18184d8332d421ca10bed5f7d59a1" },
8+
"formatter.nvim": { "branch": "master", "commit": "eb89a1f3e079f1b9680bc7293b75fffccb5e1598" },
39
"img-clip.nvim": { "branch": "main", "commit": "5ff183655ad98b5fc50c55c66540375bbd62438c" },
410
"lazy.nvim": { "branch": "main", "commit": "d8f26efd456190241afd1b0f5235fe6fdba13d4a" },
11+
"lspkind.nvim": { "branch": "master", "commit": "d79a1c3299ad0ef94e255d045bed9fa26025dab6" },
12+
"mason-lspconfig.nvim": { "branch": "main", "commit": "e942edf5c85b6a2ab74059ea566cac5b3e1514a4" },
13+
"mason.nvim": { "branch": "main", "commit": "e2f7f9044ec30067bc11800a9e266664b88cda22" },
514
"mini.nvim": { "branch": "main", "commit": "bac6c8bb77fe0a872719ea43c39e35c7c695f05e" },
615
"neo-tree.nvim": { "branch": "v3.x", "commit": "e6645ecfcba3e064446a6def1c10d788c9873f51" },
716
"nui.nvim": { "branch": "main", "commit": "53e907ffe5eedebdca1cd503b00aa8692068ca46" },
817
"nvim-cmp": { "branch": "main", "commit": "8c82d0bd31299dbff7f8e780f5e06d2283de9678" },
18+
"nvim-lspconfig": { "branch": "master", "commit": "14b5a806c928390fac9ff4a5630d20eb902afad8" },
919
"nvim-treesitter": { "branch": "master", "commit": "6df6e80417b58abe377c90f5e3f7fe94cf7dfc55" },
1020
"nvim-web-devicons": { "branch": "master", "commit": "aafa5c187a15701a7299a392b907ec15d9a7075f" },
1121
"plenary.nvim": { "branch": "master", "commit": "3707cdb1e43f5cea73afb6037e6494e7ce847a66" },

lua/config/nvim-cmp.lua

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
local has_words_before = function()
2+
unpack = unpack or table.unpack
3+
local line, col = unpack(vim.api.nvim_win_get_cursor(0))
4+
return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
5+
end
6+
7+
local luasnip = require("luasnip")
8+
local cmp = require("cmp")
9+
10+
cmp.setup({
11+
snippet = {
12+
-- REQUIRED - you must specify a snippet engine
13+
expand = function(args)
14+
require('luasnip').lsp_expand(args.body) -- For `luasnip` users.
15+
end,
16+
},
17+
mapping = cmp.mapping.preset.insert({
18+
-- Use <C-b/f> to scroll the docs
19+
['<C-b>'] = cmp.mapping.scroll_docs( -4),
20+
['<C-f>'] = cmp.mapping.scroll_docs(4),
21+
-- Use <C-k/j> to switch in items
22+
['<C-k>'] = cmp.mapping.select_prev_item(),
23+
['<C-j>'] = cmp.mapping.select_next_item(),
24+
-- Use <CR>(Enter) to confirm selection
25+
-- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items.
26+
['<CR>'] = cmp.mapping.confirm({ select = true }),
27+
28+
-- A super tab
29+
-- sourc: https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#luasnip
30+
["<Tab>"] = cmp.mapping(function(fallback)
31+
-- Hint: if the completion menu is visible select next one
32+
if cmp.visible() then
33+
cmp.select_next_item()
34+
elseif has_words_before() then
35+
cmp.complete()
36+
else
37+
fallback()
38+
end
39+
end, { "i", "s" }), -- i - insert mode; s - select mode
40+
["<S-Tab>"] = cmp.mapping(function(fallback)
41+
if cmp.visible() then
42+
cmp.select_prev_item()
43+
elseif luasnip.jumpable( -1) then
44+
luasnip.jump( -1)
45+
else
46+
fallback()
47+
end
48+
end, { "i", "s" }),
49+
}),
50+
51+
-- Let's configure the item's appearance
52+
-- source: https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance
53+
formatting = {
54+
-- Set order from left to right
55+
-- kind: single letter indicating the type of completion
56+
-- abbr: abbreviation of "word"; when not empty it is used in the menu instead of "word"
57+
-- menu: extra text for the popup menu, displayed after "word" or "abbr"
58+
fields = { 'abbr', 'menu' },
59+
60+
-- customize the appearance of the completion menu
61+
format = function(entry, vim_item)
62+
vim_item.menu = ({
63+
nvim_lsp = '[Lsp]',
64+
luasnip = '[Luasnip]',
65+
buffer = '[File]',
66+
path = '[Path]',
67+
})[entry.source.name]
68+
return vim_item
69+
end,
70+
},
71+
72+
-- Set source precedence
73+
sources = cmp.config.sources({
74+
{ name = 'nvim_lsp' }, -- For nvim-lsp
75+
{ name = 'luasnip' }, -- For luasnip user
76+
{ name = 'buffer' }, -- For buffer word completion
77+
{ name = 'path' }, -- For path completion
78+
{ name = "codeium" }, -- For codeium
79+
})
80+
})

lua/lsp.lua

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
require('mason').setup({
2+
ui = {
3+
icons = {
4+
package_installed = "",
5+
package_pending = "",
6+
package_uninstalled = ""
7+
}
8+
}
9+
})
10+
11+
require('mason-lspconfig').setup({
12+
-- A list of servers to automatically install if they're not already installed
13+
ensure_installed = { 'pylsp', 'lua_ls', 'rust_analyzer', 'eslint', 'gopls' }},
14+
})
15+
16+
17+
18+
19+
-- Set different settings for different languages' LSP
20+
-- LSP list: https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
21+
-- How to use setup({}): https://github.com/neovim/nvim-lspconfig/wiki/Understanding-setup-%7B%7D
22+
-- - the settings table is sent to the LSP
23+
-- - on_attach: a lua callback function to run after LSP atteches to a given buffer
24+
local lspconfig = require('lspconfig')
25+
26+
-- Customized on_attach function
27+
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
28+
local opts = { noremap = true, silent = true }
29+
vim.keymap.set('n', '<space>e', vim.diagnostic.open_float, opts)
30+
vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, opts)
31+
vim.keymap.set('n', ']d', vim.diagnostic.goto_next, opts)
32+
vim.keymap.set('n', '<space>q', vim.diagnostic.setloclist, opts)
33+
34+
-- Use an on_attach function to only map the following keys
35+
-- after the language server attaches to the current buffer
36+
local on_attach = function(client, bufnr)
37+
-- Enable completion triggered by <c-x><c-o>
38+
vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')
39+
40+
-- See `:help vim.lsp.*` for documentation on any of the below functions
41+
local bufopts = { noremap = true, silent = true, buffer = bufnr }
42+
vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts)
43+
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)
44+
vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts)
45+
vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts)
46+
vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts)
47+
vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, bufopts)
48+
vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts)
49+
vim.keymap.set('n', '<space>wl', function()
50+
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
51+
end, bufopts)
52+
vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, bufopts)
53+
vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, bufopts)
54+
vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, bufopts)
55+
vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
56+
vim.keymap.set("n", "<space>f", function()
57+
vim.lsp.buf.format({ async = true })
58+
end, bufopts)
59+
end
60+
61+
-- Configure each language
62+
-- How to add LSP for a specific language?
63+
-- 1. use `:Mason` to install corresponding LSP
64+
-- 2. add configuration below
65+
lspconfig.pylsp.setup({
66+
on_attach = on_attach,
67+
})
68+
69+
lspconfig.rust_analyzer.setup({
70+
on_attach = on_attach,
71+
})
72+
73+
lspconfig.eslint.setup({
74+
on_attach = on_attach,
75+
})
76+
77+
lspconfig.gopls.setup({
78+
on_attach = on_attach,
79+
})
80+
81+
lspconfig.lua_ls.setup({
82+
on_attach = on_attach,
83+
})

lua/plugins.lua

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
-- 通过 lazy 导入插件
2+
require("lazy").setup({
3+
-- LSP manager
4+
"williamboman/mason.nvim",
5+
"williamboman/mason-lspconfig.nvim",
6+
"neovim/nvim-lspconfig",
7+
"mhartington/formatter.nvim",
8+
9+
10+
-- 主题
11+
{
12+
"folke/tokyonight.nvim",
13+
lazy = false,
14+
priority = 1000,
15+
opts = {},
16+
},
17+
18+
-- markdown
19+
{
20+
'MeanderingProgrammer/render-markdown.nvim',
21+
dependencies = { 'nvim-treesitter/nvim-treesitter', 'echasnovski/mini.nvim' }, -- if you use the mini.nvim suite
22+
-- dependencies = { 'nvim-treesitter/nvim-treesitter', 'echasnovski/mini.icons' }, -- if you use standalone mini plugins
23+
-- dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-tree/nvim-web-devicons' }, -- if you prefer nvim-web-devicons
24+
---@module 'render-markdown'
25+
---@type render.md.UserConfig
26+
opts = {},
27+
},
28+
29+
-- 文件树
30+
{
31+
"nvim-neo-tree/neo-tree.nvim",
32+
branch = "v3.x",
33+
dependencies = {
34+
"nvim-lua/plenary.nvim",
35+
"nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
36+
"MunifTanjim/nui.nvim",
37+
-- "3rd/image.nvim", -- Optional image support in preview window: See `# Preview Mode` for more information
38+
},
39+
filesystem = {
40+
filtered_items = {
41+
visible = true, -- 允许显示隐藏文件和目录
42+
hide_dotfiles = false, -- 显示 . 开头的项目
43+
hide_gitignored = false, -- 显示被 .gitignore 忽略的文件
44+
}
45+
}
46+
},
47+
48+
-- 切换窗口
49+
{
50+
's1n7ax/nvim-window-picker',
51+
name = 'window-picker',
52+
event = 'VeryLazy',
53+
version = '2.*',
54+
config = function()
55+
require'window-picker'.setup()
56+
end,
57+
},
58+
59+
-- 允许图片插入到md等标记型文件
60+
{
61+
"HakonHarnes/img-clip.nvim",
62+
event = "VeryLazy",
63+
opts = {
64+
-- add options here
65+
-- or leave it empty to use the default settings
66+
},
67+
keys = {
68+
-- suggested keymap
69+
{ "<leader>p", "<cmd>PasteImage<cr>", desc = "Paste image from system clipboard" },
70+
},
71+
},
72+
73+
-- codeium
74+
{
75+
"Exafunction/codeium.nvim",
76+
dependencies = {
77+
"nvim-lua/plenary.nvim",
78+
"hrsh7th/nvim-cmp",
79+
},
80+
config = function()
81+
require("codeium").setup({
82+
})
83+
end
84+
},
85+
86+
87+
-- vscode 风格图标
88+
{
89+
"onsails/lspkind.nvim",
90+
event = { "VimEnter" },
91+
},
92+
-- 自动补全插件
93+
{
94+
"hrsh7th/nvim-cmp",
95+
dependencies = {
96+
"lspkind.nvim",
97+
"hrsh7th/cmp-nvim-lsp", -- lsp auto-completion
98+
"hrsh7th/cmp-buffer", -- buffer auto-completion
99+
"hrsh7th/cmp-path", -- path auto-completion
100+
"hrsh7th/cmp-cmdline", -- cmdline auto-completion
101+
},
102+
config = function()
103+
require("config.nvim-cmp")
104+
end,
105+
},
106+
-- 代码片段引擎
107+
{
108+
"L3MON4D3/LuaSnip",
109+
version = "v2.*",
110+
},
111+
})

0 commit comments

Comments
 (0)