diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 0000000..e34fdba --- /dev/null +++ b/.luarc.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://raw.githubusercontent.com/sumneko/vscode-lua/master/setting/schema.json", + "Lua.diagnostics.globals": [ + "vim", + "use" + ] +} \ No newline at end of file diff --git a/init.vim b/init.vim new file mode 100644 index 0000000..c481bce --- /dev/null +++ b/init.vim @@ -0,0 +1,11 @@ +lua require('basic') +lua require('keybindings') +lua require('plugins') + +" 插件配置 +lua require('plugin-config/nvim-tree') +lua require('plugin-config/nvim-treesitter') +lua require('lsp/setup') + +set background=dark +colorscheme nord diff --git a/lua/basic.lua b/lua/basic.lua new file mode 100644 index 0000000..0143b96 --- /dev/null +++ b/lua/basic.lua @@ -0,0 +1,80 @@ +-- utf8 +vim.g.encoding = "UTF-8" +vim.o.fileencoding = 'utf-8' +-- jk移动时光标下上方保留8行 +vim.o.scrolloff = 8 +vim.o.sidescrolloff = 8 +-- 使用相对行号 +vim.wo.number = true +vim.wo.relativenumber = true +-- 高亮所在行 +vim.wo.cursorline = true +-- 显示左侧图标指示列 +vim.wo.signcolumn = "yes" +-- 右侧参考线,超过表示代码太长了,考虑换行 +vim.wo.colorcolumn = "80" +-- 缩进2个空格等于一个Tab +vim.o.tabstop = 2 +vim.bo.tabstop = 2 +vim.o.softtabstop = 2 +vim.o.shiftround = true +-- >> << 时移动长度 +vim.o.shiftwidth = 2 +vim.bo.shiftwidth = 2 +-- 新行对齐当前行,空格替代tab +vim.o.expandtab = true +vim.bo.expandtab = true +vim.o.autoindent = true +vim.bo.autoindent = true +vim.o.smartindent = true +-- 搜索大小写不敏感,除非包含大写 +vim.o.ignorecase = true +vim.o.smartcase = true +-- 搜索不要高亮 +vim.o.hlsearch = false +-- 边输入边搜索 +vim.o.incsearch = true +-- 使用增强状态栏后不再需要 vim 的模式提示 +vim.o.showmode = false +-- 命令行高为2,提供足够的显示空间 +vim.o.cmdheight = 2 +-- 当文件被外部程序修改时,自动加载 +vim.o.autoread = true +vim.bo.autoread = true +-- 禁止折行 +vim.o.wrap = false +vim.wo.wrap = false +-- 行结尾可以跳到下一行 +vim.o.whichwrap = 'b,s,<,>,[,],h,l' +-- 允许隐藏被修改过的buffer +vim.o.hidden = true +-- 鼠标支持 +vim.o.mouse = "a" +-- 禁止创建备份文件 +vim.o.backup = false +vim.o.writebackup = false +vim.o.swapfile = false +-- smaller updatetime +vim.o.updatetime = 300 +-- 设置 timeoutlen 为等待键盘快捷键连击时间200毫秒,可根据需要设置 +-- 遇到问题详见:https://github.com/nshen/learn-neovim-lua/issues/1 +vim.o.timeoutlen = 200 +-- split window 从下边和右边出现 +vim.o.splitbelow = true +vim.o.splitright = true +-- 自动补全不自动选中 +vim.g.completeopt = "menu,menuone,noselect,noinsert" +-- 样式 +vim.o.background = "dark" +vim.o.termguicolors = true +vim.opt.termguicolors = true +-- 不可见字符的显示,这里只把空格显示为一个点 +vim.o.list = true +vim.o.listchars = "space:·" +-- 补全增强 +vim.o.wildmenu = true +-- Dont' pass messages to |ins-completin menu| +vim.o.shortmess = vim.o.shortmess .. 'c' +vim.o.pumheight = 10 +-- always show tabline +vim.o.showtabline = 2 diff --git a/lua/keybindings.lua b/lua/keybindings.lua new file mode 100644 index 0000000..d3321b2 --- /dev/null +++ b/lua/keybindings.lua @@ -0,0 +1,57 @@ +vim.g.mapleader = " " +vim.g.maplocalleader = " " + +local map = vim.api.nvim_set_keymap +local opt = {noremap = true, silent = true } + +map("n", "", "9k", opt) +map("n", "", "9j", opt) + +-- map('v', '<', '', '>gv', opt) + + +map("n", "sv", ":vsp", opt) +map("n", "sh", ":sp", opt) +map("n", "sc", "c", opt) +map("n", "so", "o", opt) -- close others + +map("n", "", "h", opt) +map("n", "", "j", opt) +map("n", "", "k", opt) +map("n", "", "l", opt) + + +-- nvimTree +map('n', 'ct', ':NvimTreeToggle', opt) + + +local pluginKeys = {} + +-- lsp 回调函数快捷键设置 +pluginKeys.maplsp = function(mapbuf) + -- rename + mapbuf('n', 'rn', 'lua vim.lsp.buf.rename()', opt) + -- code action + mapbuf('n', 'ca', 'lua vim.lsp.buf.code_action()', opt) + -- go xx + mapbuf('n', 'gd', 'lua vim.lsp.buf.definition()', opt) + mapbuf('n', 'gh', 'lua vim.lsp.buf.hover()', opt) + mapbuf('n', 'gD', 'lua vim.lsp.buf.declaration()', opt) + mapbuf('n', 'gi', 'lua vim.lsp.buf.implementation()', opt) + mapbuf('n', 'gr', 'lua vim.lsp.buf.references()', opt) + -- diagnostic + mapbuf('n', 'go', 'lua vim.diagnostic.open_float()', opt) + mapbuf('n', 'gp', 'lua vim.diagnostic.goto_prev()', opt) + mapbuf('n', 'gn', 'lua vim.diagnostic.goto_next()', opt) + -- mapbuf('n', 'q', 'lua vim.diagnostic.setloclist()', opt) + -- leader + = + mapbuf('n', '=', 'lua vim.lsp.buf.formatting()', opt) + -- mapbuf('n', '', 'lua vim.lsp.buf.signature_help()', opt) + -- mapbuf('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opt) + -- mapbuf('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opt) + -- mapbuf('n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opt) + -- mapbuf('n', 'D', 'lua vim.lsp.buf.type_definition()', opt) +end + +return pluginKeys diff --git a/lua/lsp/clangd.lua b/lua/lsp/clangd.lua new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/lua/lsp/clangd.lua @@ -0,0 +1 @@ + diff --git a/lua/lsp/lua.lua b/lua/lsp/lua.lua new file mode 100644 index 0000000..e69de29 diff --git a/lua/lsp/setup.lua b/lua/lsp/setup.lua new file mode 100644 index 0000000..c2ca1ad --- /dev/null +++ b/lua/lsp/setup.lua @@ -0,0 +1,38 @@ +local lsp_installer = require "nvim-lsp-installer" + +-- 安装列表 +-- https://github.com/williamboman/nvim-lsp-installer#available-lsps +-- { key: 语言 value: 配置文件 } +local servers = { + sumneko_lua = require "lsp.lua", -- /lua/lsp/lua.lua + clangd = require "lsp.lua" -- /lua/lsp/clangd.lua +} + +-- 自动安装 LanguageServers +for name, _ in pairs(servers) do + local server_is_found, server = lsp_installer.get_server(name) + if server_is_found then + if not server:is_installed() then + print("Installing " .. name) + server:install() + end + end +end + + +lsp_installer.on_server_ready(function(server) + local opts = servers[server.name] + if opts then + opts.on_attach = function(_, bufnr) + local function buf_set_keymap(...) vim.api.nvim_buf_set_keymap(bufnr, ...) end + -- local function buf_set_option(...) vim.api.nvim_buf_set_option(bufnr, ...) end + -- 绑定快捷键 + require('keybindings').maplsp(buf_set_keymap) + end + opts.flags = { + debounce_text_changes = 150, + } + server:setup(opts) + end +end) + diff --git a/lua/plugin-config/nvim-tree.lua b/lua/plugin-config/nvim-tree.lua new file mode 100644 index 0000000..f6a01cc --- /dev/null +++ b/lua/plugin-config/nvim-tree.lua @@ -0,0 +1,7 @@ +require'nvim-tree'.setup { + -- 不显示 git 状态图标 + git = { + enable = false + } +} + diff --git a/lua/plugin-config/nvim-treesitter.lua b/lua/plugin-config/nvim-treesitter.lua new file mode 100644 index 0000000..edd9ad4 --- /dev/null +++ b/lua/plugin-config/nvim-treesitter.lua @@ -0,0 +1,30 @@ +require'nvim-treesitter.configs'.setup { + -- 安装 language parser + -- :TSInstallInfo 命令查看支持的语言 + ensure_installed = {"html", "css", "lua", "javascript", "typescript", "tsx", "cpp", "c"}, + -- 启用代码高亮功能 + highlight = { + enable = true, + additional_vim_regex_highlighting = false + }, + -- 启用增量选择 + incremental_selection = { + enable = true, + keymaps = { + init_selection = '', + node_incremental = '', + node_decremental = '', + scope_incremental = '', + } + }, + -- 启用基于Treesitter的代码格式化(=) . NOTE: This is an experimental feature. + indent = { + enable = true + } +} +-- 开启 Folding +vim.wo.foldmethod = 'expr' +vim.wo.foldexpr = 'nvim_treesitter#foldexpr()' +-- 默认不要折叠 +-- https://stackoverflow.com/questions/8316139/how-to-set-the-default-to-unfolded-when-you-open-a-file +vim.wo.foldlevel = 99 diff --git a/lua/plugins.lua b/lua/plugins.lua new file mode 100644 index 0000000..151b80d --- /dev/null +++ b/lua/plugins.lua @@ -0,0 +1,32 @@ +return require('packer').startup(function() + -- Packer can manage itself + use 'wbthomason/packer.nvim' + -- gruvbox theme + use { + "ellisonleao/gruvbox.nvim", + requires = {"rktjmp/lush.nvim"} + } + use 'shaunsingh/nord.nvim' + use 'glepnir/zephyr-nvim' + -- nvim-tree (新增) + use { + 'kyazdani42/nvim-tree.lua', + requires = 'kyazdani42/nvim-web-devicons' + } + -- treesitter + use { 'nvim-treesitter/nvim-treesitter', run = ':TSUpdate' } + -- lspconfig + use {'neovim/nvim-lspconfig', 'williamboman/nvim-lsp-installer'} + -- nvim-cmp + use 'hrsh7th/cmp-nvim-lsp' -- { name = nvim_lsp } + use 'hrsh7th/cmp-buffer' -- { name = 'buffer' }, + use 'hrsh7th/cmp-path' -- { name = 'path' } + use 'hrsh7th/cmp-cmdline' -- { name = 'cmdline' } + use 'hrsh7th/nvim-cmp' + -- vsnip + use 'hrsh7th/cmp-vsnip' -- { name = 'vsnip' } + use 'hrsh7th/vim-vsnip' + use 'rafamadriz/friendly-snippets' + -- lspkind + use 'onsails/lspkind-nvim' +end) diff --git a/plugin/packer_compiled.lua b/plugin/packer_compiled.lua new file mode 100644 index 0000000..56c3250 --- /dev/null +++ b/plugin/packer_compiled.lua @@ -0,0 +1,189 @@ +-- Automatically generated packer.nvim plugin loader code + +if vim.api.nvim_call_function('has', {'nvim-0.5'}) ~= 1 then + vim.api.nvim_command('echohl WarningMsg | echom "Invalid Neovim version for packer.nvim! | echohl None"') + return +end + +vim.api.nvim_command('packadd packer.nvim') + +local no_errors, error_msg = pcall(function() + +_G._packer = _G._packer or {} +_G._packer.inside_compile = true + +local time +local profile_info +local should_profile = false +if should_profile then + local hrtime = vim.loop.hrtime + profile_info = {} + time = function(chunk, start) + if start then + profile_info[chunk] = hrtime() + else + profile_info[chunk] = (hrtime() - profile_info[chunk]) / 1e6 + end + end +else + time = function(chunk, start) end +end + +local function save_profiles(threshold) + local sorted_times = {} + for chunk_name, time_taken in pairs(profile_info) do + sorted_times[#sorted_times + 1] = {chunk_name, time_taken} + end + table.sort(sorted_times, function(a, b) return a[2] > b[2] end) + local results = {} + for i, elem in ipairs(sorted_times) do + if not threshold or threshold and elem[2] > threshold then + results[i] = elem[1] .. ' took ' .. elem[2] .. 'ms' + end + end + if threshold then + table.insert(results, '(Only showing plugins that took longer than ' .. threshold .. ' ms ' .. 'to load)') + end + + _G._packer.profile_output = results +end + +time([[Luarocks path setup]], true) +local package_path_str = "/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?.lua;/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/share/lua/5.1/?/init.lua;/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?.lua;/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/luarocks/rocks-5.1/?/init.lua" +local install_cpath_pattern = "/Users/yuhangq/.cache/nvim/packer_hererocks/2.1.0-beta3/lib/lua/5.1/?.so" +if not string.find(package.path, package_path_str, 1, true) then + package.path = package.path .. ';' .. package_path_str +end + +if not string.find(package.cpath, install_cpath_pattern, 1, true) then + package.cpath = package.cpath .. ';' .. install_cpath_pattern +end + +time([[Luarocks path setup]], false) +time([[try_loadstring definition]], true) +local function try_loadstring(s, component, name) + local success, result = pcall(loadstring(s), name, _G.packer_plugins[name]) + if not success then + vim.schedule(function() + vim.api.nvim_notify('packer.nvim: Error running ' .. component .. ' for ' .. name .. ': ' .. result, vim.log.levels.ERROR, {}) + end) + end + return result +end + +time([[try_loadstring definition]], false) +time([[Defining packer_plugins]], true) +_G.packer_plugins = { + ["cmp-buffer"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-buffer", + url = "https://github.com/hrsh7th/cmp-buffer" + }, + ["cmp-cmdline"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-cmdline", + url = "https://github.com/hrsh7th/cmp-cmdline" + }, + ["cmp-nvim-lsp"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-nvim-lsp", + url = "https://github.com/hrsh7th/cmp-nvim-lsp" + }, + ["cmp-path"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-path", + url = "https://github.com/hrsh7th/cmp-path" + }, + ["cmp-vsnip"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/cmp-vsnip", + url = "https://github.com/hrsh7th/cmp-vsnip" + }, + ["friendly-snippets"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/friendly-snippets", + url = "https://github.com/rafamadriz/friendly-snippets" + }, + ["gruvbox.nvim"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/gruvbox.nvim", + url = "https://github.com/ellisonleao/gruvbox.nvim" + }, + ["lspkind-nvim"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/lspkind-nvim", + url = "https://github.com/onsails/lspkind-nvim" + }, + ["lush.nvim"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/lush.nvim", + url = "https://github.com/rktjmp/lush.nvim" + }, + ["nord.nvim"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nord.nvim", + url = "https://github.com/shaunsingh/nord.nvim" + }, + ["nvim-cmp"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-cmp", + url = "https://github.com/hrsh7th/nvim-cmp" + }, + ["nvim-lsp-installer"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-lsp-installer", + url = "https://github.com/williamboman/nvim-lsp-installer" + }, + ["nvim-lspconfig"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-lspconfig", + url = "https://github.com/neovim/nvim-lspconfig" + }, + ["nvim-tree.lua"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-tree.lua", + url = "https://github.com/kyazdani42/nvim-tree.lua" + }, + ["nvim-treesitter"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-treesitter", + url = "https://github.com/nvim-treesitter/nvim-treesitter" + }, + ["nvim-web-devicons"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/nvim-web-devicons", + url = "https://github.com/kyazdani42/nvim-web-devicons" + }, + ["packer.nvim"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/packer.nvim", + url = "https://github.com/wbthomason/packer.nvim" + }, + ["vim-vsnip"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/vim-vsnip", + url = "https://github.com/hrsh7th/vim-vsnip" + }, + ["zephyr-nvim"] = { + loaded = true, + path = "/Users/yuhangq/.local/share/nvim/site/pack/packer/start/zephyr-nvim", + url = "https://github.com/glepnir/zephyr-nvim" + } +} + +time([[Defining packer_plugins]], false) + +_G._packer.inside_compile = false +if _G._packer.needs_bufread == true then + vim.cmd("doautocmd BufRead") +end +_G._packer.needs_bufread = false + +if should_profile then save_profiles() end + +end) + +if not no_errors then + error_msg = error_msg:gsub('"', '\\"') + vim.api.nvim_command('echohl ErrorMsg | echom "Error in packer_compiled: '..error_msg..'" | echom "Please check your config for correctness" | echohl None') +end diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..1cc9a08 --- /dev/null +++ b/test.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; +int main() { + printf("Hello World!"); + for(int i=0; i<=10; i++) { + : + } +}