From afb9b5923505574c495af3288cc2dac0dc29784a Mon Sep 17 00:00:00 2001 From: maye Date: Tue, 20 May 2025 18:36:51 +0800 Subject: [PATCH] init --- init.lua | 5 ++ lazy-lock.json | 15 +++++ lsp/gopls.lua | 28 ++++++++ lua/commons/init.lua | 29 +++++++++ lua/configs/lazy.lua | 22 +++++++ lua/configs/mappings.lua | 2 + lua/configs/options.lua | 125 ++++++++++++++++++++++++++++++++++++ lua/functions/lsp.lua | 23 +++++++ lua/plugins/autopairs.lua | 8 +++ lua/plugins/blink.lua | 97 ++++++++++++++++++++++++++++ lua/plugins/colorscheme.lua | 16 +++++ lua/plugins/conform.lua | 32 +++++++++ lua/plugins/gitsigns.lua | 10 +++ lua/plugins/lspconfig.lua | 7 ++ lua/plugins/mason.lua | 3 + lua/plugins/nvim-tree.lua | 36 +++++++++++ lua/plugins/treesitter.lua | 33 ++++++++++ lua/plugins/which-key.lua | 17 +++++ lua/utils/plugins.lua | 16 +++++ 19 files changed, 524 insertions(+) create mode 100644 init.lua create mode 100644 lazy-lock.json create mode 100644 lsp/gopls.lua create mode 100644 lua/commons/init.lua create mode 100644 lua/configs/lazy.lua create mode 100644 lua/configs/mappings.lua create mode 100644 lua/configs/options.lua create mode 100644 lua/functions/lsp.lua create mode 100644 lua/plugins/autopairs.lua create mode 100644 lua/plugins/blink.lua create mode 100644 lua/plugins/colorscheme.lua create mode 100644 lua/plugins/conform.lua create mode 100644 lua/plugins/gitsigns.lua create mode 100644 lua/plugins/lspconfig.lua create mode 100644 lua/plugins/mason.lua create mode 100644 lua/plugins/nvim-tree.lua create mode 100644 lua/plugins/treesitter.lua create mode 100644 lua/plugins/which-key.lua create mode 100644 lua/utils/plugins.lua diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..5e80811 --- /dev/null +++ b/init.lua @@ -0,0 +1,5 @@ +require('configs.options') +require('configs.mappings') +require('configs.lazy') + +require("functions.lsp") diff --git a/lazy-lock.json b/lazy-lock.json new file mode 100644 index 0000000..622e9eb --- /dev/null +++ b/lazy-lock.json @@ -0,0 +1,15 @@ +{ + "blink.cmp": { "branch": "main", "commit": "c2bac7fba61e66dfc513cf63daa98546c86a617c" }, + "conform.nvim": { "branch": "master", "commit": "a4bb5d6c4ae6f32ab13114e62e70669fa67745b9" }, + "friendly-snippets": { "branch": "main", "commit": "572f5660cf05f8cd8834e096d7b4c921ba18e175" }, + "gitsigns.nvim": { "branch": "main", "commit": "ee0606259ee5d5dd40398be26755048e8965086e" }, + "gruvbox.nvim": { "branch": "main", "commit": "00e38a379bab3389e187b3953566d67d494dfddd" }, + "lazy.nvim": { "branch": "main", "commit": "6c3bda4aca61a13a9c63f1c1d1b16b9d3be90d7a" }, + "mason.nvim": { "branch": "main", "commit": "1aceba8bc158b5aaf90649077cad06744bc23ac4" }, + "nvim-autopairs": { "branch": "master", "commit": "4d74e75913832866aa7de35e4202463ddf6efd1b" }, + "nvim-lspconfig": { "branch": "master", "commit": "ac1dfbe3b60e5e23a2cff90e3bd6a3bc88031a57" }, + "nvim-tree.lua": { "branch": "master", "commit": "be5b788f2dc1522c73fb7afad9092331c8aebe80" }, + "nvim-treesitter": { "branch": "master", "commit": "066fd6505377e3fd4aa219e61ce94c2b8bdb0b79" }, + "nvim-web-devicons": { "branch": "master", "commit": "1fb58cca9aebbc4fd32b086cb413548ce132c127" }, + "which-key.nvim": { "branch": "main", "commit": "370ec46f710e058c9c1646273e6b225acf47cbed" } +} diff --git a/lsp/gopls.lua b/lsp/gopls.lua new file mode 100644 index 0000000..f804754 --- /dev/null +++ b/lsp/gopls.lua @@ -0,0 +1,28 @@ +---@type vim.lsp.Config +return { + cmd = { "gopls" }, + filetypes = { "go", "gomod", "gosum" }, + root_markers = { + "go.mod", + "go.sum", + }, + settings = { + gopls = { + analyses = { + unusedparams = true, + }, + hints = { + assignVariableTypes = true, + compositeLiteralFields = true, + compositeLiteralTypes = true, + constantValues = true, + functionTypeParameters = true, + parameterNames = true, + rangeVariableTypes = true, + }, + staticcheck = true, + gofumpt = true, + semanticTokens = true, + }, + }, +} diff --git a/lua/commons/init.lua b/lua/commons/init.lua new file mode 100644 index 0000000..5eea63c --- /dev/null +++ b/lua/commons/init.lua @@ -0,0 +1,29 @@ +local M = { + constants = { + big_file_size_limit = 50 * 1024, -- 50KB + }, + servers = { + "lua_ls", + "gopls", + "clangd", + "bashls", + "pyright", + "yamlls", + "ts_ls", + }, + tools = { + is_version_gte_0_11 = function() + local version = vim.version() + + if version.major > 0 then + return true + elseif version.major == 0 then + return version.minor >= 11 + end + + return false + end, + }, +} + +return M diff --git a/lua/configs/lazy.lua b/lua/configs/lazy.lua new file mode 100644 index 0000000..98bd8bb --- /dev/null +++ b/lua/configs/lazy.lua @@ -0,0 +1,22 @@ +local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim" +if not vim.loop.fs_stat(lazypath) then + vim.fn.system({ + "git", + "clone", + "--filter=blob:none", + "https://github.com/folke/lazy.nvim.git", + "--branch=stable", -- latest stable release + lazypath, + }) +end +vim.opt.rtp:prepend(lazypath) + +require("utils.plugins").setup() + +require("lazy").setup({ + spec = { + { import = "plugins" }, + }, + install = { colorscheme = { "gruvbox" } }, + checker = { enabled = false }, +}) diff --git a/lua/configs/mappings.lua b/lua/configs/mappings.lua new file mode 100644 index 0000000..ad3aa5e --- /dev/null +++ b/lua/configs/mappings.lua @@ -0,0 +1,2 @@ +local map = vim.keymap.set +map("n", "s", ":w", {}) \ No newline at end of file diff --git a/lua/configs/options.lua b/lua/configs/options.lua new file mode 100644 index 0000000..da1e5d3 --- /dev/null +++ b/lua/configs/options.lua @@ -0,0 +1,125 @@ +local cmd = vim.cmd +local opt = vim.opt +local g = vim.g +local s = vim.s +local indent = 4 + +g.mapleader = " " + +cmd([[ + filetype plugin indent on +]]) + +opt.backspace = { "eol", "start", "indent" } -- allow backspacing over everything in insert mode +-- opt.clipboard = "unnamedplus" -- allow neovim to access the system clipboard +vim.opt.fileencoding = "utf-8" +opt.encoding = "utf-8" +opt.matchpairs = { "(:)", "{:}", "[:]", "<:>" } +opt.syntax = "enable" + +-- indention +opt.autoindent = true +opt.expandtab = true +opt.shiftwidth = indent +opt.smartindent = true +opt.softtabstop = indent +opt.tabstop = indent +opt.shiftround = true + +-- search +opt.hlsearch = true +opt.ignorecase = true +opt.smartcase = true +opt.wildignore = opt.wildignore + { "*/node_modules/*", "*/.git/*", "*/vendor/*" } +opt.wildmenu = true + +-- ui +opt.cursorline = true +opt.laststatus = 2 +opt.lazyredraw = true +opt.list = true +opt.listchars = { + tab = "┊ ", + trail = "·", + extends = "»", + precedes = "«", + nbsp = "×", +} + +-- Hide cmd line +opt.cmdheight = 0 + +opt.mouse = "a" +opt.number = true +opt.relativenumber = true +opt.scrolloff = 18 +opt.sidescrolloff = 3 +opt.signcolumn = "yes" +opt.splitbelow = true +opt.splitright = true +opt.wrap = false + +-- backups +opt.backup = false -- create a backup file +opt.swapfile = false -- creates a swapfile +opt.writebackup = false -- if a file is being edited by another program (or was written to file while editing with another program), it is not allowed to be edited + +-- autocomplete +opt.completeopt = { "menu", "menuone", "noselect" } -- mostly just for cmp +opt.shortmess = opt.shortmess + { + c = true, +} -- hide all the completion messages, e.g. "-- XXX completion (YYY)", "match 1 of 2", "The only match", "Pattern not found" + +opt.showmode = false + +-- perfomance +-- remember N lines in history +opt.history = 100 -- keep 100 lines of history +opt.redrawtime = 1500 +opt.timeoutlen = 250 -- time to wait for a mapped sequence to complete (in milliseconds) +opt.ttimeoutlen = 10 +opt.updatetime = 100 -- signify default updatetime 4000ms is not good for async update + +-- theme +opt.termguicolors = true -- enable 24-bit RGB colors + +-- persistent undo +-- Don"t forget to create folder $HOME/.local/share/nvim/undo +local undodir = vim.fn.stdpath("data") .. "/undo" +opt.undofile = true -- enable persistent undo +opt.undodir = undodir +opt.undolevels = 1000 +opt.undoreload = 10000 + +-- fold +opt.foldmethod = "marker" +opt.foldlevel = 99 + +-- Disable builtin plugins +local disabled_built_ins = { + "2html_plugin", + "getscript", + "getscriptPlugin", + "gzip", + "logipat", + "netrw", + "netrwPlugin", + "netrwSettings", + "netrwFileHandlers", + "matchit", + "rrhelper", + "spellfile_plugin", + "vimball", + "vimballPlugin", + "tutor", + "rplugin", + "synmenu", + "optwin", + "compiler", + "bugreport", + "ftplugin", +} + +for _, plugin in pairs(disabled_built_ins) do + g["loaded_" .. plugin] = 1 +end diff --git a/lua/functions/lsp.lua b/lua/functions/lsp.lua new file mode 100644 index 0000000..1db565e --- /dev/null +++ b/lua/functions/lsp.lua @@ -0,0 +1,23 @@ +if require("commons").tools.is_version_gte_0_11() then + for _, server in pairs(require("commons").servers) do + local ok, settings = pcall(require, "lsp." .. server) + if ok then + vim.lsp.config(server, settings) + end + vim.lsp.enable(server) + end + vim.diagnostic.config({ + update_in_insert = true, + severity_sort = true, + float = { source = true }, + virtual_text = true, + }) +else + local lsp = require("lspconfig") + for _,server in pairs(require("commons").servers) do + local ok,settings = pcall(require,"lsp."..server) + if ok then + lsp[server].setup(settings) + end + end +end diff --git a/lua/plugins/autopairs.lua b/lua/plugins/autopairs.lua new file mode 100644 index 0000000..fabcb35 --- /dev/null +++ b/lua/plugins/autopairs.lua @@ -0,0 +1,8 @@ +return { + "windwp/nvim-autopairs", + event = "InsertEnter", + config = function() + local npairs = require("nvim-autopairs") + npairs.setup() + end, +} diff --git a/lua/plugins/blink.lua b/lua/plugins/blink.lua new file mode 100644 index 0000000..d2b6604 --- /dev/null +++ b/lua/plugins/blink.lua @@ -0,0 +1,97 @@ +local not_enabled_filetype = { "markdown" } + +---@module 'blink.cmp' +---@type blink.cmp.Config +local opts = { + cmdline = { enabled = true }, + keymap = { preset = "super-tab" }, + completion = { + documentation = { + auto_show = true, + treesitter_highlighting = true, + }, + keyword = { + range = "prefix", + }, + list = { + selection = { + preselect = true, + auto_insert = false, + }, + }, + accept = { + dot_repeat = false, + create_undo_point = true, + auto_brackets = { enabled = true }, + }, + menu = { + draw = { + columns = { { "label", "label_detail", gap = 1 }, { "kind_icon", "kind", gap = 1 } }, + components = { + label = { + width = { max = 30, fill = true }, + text = function(ctx) + return ctx.label + end, + }, + label_detail = { + width = { fill = true, max = 15 }, + text = function(ctx) + return ctx.label_detail + end, + }, + source_name = {}, + }, + }, + }, + }, + appearance = { + nerd_font_variant = "mono", + }, + sources = { + default = { "lsp", "path", "snippets", "buffer" }, + providers = { + buffer = { + enabled = true, + }, + snippets = { + enabled = false, + }, + }, + }, + fuzzy = { + implementation = "rust", + use_frecency = false, + use_proximity = false, + max_typos = function(_) + return 0 + end, + prebuilt_binaries = { + download = false, + ignore_version_mismatch = true, + }, + }, + signature = { enabled = true }, +} + +local setup = function(_, opts) + return opts +end + +return { + "saghen/blink.cmp", + opts_extend = { "sources.completion.enabled_providers", "sources.compat", "sources.default" }, + dependencies = { + "rafamadriz/friendly-snippets", + { "saghen/blink.compat", optional = true, opts = {}, version = not vim.g.lazyvim_blink_main and "*" }, + }, + event = "InsertEnter", + version = "*", + enabled = function() + return not vim.tbl_contains(not_enabled_filetype, vim.bo.filetype) + and vim.bo.buftype ~= "prompt" + and vim.b.completion ~= false + end, + opts = opts, + config = setup, +} diff --git a/lua/plugins/colorscheme.lua b/lua/plugins/colorscheme.lua new file mode 100644 index 0000000..a9b8eae --- /dev/null +++ b/lua/plugins/colorscheme.lua @@ -0,0 +1,16 @@ +return { + "ellisonleao/gruvbox.nvim", + priority = 1000, + lazy = false, + config = function() + require("gruvbox").setup({ + italic = { + strings = false, + operators = false, + folds = false, + emphasis = false, + }, + }) + vim.cmd.colorscheme("gruvbox") + end, +} \ No newline at end of file diff --git a/lua/plugins/conform.lua b/lua/plugins/conform.lua new file mode 100644 index 0000000..71e74e6 --- /dev/null +++ b/lua/plugins/conform.lua @@ -0,0 +1,32 @@ +return { + "stevearc/conform.nvim", + lazy = true, + cmd = "ConformInfo", + keys = { + { + "ff", + function() + require("conform").format() + end, + mode = { "n", "v" }, + desc = "Use Conform to format current buffer", + }, + }, + opts = { + default_format_opts = { + timeout_ms = 3000, + async = false, + quiet = false, + lsp_format = "fallback", + }, + formatters_by_ft = { + lua = { "stylua" }, + python = { "black" }, + rust = { "rustfmt" }, + javascript = { "prettier", stop_after_first = true }, + sh = { "shfmt" }, + }, + formatters = { injected = { options = { ignore_errors = true } } }, + format_on_save = { timeout_ms = 500, lsp_format = "fallback" }, + }, +} diff --git a/lua/plugins/gitsigns.lua b/lua/plugins/gitsigns.lua new file mode 100644 index 0000000..d5dbc4d --- /dev/null +++ b/lua/plugins/gitsigns.lua @@ -0,0 +1,10 @@ +return { + "lewis6991/gitsigns.nvim", + key = {}, + opts = { + current_line_blame = true, + current_line_blame_opts = {delay = 100}, + on_attach = function(bufnr) + end + }, +} diff --git a/lua/plugins/lspconfig.lua b/lua/plugins/lspconfig.lua new file mode 100644 index 0000000..008222a --- /dev/null +++ b/lua/plugins/lspconfig.lua @@ -0,0 +1,7 @@ +return { + "neovim/nvim-lspconfig", +-- cond = not require("commons").tools.is_version_gte_0_11(), + dependencies = { { "saghen/blink.cmp" } }, + config = function() + end, +} diff --git a/lua/plugins/mason.lua b/lua/plugins/mason.lua new file mode 100644 index 0000000..90c8fd6 --- /dev/null +++ b/lua/plugins/mason.lua @@ -0,0 +1,3 @@ +return { + "mason-org/mason.nvim", +} diff --git a/lua/plugins/nvim-tree.lua b/lua/plugins/nvim-tree.lua new file mode 100644 index 0000000..bb43710 --- /dev/null +++ b/lua/plugins/nvim-tree.lua @@ -0,0 +1,36 @@ +return { + "nvim-tree/nvim-tree.lua", + version = "*", + lazy = false, + dependencies = { "nvim-tree/nvim-web-devicons" }, + keys = { + { + "n", + ":NvimTreeToggle", + mode = { "n" }, + desc = "Use Conform to format current buffer", + }, + }, + config = function() + vim.g.loaded_netrw = 1 + vim.g.loaded_netrwPlugin = 1 + vim.opt.termguicolors = true + + require("nvim-tree").setup({ + diagnostics = { + enable = true, + show_on_dirs = true, + }, + filters = { + git_ignored = false, + }, + renderer = { + indent_markers = { enable = true }, + }, + view = { + number = true, + relativenumber = true, + }, + }) + end, +} diff --git a/lua/plugins/treesitter.lua b/lua/plugins/treesitter.lua new file mode 100644 index 0000000..547648a --- /dev/null +++ b/lua/plugins/treesitter.lua @@ -0,0 +1,33 @@ +return { + "nvim-treesitter/nvim-treesitter", + build = function() + require("nvim-treesitter.install").update({ with_sync = true })() + end, + config = function() + local configs = require("nvim-treesitter.configs") + configs.setup({ + ensure_installed = { + 'html', + 'json', + 'cmake', + 'make', + 'python', + 'go', + 'rust', + 'yaml', + 'bash' + }, + highlight = { + enable = true, + disable = function(lang,buf) + local ok,stats = pcall(vim.uv.fs_stat, vim.api.nvim_buf_get_name(buf)) + if ok and stats and stats.size > require("commons").constants.big_file_size_limit then + return true + end + end, + }, + incremental_selection = {enable = true}, + indent = {enable = true}, + }) + end +} diff --git a/lua/plugins/which-key.lua b/lua/plugins/which-key.lua new file mode 100644 index 0000000..b3097d3 --- /dev/null +++ b/lua/plugins/which-key.lua @@ -0,0 +1,17 @@ +return { + "folke/which-key.nvim", + event = "VeryLazy", + opts = { + spec = { + {"", desc = "Decrement Selection", mode = "x"}, + {"", desc = "Increment Selection", mode = {"x", "n"}} + } + }, + keys = { + { + "?", + function() require("which-key").show({global = false}) end, + desc = "Buffer Local Keymaps (which-key)" + } + } +} diff --git a/lua/utils/plugins.lua b/lua/utils/plugins.lua new file mode 100644 index 0000000..77f868d --- /dev/null +++ b/lua/utils/plugins.lua @@ -0,0 +1,16 @@ +local M = {} + +M.lazy_file_events = { "BufReadPost", "BufNewFile", "BufWritePre" } + +function M.setup() + M.lazy_file() +end + +function M.lazy_file() + local Event = require("lazy.core.handler.event") + + Event.mappings.LazyFile = {id = "LazyFile", event = M.lazy_file_events} + Event.mappings["User LazyFile"] = Event.mappings.LazyFile +end + +return M