mirror of
https://github.com/zhenyan121/dotfiles.git
synced 2026-04-10 06:24:08 +08:00
feat: 上传我的配置
This commit is contained in:
35
.config/nvim/lua/config/lazy.lua
Normal file
35
.config/nvim/lua/config/lazy.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
-- Bootstrap lazy.nvim
|
||||
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
|
||||
if not (vim.uv or vim.loop).fs_stat(lazypath) then
|
||||
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
|
||||
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
|
||||
if vim.v.shell_error ~= 0 then
|
||||
vim.api.nvim_echo({
|
||||
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
|
||||
{ out, "WarningMsg" },
|
||||
{ "\nPress any key to exit..." },
|
||||
}, true, {})
|
||||
vim.fn.getchar()
|
||||
os.exit(1)
|
||||
end
|
||||
end
|
||||
vim.opt.rtp:prepend(lazypath)
|
||||
|
||||
-- Make sure to setup `mapleader` and `maplocalleader` before
|
||||
-- loading lazy.nvim so that mappings are correct.
|
||||
-- This is also a good place to setup other settings (vim.opt)
|
||||
vim.g.mapleader = " "
|
||||
vim.g.maplocalleader = "\\"
|
||||
|
||||
-- Setup lazy.nvim
|
||||
require("lazy").setup({
|
||||
spec = {
|
||||
-- import your plugins
|
||||
{ import = "plugins" },
|
||||
},
|
||||
-- Configure any other settings here. See the documentation for more details.
|
||||
-- colorscheme that will be used when installing plugins.
|
||||
install = { colorscheme = { "habamax" } },
|
||||
-- automatically check for plugin updates
|
||||
checker = { enabled = false },
|
||||
})
|
||||
5
.config/nvim/lua/plugins/autopairs.lua
Normal file
5
.config/nvim/lua/plugins/autopairs.lua
Normal file
@@ -0,0 +1,5 @@
|
||||
return {
|
||||
"windwp/nvim-autopairs",
|
||||
event = "InsertEnter", -- 只有进入插入模式时才加载,节省性能
|
||||
config = true -- 相当于调用 require("nvim-autopairs").setup({})
|
||||
}
|
||||
11
.config/nvim/lua/plugins/colorscheme.lua
Normal file
11
.config/nvim/lua/plugins/colorscheme.lua
Normal file
@@ -0,0 +1,11 @@
|
||||
-- ~/.config/nvim/lua/plugins/colorscheme.lua
|
||||
return {
|
||||
{
|
||||
"folke/tokyonight.nvim",
|
||||
lazy = false, -- 我们希望启动就加载
|
||||
priority = 1000, -- 高优先级,确保先于其他插件加载
|
||||
config = function()
|
||||
vim.cmd.colorscheme("tokyonight-night") -- 加载后,立即设置这个主题
|
||||
end,
|
||||
}
|
||||
}
|
||||
131
.config/nvim/lua/plugins/lsp.lua
Normal file
131
.config/nvim/lua/plugins/lsp.lua
Normal file
@@ -0,0 +1,131 @@
|
||||
return {
|
||||
{
|
||||
"neovim/nvim-lspconfig",
|
||||
dependencies = {
|
||||
"williamboman/mason.nvim",
|
||||
"williamboman/mason-lspconfig.nvim",
|
||||
"hrsh7th/nvim-cmp",
|
||||
"hrsh7th/cmp-nvim-lsp",
|
||||
},
|
||||
config = function()
|
||||
-- 1. 初始化 Mason (包管理器)
|
||||
require("mason").setup({
|
||||
ui = { border = "rounded" }
|
||||
})
|
||||
|
||||
-- 2. 配置 mason-lspconfig (自动安装LSP服务器)
|
||||
require("mason-lspconfig").setup({
|
||||
ensure_installed = { "clangd", "lua_ls" },
|
||||
-- 【重要】新框架下,mason-lspconfig 会自动处理安装和配置
|
||||
-- 你不再需要手动调用 `require('lspconfig').xxx.setup()`
|
||||
})
|
||||
|
||||
-- 3. 通用配置:快捷键与能力集
|
||||
local on_attach = function(client, bufnr)
|
||||
local opts = { buffer = bufnr, noremap = true, silent = true }
|
||||
-- 按键映射
|
||||
vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts)
|
||||
vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts)
|
||||
vim.keymap.set('n', '<leader>rn', vim.lsp.buf.rename, opts)
|
||||
vim.keymap.set('n', '<leader>ca', vim.lsp.buf.code_action, opts)
|
||||
vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts)
|
||||
end
|
||||
|
||||
-- 获取自动补全的能力集
|
||||
local capabilities = require('cmp_nvim_lsp').default_capabilities()
|
||||
|
||||
-- 4. 【核心修改】新版 LSP 配置框架
|
||||
-- 全局配置表,mason-lspconfig 会自动将这里定义的配置应用到对应服务器
|
||||
vim.lsp.config = vim.lsp.config or {}
|
||||
|
||||
-- 导入工具函数(用于根目录检测)
|
||||
local util = require("lspconfig.util")
|
||||
|
||||
-- ========== 配置 clangd (C/C++) ==========
|
||||
vim.lsp.config.clangd = {
|
||||
default_config = {
|
||||
cmd = {
|
||||
"clangd",
|
||||
"--background-index",
|
||||
"--clang-tidy",
|
||||
"--header-insertion=never",
|
||||
"--completion-style=detailed",
|
||||
"--all-scopes-completion",
|
||||
"--cross-file-rename",
|
||||
"--completion-parse=auto"
|
||||
},
|
||||
filetypes = { "c", "cpp", "objc", "objcpp", "cuda" },
|
||||
root_dir = function(fname)
|
||||
local root_markers = { '.git', 'compile_commands.json', 'compile_flags.txt', '.clangd', 'Makefile', 'build' }
|
||||
local root = util.root_pattern(unpack(root_markers))(fname)
|
||||
if not root then
|
||||
root = util.find_git_ancestor(fname) or vim.fs.dirname(fname)
|
||||
end
|
||||
return root
|
||||
end,
|
||||
single_file_support = true,
|
||||
capabilities = capabilities,
|
||||
on_attach = on_attach, -- 【重要】在这里绑定快捷键
|
||||
}
|
||||
}
|
||||
|
||||
-- ========== 配置 lua_ls (Lua) ==========
|
||||
vim.lsp.config.lua_ls = {
|
||||
default_config = {
|
||||
settings = {
|
||||
Lua = {
|
||||
runtime = { version = 'LuaJIT' },
|
||||
diagnostics = { globals = { 'vim' } },
|
||||
workspace = {
|
||||
library = vim.api.nvim_get_runtime_file("", true),
|
||||
checkThirdParty = false,
|
||||
},
|
||||
telemetry = { enable = false },
|
||||
},
|
||||
},
|
||||
capabilities = capabilities,
|
||||
on_attach = on_attach, -- 【重要】在这里绑定快捷键
|
||||
}
|
||||
}
|
||||
|
||||
-- 5. 美化UI设置
|
||||
-- 悬浮文档窗口使用圆角边框
|
||||
vim.lsp.handlers["textDocument/hover"] = vim.lsp.with(
|
||||
vim.lsp.handlers.hover, {
|
||||
border = "rounded",
|
||||
}
|
||||
)
|
||||
|
||||
-- 【注意】在新框架下,通常不需要手动创建 LspAttach 自动命令
|
||||
-- 因为每个服务器的 on_attach 已在 default_config 中定义
|
||||
-- mason-lspconfig 会自动处理服务器启动和配置应用
|
||||
end,
|
||||
},
|
||||
|
||||
-- 6. 自动补全配置 (nvim-cmp) - 保持不变,与新旧框架都兼容
|
||||
{
|
||||
"hrsh7th/nvim-cmp",
|
||||
config = function()
|
||||
local cmp = require("cmp")
|
||||
cmp.setup({
|
||||
window = {
|
||||
completion = cmp.config.window.bordered(),
|
||||
documentation = cmp.config.window.bordered(),
|
||||
},
|
||||
view = {
|
||||
entries = { name = 'custom', selection_order = 'near_cursor' }
|
||||
},
|
||||
mapping = cmp.mapping.preset.insert({
|
||||
['<C-Space>'] = cmp.mapping.complete(),
|
||||
['<CR>'] = cmp.mapping.confirm({ select = true }),
|
||||
['<Tab>'] = cmp.mapping.select_next_item(),
|
||||
['<S-Tab>'] = cmp.mapping.select_prev_item(),
|
||||
}),
|
||||
sources = cmp.config.sources({
|
||||
{ name = 'nvim_lsp' },
|
||||
{ name = 'path' },
|
||||
})
|
||||
})
|
||||
end,
|
||||
},
|
||||
}
|
||||
14
.config/nvim/lua/plugins/lualine.lua
Normal file
14
.config/nvim/lua/plugins/lualine.lua
Normal file
@@ -0,0 +1,14 @@
|
||||
return {
|
||||
|
||||
'nvim-lualine/lualine.nvim',
|
||||
dependencies = { 'nvim-tree/nvim-web-devicons' },
|
||||
config = function()
|
||||
require('lualine').setup({
|
||||
options = {
|
||||
theme = 'auto',
|
||||
icons_enabled = true, -- 确保图标功能已启用
|
||||
},
|
||||
})
|
||||
end,
|
||||
}
|
||||
|
||||
38
.config/nvim/lua/plugins/nvim-tree.lua
Normal file
38
.config/nvim/lua/plugins/nvim-tree.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
return {
|
||||
"nvim-tree/nvim-tree.lua",
|
||||
version = "*",
|
||||
lazy = false,
|
||||
dependencies = { "nvim-tree/nvim-web-devicons" },
|
||||
config = function()
|
||||
require("nvim-tree").setup({
|
||||
-- 这里可以放自定义设置,初始用默认即可
|
||||
})
|
||||
-- 设置一个常用快捷键:空格+e 打开/关闭文件树
|
||||
-- 为 nvim-tree 设置一组实用快捷键
|
||||
vim.keymap.set('n', '<leader>e', ':NvimTreeToggle<CR>', { desc = 'Toggle file tree' })
|
||||
vim.keymap.set('n', '<leader>f', function()
|
||||
local current_win = vim.api.nvim_get_current_win()
|
||||
local current_buf = vim.api.nvim_win_get_buf(current_win)
|
||||
local buf_ft = vim.api.nvim_buf_get_option(current_buf, 'filetype')
|
||||
|
||||
if buf_ft == 'NvimTree' then
|
||||
-- 从文件树返回时,尝试回到之前编辑的窗口
|
||||
vim.cmd('wincmd p')
|
||||
|
||||
-- 如果上一个窗口还是文件树(可能只有一个文件树窗口),就关闭它
|
||||
local new_win = vim.api.nvim_get_current_win()
|
||||
local new_buf = vim.api.nvim_win_get_buf(new_win)
|
||||
local new_buf_ft = vim.api.nvim_buf_get_option(new_buf, 'filetype')
|
||||
|
||||
if new_buf_ft == 'NvimTree' then
|
||||
vim.cmd('NvimTreeClose')
|
||||
end
|
||||
else
|
||||
-- 保存当前窗口ID,以便从文件树返回时能准确回来
|
||||
vim.g.last_normal_win = current_win
|
||||
vim.cmd('NvimTreeFindFile')
|
||||
end
|
||||
end, { desc = '智能切换: 文件⇄树' })
|
||||
vim.keymap.set('n', '<leader>t', ':NvimTreeFocus<CR>', { desc = 'Focus on the file tree' })
|
||||
end,
|
||||
}
|
||||
84
.config/nvim/lua/plugins/ouroboros.lua
Normal file
84
.config/nvim/lua/plugins/ouroboros.lua
Normal file
@@ -0,0 +1,84 @@
|
||||
-- 在你的 Lazy 插件配置文件中(例如:lua/plugins/cpp.lua)
|
||||
return {
|
||||
{
|
||||
"jakemason/ouroboros",
|
||||
ft = { "c", "cpp", "h", "hpp" }, -- 可选:按文件类型懒加载
|
||||
dependencies = {
|
||||
"nvim-lua/plenary.nvim" -- 明确声明依赖[citation:8]
|
||||
},
|
||||
config = function()
|
||||
require("ouroboros").setup({
|
||||
-- 基本配置
|
||||
open_all_alternates = false,
|
||||
|
||||
-- 查找策略(按顺序尝试)
|
||||
strategies = {
|
||||
"directory", -- 同一目录
|
||||
"underscore", -- main_window.cpp -> main_window.h
|
||||
"basename", -- 相同基名
|
||||
"subdirectory", -- 在 include/ 或 src/ 中查找
|
||||
"cabal", -- Cabal 项目结构
|
||||
"complement", -- 互补扩展名
|
||||
},
|
||||
|
||||
-- 扩展名映射
|
||||
extension_map = {
|
||||
h = { "cpp", "c", "cc", "cxx", "c++", "m", "mm" },
|
||||
hpp = { "cpp", "cc", "cxx", "c++" },
|
||||
hxx = { "cxx", "cpp" },
|
||||
hh = { "cc", "cpp" },
|
||||
c = { "h" },
|
||||
cc = { "h", "hh" },
|
||||
cpp = { "h", "hpp" },
|
||||
cxx = { "h", "hxx", "hpp" },
|
||||
m = { "h" },
|
||||
mm = { "h" },
|
||||
},
|
||||
|
||||
-- 目录映射(适用于标准项目结构)
|
||||
directory_map = {
|
||||
["include/(.*)%.h$"] = "src/%1.cpp",
|
||||
["src/(.*)%.cpp$"] = "include/%1.h",
|
||||
["inc/(.*)%.hpp$"] = "src/%1.cpp",
|
||||
["lib/(.*)%.c$"] = "include/%1.h",
|
||||
["source/(.*)%.cxx$"] = "headers/%1.hxx",
|
||||
},
|
||||
|
||||
-- 自定义匹配函数
|
||||
match_callback = function(filepath, strategies, bufnr)
|
||||
-- 获取文件扩展名
|
||||
local extension = filepath:match("%.(%w+)$") or ""
|
||||
|
||||
-- 如果是测试文件,寻找对应的源文件
|
||||
if filepath:match("_test%.cpp$") then
|
||||
local source_file = filepath:gsub("_test%.cpp$", ".cpp")
|
||||
if vim.fn.filereadable(source_file) == 1 then
|
||||
return { source_file }
|
||||
end
|
||||
end
|
||||
|
||||
-- 返回默认策略
|
||||
return strategies
|
||||
end,
|
||||
})
|
||||
|
||||
-- 键位映射
|
||||
vim.keymap.set("n", "<leader>oh", "<cmd>Ouroboros<cr>", {
|
||||
desc = "切换头文件/源文件",
|
||||
noremap = true,
|
||||
silent = true,
|
||||
})
|
||||
|
||||
vim.keymap.set("n", "<leader>oa", "<cmd>OuroborosAll<cr>", {
|
||||
desc = "打开所有匹配文件",
|
||||
noremap = true,
|
||||
silent = true,
|
||||
})
|
||||
|
||||
-- 可以添加更多命令
|
||||
vim.api.nvim_create_user_command("AltFile", function()
|
||||
require("ouroboros").switch()
|
||||
end, {})
|
||||
end,
|
||||
},
|
||||
}
|
||||
19
.config/nvim/lua/plugins/treesitter.lua
Normal file
19
.config/nvim/lua/plugins/treesitter.lua
Normal file
@@ -0,0 +1,19 @@
|
||||
return {
|
||||
"nvim-treesitter/nvim-treesitter",
|
||||
version = false, -- 建议使用 master 分支
|
||||
build = ":TSUpdate",
|
||||
event = { "BufReadPost", "BufNewFile" }, -- 只有打开文件时才加载,防止启动报错
|
||||
config = function()
|
||||
-- 增加一个保护性检测
|
||||
local status_ok, configs = pcall(require, "nvim-treesitter.configs")
|
||||
if not status_ok then
|
||||
return
|
||||
end
|
||||
|
||||
configs.setup({
|
||||
ensure_installed = { "lua", "vim", "vimdoc", "query", "c", "cpp" }, -- 基础必装
|
||||
highlight = { enable = true },
|
||||
indent = { enable = true }, -- 建议开启,C++ 缩进会更准确
|
||||
})
|
||||
end,
|
||||
}
|
||||
Reference in New Issue
Block a user