From 13549985ce1d79a295c79b3a748be9ee5ba44ec1 Mon Sep 17 00:00:00 2001 From: Martin Larsson Date: Sat, 20 Jan 2024 01:30:13 +0100 Subject: [PATCH] Add basic nvim conf, still wip --- nvim/init.lua | 13 ++++ nvim/lua/keymaps.lua | 103 ++++++++++++++++++++++++++ nvim/lua/language_servers/lua_ls.lua | 30 ++++++++ nvim/lua/lazy_init.lua | 12 +++ nvim/lua/plugs/autopairs.lua | 5 ++ nvim/lua/plugs/comment.lua | 9 +++ nvim/lua/plugs/copilot.lua | 3 + nvim/lua/plugs/devicons.lua | 3 + nvim/lua/plugs/leap.lua | 31 ++++++++ nvim/lua/plugs/lualine.lua | 9 +++ nvim/lua/plugs/mason_lsp.lua | 35 +++++++++ nvim/lua/plugs/plenary.lua | 3 + nvim/lua/plugs/repeat.lua | 3 + nvim/lua/plugs/surround.lua | 8 ++ nvim/lua/plugs/telescope.lua | 45 +++++++++++ nvim/lua/plugs/telescope_fzf.lua | 4 + nvim/lua/plugs/tokyonight.lua | 77 +++++++++++++++++++ nvim/lua/plugs/treesitter.lua | 18 +++++ nvim/lua/plugs/treesitter_context.lua | 3 + nvim/lua/utils.lua | 11 +++ nvim/lua/vim_opt.lua | 55 ++++++++++++++ 21 files changed, 480 insertions(+) create mode 100644 nvim/init.lua create mode 100644 nvim/lua/keymaps.lua create mode 100644 nvim/lua/language_servers/lua_ls.lua create mode 100644 nvim/lua/lazy_init.lua create mode 100644 nvim/lua/plugs/autopairs.lua create mode 100644 nvim/lua/plugs/comment.lua create mode 100644 nvim/lua/plugs/copilot.lua create mode 100644 nvim/lua/plugs/devicons.lua create mode 100644 nvim/lua/plugs/leap.lua create mode 100644 nvim/lua/plugs/lualine.lua create mode 100644 nvim/lua/plugs/mason_lsp.lua create mode 100644 nvim/lua/plugs/plenary.lua create mode 100644 nvim/lua/plugs/repeat.lua create mode 100644 nvim/lua/plugs/surround.lua create mode 100644 nvim/lua/plugs/telescope.lua create mode 100644 nvim/lua/plugs/telescope_fzf.lua create mode 100644 nvim/lua/plugs/tokyonight.lua create mode 100644 nvim/lua/plugs/treesitter.lua create mode 100644 nvim/lua/plugs/treesitter_context.lua create mode 100644 nvim/lua/utils.lua create mode 100644 nvim/lua/vim_opt.lua diff --git a/nvim/init.lua b/nvim/init.lua new file mode 100644 index 0000000..4f00025 --- /dev/null +++ b/nvim/init.lua @@ -0,0 +1,13 @@ +local merge = vim.tbl_deep_extend + + -- Load keymaps before loading any plugins +require("keymaps") + + -- change and personalize native vim settings +vim.opt = require("vim_opt") + + -- Initialize Lazy package manager +require("lazy_init") + + -- Initialize plugins, add a plugin by creating a new file in the plugins dir +require("lazy").setup("plugs") diff --git a/nvim/lua/keymaps.lua b/nvim/lua/keymaps.lua new file mode 100644 index 0000000..e091b8a --- /dev/null +++ b/nvim/lua/keymaps.lua @@ -0,0 +1,103 @@ +local g = vim.g + +g.mapleader = " " +g.maplocalleader = " " + +local keymaps = { + n = { + -- Navigation + [""] = { + cmd = "zz", + }, + [""] = { + cmd = "zz", + }, + [""] = { + cmd = "h", + }, + [""] = { + cmd = "j", + }, + [""] = { + cmd = "k", + }, + [""] = { + cmd = "l", + }, + + -- Disable current highlights + [""] = { + cmd = " noh ", + }, + + -- Save + [""] = { + cmd = " w ", + }, + + -- Copies the entire file + [""] = { + cmd = " %y+ ", + }, + + -- Allow moving the cursor through wrapped lines with and + -- http://www.reddit.com/r/vim/comments/2k4cbr/problem_with_gj_and_gk/ + -- empty mode is same as using :map + -- also don't use g[j|k] when in operator pending mode, so it doesn't alter d, y or c behaviour + [""] = { + cmd = "v:count || mode(1)[0:1] == \"no\" ? \"k\" : \"gk\"", + opts = { + expr = true + } + }, + [""] = { + cmd = "v:count || mode(1)[0:1] == \"no\" ? \"j\" : \"gj\"", + opts = { + expr = true + } + }, + }, + i = { }, + v = { + [""] = { + cmd = "v:count || mode(1)[0:1] == \"no\" ? \"k\" : \"gk\"", + opts = { + expr = true + } + }, + [""] = { + cmd = "v:count || mode(1)[0:1] == \"no\" ? \"j\" : \"gj\"", + opts = { + expr = true + } + }, + [""] = { + cmd = ">gv", + }, + [""] = { + cmd = ""] = { + cmd = "v:count || mode(1)[0:1] == \"no\" ? \"k\" : \"gk\"", + opts = { + expr = true + } + }, + [""] = { + cmd = "v:count || mode(1)[0:1] == \"no\" ? \"j\" : \"gj\"", + opts = { + expr = true + } + }, + ["p"] = { + cmd = "p:let @+=@0:let @\"=@0", + opts = { + silent = true + }, + }, + }, +} + +require("utils").add_keymaps(keymaps) diff --git a/nvim/lua/language_servers/lua_ls.lua b/nvim/lua/language_servers/lua_ls.lua new file mode 100644 index 0000000..fcc74e5 --- /dev/null +++ b/nvim/lua/language_servers/lua_ls.lua @@ -0,0 +1,30 @@ +return { + on_init = function(client) + local path = client.workspace_folders[1].name + if not vim.loop.fs_stat(path .. "/.luarc.json") and not vim.loop.fs_stat(path .. "/.luarc.jsonc") then + client.config.settings = vim.tbl_deep_extend("force", client.config.settings, { + Lua = { + runtime = { + -- Tell the language server which version of Lua you're using + -- (most likely LuaJIT in the case of Neovim) + version = "LuaJIT", + }, + -- Make the server aware of Neovim runtime files + workspace = { + checkThirdParty = false, + library = { + vim.env.VIMRUNTIME, + -- "${3rd}/luv/library" + -- "${3rd}/busted/library", + }, + -- or pull in all of 'runtimepath'. NOTE: this is a lot slower + -- library = vim.api.nvim_get_runtime_file("", true) + }, + }, + }) + + client.notify("workspace/didChangeConfiguration", { settings = client.config.settings }) + end + return true + end +} diff --git a/nvim/lua/lazy_init.lua b/nvim/lua/lazy_init.lua new file mode 100644 index 0000000..d14f916 --- /dev/null +++ b/nvim/lua/lazy_init.lua @@ -0,0 +1,12 @@ +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) diff --git a/nvim/lua/plugs/autopairs.lua b/nvim/lua/plugs/autopairs.lua new file mode 100644 index 0000000..d120e5f --- /dev/null +++ b/nvim/lua/plugs/autopairs.lua @@ -0,0 +1,5 @@ +return { + "windwp/nvim-autopairs", + event = "InsertEnter", + opts = {}, +} diff --git a/nvim/lua/plugs/comment.lua b/nvim/lua/plugs/comment.lua new file mode 100644 index 0000000..dcb70bf --- /dev/null +++ b/nvim/lua/plugs/comment.lua @@ -0,0 +1,9 @@ +return { + "numToStr/Comment.nvim", + opts = { + mappings = { + extra = false, + }, + }, + lazy = false, +} diff --git a/nvim/lua/plugs/copilot.lua b/nvim/lua/plugs/copilot.lua new file mode 100644 index 0000000..c59d48c --- /dev/null +++ b/nvim/lua/plugs/copilot.lua @@ -0,0 +1,3 @@ +return { + "github/copilot.vim", +} diff --git a/nvim/lua/plugs/devicons.lua b/nvim/lua/plugs/devicons.lua new file mode 100644 index 0000000..b8e98b8 --- /dev/null +++ b/nvim/lua/plugs/devicons.lua @@ -0,0 +1,3 @@ +return { + "nvim-tree/nvim-web-devicons", +} diff --git a/nvim/lua/plugs/leap.lua b/nvim/lua/plugs/leap.lua new file mode 100644 index 0000000..e260410 --- /dev/null +++ b/nvim/lua/plugs/leap.lua @@ -0,0 +1,31 @@ +return { + "ggandor/leap.nvim", + dependencies = { + "tpope/vim-repeat", + }, + config = function() + require("leap").create_default_mappings() + + -- Hide the (real) cursor when leaping, and restore it afterwards. + vim.api.nvim_create_autocmd( + "User", + { + pattern = "LeapEnter", + callback = function() + vim.cmd.hi("Cursor", "blend=100") + vim.opt.guicursor:append { "a:Cursor/lCursor" } + end, + } + ) + vim.api.nvim_create_autocmd( + "User", + { + pattern = "LeapLeave", + callback = function() + vim.cmd.hi("Cursor", "blend=0") + vim.opt.guicursor:remove { "a:Cursor/lCursor" } + end, + } + ) + end, +} diff --git a/nvim/lua/plugs/lualine.lua b/nvim/lua/plugs/lualine.lua new file mode 100644 index 0000000..dbe082b --- /dev/null +++ b/nvim/lua/plugs/lualine.lua @@ -0,0 +1,9 @@ +return { + "nvim-lualine/lualine.nvim", + dependencies = { + "nvim-tree/nvim-web-devicons" + }, + opts = { + theme = "tokyonight-storm", + }, +} diff --git a/nvim/lua/plugs/mason_lsp.lua b/nvim/lua/plugs/mason_lsp.lua new file mode 100644 index 0000000..0e67b6c --- /dev/null +++ b/nvim/lua/plugs/mason_lsp.lua @@ -0,0 +1,35 @@ +return { + "williamboman/mason.nvim", + dependencies = { + "williamboman/mason-lspconfig.nvim", + "neovim/nvim-lspconfig", + "WhoIsSethDaniel/mason-tool-installer.nvim", + "RubixDev/mason-update-all", + }, + config = function() + -- Missing Rust because rust-analyzer is deprecated, and rustaceanvim is not included in mason yet, so it needs a custom setup. + -- Make sure that these are named according to lspconfig and not mason packages + local servers_names = { + "lua_ls", + } + + require("mason").setup() + require("mason-lspconfig").setup() + require("mason-tool-installer").setup({ + ensure_installed = servers_names, + }) + require("mason-update-all").setup() + + -- Iterate each server and setup + local lspconfig = require("lspconfig") + for _, server_name in ipairs(servers_names) do + local server = lspconfig[server_name] + if server then + server.setup(require("lua/language_servers/" .. server_name)) + else + error("LSP server not found: " .. server_name) + end + + end + end, +} diff --git a/nvim/lua/plugs/plenary.lua b/nvim/lua/plugs/plenary.lua new file mode 100644 index 0000000..857227c --- /dev/null +++ b/nvim/lua/plugs/plenary.lua @@ -0,0 +1,3 @@ +return { + "nvim-lua/plenary.nvim", +} diff --git a/nvim/lua/plugs/repeat.lua b/nvim/lua/plugs/repeat.lua new file mode 100644 index 0000000..4bc7496 --- /dev/null +++ b/nvim/lua/plugs/repeat.lua @@ -0,0 +1,3 @@ +return { + "tpope/vim-repeat", +} diff --git a/nvim/lua/plugs/surround.lua b/nvim/lua/plugs/surround.lua new file mode 100644 index 0000000..561ef50 --- /dev/null +++ b/nvim/lua/plugs/surround.lua @@ -0,0 +1,8 @@ +return { + "kylechui/nvim-surround", + version = "*", + event = "VeryLazy", + config = function() + require("nvim-surround").setup({}) + end, +} diff --git a/nvim/lua/plugs/telescope.lua b/nvim/lua/plugs/telescope.lua new file mode 100644 index 0000000..8bf0b0b --- /dev/null +++ b/nvim/lua/plugs/telescope.lua @@ -0,0 +1,45 @@ +return { + "nvim-telescope/telescope.nvim", + dependencies = { + "nvim-lua/plenary.nvim", + "nvim-telescope/telescope-fzf-native.nvim", + }, + config = function() + require("telescope").setup({ }) + require("telescope").load_extension("fzf") + + local keymaps = { + n = { + -- File search + ["to"] = { + cmd = ":Telescope find_files ", + }, + ["tf"] = { + cmd = ":Telescope current_buffer_fuzzy_find", + }, + ["ta"] = { + cmd = ":Telescope live_grep find_command=rg,--ignore-file,.gitignore,--exclude,*.git,--exclude,*.svn,--exclude,*.vs,--exclude,*.idea", + }, + -- Git + ["gc"] = { + cmd = " Telescope git_commits ", + }, + ["gs"] = { + cmd = " Telescope git_status ", + }, + ["gh"] = { + cmd = " Telescope git_bcommits ", + }, + ["gb"] = { + cmd = " Telescope git_branches ", + }, + -- Misc + ["tb"] = { + cmd = " Telescope marks ", + } + } + } + + require("utils").add_keymaps(keymaps) + end, +} diff --git a/nvim/lua/plugs/telescope_fzf.lua b/nvim/lua/plugs/telescope_fzf.lua new file mode 100644 index 0000000..01f4759 --- /dev/null +++ b/nvim/lua/plugs/telescope_fzf.lua @@ -0,0 +1,4 @@ +return { + "nvim-telescope/telescope-fzf-native.nvim", + build = "cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release && cmake --build build --config Release && cmake --install build --prefix build", +} diff --git a/nvim/lua/plugs/tokyonight.lua b/nvim/lua/plugs/tokyonight.lua new file mode 100644 index 0000000..2fbf35f --- /dev/null +++ b/nvim/lua/plugs/tokyonight.lua @@ -0,0 +1,77 @@ +return { + "folke/tokyonight.nvim", + lazy = false, + priority = 1000, + config = function() + require("tokyonight").setup({ + style = "storm", + light_style = "day", + transparent = true, + terminal_colors = true, + styles = { + comments = {}, + keywords = {}, + functions = {}, + variables = {}, + sidebars = "dark", + floats = "dark" + }, + sidebars = { "qf", "help" }, + day_brightness = 0.3, + hide_inactive_statusline = false, + dim_inactive = false, + lualine_bold = true, + + --- You can override specific color groups to use other groups or a hex color + --- function will be called with a ColorScheme table + ---@param colors ColorScheme + on_colors = function(colors) end, + + --- You can override specific highlights to use other groups or a hex color + --- function will be called with a Highlights and ColorScheme table + ---@param highlights Highlights + ---@param colors ColorScheme + on_highlights = function(highlights, colors) + local prompt = "#2d3149" + highlights.TelescopeNormal = { + bg = colors.bg_dark, + fg = colors.fg_dark, + } + highlights.TelescopeBorder = { + bg = colors.bg_dark, + fg = colors.bg_dark, + } + highlights.TelescopePromptNormal = { + bg = prompt, + } + highlights.TelescopePromptBorder = { + bg = prompt, + fg = prompt, + } + highlights.TelescopePromptTitle = { + bg = prompt, + fg = prompt, + } + highlights.TelescopePreviewTitle = { + bg = colors.bg_dark, + fg = colors.bg_dark, + } + highlights.TelescopeResultsTitle = { + bg = colors.bg_dark, + fg = colors.bg_dark, + } + end, + }) + + vim.cmd[[colorscheme tokyonight-storm]] + end +} + +-- Usecase example for other plugins when adding colorscheme support +--[[ +local colors = require("tokyonight.colors").setup() -- pass in any of the config options as explained above +local util = require("tokyonight.util") + +aplugin.background = colors.bg_dark +aplugin.my_error = util.lighten(colors.red1, 0.3) -- number between 0 and 1. 0 results in white, 1 results in red1 +]]-- diff --git a/nvim/lua/plugs/treesitter.lua b/nvim/lua/plugs/treesitter.lua new file mode 100644 index 0000000..e80a5c3 --- /dev/null +++ b/nvim/lua/plugs/treesitter.lua @@ -0,0 +1,18 @@ +return { + "nvim-treesitter/nvim-treesitter", + opts = { + ensure_installed = { + "vim", + "vimdoc", + "lua" + }, + sync_install = false, + -- This can be updated to a list of languages instead of defaulting to true + highlight = { enable = true }, + indent = {enable = true }, + }, + build = ":TSUpdate", + dependencies = { + "nvim-treesitter/nvim-treesitter-context" + } +} diff --git a/nvim/lua/plugs/treesitter_context.lua b/nvim/lua/plugs/treesitter_context.lua new file mode 100644 index 0000000..ade5591 --- /dev/null +++ b/nvim/lua/plugs/treesitter_context.lua @@ -0,0 +1,3 @@ +return { + "nvim-treesitter/nvim-treesitter-context" +} diff --git a/nvim/lua/utils.lua b/nvim/lua/utils.lua new file mode 100644 index 0000000..802003f --- /dev/null +++ b/nvim/lua/utils.lua @@ -0,0 +1,11 @@ +local M = {} + +function M.add_keymaps(maps) + for mode, entries in pairs(maps) do + for code, info in pairs(entries) do + vim.keymap.set(mode, code, info.cmd, info.opts) + end + end +end + +return M diff --git a/nvim/lua/vim_opt.lua b/nvim/lua/vim_opt.lua new file mode 100644 index 0000000..a0f88d5 --- /dev/null +++ b/nvim/lua/vim_opt.lua @@ -0,0 +1,55 @@ +local opt = vim.opt + + -- Disable tabs, will use telescope and harpoon instead +opt.showtabline = 0 + + -- Make Vim use the system clipboard +opt.clipboard = "unnamedplus" + + -- Highlight the currently selected row +opt.cursorline = true +opt.cursorlineopt = "both" + + -- Indenting +opt.expandtab = true +opt.smartindent = true +opt.breakindent = true +opt.shiftwidth = 4 +opt.tabstop = 4 +opt.softtabstop = 4 + + -- Disable home screen +opt.shortmess:append("sI") + + -- Signcolumn +opt.signcolumn = "yes:1" -- Adds a spacing to the left which can contain gutter icons +opt.fillchars = { eob = " " } -- Remove the fill character for empty lines which defaults to: "~" + + -- Search +opt.ignorecase = true +opt.smartcase = true +opt.incsearch = true + + -- Disable mouse support +opt.mouse = "" + + -- Numbers +opt.number = true +opt.relativenumber = true +opt.numberwidth = 4 + + -- Decrease update time +opt.updatetime = 250 +opt.timeoutlen = 300 + + -- Richer colors in terminal, not all terminals support this +opt.termguicolors = true + + -- Disable swapfile, 99/100 times it just gets in the way +opt.swapfile = false + + -- Buffers +opt.splitright = true +opt.splitbelow = true + +return opt