localopt=vim.optlocalo=vim.olocalg=vim.g-------------------------------------- options ------------------------------------------o.laststatus=3o.showmode=falseo.splitkeep="screen"o.clipboard="unnamedplus"o.cursorline=trueo.cursorlineopt="number"-- Indentingo.expandtab=trueo.shiftwidth=2o.smartindent=trueo.tabstop=2o.softtabstop=2opt.fillchars={eob=" "}o.ignorecase=trueo.smartcase=trueo.mouse="a"-- Numberso.number=trueo.numberwidth=2o.ruler=false-- disable nvim introopt.shortmess:append"sI"o.signcolumn="yes"o.splitbelow=trueo.splitright=trueo.timeoutlen=400o.undofile=true-- interval for writing swap file to disk, also used by gitsignso.updatetime=250-- go to previous/next line with h,l,left arrow and right arrow-- when cursor reaches end/beginning of lineopt.whichwrap:append"<>[]hl"-- disable some default providersg.loaded_node_provider=0g.loaded_python3_provider=0g.loaded_perl_provider=0g.loaded_ruby_provider=0-- add binaries installed by mason.nvim to pathlocalis_windows=vim.fn.has"win32"~=0localsep=is_windowsand"\\"or"/"localdelim=is_windowsand";"or":"vim.env.PATH=table.concat({vim.fn.stdpath"data","mason","bin"},sep)..delim..vim.env.PATH
require"nvchad.mappings"localmap=vim.keymap.setmap("n",";",":",{desc="CMD enter command mode"})map("i","<A-h>","<ESC>^i",{desc="move beginning of line"})map("i","<A-l>","<End>",{desc="move end of line"})map("n","F","%",{desc="jump between match-pair"})-- use windows like keymaps map({"n","i","v"},"<C-s>","<cmd> w <cr>",{desc="file save"})map({"n"},"<C-a>","ggVG",{desc="select all file"})map({"n","i","v"},"<C-z>","<cmd> undo <cr>",{desc="history undo"})map({"n","i","v"},"<C-y>","<cmd> redo <cr>",{desc="history redo"})map("n","<C-/>","gcc",{desc="comment toggle",remap=true})map("i","<C-/>","<Esc>gcc^i",{desc="comment toggle",remap=true})-- visual studio code like keymapsmap("n","gb","<C-o>",{desc="jump back"})map("n","gh","<cmd> lua vim.lsp.buf.hover() <cr>",{desc="LSP hover"})map("n","ge","<cmd> lua vim.diagnostic.open_float() <cr>",{desc="LSP show diagnostics"})map("n","ge","<cmd> lua vim.diagnostic.open_float() <cr>",{desc="LSP show diagnostics"})map({"n","i"},"<A-j>","<cmd> :m +1 <cr>",{desc="move one line down "})map({"n","i"},"<A-k>","<cmd> :m -2 <cr>",{desc="move one line up "})map("v","<A-j>",":m '>+1<CR>gv=gv",{desc="Move selected lines down"})map("v","<A-k>",":m '<-2<CR>gv=gv",{desc="Move selected lines up"})
return{-- the colorscheme should be available when starting Neovim{"folke/tokyonight.nvim",lazy=false,-- make sure we load this during startup if it is your main colorschemepriority=1000,-- make sure to load this before all the other start pluginsconfig=function()-- load the colorscheme herevim.cmd([[colorscheme tokyonight]])end,},-- I have a separate config.mappings file where I require which-key.-- With lazy the plugin will be automatically loaded when it is required somewhere{"folke/which-key.nvim",lazy=true},{"nvim-neorg/neorg",-- lazy-load on filetypeft="norg",-- options for neorg. This will automatically call `require("neorg").setup(opts)`opts={load={["core.defaults"]={},},},},{"dstein64/vim-startuptime",-- lazy-load on a commandcmd="StartupTime",-- init is called during startup. Configuration for vim plugins typically should be set in an init functioninit=function()vim.g.startuptime_tries=10end,},{"hrsh7th/nvim-cmp",-- load cmp on InsertEnterevent="InsertEnter",-- these dependencies will only be loaded when cmp loads-- dependencies are always lazy-loaded unless specified otherwisedependencies={"hrsh7th/cmp-nvim-lsp","hrsh7th/cmp-buffer",},config=function()-- ...end,},-- if some code requires a module from an unloaded plugin, it will be automatically loaded.-- So for api plugins like devicons, we can always set lazy=true{"nvim-tree/nvim-web-devicons",lazy=true},-- you can use the VeryLazy event for things that can-- load later and are not important for the initial UI{"stevearc/dressing.nvim",event="VeryLazy"},{"Wansmer/treesj",keys={{"J","<cmd>TSJToggle<cr>",desc="Join Toggle"},},opts={use_default_keymaps=false,max_join_length=150},},{"monaqa/dial.nvim",-- lazy-load on keys-- mode is `n` by default. For more advanced options, check the section on key mappingskeys={"<C-a>",{"<C-x>",mode="n"}},},-- local plugins need to be explicitly configured with dir{dir="~/projects/secret.nvim"},-- you can use a custom url to fetch a plugin{url="git@github.com:folke/noice.nvim.git"},-- local plugins can also be configured with the dev option.-- This will use {config.dev.path}/noice.nvim/ instead of fetching it from GitHub-- With the dev option, you can easily switch between the local and installed version of a plugin{"folke/noice.nvim",dev=true},}
localluaSnip=require("luasnip")-- snippetlocals=luaSnip.s-- insert nodelocali=luaSnip.i-- text nodelocalt=luaSnip.t-- formatter toollocalfmt=require("luasnip.extras.fmt").fmtluaSnip.add_snippets("cpp",{s({trig="demo",name="Competitive Programming Template",dscr="A template for competitive programming. "},t({-- some vsocde like snippets here}))}
需要承认的是, LSP 可能是配置 Neovim 最复杂的一部分。这或许也是这篇文章最重要的一部分,只有配置好了它,你才能在 Neovim 上享受完整的代码补全能力 —— but first, what is LSP ?
简单来说 , LSP 协议包含两个核心组件, Language Client 和 Language Server ,正如字面意思,Language Client 负责用户界面的渲染 (高亮,悬浮提示) ,监听用户的行为并将其转化特定语言的请求转发给 Language Server 。而 Language Server 负责接收 Language Client 的信息进行处理,并且将结果 (代码补全,错误信息等) 回转给 Language Client 。
协议使得完整语言支持的 “前端” 和 “后端” 逻辑分离了。自此,编辑器/IDE 只需要负责实现 Language Client ,而语言的开发者和维护者只需要负责实现 Language Server 。会大大减小开发者的工作量和用户的体验舒适程度。