feat: add neovim config

This commit is contained in:
Dan Anglin 2022-03-13 15:35:55 +00:00
parent 25604c2233
commit 2c973bb386
Signed by: dananglin
GPG key ID: 0C1D44CFBEE68638
14 changed files with 394 additions and 4 deletions

20
LICENSE
View file

@ -1,9 +1,21 @@
MIT License MIT License
Copyright (c) <year> <copyright holders> Copyright (c) 2022 Dan Anglin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

5
Makefile Normal file
View file

@ -0,0 +1,5 @@
.PHONY: neovim_config
neovim_config:
@./helpers/install-neovim-config.sh
@./helpers/install-plugins.sh

View file

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$( cd "$( dirname $0 )/.." && pwd )"
NEOVIM_CONFIG_SOURCE_DIR="${ROOT_DIR}/neovim"
NEOVIM_CONFIG_DESTINATION_DIR="${XDG_CONFIG_HOME}/nvim"
mkdir -p ${NEOVIM_CONFIG_DESTINATION_DIR}
# Syncing the neovim configuration files
echo "INFO: syncing ${NEOVIM_CONFIG_SOURCE_DIR} to ${NEOVIM_CONFIG_DESTINATION_DIR}"
rsync -avh ${NEOVIM_CONFIG_SOURCE_DIR}/ ${NEOVIM_CONFIG_DESTINATION_DIR}
chmod a-rwx,u+rwx ${NEOVIM_CONFIG_DESTINATION_DIR}
# Ensuring the data directories are present
mkdir -p ${XDG_DATA_HOME}/nvim/{backup,swap,undo,view,spell}
chmod 0700 ${XDG_DATA_HOME}/nvim/{backup,swap,undo,view,spell}

14
helpers/install-plugins.sh Executable file
View file

@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
PACKER_NVIM_SOURCE="https://github.com/wbthomason/packer.nvim"
PACKER_NVIM_DESTINATION="${XDG_DATA_HOME}/nvim/site/pack/packer/start/packer.nvim"
if [ -d ${PACKER_NVIM_DESTINATION}/.git ]; then
echo "INFO: packer.nvim is already installed."
exit 0
fi
echo "INFO: Cloning ${PACKER_NVIM_SOURCE} to ${PACKER_NVIM_DESTINATION}..."
git clone --depth=1 ${PACKER_NVIM_SOURCE} ${PACKER_NVIM_DESTINATION}

View file

@ -0,0 +1,7 @@
local setlocal = vim.opt_local
-- Tabs and spaces
setlocal.expandtab = false
setlocal.tabstop = 8
setlocal.shiftwidth = 8
setlocal.softtabstop = 8

View file

@ -0,0 +1,7 @@
local setlocal = vim.opt_local
-- Tabs and spaces
setlocal.expandtab = false
setlocal.tabstop = 4
setlocal.shiftwidth = 4
setlocal.softtabstop = 4

View file

@ -0,0 +1,6 @@
local setlocal = vim.opt_local
-- Tabs and spaces
setlocal.tabstop = 4
setlocal.shiftwidth = 4
setlocal.softtabstop = 4

View file

@ -0,0 +1,6 @@
local setlocal = vim.opt_local
--- Tabs and spaces
setlocal.tabstop = 4
setlocal.shiftwidth = 4
setlocal.softtabstop = 4

View file

@ -0,0 +1,68 @@
local function t(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
function _G.smart_tab()
return vim.fn.pumvisible() == 1 and t'<C-n>' or t'<TAB>'
end
function _G.reverse_smart_tab()
return vim.fn.pumvisible() == 1 and t'<C-p>' or t'<TAB>'
end
-- Disable arrow key navigation
vim.api.nvim_set_keymap("n", "<Up>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("n", "<Down>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("n", "<Left>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("n", "<Right>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("i", "<Up>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("i", "<Down>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("i", "<Left>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("i", "<Right>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("v", "<Up>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("v", "<Down>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("v", "<Left>", "<Nop>", { noremap = true })
vim.api.nvim_set_keymap("v", "<Right>", "<Nop>", { noremap = true })
-- Copy and paste to/from the OS clipboard
vim.api.nvim_set_keymap("n", "<leader>y", "\"+y", { noremap = true})
vim.api.nvim_set_keymap("n", "<leader>yy", "\"+yy", { noremap = true})
vim.api.nvim_set_keymap("v", "<leader>y", "\"+y", { noremap = true})
vim.api.nvim_set_keymap("n", "<leader>p", "\"+p", { noremap = true})
vim.api.nvim_set_keymap("n", "<leader>P", "\"+P", { noremap = true})
vim.api.nvim_set_keymap("v", "<leader>p", "\"+p", { noremap = true})
vim.api.nvim_set_keymap("v", "<leader>P", "\"+P\"`\"`", { noremap = true})
-- copy the whole file to the OS clipboard
vim.api.nvim_set_keymap("n", "<leader>c", ":%y+<CR>", { noremap = true})
-- Control buffer splits
vim.api.nvim_set_keymap("n", "<C-l>", ":vert resize +5<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<C-h>", ":vert resize -5<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<C-k>", ":resize -5<CR>", { noremap = true })
vim.api.nvim_set_keymap("n", "<C-j>", ":resize +5<CR>", { noremap = true })
-- Add matching curly brace
vim.api.nvim_set_keymap("i", "\\{", "{<cr>}<ESC>O", { noremap = true })
vim.api.nvim_set_keymap("i", ">{", "{}<ESC>a", { noremap = true })
vim.api.nvim_set_keymap("i", "<{", "{}<ESC>i", { noremap = true })
-- Add matching parenthesis
vim.api.nvim_set_keymap("i", "\\(", "(<cr>)<ESC>O", { noremap = true })
vim.api.nvim_set_keymap("i", ">(", "()<ESC>a", { noremap = true })
vim.api.nvim_set_keymap("i", "<(", "()<ESC>i", { noremap = true })
-- Add matching square brace
vim.api.nvim_set_keymap("i", "\\[", "[<cr>]<ESC>O", { noremap = true })
vim.api.nvim_set_keymap("i", ">[", "[]<ESC>a", { noremap = true })
vim.api.nvim_set_keymap("i", "<[", "[]<ESC>i", { noremap = true })
-- Use the TAB key to cycle through the autocompletion popup menu
vim.api.nvim_set_keymap("i", "<TAB>", "v:lua.smart_tab()", { expr = true, noremap = true })
vim.api.nvim_set_keymap("i", "<S-TAB>", "v:lua.reverse_smart_tab()", { expr = true, noremap = true })
-- Use CTRL+Space to open the autocompletion popup menu
vim.api.nvim_set_keymap("i", "<C-SPACE>", "<C-x><C-o>", { noremap = true })

101
neovim/after/plugin/lsp.lua Normal file
View file

@ -0,0 +1,101 @@
local custom_attach = function(client, bufnr)
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
local function buf_set_option(...)
vim.api.nvim_buf_set_option(bufnr, ...)
end
buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc')
local opts = { noremap=true, silent=true }
-- See `:help vim.lsp.*` for documentation on any of the below functions
buf_set_keymap('n', '<leader>h', '<Cmd>lua vim.lsp.buf.hover()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>gd', '<Cmd>lua vim.lsp.buf.definition()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>gr', '<cmd>lua vim.lsp.buf.references()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>dl', '<cmd>lua vim.lsp.diagnostic.set_loclist()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>dn', '<cmd>lua vim.lsp.diagnostic.goto_next()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>dp', '<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>de', '<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>gt', '<cmd>lua vim.lsp.buf.type_definition()<CR>', { noremap=true, silent=true })
buf_set_keymap('n', '<leader>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', { noremap=true, silent=true })
buf_set_keymap("n", "<leader>ff", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
buf_set_keymap('n', '<leader>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
buf_set_keymap('n', '<leader>dec', '<Cmd>lua vim.lsp.buf.declaration()<CR>', opts)
--buf_set_keymap('n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
--buf_set_keymap('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
--buf_set_keymap('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
--buf_set_keymap('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
end
-- Go
require("lspconfig").gopls.setup{
on_attach = custom_attach,
cmd = {"gopls", "serve"},
settings = {
gopls = {
analyses = {
unusedparams = true,
},
staticcheck = true,
},
init_options = {
usePlaceholders = true,
completeUnimported = true,
hoverKind = "FullDocumentation",
},
},
}
-- Golangci-lint language server
-- requires golangci-lint and golangci-lint-langserver
require("lspconfig").golangci_lint_ls.setup{}
-- Terraform
require("lspconfig").terraformls.setup{
on_attach = custom_attach,
cmd = {"terraform-ls", "serve"},
filetypes = {"terraform", "hcl", "tf"},
root_dir = require("lspconfig").util.root_pattern{".terraform", ".git", "main.tf"},
}
-- Python
require("lspconfig").pylsp.setup{
on_attach = custom_attach,
cmd = { "pylsp" },
filetypes = { "python" },
}
-- Lua
HOME = vim.fn.expand('$HOME')
local sumneko_root_path = HOME .. "/Git/github.com/sumneko/lua-language-server"
local sumneko_binary = sumneko_root_path .. "/bin/Linux/lua-language-server"
require'lspconfig'.sumneko_lua.setup {
cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"},
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = 'LuaJIT',
-- Setup your lua path
path = vim.split(package.path, ';')
},
diagnostics = {
-- Get the language server to recognize the vim and awesome globals
globals = {'vim', 'awesome'}
},
workspace = {
-- Make the server aware of Neovim runtime files
library = vim.api.nvim_get_runtime_file("", true),
},
telemetry = {
-- Do not send telemetry data containing a randomized but unique identifier
enable = false,
},
}
}
}

View file

@ -0,0 +1,9 @@
return require("packer").startup(function(use)
-- Packer can manage itself
use "wbthomason/packer.nvim"
use "folke/tokyonight.nvim"
use "rafamadriz/neon"
use "navarasu/onedark.nvim"
use "neovim/nvim-lspconfig"
end)

View file

@ -0,0 +1,2 @@
local statusline = require("statusline")
vim.opt.statusline = statusline.output()

67
neovim/init.lua Normal file
View file

@ -0,0 +1,67 @@
local set = vim.opt
local g = vim.g
local cmd = vim.cmd
g.mapleader = ","
-- Backups, Undos and Swaps
set.backup = true
set.writebackup = true
set.backupdir = vim.fn.stdpath("data").."/backup"
set.swapfile = true
set.directory = vim.fn.stdpath("data").."/swap"
set.undofile = true
set.undodir = vim.fn.stdpath("data").."/undo"
set.viewdir = vim.fn.stdpath("data").."/view"
set.spellfile = vim.fn.stdpath("data").."/spell/en.utf-8.add"
-- Searching
set.ignorecase = true -- ignore case when searching.
set.smartcase = true -- override ignorecase when upper case characters are used when searching.
set.incsearch = true -- set incremental searching.
-- Default tabs and spaces
set.expandtab = true -- turn tabs into spaces
set.tabstop = 2 -- number of spaces per tab
set.softtabstop = 2 -- number of spaces per tab while editing
set.shiftwidth = 2 -- specifies the amount of whitespaces to insert/remove using indentation commands in normal mode.
-- File format and encoding
set.fileformat = "unix"
set.encoding = "utf-8"
set.fileencoding = "utf-8"
set.fileencodings = "utf-8"
-- Colour scheme
set.termguicolors = true
set.background = "dark"
g.tokyonight_style = "night"
g.tokyonight_sidebars = {"packer", "terminal"}
cmd "colorscheme tokyonight"
-- UI and UX
set.number = true -- enable line numbers
set.relativenumber = true -- enable releative line numbers
set.lazyredraw = true -- do not redraw screen when executing macros, registers, etc that have not been typed
set.splitright = true -- split vertical windows to the right
set.splitbelow = true -- split horizontal windows below
g.pastetoggle = "<F3>" -- Use <F3> to toggle paste/nopaste
set.confirm = true -- raise a dialogue to confirm if you wish to save a file before continuing.
set.cmdheight = 2 -- set the height of the command window to 2 lines.
set.modelines = 5 -- the first 5 lines are checked for modelines.
set.cursorline = true -- highlight the line that the cursor is on
set.smartindent = true -- smart autoidenting
set.scrolloff = 5
set.signcolumn = "number"
set.completeopt = "longest,menuone"
-- Netrw settings
g.netrw_banner = 0 -- disable the banner
g.netrw_liststyle = 3 -- display files and folders in a tree style
g.netrw_browse_split = 2 -- open files in a vertical split by default
g.netrw_altv = 1 -- open split to the right
g.netrw_winsize = 10 -- set the size of the Explorer window
g.netrw_keepdir = 0 -- keep the browsing directory the same as the current directory
--- Configure Omnifunc to use the LSP client.
set.omnifunc = "v:lua.vim.lsp.omnifunc"

68
neovim/lua/statusline.lua Normal file
View file

@ -0,0 +1,68 @@
local M = {}
local function t(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
function _G.format_mode(mode)
local map_table = {
["n"] = "normal",
["no"] = "normal·operator pending",
["v"] = "visual",
["V"] = "visual(line)",
[t('<C-v>')] = "visual(block)",
["s"] = "select",
["S"] = "select(line)",
[t('<C-s>')] = "select(block)",
["i"] = "insert",
["R"] = "replace",
["Rv"] = "v·replace",
["c"] = "command",
["cv"] = "vim ex",
["ce"] = "ex",
["r"] = "prompt",
["rm"] = "more",
["r?"] = "confirm",
["!" ] = "shell",
["t"] = "terminal",
}
local output = map_table[mode]
if output == nil then
output = mode
end
return output
end
function M.output()
local separator = ""
local mode = "%{%v:lua.format_mode(v:lua.vim.fn.mode())%} "
local open_file = separator .. "%r%t %m"
local buf_nr = "[%n]"
local right_align = "%="
local file_format = "%{&ff}"
local file_type = "%y "
local encoding = "%{''.(&fenc!=''?&fenc:&enc).''} "
local line_no = "line: %03l/%03L "
local percentage = "[%3p%%]"
return string.format(
"%s%s%s%s%s%s%s%s%s%s%s%s",
separator,
mode,
open_file,
buf_nr,
right_align,
separator,
file_format,
file_type,
encoding,
separator,
line_no,
percentage
)
end
return M