From f9d699c215d4c8a22d64a5712b007ad583434862 Mon Sep 17 00:00:00 2001 From: Kenneth Date: Fri, 3 May 2024 15:25:40 +0100 Subject: [PATCH] add neovim files --- README.md | 1 + nvim/init.lua | 462 ++++++++++++++++++++++++++++++++++++++++++++ nvim/lazy-lock.json | 29 +++ 3 files changed, 492 insertions(+) create mode 100644 nvim/init.lua create mode 100644 nvim/lazy-lock.json diff --git a/README.md b/README.md index bfb6942..ee6bd7d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ my dotfiles. includes: - [starship](https://starship.rs) +- [neovim](https://neovim.io/) # starship diff --git a/nvim/init.lua b/nvim/init.lua new file mode 100644 index 0000000..bf2d862 --- /dev/null +++ b/nvim/init.lua @@ -0,0 +1,462 @@ +function init_lazy_nvim() + 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("lazy").setup(PLUGINS, opts) +end + +function define_keymaps() + -- space+f to open telescope browser in normal mode + vim.api.nvim_set_keymap("n", "f", ":NvimTreeToggle", { noremap = true }) + + -- space+l to open lazy plugin mgr in normal mode + vim.api.nvim_set_keymap("n", "l", ":Lazy home", { noremap = true }) + + vim.api.nvim_set_keymap("n", "t", ":FloatermToggle", { noremap = true }) + + -- keymap for nvim-lspconfig + -- Global mappings. + -- See `:help vim.diagnostic.*` for documentation on any of the below functions + vim.keymap.set("n", "e", vim.diagnostic.open_float) + vim.keymap.set("n", "[d", vim.diagnostic.goto_prev) + vim.keymap.set("n", "]d", vim.diagnostic.goto_next) + vim.keymap.set("n", "q", vim.diagnostic.setloclist) + vim.keymap.set("n", "a", vim.lsp.buf.code_action) + + -- Use LspAttach autocommand to only map the following keys + -- after the language server attaches to the current buffer + vim.api.nvim_create_autocmd("LspAttach", { + group = vim.api.nvim_create_augroup("UserLspConfig", {}), + callback = function(ev) + -- Enable completion triggered by + vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" + + -- Buffer local mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + local opts = { buffer = ev.buf } + vim.keymap.set("n", "D", vim.lsp.buf.declaration, opts) + vim.keymap.set("n", "d", vim.lsp.buf.definition, opts) + vim.keymap.set("n", "K", vim.lsp.buf.hover, opts) + vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts) + vim.keymap.set("n", "", vim.lsp.buf.signature_help, opts) + vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, opts) + vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, opts) + vim.keymap.set("n", "wl", function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, opts) + vim.keymap.set("n", "r", vim.lsp.buf.rename, opts) + vim.keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, opts) + vim.keymap.set("n", "gr", vim.lsp.buf.references, opts) + + vim.keymap.set("n", "h", ":ClangdSwitchSourceHeader", { noremap = true }) + end, + }) + + local builtin = require("telescope.builtin") + vim.keymap.set("n", "F", builtin.find_files, {}) + vim.keymap.set("n", "g", builtin.live_grep, {}) + vim.keymap.set("n", "b", builtin.buffers, {}) +end + +function setup_plugins() + require("nvim-treesitter.configs").setup({ + -- A list of parser names, or "all" (the five listed parsers should always be installed) + ensure_installed = { "c", "lua", "vim", "vimdoc" }, + + -- Install parsers synchronously (only applied to `ensure_installed`) + sync_install = false, + + -- Automatically install missing parsers when entering buffer + -- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally + auto_install = true, + + -- List of parsers to ignore installing (or "all") + ignore_install = { "javascript" }, + + ---- If you need to change the installation directory of the parsers (see -> Advanced Setup) + -- parser_install_dir = "/some/path/to/store/parsers", -- Remember to run vim.opt.runtimepath:append("/some/path/to/store/parsers")! + + highlight = { + enable = true, + + -- Setting this to true will run `:h syntax` and tree-sitter at the same time. + -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation). + -- Using this option may slow down your editor, and you may see some duplicate highlights. + -- Instead of true it can also be a list of languages + additional_vim_regex_highlighting = false, + }, + }) + + require("rose-pine").setup({ + styles = { + italic = false, + }, + highlight_groups = { EndOfBuffer = { fg = "base" } }, + }) + + require("auto-dark-mode").setup({ + set_dark_mode = function() + vim.api.nvim_set_option("background", "dark") + vim.cmd("colorscheme nightfox") + end, + + set_light_mode = function() + vim.api.nvim_set_option("background", "light") + vim.cmd("colorscheme dayfox") + end, + }) + + require("no-neck-pain").setup({ + width = 120, + fallbackOnBufferDelete = true, + autocmds = { + enableOnVimEnter = true, + reloadOnColorSchemeChange = true, + }, + }) + + require("nvim-tree").setup({ + disable_netrw = true, + hijack_netrw = true, + respect_buf_cwd = true, + sync_root_with_cwd = true, + filters = { dotfiles = false, custom = { "^.git$" } }, + view = { + relativenumber = true, + float = { + enable = true, + open_win_config = function() + local screen_w = vim.opt.columns:get() + local screen_h = vim.opt.lines:get() - vim.opt.cmdheight:get() + local window_w = screen_w * 0.5 + local window_h = screen_h * 0.5 + local window_w_int = math.floor(window_w) + local window_h_int = math.floor(window_h) + local center_x = (screen_w - window_w) / 2 + local center_y = ((vim.opt.lines:get() - window_h) / 2) - vim.opt.cmdheight:get() + return { + border = "rounded", + relative = "editor", + row = center_y, + col = center_x, + width = window_w_int, + height = window_h_int, + } + end, + }, + width = function() + return math.floor(vim.opt.columns:get() * 0.5) + end, + }, + }) + + require("telescope").setup({ + defaults = { + file_ignore_patterns = { "node_modules" }, + }, + }) + + require("nvim-web-devicons").setup() + + local lsps = { + lua_ls = { + settings = { + Lua = { + diagnostics = { + globals = { "vim" }, + }, + completion = { + callSnippet = "Replace", + }, + -- You can toggle below to ignore Lua_LS's noisy missing-fields warnings + -- diagnostics = { disable = { 'missing-fields' } }, + }, + }, + }, + + dartls = { + cmd = { "dart", "language-server", "--protocol=lsp" }, + }, + } + + require("mason").setup() + require("mason-lspconfig").setup() + require("mason-lspconfig").setup_handlers({ + function(server_name) + local server = lsps[server_name] or {} + require("lspconfig")[server_name].setup(server) + end, + }) + + require("lualine").setup({ + options = { + icons_enabled = true, + component_separators = "", + section_separators = { left = "", right = "" }, + disabled_filetypes = { + statusline = {}, + winbar = {}, + }, + ignore_focus = {}, + always_divide_middle = true, + globalstatus = true, + refresh = { + statusline = 1000, + tabline = 1000, + winbar = 1000, + }, + }, + sections = { + lualine_a = { "mode" }, + lualine_b = { "branch", "diff" }, + lualine_c = {}, + lualine_x = {}, + lualine_y = { "os.date('%a %b %d %H:%M')" }, + lualine_z = {}, + }, + inactive_sections = { + lualine_a = {}, + lualine_b = {}, + lualine_c = {}, + lualine_x = { "location" }, + lualine_y = {}, + lualine_z = {}, + }, + tabline = {}, + winbar = { + lualine_a = {}, + lualine_b = { "filename" }, + lualine_c = {}, + lualine_x = { "searchcount", "encoding", "diagnostics" }, + lualine_y = { "filetype" }, + lualine_z = {}, + }, + inactive_winbar = {}, + extensions = {}, + }) + + require("formatter").setup({ + filetype = { + javascript = { + require("formatter.filetypes.javascript").biome, + }, + javascriptreact = { + require("formatter.filetypes.javascriptreact").biome, + }, + typescript = { + require("formatter.filetypes.typescript").biome, + }, + typescriptreact = { + require("formatter.filetypes.typescriptreact").biome, + }, + json = { + require("formatter.filetypes.json").biome, + }, + lua = { require("formatter.filetypes.lua").stylua }, + cpp = { require("formatter.filetypes.cpp").clangformat }, + dart = { require("formatter.filetypes.dart").dartformat }, + python = { require("formatter.filetypes.python").black }, + }, + }) + + local cmp = require("cmp") + cmp.setup({ + window = { + completion = { + winhighlight = "Normal:CmpNormal,FloatBorder:CmpNormal,Search:None", + side_padding = 0, + border = "rounded", + scrollbar = "║", + }, + }, + + formatting = { + fields = { "kind", "abbr", "menu" }, + format = function(entry, vim_item) + local kind = require("lspkind").cmp_format({ + mode = "symbol_text", + maxwidth = 50, + symbol_map = { + Text = "󰦨", + Variable = "󰫧", + Keyword = "", + Field = "", + }, + })(entry, vim_item) + local strings = vim.split(kind.kind, "%s", { trimempty = true }) + kind.kind = " " .. (strings[1] or "") .. " " + kind.menu = " [" .. (strings[2] or "") .. "]" + + return kind + end, + }, + + snippet = { + expand = function(args) + require("luasnip").lsp_expand(args.body) -- For `luasnip` users. + end, + }, + + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.abort(), + -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. + [""] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping.select_prev_item(), + [""] = cmp.mapping.select_next_item(), + }), + + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + { name = "luasnip" }, + { name = "buffer" }, + }), + }) + + require("gitsigns").setup({ + on_attach = function(bufnr) + local gitsigns = require("gitsigns") + + local function map(mode, l, r, opts) + opts = opts or {} + opts.buffer = bufnr + vim.keymap.set(mode, l, r, opts) + end + + -- Navigation + map("n", "]h", function() + if vim.wo.diff then + vim.cmd.normal({ "]h", bang = true }) + else + gitsigns.nav_hunk("next") + end + end) + + map("n", "[h", function() + if vim.wo.diff then + vim.cmd.normal({ "[h", bang = true }) + else + gitsigns.nav_hunk("prev") + end + end) + + map("n", "hr", gitsigns.reset_hunk) + map("n", "hp", gitsigns.preview_hunk) + end, + }) +end + +function config_vim() + vim.cmd("colorscheme nightfox") + + vim.opt.tabstop = 2 + vim.opt.shiftwidth = 2 + vim.opt.number = true + vim.opt.relativenumber = true + vim.opt.laststatus = 3 + vim.opt.pumheight = 10 + vim.opt.scrolloff = 10 + vim.opt.listchars = {} + + vim.filetype.add({ + extension = { + typ = "typst", + }, + }) + + local augroup = vim.api.nvim_create_augroup + local autocmd = vim.api.nvim_create_autocmd + augroup("__formatter__", { clear = true }) + autocmd("BufWritePost", { + group = "__formatter__", + command = ":FormatWrite", + }) + + vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, { + virtual_text = false, + }) +end + +PLUGINS = { + { "EdenEast/nightfox.nvim" }, + { "rose-pine/neovim" }, + { "shortcuts/no-neck-pain.nvim" }, + { "nvim-tree/nvim-web-devicons" }, + { "nvim-tree/nvim-tree.lua" }, + { "nvim-lualine/lualine.nvim" }, + { "williamboman/mason.nvim" }, + { "williamboman/mason-lspconfig.nvim" }, + { "neovim/nvim-lspconfig" }, + { "onsails/lspkind.nvim" }, + { "hrsh7th/cmp-nvim-lsp" }, + { "hrsh7th/cmp-buffer" }, + { "hrsh7th/cmp-path" }, + { "hrsh7th/cmp-cmdline" }, + { "hrsh7th/nvim-cmp" }, + { "mhartington/formatter.nvim" }, + { + "L3MON4D3/LuaSnip", + -- install jsregexp (optional!). + build = "make install_jsregexp", + }, + { "voldikss/vim-floaterm" }, + { "f-person/auto-dark-mode.nvim" }, + { + "nvim-telescope/telescope.nvim", + tag = "0.1.6", + dependencies = { "nvim-lua/plenary.nvim" }, + }, + { + { + "kdheepak/lazygit.nvim", + cmd = { + "LazyGit", + "LazyGitConfig", + "LazyGitCurrentFile", + "LazyGitFilter", + "LazyGitFilterCurrentFile", + }, + -- optional for floating window border decoration + dependencies = { + "nvim-lua/plenary.nvim", + }, + -- setting the keybinding for LazyGit with 'keys' is recommended in + -- order to load the plugin when the command is run for the first time + keys = { + { "lg", "LazyGit", desc = "LazyGit" }, + }, + }, + }, + { "lewis6991/gitsigns.nvim" }, + { "nvim-treesitter/nvim-treesitter" }, + { + "chomosuke/typst-preview.nvim", + lazy = false, -- or ft = 'typst' + version = "0.2.0", + build = function() + require("typst-preview").update() + end, + }, + { + "windwp/nvim-autopairs", + event = "InsertEnter", + config = true, + }, +} + +init_lazy_nvim() +setup_plugins() +define_keymaps() +config_vim() diff --git a/nvim/lazy-lock.json b/nvim/lazy-lock.json new file mode 100644 index 0000000..1ba8d5b --- /dev/null +++ b/nvim/lazy-lock.json @@ -0,0 +1,29 @@ +{ + "LuaSnip": { "branch": "master", "commit": "be7be2ca7f55bb881a7ffc16b2efa5af034ab06b" }, + "auto-dark-mode.nvim": { "branch": "master", "commit": "e328dc463d238cb7d690fb4daf068eba732a5a14" }, + "cmp-buffer": { "branch": "main", "commit": "3022dbc9166796b644a841a02de8dd1cc1d311fa" }, + "cmp-cmdline": { "branch": "main", "commit": "d250c63aa13ead745e3a40f61fdd3470efde3923" }, + "cmp-nvim-lsp": { "branch": "main", "commit": "5af77f54de1b16c34b23cba810150689a3a90312" }, + "cmp-path": { "branch": "main", "commit": "91ff86cd9c29299a64f968ebb45846c485725f23" }, + "formatter.nvim": { "branch": "master", "commit": "ad246d34ce7a32f752071ed81b09b94e6b127fad" }, + "gitsigns.nvim": { "branch": "main", "commit": "9cafac31a091267838e1e90fd6e083d37611f516" }, + "lazy.nvim": { "branch": "main", "commit": "31ddbea7c10b6920c9077b66c97951ca8682d5c8" }, + "lazygit.nvim": { "branch": "main", "commit": "0ada6c6e7e138df92f5009b6952f4ac41248305a" }, + "lspkind.nvim": { "branch": "master", "commit": "1735dd5a5054c1fb7feaf8e8658dbab925f4f0cf" }, + "lualine.nvim": { "branch": "master", "commit": "0a5a66803c7407767b799067986b4dc3036e1983" }, + "mason-lspconfig.nvim": { "branch": "main", "commit": "44509689b9bf3984d729cc264aacb31cb7f41668" }, + "mason.nvim": { "branch": "main", "commit": "751b1fcbf3d3b783fcf8d48865264a9bcd8f9b10" }, + "neovim": { "branch": "main", "commit": "9cd7d8aad3606a95d733a45d16275eb7a5a836a3" }, + "nightfox.nvim": { "branch": "main", "commit": "ce0cdf8538c8c0b9c8fb2884d3d1090c8faf515d" }, + "no-neck-pain.nvim": { "branch": "main", "commit": "34625be12649666b7ccb08761087cc97bb788552" }, + "nvim-autopairs": { "branch": "master", "commit": "4f41e5940bc0443fdbe5f995e2a596847215cd2a" }, + "nvim-cmp": { "branch": "main", "commit": "ce16de5665c766f39c271705b17fff06f7bcb84f" }, + "nvim-lspconfig": { "branch": "master", "commit": "b3014f2209503944f2714cf27c95591433a0c7d8" }, + "nvim-tree.lua": { "branch": "master", "commit": "81eb8d519233c105f30dc0a278607e62b20502fd" }, + "nvim-treesitter": { "branch": "master", "commit": "ef267f0c285928ea3a0d3362a260a0728fd4a146" }, + "nvim-web-devicons": { "branch": "master", "commit": "6e355632387a085f15a66ad68cf681c1d7374a04" }, + "plenary.nvim": { "branch": "master", "commit": "8aad4396840be7fc42896e3011751b7609ca4119" }, + "telescope.nvim": { "branch": "master", "commit": "6312868392331c9c0f22725041f1ec2bef57c751" }, + "typst-preview.nvim": { "branch": "master", "commit": "36a82aaff8931f96015ee7365afe2e253ab3b1ea" }, + "vim-floaterm": { "branch": "master", "commit": "4e28c8dd0271e10a5f55142fb6fe9b1599ee6160" } +} \ No newline at end of file