From 2c973bb3863e9dfca504e1d857dbfbe59d223891 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Sun, 13 Mar 2022 15:35:55 +0000 Subject: [PATCH] feat: add neovim config --- LICENSE | 20 ++++-- Makefile | 5 ++ helpers/install-neovim-config.sh | 18 +++++ helpers/install-plugins.sh | 14 ++++ neovim/after/ftplugin/go.lua | 7 ++ neovim/after/ftplugin/make.lua | 7 ++ neovim/after/ftplugin/python.lua | 6 ++ neovim/after/ftplugin/sh.lua | 6 ++ neovim/after/plugin/keybindings.lua | 68 +++++++++++++++++++ neovim/after/plugin/lsp.lua | 101 ++++++++++++++++++++++++++++ neovim/after/plugin/plugins.lua | 9 +++ neovim/after/plugin/statusline.lua | 2 + neovim/init.lua | 67 ++++++++++++++++++ neovim/lua/statusline.lua | 68 +++++++++++++++++++ 14 files changed, 394 insertions(+), 4 deletions(-) create mode 100644 Makefile create mode 100755 helpers/install-neovim-config.sh create mode 100755 helpers/install-plugins.sh create mode 100644 neovim/after/ftplugin/go.lua create mode 100644 neovim/after/ftplugin/make.lua create mode 100644 neovim/after/ftplugin/python.lua create mode 100644 neovim/after/ftplugin/sh.lua create mode 100644 neovim/after/plugin/keybindings.lua create mode 100644 neovim/after/plugin/lsp.lua create mode 100644 neovim/after/plugin/plugins.lua create mode 100644 neovim/after/plugin/statusline.lua create mode 100644 neovim/init.lua create mode 100644 neovim/lua/statusline.lua diff --git a/LICENSE b/LICENSE index 2071b23..cae7643 100644 --- a/LICENSE +++ b/LICENSE @@ -1,9 +1,21 @@ MIT License -Copyright (c) +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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5f40f36 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +.PHONY: neovim_config + +neovim_config: + @./helpers/install-neovim-config.sh + @./helpers/install-plugins.sh diff --git a/helpers/install-neovim-config.sh b/helpers/install-neovim-config.sh new file mode 100755 index 0000000..aa909f4 --- /dev/null +++ b/helpers/install-neovim-config.sh @@ -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} diff --git a/helpers/install-plugins.sh b/helpers/install-plugins.sh new file mode 100755 index 0000000..0dd35be --- /dev/null +++ b/helpers/install-plugins.sh @@ -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} diff --git a/neovim/after/ftplugin/go.lua b/neovim/after/ftplugin/go.lua new file mode 100644 index 0000000..4ae46d6 --- /dev/null +++ b/neovim/after/ftplugin/go.lua @@ -0,0 +1,7 @@ +local setlocal = vim.opt_local + +-- Tabs and spaces +setlocal.expandtab = false +setlocal.tabstop = 8 +setlocal.shiftwidth = 8 +setlocal.softtabstop = 8 diff --git a/neovim/after/ftplugin/make.lua b/neovim/after/ftplugin/make.lua new file mode 100644 index 0000000..93fdb51 --- /dev/null +++ b/neovim/after/ftplugin/make.lua @@ -0,0 +1,7 @@ +local setlocal = vim.opt_local + +-- Tabs and spaces +setlocal.expandtab = false +setlocal.tabstop = 4 +setlocal.shiftwidth = 4 +setlocal.softtabstop = 4 diff --git a/neovim/after/ftplugin/python.lua b/neovim/after/ftplugin/python.lua new file mode 100644 index 0000000..82b296e --- /dev/null +++ b/neovim/after/ftplugin/python.lua @@ -0,0 +1,6 @@ +local setlocal = vim.opt_local + +-- Tabs and spaces +setlocal.tabstop = 4 +setlocal.shiftwidth = 4 +setlocal.softtabstop = 4 diff --git a/neovim/after/ftplugin/sh.lua b/neovim/after/ftplugin/sh.lua new file mode 100644 index 0000000..88bc121 --- /dev/null +++ b/neovim/after/ftplugin/sh.lua @@ -0,0 +1,6 @@ +local setlocal = vim.opt_local + +--- Tabs and spaces +setlocal.tabstop = 4 +setlocal.shiftwidth = 4 +setlocal.softtabstop = 4 diff --git a/neovim/after/plugin/keybindings.lua b/neovim/after/plugin/keybindings.lua new file mode 100644 index 0000000..9b78c7e --- /dev/null +++ b/neovim/after/plugin/keybindings.lua @@ -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'' or t'' +end + +function _G.reverse_smart_tab() + return vim.fn.pumvisible() == 1 and t'' or t'' +end + +-- Disable arrow key navigation +vim.api.nvim_set_keymap("n", "", "", { noremap = true }) +vim.api.nvim_set_keymap("n", "", "", { noremap = true }) +vim.api.nvim_set_keymap("n", "", "", { noremap = true }) +vim.api.nvim_set_keymap("n", "", "", { noremap = true }) + +vim.api.nvim_set_keymap("i", "", "", { noremap = true }) +vim.api.nvim_set_keymap("i", "", "", { noremap = true }) +vim.api.nvim_set_keymap("i", "", "", { noremap = true }) +vim.api.nvim_set_keymap("i", "", "", { noremap = true }) + +vim.api.nvim_set_keymap("v", "", "", { noremap = true }) +vim.api.nvim_set_keymap("v", "", "", { noremap = true }) +vim.api.nvim_set_keymap("v", "", "", { noremap = true }) +vim.api.nvim_set_keymap("v", "", "", { noremap = true }) + +-- Copy and paste to/from the OS clipboard +vim.api.nvim_set_keymap("n", "y", "\"+y", { noremap = true}) +vim.api.nvim_set_keymap("n", "yy", "\"+yy", { noremap = true}) +vim.api.nvim_set_keymap("v", "y", "\"+y", { noremap = true}) + +vim.api.nvim_set_keymap("n", "p", "\"+p", { noremap = true}) +vim.api.nvim_set_keymap("n", "P", "\"+P", { noremap = true}) +vim.api.nvim_set_keymap("v", "p", "\"+p", { noremap = true}) +vim.api.nvim_set_keymap("v", "P", "\"+P\"`\"`", { noremap = true}) + +-- copy the whole file to the OS clipboard +vim.api.nvim_set_keymap("n", "c", ":%y+", { noremap = true}) + +-- Control buffer splits +vim.api.nvim_set_keymap("n", "", ":vert resize +5", { noremap = true }) +vim.api.nvim_set_keymap("n", "", ":vert resize -5", { noremap = true }) +vim.api.nvim_set_keymap("n", "", ":resize -5", { noremap = true }) +vim.api.nvim_set_keymap("n", "", ":resize +5", { noremap = true }) + +-- Add matching curly brace +vim.api.nvim_set_keymap("i", "\\{", "{}O", { noremap = true }) +vim.api.nvim_set_keymap("i", ">{", "{}a", { noremap = true }) +vim.api.nvim_set_keymap("i", "<{", "{}i", { noremap = true }) + +-- Add matching parenthesis +vim.api.nvim_set_keymap("i", "\\(", "()O", { noremap = true }) +vim.api.nvim_set_keymap("i", ">(", "()a", { noremap = true }) +vim.api.nvim_set_keymap("i", "<(", "()i", { noremap = true }) + +-- Add matching square brace +vim.api.nvim_set_keymap("i", "\\[", "[]O", { noremap = true }) +vim.api.nvim_set_keymap("i", ">[", "[]a", { noremap = true }) +vim.api.nvim_set_keymap("i", "<[", "[]i", { noremap = true }) + +-- Use the TAB key to cycle through the autocompletion popup menu +vim.api.nvim_set_keymap("i", "", "v:lua.smart_tab()", { expr = true, noremap = true }) +vim.api.nvim_set_keymap("i", "", "v:lua.reverse_smart_tab()", { expr = true, noremap = true }) + +-- Use CTRL+Space to open the autocompletion popup menu +vim.api.nvim_set_keymap("i", "", "", { noremap = true }) diff --git a/neovim/after/plugin/lsp.lua b/neovim/after/plugin/lsp.lua new file mode 100644 index 0000000..a758864 --- /dev/null +++ b/neovim/after/plugin/lsp.lua @@ -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', 'h', 'lua vim.lsp.buf.hover()', { noremap=true, silent=true }) + buf_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', { noremap=true, silent=true }) + buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', { noremap=true, silent=true }) + buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', { noremap=true, silent=true }) + buf_set_keymap('n', 'dl', 'lua vim.lsp.diagnostic.set_loclist()', { noremap=true, silent=true }) + buf_set_keymap('n', 'dn', 'lua vim.lsp.diagnostic.goto_next()', { noremap=true, silent=true }) + buf_set_keymap('n', 'dp', 'lua vim.lsp.diagnostic.goto_prev()', { noremap=true, silent=true }) + buf_set_keymap('n', 'de', 'lua vim.lsp.diagnostic.show_line_diagnostics()', { noremap=true, silent=true }) + buf_set_keymap('n', 'gt', 'lua vim.lsp.buf.type_definition()', { noremap=true, silent=true }) + buf_set_keymap('n', 'rn', 'lua vim.lsp.buf.rename()', { noremap=true, silent=true }) + buf_set_keymap("n", "ff", "lua vim.lsp.buf.formatting()", opts) + buf_set_keymap('n', 'ca', 'lua vim.lsp.buf.code_action()', opts) + buf_set_keymap('n', 'dec', 'lua vim.lsp.buf.declaration()', opts) + --buf_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()', opts) + --buf_set_keymap('n', 'wa', 'lua vim.lsp.buf.add_workspace_folder()', opts) + --buf_set_keymap('n', 'wr', 'lua vim.lsp.buf.remove_workspace_folder()', opts) + --buf_set_keymap('n', 'wl', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', 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, + }, + } + } +} diff --git a/neovim/after/plugin/plugins.lua b/neovim/after/plugin/plugins.lua new file mode 100644 index 0000000..923fae4 --- /dev/null +++ b/neovim/after/plugin/plugins.lua @@ -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) diff --git a/neovim/after/plugin/statusline.lua b/neovim/after/plugin/statusline.lua new file mode 100644 index 0000000..3b3f972 --- /dev/null +++ b/neovim/after/plugin/statusline.lua @@ -0,0 +1,2 @@ +local statusline = require("statusline") +vim.opt.statusline = statusline.output() diff --git a/neovim/init.lua b/neovim/init.lua new file mode 100644 index 0000000..a05afd5 --- /dev/null +++ b/neovim/init.lua @@ -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 = "" -- Use 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" diff --git a/neovim/lua/statusline.lua b/neovim/lua/statusline.lua new file mode 100644 index 0000000..20be131 --- /dev/null +++ b/neovim/lua/statusline.lua @@ -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('')] = "visual(block)", + ["s"] = "select", + ["S"] = "select(line)", + [t('')] = "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