From a7b603a03658b17f0120e65a052419ee3634bc69 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Sun, 11 Jun 2023 01:19:56 +0200 Subject: [PATCH] Added init.lua --- config/nvim/init.lua | 604 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 604 insertions(+) create mode 100644 config/nvim/init.lua diff --git a/config/nvim/init.lua b/config/nvim/init.lua new file mode 100644 index 0000000..f433e35 --- /dev/null +++ b/config/nvim/init.lua @@ -0,0 +1,604 @@ +vim.g.mapleader = ' ' +vim.g.maplocalleader = ' ' + +-- Set highlight on search +vim.o.hlsearch = false + +-- Make line numbers default +vim.wo.number = true + +-- Enable mouse mode +vim.o.mouse = 'a' + +-- Sync clipboard between OS and Neovim. +vim.o.clipboard = 'unnamedplus' + +-- Enable break indent +vim.o.breakindent = true + +-- Save undo history +vim.o.undofile = true + +-- Case insensitive searching UNLESS /C or capital in search +vim.o.ignorecase = true +vim.o.smartcase = true + +-- Keep signcolumn on by default +vim.wo.signcolumn = 'yes' + +-- Set completeopt to have a better completion experience +vim.o.completeopt = 'menuone,noselect,noinsert' + +-- Use terminal colors +vim.o.termguicolors = true + +-- Tabs +vim.o.shiftwidth = 4 +vim.o.smarttab = true +vim.o.tabstop = 4 +vim.o.expandtab = true +vim.o.softtabstop = 4 + +-- Show cursor line +vim.o.cursorline = true + +vim.o.pumheight = 10 + +vim.o.relativenumber = true +vim.o.scrolloff = 8 + +vim.o.showmode = false + +vim.o.showtabline = 2 + +vim.o.wrap = false + +vim.o.cmdheight = 0 + +------------------------------------------------------------------- +-- Plugin management +------------------------------------------------------------------- +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) + +------------------------------------------------------------------- +-- Plugin installation +------------------------------------------------------------------- +require('lazy').setup({ + { + 'Alexis12119/nightly.nvim', + priority = 1000, + config = function() + vim.cmd.colorscheme 'nightly' + end, + }, + + { + -- LSP Configuration & Plugins + 'neovim/nvim-lspconfig', + event = { "BufReadPre", "BufNewFile" }, + dependencies = { + { 'williamboman/mason.nvim', config = true }, + 'williamboman/mason-lspconfig.nvim', + }, + }, + + { + -- Autocompletion + 'hrsh7th/nvim-cmp', + dependencies = { + 'onsails/lspkind.nvim', + 'L3MON4D3/LuaSnip', + 'saadparwaiz1/cmp_luasnip', + 'hrsh7th/cmp-nvim-lsp', + 'rafamadriz/friendly-snippets', + 'hrsh7th/cmp-buffer', + 'hrsh7th/cmp-path', + 'hrsh7th/cmp-cmdline', + }, + }, + + -- "gc" to comment visual regions/lines + { 'numToStr/Comment.nvim', opts = {} }, + + -- Fuzzy Finder (files, lsp, etc) + { + 'nvim-telescope/telescope.nvim', + branch = '0.1.x', + dependencies = { 'nvim-lua/plenary.nvim' }, + opts = function() + local actions = require "telescope.actions" + return { + defaults = { + path_display = { "truncate" }, + mappings = { + i = { + [""] = actions.cycle_history_next, + [""] = actions.cycle_history_prev, + [""] = actions.move_selection_next, + [""] = actions.move_selection_previous, + [""] = actions.select_default, + [""] = actions.close, + }, + n = { + ["q"] = actions.close, + [""] = actions.close, + }, + }, + }, + } + end, + }, + + { + "nvim-telescope/telescope-file-browser.nvim", + dependencies = { "nvim-telescope/telescope.nvim", "nvim-lua/plenary.nvim" }, + }, + + { + -- Highlight, edit, and navigate code + 'nvim-treesitter/nvim-treesitter', + dependencies = { + 'nvim-treesitter/nvim-treesitter-textobjects', + }, + build = ':TSUpdate', + }, + + { + "max397574/better-escape.nvim", + opts = { timeout = 300 } + }, + + -- Copilot plugin to autocomplete text + { + 'zbirenbaum/copilot-cmp', + event = { 'BufRead', 'BufNewFile' }, + dependencies = { + { + 'zbirenbaum/copilot.lua', + config = function() + require('copilot').setup { + suggestion = { enabled = false }, + panel = { enabled = false }, + filetypes = { markdown = true }, + } + end, + }, + }, + config = function() require('copilot_cmp').setup() end, + }, + + { + -- Set lualine as statusline + 'nvim-lualine/lualine.nvim', + opts = { + options = { + icons_enabled = true, + theme = 'auto', + component_separators = '|', + section_separators = '', + globalstatus = true, + }, + }, + }, + + { + -- Adds git releated signs to the gutter, as well as utilities for managing changes + 'lewis6991/gitsigns.nvim', + opts = { + signs = { + add = { text = "▎" }, + change = { text = "▎" }, + delete = { text = "▎" }, + topdelete = { text = "契" }, + changedelete = { text = "▎" }, + untracked = { text = "▎" }, + }, + on_attach = function(bufnr) + vim.keymap.set('n', 'gp', require('gitsigns').prev_hunk, + { buffer = bufnr, desc = '[G]o to [P]revious Hunk' }) + vim.keymap.set('n', 'gn', require('gitsigns').next_hunk, + { buffer = bufnr, desc = '[G]o to [N]ext Hunk' }) + vim.keymap.set('n', 'ph', require('gitsigns').preview_hunk, + { buffer = bufnr, desc = '[P]review [H]unk' }) + end, + }, + }, + + { + -- Add indentation guides even on blank lines + 'lukas-reineke/indent-blankline.nvim', + opts = { + buftype_exclude = { + "nofile", + "terminal", + }, + filetype_exclude = { + "help", + "startify", + "aerial", + "alpha", + "dashboard", + "lazy", + "neogitstatus", + "NvimTree", + "neo-tree", + "Trouble", + }, + context_patterns = { + "class", + "return", + "function", + "method", + "^if", + "^while", + "jsx_element", + "^for", + "^object", + "^table", + "block", + "arguments", + "if_statement", + "else_clause", + "jsx_element", + "jsx_self_closing_element", + "try_statement", + "catch_clause", + "import_statement", + "operation_type", + }, + show_trailing_blankline_indent = false, + use_treesitter = true, + char = "▏", + context_char = "▏", + show_current_context = true, + } + }, + + { 'mrjones2014/smart-splits.nvim' }, + + { + "folke/neodev.nvim", + opts = {} + }, + + { + "kdheepak/lazygit.nvim", + dependencies = { + "nvim-lua/plenary.nvim", + }, + }, + +}, {}) + +------------------------------------------------------------------- +-- LSP Configurations +------------------------------------------------------------------- +local on_attach = function(_, bufnr) + local nmap = function(keys, func, desc) + if desc then + desc = 'LSP: ' .. desc + end + vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc }) + end + + nmap('rn', vim.lsp.buf.rename, 'Rename') + nmap('ca', vim.lsp.buf.code_action, 'Code Action') + nmap('gd', vim.lsp.buf.definition, 'Goto Definition') + nmap('gD', vim.lsp.buf.declaration, 'Goto Declaration') + nmap('gI', vim.lsp.buf.implementation, 'Goto Implementation') + nmap('gr', require('telescope.builtin').lsp_references, 'Goto References') + nmap('D', vim.lsp.buf.type_definition, 'Type Definition') + nmap('K', vim.lsp.buf.hover, 'Hover Documentation') + nmap('', vim.lsp.buf.signature_help, 'Signature Documentation') + -- nmap('', vim.lsp.buf.format(), 'Format current buffer with LSP') + nmap('ds', require('telescope.builtin').lsp_document_symbols, 'Document Symbols') + nmap('ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, 'Workspace Symbols') + + -- Create a command `:Format` local to the LSP buffer + vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_) + vim.lsp.buf.format() + end, { desc = 'Format current buffer with LSP' }) +end + +-- Add LSP servers +local servers = { + lua_ls = { + Lua = { + workspace = { checkThirdParty = false }, + telemetry = { enable = false }, + }, + }, + clangd = { + cmd = { 'clangd', '--background-index' }, + filetypes = { 'c', 'cpp', 'objc', 'objcpp' }, + }, + rust_analyzer = {}, +} + +------------------------------------------------------------------- +-- Plugin Setup +------------------------------------------------------------------- +-- nvim-cmp supports additional completion capabilities, so broadcast that to servers +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities) + +-- Ensure the servers above are installed +local mason_lspconfig = require 'mason-lspconfig' + +mason_lspconfig.setup { + ensure_installed = vim.tbl_keys(servers), +} + +mason_lspconfig.setup_handlers { + function(server_name) + require('lspconfig')[server_name].setup { + capabilities = capabilities, + on_attach = on_attach, + settings = servers[server_name], + } + end, +} + +local handlers = { + ["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = "single" }), + ["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = "single" }), +} + +require('lspconfig').rust_analyzer.setup { + on_attach = on_attach, + handlers = handlers, +} + +require('lspconfig').clangd.setup { + on_attach = on_attach, + capabilities = { offsetEncoding = 'utf-16' }, + handlers = handlers, +} + +local runtime_path = vim.split(package.path, ';') +table.insert(runtime_path, "lua/?.lua") +table.insert(runtime_path, "lua/?/init.lua") +require 'lspconfig'.lua_ls.setup { + on_attach = on_attach, + capabilities = capabilities, + handlers = handlers, + settings = { + Lua = { + runtime = { + version = 'LuaJIT', + path = runtime_path, + }, + diagnostics = { + globals = { 'vim' }, + }, + workspace = { + library = vim.api.nvim_get_runtime_file("", true), + }, + telemetry = { + enable = false, + }, + }, + }, +} + +-- Setup neovim lua configuration +require("telescope").load_extension "file_browser" + +-- [[ Configure nvim-cmp ]] +local cmp = require 'cmp' +local luasnip = require 'luasnip' +require('luasnip.loaders.from_vscode').lazy_load() +luasnip.config.setup {} + +cmp.setup { + preselect = cmp.PreselectMode.None, + completion = { + completion = { completeopt = 'menu,menuone,noinsert,noselect' }, + }, + window = { + documentation = { + winhighlight = 'Normal:Pmenu,FloatBorder:Pmenu,Search:None', + border = "single", + }, + completion = { + winhighlight = 'Normal:Pmenu,FloatBorder:Pmenu,Search:None', + border = "single", + }, + }, + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + duplicates = { + nvim_lsp = 1, + luasnip = 1, + cmp_tabnine = 1, + buffer = 1, + path = 1, + }, + mapping = cmp.mapping.preset.insert { + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.select_prev_item(), + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + [''] = cmp.mapping.complete {}, + [''] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = false, + }, + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_locally_jumpable() then + luasnip.expand_or_jump() + else + fallback() + end + end, { 'i', 's' }), + [''] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.locally_jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { 'i', 's' }), + }, + sources = { + { name = 'copilot', priority = 2000 }, + { name = 'nvim_lsp', priority = 1000 }, + { name = 'luasnip', priority = 750 }, + { name = 'buffer', priority = 500 }, + { name = 'path', priority = 250 }, + }, +} + +cmp.setup.cmdline({ '/', '?' }, { + mapping = cmp.mapping.preset.cmdline(), + window = { + documentation = { + winhighlight = 'Normal:Pmenu,FloatBorder:Pmenu,Search:None', + border = "single", + }, + completion = { + winhighlight = 'Normal:Pmenu,FloatBorder:Pmenu,Search:None', + border = "single", + }, + }, + sources = { + { name = 'buffer' } + } +}) + +cmp.setup.cmdline(':', { + mapping = cmp.mapping.preset.cmdline(), + window = { + documentation = { + winhighlight = 'Normal:Pmenu,FloatBorder:Pmenu,Search:None', + border = "single", + }, + completion = { + winhighlight = 'Normal:Pmenu,FloatBorder:Pmenu,Search:None', + border = "single", + }, + }, + sources = cmp.config.sources({ + { name = 'path' } + }, { + { name = 'cmdline' } + }) +}) + +-- [[ Configure Treesitter ]] +require('nvim-treesitter.configs').setup { + -- Add languages to be installed here that you want installed for treesitter + ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'typescript', 'vimdoc', 'vim' }, + + -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!) + auto_install = false, + + highlight = { enable = true }, + indent = { enable = true }, + incremental_selection = { + enable = true, + keymaps = { + init_selection = '', + node_incremental = '', + scope_incremental = '', + node_decremental = '', + }, + }, + textobjects = { + select = { + enable = true, + lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim + keymaps = { + -- You can use the capture groups defined in textobjects.scm + ['aa'] = '@parameter.outer', + ['ia'] = '@parameter.inner', + ['af'] = '@function.outer', + ['if'] = '@function.inner', + ['ac'] = '@class.outer', + ['ic'] = '@class.inner', + }, + }, + move = { + enable = true, + set_jumps = true, -- whether to set jumps in the jumplist + goto_next_start = { + [']m'] = '@function.outer', + [']]'] = '@class.outer', + }, + goto_next_end = { + [']M'] = '@function.outer', + [']['] = '@class.outer', + }, + goto_previous_start = { + ['[m'] = '@function.outer', + ['[['] = '@class.outer', + }, + goto_previous_end = { + ['[M'] = '@function.outer', + ['[]'] = '@class.outer', + }, + }, + swap = { + enable = true, + swap_next = { + ['a'] = '@parameter.inner', + }, + swap_previous = { + ['A'] = '@parameter.inner', + }, + }, + }, +} + +-- resizing splits +vim.keymap.set('n', '', require('smart-splits').resize_left, { desc = 'Resize left' }) +vim.keymap.set('n', '', require('smart-splits').resize_down, { desc = 'Resize down' }) +vim.keymap.set('n', '', require('smart-splits').resize_up, { desc = 'Resize up' }) +vim.keymap.set('n', '', require('smart-splits').resize_right, { desc = 'Resize right' }) +-- moving between splits +vim.keymap.set('n', '', require('smart-splits').move_cursor_left, { desc = 'Move cursor left' }) +vim.keymap.set('n', '', require('smart-splits').move_cursor_down, { desc = 'Move cursor down' }) +vim.keymap.set('n', '', require('smart-splits').move_cursor_up, { desc = 'Move cursor up' }) +vim.keymap.set('n', '', require('smart-splits').move_cursor_right, { desc = 'Move cursor right' }) +-- swapping buffers between windows +vim.keymap.set('n', 'h', require('smart-splits').swap_buf_left, { desc = 'Swap buffer left' }) +vim.keymap.set('n', 'j', require('smart-splits').swap_buf_down, { desc = 'Swap buffer down' }) +vim.keymap.set('n', 'k', require('smart-splits').swap_buf_up, { desc = 'Swap buffer up' }) +vim.keymap.set('n', 'l', require('smart-splits').swap_buf_right, { desc = 'Swap buffer right' }) +-- telescope +vim.keymap.set('n', 'fo', require('telescope.builtin').oldfiles, { desc = 'Find recently opened files' }) +vim.keymap.set('n', 'ff', require('telescope.builtin').find_files, { desc = 'Find files' }) +vim.keymap.set('n', 'fb', ":Telescope file_browser", { desc = 'File browser', silent = true }) +vim.keymap.set('n', 'sh', require('telescope.builtin').help_tags, { desc = 'Search help' }) +vim.keymap.set('n', 'sw', require('telescope.builtin').grep_string, { desc = 'Search current word' }) +vim.keymap.set('n', 'sg', require('telescope.builtin').live_grep, { desc = 'Search by grep' }) +vim.keymap.set('n', 'sd', require('telescope.builtin').diagnostics, { desc = 'Search diagnostics' }) + +vim.keymap.set('n', '', ":tabNext", { desc = 'Next tab', silent = true }) +vim.keymap.set('n', '', ":Format", { desc = 'Format code', silent = true }) +vim.keymap.set('n', 'gg', ":LazyGitCurrentFile", { desc = 'Format code', silent = true }) +vim.keymap.set('n', '', ":make", { desc = 'Format code', silent = true }) +vim.keymap.set('n', '', ":make clean", { desc = 'Format code', silent = true }) + +-- Customization for Pmenu +-- vim.api.nvim_set_hl(0, "PmenuSel", { bg = "#555555", fg = "NONE" }) +-- vim.api.nvim_set_hl(0, "Pmenu", { fg = "#C5CDD9", bg = "#000000" })