From 721bc03588dda960b02f3a47e138cf495f1e3033 Mon Sep 17 00:00:00 2001 From: Vladan Popovic Date: Sun, 30 Jul 2023 04:26:25 +0200 Subject: [PATCH] huge refactor - tidy up - shuffle code around - use same abstractions for all usecases --- .gitignore | 1 + init.lua | 84 ++---------------------- lua/config/init.lua | 1 + lua/config/treesitter.lua | 19 ++++++ lua/keymappings.lua | 18 ++++++ lua/{lang.lua => lsp.lua} | 102 +++++++++--------------------- lua/lsp_lua.lua | 18 ------ lua/packer_setup.lua | 9 +++ lua/plugins.lua | 1 - lua/settings.lua | 26 ++++++-- lua/theme.lua | 13 ++++ lua/{utils/init.lua => utils.lua} | 2 +- 12 files changed, 116 insertions(+), 178 deletions(-) create mode 100644 lua/config/treesitter.lua rename lua/{lang.lua => lsp.lua} (54%) delete mode 100644 lua/lsp_lua.lua create mode 100644 lua/packer_setup.lua create mode 100644 lua/theme.lua rename lua/{utils/init.lua => utils.lua} (87%) diff --git a/.gitignore b/.gitignore index c84aa4a..fe84bef 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ plugin/packer_compiled.lua +*.spl diff --git a/init.lua b/init.lua index 1dab1e3..d5af289 100644 --- a/init.lua +++ b/init.lua @@ -1,86 +1,12 @@ --- LSP --- Map leader to space +-- remap leader to vim.g.mapleader = ' ' -local fn = vim.fn -local execute = vim.api.nvim_command - -- Sensible defaults require('settings') - --- Auto install packer.nvim if not exists -local install_path = fn.stdpath('data')..'/site/pack/packer/opt/packer.nvim' -if fn.empty(fn.glob(install_path)) > 0 then - execute('!git clone https://github.com/wbthomason/packer.nvim '..install_path) -end -vim.cmd [[packadd packer.nvim]] -vim.cmd 'autocmd BufWritePost plugins.lua PackerCompile' -- Auto compile when there are changes in plugins.lua - -vim.opt.autoindent = true -vim.opt.background = 'light' -vim.opt.errorformat:prepend('%f|%l col %c|%m') -vim.opt.expandtab = true -vim.opt.fileformat = 'unix' -vim.opt.number = true -vim.opt.pastetoggle = '' -vim.opt.shiftwidth = 4 -vim.opt.softtabstop = 4 -vim.opt.tabstop = 4 -vim.opt.textwidth = 79 -vim.opt.undofile = true --- Enable vimrc files per project and disable unsafe commands in project vimrc -vim.opt.exrc = true -vim.opt.secure = true - --- Toggles on different plugins/modes -vim.cmd 'nmap :set nonumber!' -vim.cmd 'nmap :NvimTreeToggle' --- --- Highlight trailing whitespace (darker red) and remove all with Ctrl+k -vim.cmd 'highlight WhitespaceEOL ctermbg=red guibg=#ab0d0d' -vim.cmd 'match WhitespaceEOL /\\s\\+\\%#\\@ :let _s=@/:%s/\\s\\+$//e:let @/=_s' - -vim.cmd 'highlight Visual cterm=bold ctermbg=Grey ctermfg=NONE' -vim.cmd 'highlight Search ctermfg=Black' -vim.cmd 'highlight Comment ctermfg=DarkGrey' -vim.cmd 'highlight SignColumn ctermbg=0' -vim.cmd 'highlight LineNr ctermbg=0 ctermfg=248' -vim.cmd 'highlight Folded ctermbg=Green ctermfg=Black' -vim.cmd 'highlight Pmenu ctermbg=253 guibg=253' -vim.cmd 'highlight Pmenu ctermfg=232 guifg=232' -vim.cmd 'let g:airline_theme=\'papercolor\'' - -local keymap = vim.api.nvim_set_keymap -local default_opts = { noremap = true, silent = true } - --- Use tab and shift+tab to cycle buffers -keymap("n", "", "bn", default_opts) -keymap("n", "", "bp", default_opts) - --- Find files, buffers, grep in folder using Telescope command-line sugar. -keymap("n", "ff", "Telescope find_files", default_opts) -keymap("n", "fg", "Telescope live_grep", default_opts) -keymap("n", "fb", "Telescope buffers", default_opts) -keymap("n", "fh", "Telescope help_tags", default_opts) -keymap("n", "fl", "Telescope git_files", default_opts) - --- Turn spellchecker on -keymap("n", "s", "setlocal spell spelllang=en_us", default_opts) - --- Install plugins +require('packer_setup') require('plugins') - --- Key mappings require('keymappings') - --- Setup Lua language server using submodule -require('lsp_lua') - --- OR you can invoke them individually here ---require('colorscheme') -- color scheme -require('config.compe') -- completion ---require('fugitive') -- fugitive - -require('lang') +require('lsp') +require('config') require('filetypes') +require('theme') diff --git a/lua/config/init.lua b/lua/config/init.lua index 97bca80..53a8971 100644 --- a/lua/config/init.lua +++ b/lua/config/init.lua @@ -1,2 +1,3 @@ -- nvim-compe require('config.compe') +require('config.treesitter') diff --git a/lua/config/treesitter.lua b/lua/config/treesitter.lua new file mode 100644 index 0000000..0643a2b --- /dev/null +++ b/lua/config/treesitter.lua @@ -0,0 +1,19 @@ +require('nvim-treesitter.configs').setup { + ensure_installed = { "lua", "rust", "elm", "python", "yaml", "toml" }, + auto_install = true, + highlight = { + enable = true, + additional_vim_regex_highlighting=false, + }, + ident = { + enable = true, + }, + rainbow = { + enable = true, + extended_mode = false, + max_file_lines = nil, + } +} + +vim.api.nvim_set_hl(0, "@variable", { }) +vim.api.nvim_set_hl(0, "@parameter", { }) diff --git a/lua/keymappings.lua b/lua/keymappings.lua index 958a746..e94a803 100644 --- a/lua/keymappings.lua +++ b/lua/keymappings.lua @@ -1,2 +1,20 @@ local utils = require('utils') utils.map('n', '', 'noh') -- Clear highlights + +-- Use tab and shift+tab to cycle buffers +utils.map("n", "", "bn") +utils.map("n", "", "bp") + +-- Find files, buffers, grep in folder using Telescope command-line sugar. +utils.map("n", "ff", "Telescope find_files") +utils.map("n", "fg", "Telescope live_grep") +utils.map("n", "fb", "Telescope buffers") +utils.map("n", "fh", "Telescope help_tags") +utils.map("n", "fl", "Telescope git_files") +-- Turn spellchecker on +utils.map("n", "s", "setlocal spell spelllang=en_us") +utils.map("n", "", "let _s=@/:%s/\\s\\+$//e:let @/=_s") + +-- Toggles on different plugins/modes +utils.map("n", "", "set nonumber!") +utils.map("n", "", "NvimTreeToggle") diff --git a/lua/lang.lua b/lua/lsp.lua similarity index 54% rename from lua/lang.lua rename to lua/lsp.lua index 1a1c862..b1278da 100644 --- a/lua/lang.lua +++ b/lua/lsp.lua @@ -1,48 +1,34 @@ -USER = vim.fn.expand('$USER') - --- Language specific key mappings ---require('lang.keymappings') - local on_attach = function(client, bufnr) - + local utils = require('utils') require'lsp_signature'.on_attach(client) - 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') + utils.opt("o", "omnifunc", "v:lua.vim.lsp.omnifunc") -- Mappings. - local opts = {noremap = true, silent = true} - 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', 'gi', 'lua vim.lsp.buf.implementation()', opts) - buf_set_keymap('n', 'gr', 'lua vim.lsp.buf.references()', opts) - buf_set_keymap('n', 'K', 'lua vim.lsp.buf.hover()', opts) - buf_set_keymap('n', '', 'lua vim.lsp.buf.signature_help()', opts) - buf_set_keymap('n', '[d', 'lua vim.diagnostic.goto_prev()', opts) - buf_set_keymap('n', ']d', 'lua vim.diagnostic.goto_next()', opts) - buf_set_keymap('n', '[l', 'lua vim.diagnostic.show_line_diagnostics()', opts) - buf_set_keymap('n', ']l', 'lua vim.diagnostic.set_loclist()', opts) - buf_set_keymap('n', 'law', 'lua vim.lsp.buf.add_workspace_folder()', opts) - buf_set_keymap('n', 'lrw', 'lua vim.lsp.buf.remove_workspace_folder()', opts) - buf_set_keymap('n', 'llw', 'lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))', opts) - buf_set_keymap('n', 'lt', 'lua vim.lsp.buf.type_definition()', opts) - buf_set_keymap('n', 'lrn', 'lua vim.lsp.buf.rename()', opts) - buf_set_keymap('n', '', 'lua vim.lsp.buf.code_action()', opts) + utils.map("n", "gD", "lua vim.lsp.buf.declaration()") + utils.map("n", "gd", "lua vim.lsp.buf.definition()") + utils.map("n", "gi", "lua vim.lsp.buf.implementation()") + utils.map("n", "gr", "lua vim.lsp.buf.references()") + utils.map("n", "K", "lua vim.lsp.buf.hover()") + utils.map("n", "", "lua vim.lsp.buf.signature_help()") + utils.map("n", "[d", "lua vim.diagnostic.goto_prev()") + utils.map("n", "]d", "lua vim.diagnostic.goto_next()") + utils.map("n", "[l", "lua vim.diagnostic.show_line_diagnostics()") + utils.map("n", "]l", "lua vim.diagnostic.set_loclist()") + utils.map("n", "", "lua vim.lsp.buf.code_action()") + utils.map("n", "", "lua vim.lsp.buf.rename()()") + utils.map("n", "", "lua vim.lsp.buf.type_definition()") + -- list/add/remove workspace folders, not that I use them often + utils.map("n", "wl", "lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))") + utils.map("n", "wa", "lua vim.lsp.buf.add_workspace_folder()") + utils.map("n", "wr", "lua vim.lsp.buf.remove_workspace_folder()") - -- Set some keybinds conditional on server capabilities + -- Set formatting keybinds conditional on server capabilities if client.server_capabilities.document_formatting then - buf_set_keymap("n", "lf", - "lua vim.lsp.buf.formatting()", opts) + utils.map('n', 'lf', 'lua vim.lsp.buf.formatting()') elseif client.server_capabilities.document_range_formatting then - buf_set_keymap("n", "lf", - "lua vim.lsp.buf.range_formatting()", opts) + utils.map('n', 'lf', 'lua vim.lsp.buf.range_formatting()') end client.server_capabilities.semanticTokensProvider = nil @@ -62,47 +48,18 @@ local on_attach = function(client, bufnr) end end -require('nvim-treesitter.configs').setup { - ensure_installed = { "lua", "rust", "elm", "python", "toml" }, - auto_install = true, - highlight = { - enable = true, - additional_vim_regex_highlighting=false, - }, - ident = { - enable = true, - }, - rainbow = { - enable = true, - extended_mode = false, - max_file_lines = nil, - } -} -vim.api.nvim_set_hl(0, "@variable", { }) -vim.api.nvim_set_hl(0, "@parameter", { }) - - local nvim_lsp = require('lspconfig') local capabilities = vim.lsp.protocol.make_client_capabilities() --- Code actions -capabilities.textDocument.codeAction = { - dynamicRegistration = true, - codeActionLiteralSupport = { - codeActionKind = { - valueSet = (function() - local res = vim.tbl_values(vim.lsp.protocol.CodeActionKind) - table.sort(res) - return res - end)() - } - } -} - +-- show snippets in code completion popup capabilities.textDocument.completion.completionItem.snippetSupport = true; -- LSPs -local servers = {"vimls", "ocamllsp",} +local servers = { + "lua_ls", + "ocamllsp", + "vimls", +} for _, lsp in ipairs(servers) do nvim_lsp[lsp].setup {capabilities = capabilities, on_attach = on_attach} end @@ -148,6 +105,8 @@ nvim_lsp.rust_analyzer.setup({ } }) +vim.g.code_action_menu_window_border = 'single' + -- symbols-outline.nvim vim.g.symbols_outline = { highlight_hovered_item = true, @@ -192,6 +151,5 @@ do table.insert(qflist, d) end end - -- setqflist(qflist) end end diff --git a/lua/lsp_lua.lua b/lua/lsp_lua.lua deleted file mode 100644 index 43c873e..0000000 --- a/lua/lsp_lua.lua +++ /dev/null @@ -1,18 +0,0 @@ --- Your custom attach function for nvim-lspconfig goes here. -local on_attach = function(client, bufnr) - require('completion').on_attach() - - 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') - - -- Mappings - local opts = { noremap=true, silent=true } -end - --- To get builtin LSP running, do something like: --- NOTE: This replaces the calls where you would have before done `require('nvim_lsp').sumneko_lua.setup()` ---require('nlua.lsp.nvim').setup(require('lspconfig'), { --- on_attach = on_attach, ---}) diff --git a/lua/packer_setup.lua b/lua/packer_setup.lua new file mode 100644 index 0000000..12ddb2a --- /dev/null +++ b/lua/packer_setup.lua @@ -0,0 +1,9 @@ +-- Auto install packer.nvim if not exists +local install_path = vim.fn.stdpath('data')..'/site/pack/packer/opt/packer.nvim' +if vim.fn.empty(vim.fn.glob(install_path)) > 0 then + vim.api.nvim_command('!git clone https://github.com/wbthomason/packer.nvim '..install_path) +end + +vim.cmd [[packadd packer.nvim]] +-- Auto compile when there are changes in plugins.lua +vim.cmd 'autocmd BufWritePost plugins.lua PackerCompile' diff --git a/lua/plugins.lua b/lua/plugins.lua index 832c644..bff663e 100644 --- a/lua/plugins.lua +++ b/lua/plugins.lua @@ -106,7 +106,6 @@ return require('packer').startup(function() -- Fugitive for Git use { 'tpope/vim-fugitive' } - -- Fugitive for Git use { 'ray-x/lsp_signature.nvim' } -- Airline status bar diff --git a/lua/settings.lua b/lua/settings.lua index d67e471..a683417 100644 --- a/lua/settings.lua +++ b/lua/settings.lua @@ -1,14 +1,17 @@ local utils = require('utils') - -local cmd = vim.cmd local indent = 4 -cmd 'syntax enable' -cmd 'filetype plugin indent on' +vim.cmd 'syntax enable' +vim.cmd 'filetype plugin indent on' +-- +-- Highlight on yank +vim.cmd 'au TextYankPost * lua vim.highlight.on_yank {on_visual = false}' + utils.opt('b', 'expandtab', true) utils.opt('b', 'shiftwidth', indent) -utils.opt('b', 'smartindent', true) utils.opt('b', 'tabstop', indent) +utils.opt('b', 'softtabstop', indent) +utils.opt('b', 'smartindent', true) utils.opt('o', 'hidden', true) utils.opt('o', 'ignorecase', true) utils.opt('o', 'scrolloff', 4 ) @@ -20,5 +23,14 @@ utils.opt('o', 'wildmode', 'list:longest') utils.opt('w', 'number', true) utils.opt('o', 'clipboard','unnamed,unnamedplus') --- Highlight on yank -vim.cmd 'au TextYankPost * lua vim.highlight.on_yank {on_visual = false}' +-- see smartindent +-- vim.opt.autoindent = true +vim.opt.background = 'light' +vim.opt.errorformat:prepend('%f|%l col %c|%m') +vim.opt.fileformat = 'unix' +vim.opt.pastetoggle = '' +vim.opt.textwidth = 79 +vim.opt.undofile = true +-- Enable vimrc files per project and disable unsafe commands in project vimrc +vim.opt.exrc = true +vim.opt.secure = true diff --git a/lua/theme.lua b/lua/theme.lua new file mode 100644 index 0000000..ad482d8 --- /dev/null +++ b/lua/theme.lua @@ -0,0 +1,13 @@ +-- Highlight trailing whitespace (darker red) and remove all with Ctrl+k +vim.cmd 'highlight WhitespaceEOL ctermbg=red guibg=#ab0d0d' +vim.cmd 'match WhitespaceEOL /\\s\\+\\%#\\@