-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
508 lines (459 loc) · 15.7 KB
/
init.lua
File metadata and controls
508 lines (459 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
-- init.lua — Neovim 0.11+ 原生 LSP 配置(默认启用)
--
-- 要求:Neovim nightly/0.11+(提供 vim.lsp.config 原生接口)
-- 提示:如果仍在使用 0.10.x 及更早版本,请切换到 `config.vim`
-- 或使用仓库历史版本中的 `init.lua`(基于 nvim-lspconfig)。
------------------------------------------------------------
-- 0) 基础选项
------------------------------------------------------------
vim.g.mapleader = " "
vim.g.maplocalleader = " "
-- 基本设置
vim.opt.number = true
vim.opt.relativenumber = true
vim.opt.cursorline = true
vim.opt.termguicolors = true
vim.opt.updatetime = 250
vim.opt.signcolumn = "yes"
vim.opt.hidden = true
vim.opt.mouse = "a"
vim.opt.splitright = true
vim.opt.splitbelow = true
vim.opt.clipboard = "unnamedplus"
-- 缩进和搜索
vim.opt.expandtab = true
vim.opt.shiftwidth = 2
vim.opt.tabstop = 2
vim.opt.softtabstop = 2
vim.opt.smartindent = true
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.incsearch = true
vim.opt.hlsearch = true
-- 路径设置
vim.opt.path:append("**")
vim.opt.wildmenu = true
------------------------------------------------------------
-- 1) lazy.nvim 插件管理
------------------------------------------------------------
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
print("Installing lazy.nvim plugin manager...")
print("This may take a minute on first launch...")
local output = vim.fn.system({
"git", "clone", "--filter=blob:none",
"https://github.com/folke/lazy.nvim.git", "--branch=stable", lazypath
})
if vim.v.shell_error ~= 0 then
vim.api.nvim_err_writeln("Failed to clone lazy.nvim!")
vim.api.nvim_err_writeln("Please check your Git installation and internet connection.")
vim.api.nvim_err_writeln("Error output: " .. output)
return
end
print("lazy.nvim installed successfully!")
print("Neovim will now install plugins. Please wait...")
end
vim.opt.rtp:prepend(lazypath)
------------------------------------------------------------
-- 2) 插件声明(Neovim 0.11+ 原生 LSP 所需组件)
------------------------------------------------------------
local lazy_ok, lazy = pcall(require, "lazy")
if not lazy_ok then
vim.api.nvim_err_writeln("Failed to load lazy.nvim!")
vim.api.nvim_err_writeln("Please restart Neovim or run: :lua vim.fn.delete(vim.fn.stdpath('data') .. '/lazy', 'rf')")
return
end
lazy.setup({
-- 配色方案
{
"tomasr/molokai",
lazy = false,
priority = 1000,
config = function()
vim.cmd([[colorscheme molokai]])
end,
},
-- 文件树
{
"nvim-tree/nvim-tree.lua",
dependencies = { "nvim-tree/nvim-web-devicons" },
keys = {
{ "<C-n>", "<cmd>NvimTreeToggle<cr>", desc = "Toggle file tree" },
{ "<leader>e", "<cmd>NvimTreeToggle<cr>", desc = "Toggle file tree" },
{ "<leader>f", "<cmd>NvimTreeFindFile<cr>", desc = "Find current file" },
},
config = function()
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
require("nvim-tree").setup({
view = { width = 32 },
renderer = { group_empty = true },
filters = { dotfiles = false },
git = { enable = true, ignore = false },
})
-- 兼容命令
vim.api.nvim_create_user_command("NERDTreeToggle", "NvimTreeToggle", {})
vim.api.nvim_create_user_command("NERDTreeFind", "NvimTreeFindFile", {})
end,
},
-- 状态栏
{
"nvim-lualine/lualine.nvim",
dependencies = { "nvim-tree/nvim-web-devicons" },
config = function()
require("lualine").setup({ options = { theme = "auto" } })
end,
},
-- which-key
{
"folke/which-key.nvim",
event = "VeryLazy",
config = function()
require("which-key").setup({ plugins = { spelling = true } })
end,
},
-- Git 增强
{
"lewis6991/gitsigns.nvim",
event = { "BufReadPre", "BufNewFile" },
opts = {
current_line_blame = true,
on_attach = function(bufnr)
local gs = package.loaded.gitsigns
local map = function(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc })
end
map("n", "]g", gs.next_hunk, "[g]it next hunk")
map("n", "[g", gs.prev_hunk, "[g]it prev hunk")
map("n", "<leader>hs", gs.stage_hunk, "[h]unk [s]tage")
map("n", "<leader>hr", gs.reset_hunk, "[h]unk [r]eset")
map("n", "<leader>hp", gs.preview_hunk, "[h]unk [p]review")
end,
},
},
-- Telescope
{
"nvim-telescope/telescope.nvim",
branch = "0.1.x",
dependencies = { "nvim-lua/plenary.nvim" },
keys = {
{ "<leader>ff", function() require("telescope.builtin").find_files() end, desc = "Find files" },
{ "<leader>fg", function() require("telescope.builtin").live_grep() end, desc = "Live grep" },
{ "<leader>fb", function() require("telescope.builtin").buffers() end, desc = "Buffers" },
{ "<leader>fh", function() require("telescope.builtin").help_tags() end, desc = "Help" },
{ "<leader>fo", function() require("telescope.builtin").oldfiles() end, desc = "Recent files" },
},
config = function()
require("telescope").setup({ defaults = { mappings = { i = { ["<C-u>"] = false, ["<C-d>"] = false } } } })
end,
},
-- Treesitter
{
"nvim-treesitter/nvim-treesitter",
build = ":TSUpdate",
event = { "BufReadPost", "BufNewFile" },
dependencies = { "nvim-treesitter/nvim-treesitter-textobjects" },
opts = {
ensure_installed = { "c", "cpp", "lua", "vim", "vimdoc", "query", "python", "bash" },
auto_install = true,
highlight = { enable = true, additional_vim_regex_highlighting = false },
indent = { enable = true },
incremental_selection = {
enable = true,
keymaps = {
init_selection = "<C-space>",
node_incremental = "<C-space>",
scope_incremental = false,
node_decremental = "<bs>",
},
},
},
config = function(_, opts)
require("nvim-treesitter.configs").setup(opts)
end,
},
-- ⚠️ 注意:不再需要 nvim-lspconfig 插件
-- Neovim 0.11+ 使用内置 vim.lsp.config
-- Mason(LSP 服务器管理)
{
"williamboman/mason.nvim",
build = ":MasonUpdate",
config = function()
require("mason").setup({
ui = {
icons = {
package_installed = "✓",
package_pending = "➜",
package_uninstalled = "✗"
}
}
})
end,
},
-- nvim-cmp 补全栈
{
"hrsh7th/nvim-cmp",
event = "InsertEnter",
dependencies = {
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"L3MON4D3/LuaSnip",
"saadparwaiz1/cmp_luasnip",
"rafamadriz/friendly-snippets",
},
},
-- 格式化
{
"stevearc/conform.nvim",
event = { "BufWritePre" },
keys = {
{
"<leader>lf",
function()
require("conform").format({ async = true, lsp_fallback = true })
end,
mode = { "n", "v" },
desc = "Format buffer",
},
},
opts = {
formatters_by_ft = {
c = { "clang_format" },
cpp = { "clang_format" },
lua = { "stylua" },
python = { "black" },
},
format_on_save = nil,
},
},
-- AsyncRun(保留)
{ "skywind3000/asyncrun.vim" },
-- 注释插件
{
"numToStr/Comment.nvim",
event = { "BufReadPre", "BufNewFile" },
config = function()
require("Comment").setup()
end,
},
}, {
ui = { border = "rounded" },
performance = {
rtp = {
disabled_plugins = {
"gzip", "tarPlugin", "tohtml", "tutor", "zipPlugin",
},
},
},
})
------------------------------------------------------------
-- 3) 使用 vim.lsp.config (Neovim 0.11+ 新方式)
------------------------------------------------------------
-- nvim-cmp 补全配置
local cmp = require("cmp")
local luasnip = require("luasnip")
require("luasnip.loaders.from_vscode").lazy_load()
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
window = {
completion = cmp.config.window.bordered(),
documentation = cmp.config.window.bordered(),
},
mapping = cmp.mapping.preset.insert({
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
["<CR>"] = cmp.mapping.confirm({ select = true }),
["<Tab>"] = 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" }),
["<S-Tab>"] = 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" }),
["<Down>"] = cmp.mapping.select_next_item(),
["<Up>"] = cmp.mapping.select_prev_item(),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp", priority = 1000 },
{ name = "luasnip", priority = 750 },
{ name = "path", priority = 500 },
{ name = "buffer", priority = 250 },
}),
formatting = {
format = function(entry, vim_item)
vim_item.menu = ({
nvim_lsp = "[LSP]",
luasnip = "[Snip]",
buffer = "[Buf]",
path = "[Path]",
})[entry.source.name]
return vim_item
end,
},
})
-- 获取补全能力
local capabilities = require("cmp_nvim_lsp").default_capabilities()
-- 统一 on_attach 函数
local on_attach = function(client, bufnr)
local map = function(mode, lhs, rhs, desc)
vim.keymap.set(mode, lhs, rhs, { buffer = bufnr, desc = desc })
end
-- LSP 功能键位
map("n", "gd", vim.lsp.buf.definition, "Go to definition")
map("n", "gD", vim.lsp.buf.declaration, "Go to declaration")
map("n", "gr", vim.lsp.buf.references, "References")
map("n", "gi", vim.lsp.buf.implementation, "Implementation")
map("n", "gt", vim.lsp.buf.type_definition, "Type definition")
map("n", "K", vim.lsp.buf.hover, "Hover documentation")
map("n", "<C-k>", vim.lsp.buf.signature_help, "Signature help")
map("n", "<leader>rn", vim.lsp.buf.rename, "Rename symbol")
map({"n","v"}, "<leader>ca", vim.lsp.buf.code_action, "Code action")
map({"n","v"}, "<leader>lf", function() vim.lsp.buf.format({ async = false }) end, "Format")
-- 诊断导航(注意:<leader>e 已被 NvimTreeToggle 占用)
map("n", "[d", vim.diagnostic.goto_prev, "Prev diagnostic")
map("n", "]d", vim.diagnostic.goto_next, "Next diagnostic")
map("n", "<leader>q", vim.diagnostic.setloclist, "Quickfix diagnostics")
map("n", "gl", vim.diagnostic.open_float, "Line diagnostics")
end
-- ⚠️ Neovim 0.11+ 新 API(2025-10-17 修正)
-- 注意:vim.lsp.config 需要 (name, opts) 两个参数,然后用 vim.lsp.enable(name) 启用
-- 检查是否支持新 API
if vim.lsp.config then
-- 定义公共项
local common = { on_attach = on_attach, capabilities = capabilities }
-- 定义 clangd
vim.lsp.config("clangd", vim.tbl_deep_extend("force", common, {
cmd = {
"clangd",
"--background-index",
"--clang-tidy",
"--header-insertion=iwyu",
"--completion-style=detailed",
"--function-arg-placeholders",
},
filetypes = { "c", "cpp", "objc", "objcpp", "cuda" },
}))
-- 定义 lua_ls
vim.lsp.config("lua_ls", vim.tbl_deep_extend("force", common, {
settings = {
Lua = {
diagnostics = { globals = { "vim" } },
workspace = {
library = vim.api.nvim_get_runtime_file("", true),
checkThirdParty = false,
},
telemetry = { enable = false },
},
},
filetypes = { "lua" }, -- 显式写上更清晰
}))
-- 启用(会按上面的 filetypes 自动生效)
vim.lsp.enable("clangd")
vim.lsp.enable("lua_ls")
vim.notify("Using Neovim 0.11+ native LSP config", vim.log.levels.INFO)
else
vim.notify("vim.lsp.config not available, falling back to nvim-lspconfig", vim.log.levels.WARN)
return
end
-- 诊断配置
vim.diagnostic.config({
virtual_text = true,
signs = true,
update_in_insert = false,
underline = true,
severity_sort = true,
float = {
border = "rounded",
source = "always",
header = "",
prefix = "",
},
})
local signs = { Error = "✘", Warn = "▲", Hint = "⚑", Info = "»" }
for type, icon in pairs(signs) do
local hl = "DiagnosticSign" .. type
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
end
------------------------------------------------------------
-- 4) 其他配置(沿用经典工作流)
------------------------------------------------------------
-- AsyncRun 快捷键
vim.g.asyncrun_open = 6
vim.keymap.set("n", "<F7>", ":AsyncRun -save=2 make<CR>", { desc = "Make" })
vim.keymap.set("n", "<F8>", ":AsyncRun -save=2 make run<CR>", { desc = "Make run" })
vim.keymap.set("n", "<F6>", ":AsyncRun -save=2 make test<CR>", { desc = "Make test" })
vim.keymap.set("n", "<F10>", ":cwindow<CR>", { desc = "Toggle quickfix" })
-- F9: 单文件编译
vim.keymap.set("n", "<F9>", function()
local ft = vim.bo.filetype
if ft == "c" then
vim.cmd("AsyncRun -save=2 gcc -O2 -std=c11 % -o %<")
elseif ft == "cpp" then
vim.cmd("AsyncRun -save=2 g++ -O2 -std=c++17 % -o %<")
else
print("No single-file build rule for " .. ft)
end
end, { desc = "Build current file" })
-- F4: 运行当前文件对应的可执行文件(Win/Unix 通用)
vim.keymap.set("n", "<F4>", function()
local root = vim.fn.expand("%:r") -- 不带扩展名的基名
local is_win = vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1 or vim.fn.has("win32unix") == 1
local exe_path
if is_win then
-- Windows: 优先找 xxx.exe;若你自己生成了无后缀的文件也兼容
if vim.fn.filereadable(root .. ".exe") == 1 then
exe_path = root .. ".exe"
elseif vim.fn.filereadable(root) == 1 then
exe_path = root
end
else
-- 类 Unix:优先找 ./xxx(已在当前目录)
if vim.fn.filereadable(root) == 1 then
exe_path = "./" .. root
elseif vim.fn.filereadable(root .. ".out") == 1 then
exe_path = "./" .. root .. ".out"
end
end
if exe_path then
-- shellescape 处理空格等字符
vim.cmd("AsyncRun " .. vim.fn.shellescape(exe_path))
else
print("Binary not found. Build first (F9).")
end
end, { desc = "Run compiled binary (portable)" })
-- 兼容命令
vim.api.nvim_create_user_command("ALEFix", function()
require("conform").format({ async = false, lsp_fallback = true })
end, { desc = "Format current buffer (ALEFix compatibility)" })
-- Tab/Window 管理
vim.keymap.set("n", "<leader>tn", ":tabnew<CR>", { desc = "New tab" })
vim.keymap.set("n", "<leader>tc", ":tabclose<CR>", { desc = "Close tab" })
vim.keymap.set("n", "<leader>q", ":q<CR>", { desc = "Quit" })
-- 快速编辑
vim.keymap.set("i", "fj", "<Esc>", { desc = "Exit insert mode" })
vim.keymap.set("i", "vv", "<Esc>", { desc = "Exit insert mode" })
vim.keymap.set("n", "<C-A>", "ggVG", { desc = "Select all" })
vim.keymap.set("n", "<leader>k", ":g/^$/d<CR>", { desc = "Delete blank lines" })
-- 注释键位
vim.keymap.set("n", "<leader>cc", function()
return vim.v.count == 0 and "<Plug>(comment_toggle_linewise_current)" or "<Plug>(comment_toggle_linewise_count)"
end, { expr = true, desc = "Toggle comment" })
vim.keymap.set("v", "<leader>cc", "<Plug>(comment_toggle_linewise_visual)", { desc = "Toggle comment" })
-- 高亮复制文本
vim.api.nvim_create_autocmd("TextYankPost", {
desc = "Highlight when yanking text",
group = vim.api.nvim_create_augroup("highlight-yank", { clear = true }),
callback = function()
vim.highlight.on_yank()
end,
})
-- 完成提示
vim.notify("Neovim modern config loaded (vim.lsp.config)! Press <Space> for keybindings.", vim.log.levels.INFO)