From 1e65796eb8e22fde348fee1fd429770761bfc312 Mon Sep 17 00:00:00 2001 From: Jim Richter Date: Tue, 10 Aug 2021 21:50:49 +0200 Subject: [PATCH] feat: refactor init.vim to use lua --- configs/.bashrc | 1 + configs/.zshrc | 5 + configs/init.lua | 224 ++++++++++++++++++++++++++++++++++++++++ configs/init.vim | 118 --------------------- configs/lua/utils.lua | 13 +++ configs/zsh/aliases.sh | 2 +- install/install-nvim.sh | 16 +-- 7 files changed, 252 insertions(+), 127 deletions(-) create mode 100644 configs/init.lua delete mode 100644 configs/init.vim create mode 100644 configs/lua/utils.lua diff --git a/configs/.bashrc b/configs/.bashrc index 5b28db6..44c31e2 100644 --- a/configs/.bashrc +++ b/configs/.bashrc @@ -117,3 +117,4 @@ export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion [[ -s "/home/jim/.gvm/scripts/gvm" ]] && source "/home/jim/.gvm/scripts/gvm" +[ -z $DISPLAY ] && export DISPLAY=127.0.0.1:0.0 diff --git a/configs/.zshrc b/configs/.zshrc index 4792e4b..a7bf6d0 100644 --- a/configs/.zshrc +++ b/configs/.zshrc @@ -89,3 +89,8 @@ fi # tabtab source for packages # uninstall by removing these lines [[ -f ~/.config/tabtab/zsh/__tabtab.zsh ]] && . ~/.config/tabtab/zsh/__tabtab.zsh || true + +export PNPM_HOME="/home/jim/.local/share/pnpm" +export PATH="$PNPM_HOME:$PATH" + +alias luamake=/home/jim/bin/lua-language-server/3rd/luamake/luamake diff --git a/configs/init.lua b/configs/init.lua new file mode 100644 index 0000000..8f6a2db --- /dev/null +++ b/configs/init.lua @@ -0,0 +1,224 @@ +local u = require("utils") + +local o = vim.o +local opt = vim.opt +local wo = vim.wo +local bo = vim.bo +local g = vim.g +local fn = vim.fn + +-- Global options +o.number = true +o.tabstop = 2 +o.shiftwidth = 2 -- Indents will have a width of 4 +o.softtabstop = 2 -- Sets the number of columns for a TAB +o.expandtab = false -- Expand TABs to spaces +vim.cmd [[set mouse=a]] + +require "paq" { + "savq/paq-nvim", -- Let Paq manage itself + -- Theming Plugins + {"kaicataldo/material.vim", branch = "main"}, + "ryanoasis/vim-devicons", + -- Layout Plugins + "preservim/nerdtree", + "hoob3rt/lualine.nvim", + -- Code Navigation + "nvim-lua/popup.nvim", + "nvim-lua/plenary.nvim", + "nvim-telescope/telescope.nvim", + -- Syntax / Autocomplete + "neovim/nvim-lspconfig", + "nvim-lua/completion-nvim", + {"nvim-treesitter/nvim-treesitter", run = ":TSUpdate"}, + -- Formatting + "mhartington/formatter.nvim", + -- Git Interface + "akinsho/nvim-toggleterm.lua" +} +-- Apply Theme +o.syntax = "enable" +o.termguicolors = true +g.NERDTreeShowHidden = true +g.material_theme_style = "ocean_community" +vim.cmd [[colorscheme material]] +vim.cmd [[highlight Normal guibg=none]] +vim.cmd [[highlight NonText guibg=none]] + +local ts = require "nvim-treesitter.configs" +ts.setup {ensure_installed = "maintained", highlight = {enable = true}} + +local Terminal = require("toggleterm.terminal").Terminal +local lazygit = + Terminal:new( + { + cmd = "lazygit", + dir = "git_dir", + direction = "float", + float_opts = { + border = "double" + }, + -- function to run on opening the terminal + on_open = function(term) + vim.cmd("startinsert!") + vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "close", {noremap = true, silent = true}) + end, + -- function to run on closing the terminal + on_close = function(term) + vim.cmd("Closing terminal") + end + } +) + +function _lazygit_toggle() + lazygit:toggle() +end + +vim.api.nvim_set_keymap("n", "", "lua _lazygit_toggle()", {noremap = true, silent = true}) + +require("toggleterm").setup {} + +-- KeyBindings +local map = vim.api.nvim_set_keymap + +map("n", ",", "", {}) +g.mapleader = "," + +options = {noremap = true} +map("n", "", ":Telescope find_files", options) + +-- Navigate Buffers +map("n", "", "h", options) +map("n", "", "j", options) +map("n", "", "k", options) +map("n", "", "l", options) + +-- Make ctrl+s work +map("n", "", ":w", options) +map("i", "", ":wi", options) + +-- Update vim config +map("n", "", ":source $MYVIMRC", options) + +-- Autocommands +u.create_augroup( + { + {"VimEnter", "*", "NERDTree"}, + {"VimEnter", "*", "if &filetype !=# 'gitcommit' | NERDTree | wincmd p | endif"} + }, + "Nerdtree" +) + +-- LSP Config +local lsp = require "lspconfig" +local completion = require "completion" + +lsp.svelte.setup {on_attach = completion.on_attach} +-- lsp.sumneko_lua.setup {on_attach = completion.on_attach} +lsp.tsserver.setup {on_attach = completion.on_attach} + +opt.completeopt = {"menuone", "noinsert", "noselect"} +opt.shortmess:append({c = true}) + +-- Config Formatter +u.create_augroup( + { + {"BufWritePost", "*.js,*.jsx,*.ts,*.tsx,*.rs,*.lua", "FormatWrite"} + }, + "FormatAutogroup" +) + +require("formatter").setup( + { + logging = false, + filetype = { + typescript = { + -- prettier + function() + return { + exe = "prettier", + args = {"--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote"}, + stdin = true + } + end + }, + javascript = { + -- prettier + function() + return { + exe = "prettier", + args = {"--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote"}, + stdin = true + } + end + }, + svelte = { + -- prettier + function() + return { + exe = "prettier", + args = {"--stdin-filepath", vim.api.nvim_buf_get_name(0), "--single-quote"}, + stdin = true + } + end + }, + lua = { + -- luafmt + function() + return { + exe = "luafmt", + args = {"--indent-count", 2, "--stdin"}, + stdin = true + } + end + } + } + } +) + +-- Linting +lsp.diagnosticls.setup { + filetypes = {"javascript"}, + init_options = { + linters = { + eslint = { + command = "./node_modules/.bin/eslint", + rootPatterns = {".git"}, + debounce = 100, + args = { + "--stdin", + "--stdin-filename", + "%filepath", + "--format", + "json" + }, + sourceName = "eslint", + parseJson = { + errorsRoot = "[0].messages", + line = "line", + column = "column", + endLine = "endLine", + endColumn = "endColumn", + message = "${message} [${ruleId}]", + security = "severity" + }, + securities = { + [2] = "error", + [1] = "warning" + } + } + }, + filetypes = { + javascript = "eslint" + }, + formatters = { + prettier = { + command = "./node_modules/.bin/prettier", + args = {"--stdin-filepath", "%filepath", "--single-quote", "--print-width 120"} + } + }, + formatFiletypes = { + javascript = "prettier" + } + } +} diff --git a/configs/init.vim b/configs/init.vim deleted file mode 100644 index 956a4cf..0000000 --- a/configs/init.vim +++ /dev/null @@ -1,118 +0,0 @@ -set number - -set tabstop=2 " The width of a TAB is set to 4. - " Still it is a \t. It is just that - " Vim will interpret it to be having - " a width of 4. - -set shiftwidth=2 " Indents will have a width of 4 - -set softtabstop=2 " Sets the number of columns for a TAB - -set expandtab " Expand TABs to spaces - -" Plugins will be downloaded under the specified directory. -call plug#begin('~/.vim/plugged') - -" Declare the list of plugins. -Plug 'tpope/vim-sensible' -Plug 'prettier/vim-prettier', { - \ 'do': 'yarn install', - \ 'for': ['javascript', 'typescript', 'css', 'less', 'scss', 'json', 'graphql', 'markdown', 'vue', 'svelte', 'yaml', 'html'] } -Plug 'kaicataldo/material.vim', { 'branch': 'main' } -Plug 'ryanoasis/vim-devicons' -Plug 'preservim/nerdtree' -" For searching through file contents -Plug 'dyng/ctrlsf.vim' - -" A status line to the bottom -" Plug 'itchyny/lightline.vim' -Plug 'vim-airline/vim-airline' - -" Asynchronous Lint Engine -Plug 'dense-analysis/ale' - -" Autocompletion engine -Plug 'neoclide/coc.nvim', {'branch': 'release'} -Plug 'mattn/emmet-vim' -Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && yarn install' } - -" Display Images in Vim -" Plug 'ashisha/image.vim' - -" Svelte support -Plug 'leafOfTree/vim-svelte-plugin' -Plug 'evanleck/vim-svelte', {'branch': 'main'} - -" Multi Cursor select -Plug 'mg979/vim-visual-multi', {'branch': 'master'} - -" Plug 'ctrlpvim/ctrlp.vim' -Plug 'junegunn/fzf', { 'do': { -> fzf#install() } } - -Plug 'kristijanhusak/vim-dadbod-ui' - -Plug 'liuchengxu/vim-clap' - -Plug 'glepnir/dashboard-nvim' - -" List ends here. Plugins become visible to Vim after this call. -call plug#end() - -" Or if you have Neovim >= 0.1.5 -if (has("termguicolors")) - set termguicolors -endif - -let g:svelte_preprocessors = ['typescript'] - -" Theme -syntax enable -let g:material_theme_style = 'ocean-community' -colorscheme material - -" Remove background color -highlight Normal guibg=none -highlight NonText guibg=none - -let g:lightline = { - \ 'colorscheme': 'one', - \ 'background': 'dark', - \ } - -let mapleader = "," -nmap rn (coc-rename) - -" -nmap :FZF - -" COC Configs -set updatetime=300 - -" NerdTREE config -nmap :NERDTreeToggle -let NERDTreeShowHidden=1 -au VimEnter * NERDTree -au VimEnter * if &filetype !=# 'gitcommit' | NERDTree | wincmd p | endif -nmap h -nmap j -nmap k -nmap l - -nnoremap H gT -nnoremap L gt -:tnoremap - -autocmd WinEnter * call s:CloseIfOnlyNerdTreeLeft() - -" Close all open buffers on entering a window if the only -" buffer that's left is the NERDTree buffer -function! s:CloseIfOnlyNerdTreeLeft() - if exists("t:NERDTreeBufName") - if bufwinnr(t:NERDTreeBufName) != -1 - if winnr("$") == 1 - q - endif - endif - endif -endfunction diff --git a/configs/lua/utils.lua b/configs/lua/utils.lua new file mode 100644 index 0000000..e42f7ec --- /dev/null +++ b/configs/lua/utils.lua @@ -0,0 +1,13 @@ +local M = {} +local cmd = vim.cmd + +function M.create_augroup(autocmds, name) + cmd('augroup ' .. name) + cmd('autocmd!') + for _, autocmd in ipairs(autocmds) do + cmd('autocmd ' .. table.concat(autocmd, ' ')) + end + cmd('augroup END') +end + +return M diff --git a/configs/zsh/aliases.sh b/configs/zsh/aliases.sh index e7850a7..40f0e8a 100644 --- a/configs/zsh/aliases.sh +++ b/configs/zsh/aliases.sh @@ -1,7 +1,7 @@ ## ALIASIES ## alias -s {yml,yaml,ts,json,js,vim,rc}=nvim -alias c="code-insiders ." +alias c="code ." alias ca="c -a" alias cr="c -r" diff --git a/install/install-nvim.sh b/install/install-nvim.sh index a99290a..3330666 100755 --- a/install/install-nvim.sh +++ b/install/install-nvim.sh @@ -1,15 +1,15 @@ -if [ -f "$HOME/.config/nvim/init.vim" ]; then - echo " - moving old file to init.vim.BAK" - mv "$HOME/.config/nvim/init.vim" "$HOME/.config/nvim/init.vim.BAK" +if [ -f "$HOME/.config/nvim/init.lua" ]; then + echo " - moving old file to init.lua.BAK" + mv "$HOME/.config/nvim/init.lua" "$HOME/.config/nvim/init.lua.BAK" fi echo " - linking init.vim --> init.vim" mkdir -p $HOME/.config/nvim -ln -s "$HOME/.dotfiles/configs/init.vim" "$HOME/.config/nvim/init.vim" +ln -s "$HOME/.dotfiles/configs/init.lua" "$HOME/.config/nvim/init.lua" +ln -s "$HOME/.dotfiles/configs/lua" "$HOME/.config/nvim/lua" -echo " - installing vim-plug" -sh -c 'curl -fLo "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/autoload/plug.vim --create-dirs \ - https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim' +echo " - installing paq-nvim" +sh -c 'git clone --depth=1 https://github.com/savq/paq-nvim.git "${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/pack/paqs/start/paq-nvim' echo " - installing vim plugins" -nvim --headless +PlugInstall +qa +nvim --headless +PaqSync +qa