From 085c17c26d069320167bc45258c2d56e2e53195c Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Thu, 15 Jul 2021 16:28:28 +0800 Subject: [PATCH] Convert Neovim VimL config to Lua Well, I also explored a little bit with the ecosystem of Lua-written Neovim plugins. I think it's ready to flourish along with the existing plugins. Lua is also nice to write compared to VimL especially with the compound data and the like. --- nvim/init.lua | 274 ++++++++++++++++++++++++++++++++++++++++++++++++++ nvim/init.vim | 216 --------------------------------------- 2 files changed, 274 insertions(+), 216 deletions(-) create mode 100644 nvim/init.lua delete mode 100755 nvim/init.vim diff --git a/nvim/init.lua b/nvim/init.lua new file mode 100644 index 0000000..a2fd36b --- /dev/null +++ b/nvim/init.lua @@ -0,0 +1,274 @@ +-- Aliases +local api = vim.api +local g, b = vim.g, vim.b +local cmd = vim.cmd +local highlight = vim.highlight +local opt, opt_local = vim.opt, vim.opt_local +local go = vim.go +local map = vim.api.nvim_set_keymap +local fn = vim.fn + +g['mapleader'] = " " +g['syntax'] = true + +-- Bootstrapping for the package manager +local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim' + +if fn.empty(fn.glob(install_path)) > 0 then + fn.system({'git', 'clone', 'https://github.com/wbthomason/packer.nvim', install_path}) + api.nvim_command('packadd packer.nvim') +end + +cmd [[packadd packer.nvim]] +-- Plugins +require("packer").startup(function() + -- Let the package manager manage itself. + use {"wbthomason/packer.nvim", opt = true} + + -- THEMES! + use {"chriskempson/base16-vim"} + + -- EditorConfig plugin + use {"editorconfig/editorconfig-vim"} + + -- Colorize common color strings + use {'lilydjwg/colorizer'} + + -- A snippets engine. + -- One of the must-haves for me. + use { 'sirver/ultisnips' } + + -- Text editor integration for the browser + use {'subnut/nvim-ghost.nvim', run = ':call nvim_ghost#installer#install()'} + + -- Fuzzy finder of lists + use { + 'nvim-telescope/telescope.nvim', + requires = {{'nvim-lua/popup.nvim'}, {'nvim-lua/plenary.nvim'}} + } + + -- Contains various snippets for UltiSnips. + use {'honza/vim-snippets'} + + -- A completion engine. + -- I chose this engine since it is linked from UltiSnips. + use { "hrsh7th/nvim-compe" } + + -- A linting engine, a DAP client, and an LSP client entered into a bar. + use { + 'dense-analysis/ale', + config = function() + g['ale_disable_lsp'] = 1 + g['ale_linters'] = { + javascript = {"eslint", "prettier"} + } + end + } + use {'neovim/nvim-lspconfig'} + use {'mfussenegger/nvim-dap'} + + -- One of the most popular plugins. + -- Allows to create more substantial status bars. + use { + 'vim-airline/vim-airline', + config = function() + g['airline_powerline_fonts'] = 1 + g['airline#extensions#ale#enabled'] = 1 + end + } + + -- Fuzzy finder for finding files freely and fastly. + use {'junegunn/fzf'} + use {'junegunn/fzf.vim'} + + -- A full LaTeX toolchain plugin for Vim. + -- Also a must-have for me since writing LaTeX can be a PITA. + -- Most of the snippets and workflow is inspired from Gilles Castel's posts (at https://castel.dev/). + use { + 'lervag/vimtex', + cmd = 'ALEEnable', + config = function() + -- Vimtex configuration + g['tex_flavor'] = 'latex' + g['vimtex_view_method'] = 'zathura' + g['vimtex_quickfix_mode'] = 0 + g['tex_conceal'] = 'abdmg' + g['vimtex_compiler_latexmk'] = { + options = { + '-bibtex', + '-shell-escape', + '-verbose', + '-file-line-error', + } + } + + -- I use LuaLaTeX for my documents so let me have it as the default, please? + g['vimtex_compiler_latexmk_engines'] = { + _ = '-lualatex', + pdflatex = '-pdf', + dvipdfex = '-pdfdvi', + lualatex = '-lualatex', + xelatex = '-xelatex', + } + end + } + + -- Enable visuals for addition/deletion of lines in the gutter (side) similar to Visual Studio Code. + use {'airblade/vim-gitgutter'} + + -- Language plugins. + use {'LnL7/vim-nix'} + use {'vmchale/dhall-vim'} +end) + +g['UltiSnipsExpandTrigger'] = "" +g['UltiSnipsEditSplit'] = "context" +g['UltiSnipsSnippetDirectories'] = {vim.env.HOME .. "/.config/nvim/own-snippets", ".snippets"} + +-- Compe configuration +require'compe'.setup { + enabled = true; + autocomplete = true; + debug = false; + min_length = 1; + preselect = 'enable'; + throttle_time = 80; + source_timeout = 200; + resolve_timeout = 800; + incomplete_delay = 400; + max_abbr_width = 100; + max_kind_width = 100; + max_menu_width = 100; + documentation = { + border = { '', '' ,'', ' ', '', '', '', ' ' }, -- the border option is the same as `|help nvim_open_win|` + winhighlight = "NormalFloat:CompeDocumentation,FloatBorder:CompeDocumentationBorder", + max_width = 120, + min_width = 60, + max_height = math.floor(vim.o.lines * 0.3), + min_height = 1, + }; + + source = { + path = true; + buffer = true; + calc = true; + nvim_lsp = true; + nvim_lua = true; + ultisnips = true; + luasnip = true; + }; +} + +local t = function(str) + return vim.api.nvim_replace_termcodes(str, true, true, true) +end + +local check_back_space = function() + local col = vim.fn.col('.') - 1 + if col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then + return true + else + return false + end +end + +-- Use (s-)tab to: +--- move to prev/next item in completion menuone +--- jump to prev/next snippet's placeholder +_G.tab_complete = function() + if vim.fn.pumvisible() == 1 then + return t "" + elseif check_back_space() then + return t "" + else + return vim.fn['compe#complete']() + end +end + +_G.s_tab_complete = function() + if vim.fn.pumvisible() == 1 then + return t "" + else + return t "" + end +end + +-- Editor configuration +opt.completeopt = "menuone,noselect" +opt.encoding = "utf-8" +opt.number = true +opt.relativenumber = true +opt.cursorline = true +opt.expandtab = true +opt.shiftwidth = 4 +opt.tabstop = 4 +opt.conceallevel = 1 +opt.list = true +opt.listchars = { tab = " ", trail = "·" } +opt_local.spell = true +opt.smartindent = true + +-- I have yet to solve how to do the following in Lua, lmao +cmd "highlight clear SpellBad" +cmd "highlight clear SpellLocal" +cmd "highlight clear SpellCap" +cmd "highlight clear SpellRare" +cmd "highlight CursorLineNr ctermfg=cyan" +cmd "highlight Visual term=reverse cterm=reverse" + +-- Keybindings +map('i', 'jk', '', {}) +map('n', 'hr', 'source $MYVIMRC', {}) +map('i', "", "v:lua.tab_complete()", { expr = true }) +map('s', "", "v:lua.tab_complete()", { expr = true }) +map('i', "", "v:lua.s_tab_complete()", { expr = true }) +map('s', "", "v:lua.s_tab_complete()", { expr = true }) +map('i', '', 'compe#confirm("")', { expr = true }) +map('i', '', 'compe#complete()', { expr = true }) + +-- LSP config +local nvim_lsp = require('lspconfig') + +-- Use an on_attach function to only map the following keys +-- after the language server attaches to the current buffer +local on_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 + + -- Enable completion triggered by + buf_set_option('omnifunc', 'v:lua.vim.lsp.omnifunc') + + -- Mappings. + local opts = { noremap=true, silent=true } + + -- See `:help vim.lsp.*` for documentation on any of the below functions + buf_set_keymap('n', 'gD', 'lua vim.lsp.buf.declaration()', opts) + buf_set_keymap('n', 'gd', 'lua vim.lsp.buf.definition()', opts) + buf_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()', opts) + buf_set_keymap('n', 'gi', 'lua vim.lsp.buf.implementation()', 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) + buf_set_keymap('n', 'D', 'lua vim.lsp.buf.type_definition()', opts) + buf_set_keymap('n', 'rn', 'lua vim.lsp.buf.rename()', opts) + buf_set_keymap('n', 'ca', 'lua vim.lsp.buf.code_action()', opts) + buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', opts) + buf_set_keymap('n', 'e', 'lua vim.lsp.diagnostic.show_line_diagnostics()', opts) + buf_set_keymap('n', '[d', 'lua vim.lsp.diagnostic.goto_prev()', opts) + buf_set_keymap('n', ']d', 'lua vim.lsp.diagnostic.goto_next()', opts) + buf_set_keymap('n', 'q', 'lua vim.lsp.diagnostic.set_loclist()', opts) + buf_set_keymap("n", "f", "lua vim.lsp.buf.formatting()", opts) +end + +local capabilities = vim.lsp.protocol.make_client_capabilities() +capabilities.textDocument.completion.completionItem.snippetSupport = true + +-- Enable the following language servers +local servers = { 'clangd', 'rust_analyzer', 'pyright', 'tsserver' } +for _, lsp in ipairs(servers) do + nvim_lsp[lsp].setup { + on_attach = on_attach, + capabilities = capabilities, + } +end diff --git a/nvim/init.vim b/nvim/init.vim deleted file mode 100755 index 42c3ad5..0000000 --- a/nvim/init.vim +++ /dev/null @@ -1,216 +0,0 @@ -""""""""""" -" PLUGINS " -""""""""""" - -" This configuration uses vim-plug (https://github.com/junegunn/vim-plug) as -" the plugin manager. -" Activate it with ':PlugInstall' for the first time (and when adding new plugins). -" And run ':PlugUpgrade' for upgrading the plugins. -call plug#begin('~/.config/nvim/plugged') - -" Nord color scheme -Plug 'arcticicestudio/nord-vim' -Plug 'gruvbox-community/gruvbox' -Plug 'chriskempson/base16-vim' - -" EditorConfig plugin -Plug 'editorconfig/editorconfig-vim' - -" Colorize common color strings -Plug 'lilydjwg/colorizer' - -" A snippets engine. -" One of the must-haves for me. -Plug 'sirver/ultisnips' - -" Text editor integration for the browser -Plug 'subnut/nvim-ghost.nvim', {'do': ':call nvim_ghost#installer#install()'} - -" Setting my private snippets in a consistent home directory and a relative snippets directory for project-specific snippets. -let g:UltiSnipsSnippetDirectories = [$HOME . "/.config/nvim/own-snippets", ".snippets"] -let g:UltiSnipsExpandTrigger="" -let g:UltiSnipsJumpForwardTrigger="" -let g:UltiSnipsJumpBackwardTrigger="" -let g:UltiSnipsEditSplit="context" - -" Contains various snippets for UltiSnips. -Plug 'honza/vim-snippets' - -" A completion engine. -" I chose this engine since it is linked from UltiSnips. -if has('nvim') - Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' } -else - Plug 'Shougo/deoplete.nvim' - Plug 'roxma/nvim-yarp' - Plug 'roxma/vim-hug-neovim-rpc' -endif - -let g:deoplete#enable_at_startup = 1 - -" A linting engine and an LSP client. -Plug 'dense-analysis/ale' - -" One of the most popular plugins. -" Allows to create more substantial status bars. -Plug 'vim-airline/vim-airline' -let g:airline_powerline_fonts = 1 - -if !exists('g:airline_symbols') - let g:airline_symbols = {} -endif - -" Fuzzy finder for finding files freely and fastly. -Plug 'junegunn/fzf' -Plug 'junegunn/fzf.vim' - -" Emmet is an HTML plugin for easy HTML writing -Plug 'mattn/emmet-vim' - -" A full LaTeX toolchain plugin for Vim. -" Also a must-have for me since writing LaTeX can be a PITA. -" Most of the snippets and workflow is inspired from Gilles Castel's posts (at https://castel.dev/). -Plug 'lervag/vimtex' -let g:tex_flavor='latex' -let g:vimtex_view_method='zathura' -let g:vimtex_quickfix_mode=0 -set conceallevel=1 -let g:tex_conceal='abdmg' -let g:vimtex_compiler_latexmk = { - \ 'options': [ - \ '-bibtex', - \ '-shell-escape', - \ '-verbose', - \ '-file-line-error', - \ ] -\} - -" I use LuaLaTeX for my documents so let me have it as the default, please? -let g:vimtex_compiler_latexmk_engines = { - \ '_' : '-lualatex', - \ 'pdflatex' : '-pdf', - \ 'dvipdfex' : '-pdfdvi', - \ 'lualatex' : '-lualatex', - \ 'xelatex' : '-xelatex', - \ 'context (pdftex)' : '-pdf -pdflatex=texexec', - \ 'context (luatex)' : '-pdf -pdflatex=context', - \ 'context (xetex)' : '-pdf -pdflatex=''texexec --xtx''', -\} - -" Enable visuals for addition/deletion of lines in the gutter (side) similar to Visual Studio Code. -Plug 'airblade/vim-gitgutter' - -" Plugin for distraction-free writing. -Plug 'junegunn/goyo.vim' - -" Language plugins. -Plug 'LnL7/vim-nix' -Plug 'vmchale/dhall-vim' -call plug#end() - - - - -""""""""""""""""""""""""" -" EDITOR CONFIGURATIONS " -""""""""""""""""""""""""" -set encoding=utf-8 - -let mapleader=" " - -colorscheme nord - -" Setting number lines in the gutter. -set number relativenumber -highlight CursorLineNr ctermfg=cyan -highlight Visual term=reverse cterm=reverse - -" Setting line highlighting based on the position of the cursor. -set cursorline - -" Set tab to enter spaces, instead. -set expandtab - -" Set entering tab to 4 spaces. -set shiftwidth=4 tabstop=4 - -" Set list and other listing characters (:h listchars). -set list listchars=tab:→\ ,trail:· - -" Enabling spell checker (for your local language, anyway). -setlocal spell -set spelllang=en_us -inoremap u[s1z=`]au - -" Quick escape to default mode. -inoremap jk - -" Instant Goyo toggle. -map w :Goyo - -" Trim all trailing whitespaces. -map s :%s/\s\+$/ - -" Reload $MYVIMRC. -map hr :source $MYVIMRC - -" File explorer toggle. -" Turns out vim (and nvim) has a native file explorer with :Explore. -map ff :Lexplore:vertical resize 40 - -" Changing style of words. -highlight clear SpellBad - -highlight clear SpellLocal - -highlight clear SpellCap - -highlight clear SpellRare - - - - -"""""""""" -" EVENTS " -"""""""""" - -" Show leading spaces. -" SOURCE: https://www.reddit.com/r/vim/comments/5fxsfy/show_leading_spaces/ -hi Conceal guibg=NONE ctermbg=NONE ctermfg=DarkGrey -autocmd BufWinEnter * setl conceallevel=1 -autocmd BufWinEnter * syn match LeadingSpace /\(^ *\)\@<= / containedin=ALL conceal cchar=· -autocmd BufReadPre * setl conceallevel=1 -autocmd BufReadPre * syn match LeadingSpace /\(^ *\)\@<= / containedin=ALL conceal cchar=· - -" The template list is simply an array composed of vector that represents the -" prefix and the suffix of the template file name. -let template_list = [ -\ ["_minted-", ""], -\ ["", ".synctex"], -\] - -" Additional LaTeX files cleanup. -function VimtexAdditionalCleanup(template_list) - call vimtex#compiler#clean(1) - let file_name = expand("%:t:r") - let file_path = expand("%:p:h") . "/" - for template in a:template_list - let prefix = template[0] - let suffix = template[1] - let full_template_path = file_path . prefix . file_name . suffix - call delete(full_template_path, "rf") - endfor -endfunction - -" Initiate LaTeX file compilation at the start and auto clean up. -augroup vimtex_events - au! - " auto-clean - au User QuitPre call VimtexAdditionalCleanup(template_list) - au User VimLeave call VimtexAdditionalCleanup(template_list) - au User VimtexEventQuit call vimtex#compiler#clean(1) - au User VimtexEventQuit call VimtexAdditionalCleanup(template_list) - - " Auto-compile - au User VimtexEventInitPost call vimtex#compiler#compile() -augroup END