Added init.lua

This commit is contained in:
Christoffer Martinsson 2023-06-11 01:19:56 +02:00
parent 4426789cbe
commit a7b603a036

604
config/nvim/init.lua Normal file
View File

@ -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 = {
["<C-n>"] = actions.cycle_history_next,
["<C-p>"] = actions.cycle_history_prev,
["<C-j>"] = actions.move_selection_next,
["<C-k>"] = actions.move_selection_previous,
["<C-l>"] = actions.select_default,
["<C-c>"] = actions.close,
},
n = {
["q"] = actions.close,
["<C-c>"] = 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', '<leader>gp', require('gitsigns').prev_hunk,
{ buffer = bufnr, desc = '[G]o to [P]revious Hunk' })
vim.keymap.set('n', '<leader>gn', require('gitsigns').next_hunk,
{ buffer = bufnr, desc = '[G]o to [N]ext Hunk' })
vim.keymap.set('n', '<leader>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('<leader>rn', vim.lsp.buf.rename, 'Rename')
nmap('<leader>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('<leader>D', vim.lsp.buf.type_definition, 'Type Definition')
nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
-- nmap('<M-f>', vim.lsp.buf.format(), 'Format current buffer with LSP')
nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, 'Document Symbols')
nmap('<leader>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 {
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-d>'] = cmp.mapping.scroll_docs(-4),
['<C-f>'] = cmp.mapping.scroll_docs(4),
['<C-Space>'] = cmp.mapping.complete {},
['<CR>'] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = false,
},
['<Tab>'] = 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' }),
['<S-Tab>'] = 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 = '<c-space>',
node_incremental = '<c-space>',
scope_incremental = '<c-s>',
node_decremental = '<M-space>',
},
},
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 = {
['<leader>a'] = '@parameter.inner',
},
swap_previous = {
['<leader>A'] = '@parameter.inner',
},
},
},
}
-- resizing splits
vim.keymap.set('n', '<A-h>', require('smart-splits').resize_left, { desc = 'Resize left' })
vim.keymap.set('n', '<A-j>', require('smart-splits').resize_down, { desc = 'Resize down' })
vim.keymap.set('n', '<A-k>', require('smart-splits').resize_up, { desc = 'Resize up' })
vim.keymap.set('n', '<A-l>', require('smart-splits').resize_right, { desc = 'Resize right' })
-- moving between splits
vim.keymap.set('n', '<C-h>', require('smart-splits').move_cursor_left, { desc = 'Move cursor left' })
vim.keymap.set('n', '<C-j>', require('smart-splits').move_cursor_down, { desc = 'Move cursor down' })
vim.keymap.set('n', '<C-k>', require('smart-splits').move_cursor_up, { desc = 'Move cursor up' })
vim.keymap.set('n', '<C-l>', require('smart-splits').move_cursor_right, { desc = 'Move cursor right' })
-- swapping buffers between windows
vim.keymap.set('n', '<leader><leader>h', require('smart-splits').swap_buf_left, { desc = 'Swap buffer left' })
vim.keymap.set('n', '<leader><leader>j', require('smart-splits').swap_buf_down, { desc = 'Swap buffer down' })
vim.keymap.set('n', '<leader><leader>k', require('smart-splits').swap_buf_up, { desc = 'Swap buffer up' })
vim.keymap.set('n', '<leader><leader>l', require('smart-splits').swap_buf_right, { desc = 'Swap buffer right' })
-- telescope
vim.keymap.set('n', '<leader>fo', require('telescope.builtin').oldfiles, { desc = 'Find recently opened files' })
vim.keymap.set('n', '<leader>ff', require('telescope.builtin').find_files, { desc = 'Find files' })
vim.keymap.set('n', '<leader>fb', ":Telescope file_browser<cr>", { desc = 'File browser', silent = true })
vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = 'Search help' })
vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = 'Search current word' })
vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = 'Search by grep' })
vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = 'Search diagnostics' })
vim.keymap.set('n', '<tab>', ":tabNext<cr>", { desc = 'Next tab', silent = true })
vim.keymap.set('n', '<A-f>', ":Format<cr>", { desc = 'Format code', silent = true })
vim.keymap.set('n', '<leader>gg', ":LazyGitCurrentFile<cr>", { desc = 'Format code', silent = true })
vim.keymap.set('n', '<C-m>', ":make<cr>", { desc = 'Format code', silent = true })
vim.keymap.set('n', '<C-n>', ":make clean<cr>", { 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" })