From 462f0602072db828b12f8f165412c636999043dd Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Wed, 21 Sep 2022 09:16:53 +0200 Subject: [PATCH] Move to lua config --- dotfiles/.config/nvim/init.lua | 8 + dotfiles/.config/nvim/lua/custom/keymaps.lua | 70 +++++++ .../nvim/{init.vim => lua/custom/lsp.lua} | 110 ++-------- .../.config/nvim/lua/custom/nvim-tree.lua | 69 +++++++ dotfiles/.config/nvim/lua/custom/options.lua | 45 +++++ dotfiles/.config/nvim/lua/custom/plugins.lua | 27 +++ .../.config/nvim/lua/custom/treesitter.lua | 24 +++ dotfiles/.config/nvim/lua/custom/utils.lua | 3 + dotfiles/.config/nvim/lua/custom/whichkey.lua | 188 ++++++++++++++++++ 9 files changed, 448 insertions(+), 96 deletions(-) create mode 100644 dotfiles/.config/nvim/init.lua create mode 100644 dotfiles/.config/nvim/lua/custom/keymaps.lua rename dotfiles/.config/nvim/{init.vim => lua/custom/lsp.lua} (61%) create mode 100644 dotfiles/.config/nvim/lua/custom/nvim-tree.lua create mode 100644 dotfiles/.config/nvim/lua/custom/options.lua create mode 100644 dotfiles/.config/nvim/lua/custom/plugins.lua create mode 100644 dotfiles/.config/nvim/lua/custom/treesitter.lua create mode 100644 dotfiles/.config/nvim/lua/custom/utils.lua create mode 100644 dotfiles/.config/nvim/lua/custom/whichkey.lua diff --git a/dotfiles/.config/nvim/init.lua b/dotfiles/.config/nvim/init.lua new file mode 100644 index 0000000..fa8c8f7 --- /dev/null +++ b/dotfiles/.config/nvim/init.lua @@ -0,0 +1,8 @@ +require "custom.plugins" +require "custom.options" +require "custom.lsp" +require "custom.utils" +require "custom.treesitter" +require "custom.keymaps" +require "custom.whichkey" +require "custom.nvim-tree" diff --git a/dotfiles/.config/nvim/lua/custom/keymaps.lua b/dotfiles/.config/nvim/lua/custom/keymaps.lua new file mode 100644 index 0000000..7bf365b --- /dev/null +++ b/dotfiles/.config/nvim/lua/custom/keymaps.lua @@ -0,0 +1,70 @@ +local opts = { noremap = true, silent = true } + +local term_opts = { silent = true } + +-- Shorten function name +local keymap = vim.api.nvim_set_keymap + +--Map \ as leader key +-- keymap("", "", "", opts) +vim.g.mapleader = '\\' +vim.g.maplocalleader = '\\' + +-- Modes +-- normal_mode = "n", +-- insert_mode = "i", +-- visual_mode = "v", +-- visual_block_mode = "x", +-- term_mode = "t", +-- command_mode = "c", + +-- Normal -- +-- Better window navigation +keymap("n", "", "h", opts) +keymap("n", "", "j", opts) +keymap("n", "", "k", opts) +keymap("n", "", "l", opts) + +-- Resize with arrows +keymap("n", "", ":resize -2", opts) +keymap("n", "", ":resize +2", opts) +keymap("n", "", ":vertical resize -2", opts) +keymap("n", "", ":vertical resize +2", opts) + +-- Navigate buffers +keymap("n", "", ":bnext", opts) +keymap("n", "", ":bprevious", opts) + +-- Move text up and down +keymap("n", "", ":m .+1==gi", opts) +keymap("n", "", ":m .-2==gi", opts) + +-- Insert -- +-- Press jk fast to exit insert mode +keymap("i", "jk", "", opts) + +-- Visual -- +-- Stay in indent mode +keymap("v", "<", "", ">gv", opts) + +-- Move text up and down +keymap("v", "", ":m .+1==", opts) +keymap("v", "", ":m .-2==", opts) +keymap("v", "p", '"_dP', opts) + +-- Visual Block -- +-- Move text up and down +keymap("x", "J", ":move '>+1gv-gv", opts) +keymap("x", "K", ":move '<-2gv-gv", opts) +keymap("x", "", ":move '>+1gv-gv", opts) +keymap("x", "", ":move '<-2gv-gv", opts) + +-- Terminal -- +-- Better terminal navigation +-- keymap("t", "", "h", term_opts) +-- keymap("t", "", "j", term_opts) +-- keymap("t", "", "k", term_opts) +-- keymap("t", "", "l", term_opts) + + diff --git a/dotfiles/.config/nvim/init.vim b/dotfiles/.config/nvim/lua/custom/lsp.lua similarity index 61% rename from dotfiles/.config/nvim/init.vim rename to dotfiles/.config/nvim/lua/custom/lsp.lua index 299299d..06b4151 100644 --- a/dotfiles/.config/nvim/init.vim +++ b/dotfiles/.config/nvim/lua/custom/lsp.lua @@ -1,53 +1,4 @@ -call plug#begin(stdpath('data') . '/plugged') -Plug 'neovim/nvim-lspconfig' -Plug 'hrsh7th/cmp-nvim-lsp' -Plug 'hrsh7th/cmp-buffer' -Plug 'hrsh7th/cmp-path' -Plug 'hrsh7th/cmp-cmdline' -Plug 'hrsh7th/nvim-cmp' -Plug 'hrsh7th/cmp-vsnip' -Plug 'hrsh7th/vim-vsnip' -Plug 'nvim-lua/completion-nvim' -Plug 'nvim-lua/diagnostic-nvim' -Plug 'nvim-lua/plenary.nvim' " Dependecy for telescope -Plug 'nvim-lua/telescope.nvim' -Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'} " Dependecy for telescope -Plug 'preservim/nerdtree', { 'on': 'NERDTreeToggle' } -Plug 'rodjek/vim-puppet' -Plug 'tpope/vim-surround' -Plug 'mrk21/yaml-vim' " For hieradata -Plug 'vim-ruby/vim-ruby' " For Facts, Ruby functions, and custom providers -Plug 'WhoIsSethDaniel/toggle-lsp-diagnostics.nvim' -call plug#end() - -" set leaderkey -let mapleader = '\' - -" set complete opts -set completeopt=menu,menuone,noselect - -" Go to last edited line -if has("autocmd") - au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif -endif - -" Telescope settings -nnoremap ff Telescope find_files -nnoremap fg Telescope live_grep -nnoremap fb Telescope buffers -nnoremap fh Telescope help_tags - -"" NERDTree settings -"" Start NERDTree and put the cursor back in the other window. -"autocmd VimEnter * NERDTree | wincmd p -"" If another buffer tries to replace NERDTree, put it in the other window, and bring back NERDTree. -"autocmd BufEnter * if bufname('#') =~ 'NERD_tree_\d\+' && bufname('%') !~ 'NERD_tree_\d\+' && winnr('$') > 1 | -" \ let buf=bufnr() | buffer# | execute "normal! \w" | execute 'buffer'.buf | endif -"" Exit Vim if NERDTree is the only window remaining in the only tab. -"autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif -" -" lsp diagnostics -lua <td (toggle-lsp-diag) - -" Lua config for treesitter -lua <", "o" }, cb = tree_cb "edit" }, + { key = "h", cb = tree_cb "close_node" }, + { key = "v", cb = tree_cb "vsplit" }, + }, + }, + }, +} + diff --git a/dotfiles/.config/nvim/lua/custom/options.lua b/dotfiles/.config/nvim/lua/custom/options.lua new file mode 100644 index 0000000..b271819 --- /dev/null +++ b/dotfiles/.config/nvim/lua/custom/options.lua @@ -0,0 +1,45 @@ +local options = { + backup = false, -- creates a backup file + clipboard = "unnamedplus", -- allows neovim to access the system clipboard + cmdheight = 2, -- more space in the neovim command line for displaying messages + completeopt = { "menu", "menuone", "noselect" }, -- mostly just for cmp + conceallevel = 0, -- so that `` is visible in markdown files + fileencoding = "utf-8", -- the encoding written to a file + hlsearch = true, -- highlight all matches on previous search pattern + ignorecase = true, -- ignore case in search patterns + mouse = "", -- allow the mouse to be used in neovim + pumheight = 10, -- pop up menu height + showmode = true, -- we don't need to see things like -- INSERT -- anymore + showtabline = 2, -- always show tabs + smartcase = true, -- smart case + smartindent = true, -- make indenting smarter again + splitbelow = true, -- force all horizontal splits to go below current window + splitright = true, -- force all vertical splits to go to the right of current window + swapfile = false, -- creates a swapfile + -- termguicolors = true, -- set term gui colors (most terminals support this) + timeoutlen = 100, -- time to wait for a mapped sequence to complete (in milliseconds) + undofile = true, -- enable persistent undo + updatetime = 300, -- faster completion (4000ms default) + writebackup = false, -- if a file is being edited by another program (or was written to file while editing with another program), it is not allowed to be edited + expandtab = true, -- convert tabs to spaces + shiftwidth = 4, -- the number of spaces inserted for each indentation + tabstop = 4, -- insert 2 spaces for a tab + cursorline = true, -- highlight the current line + number = true, -- set numbered lines + relativenumber = false, -- set relative numbered lines + numberwidth = 4, -- set number column width to 2 {default 4} + signcolumn = "yes", -- always show the sign column, otherwise it would shift the text each time + wrap = false, -- display lines as one long line + scrolloff = 8, -- is one of my fav + sidescrolloff = 8, + guifont = "monospace:h17", -- the font used in graphical neovim applications +} + +vim.opt.shortmess:append "c" + +for k, v in pairs(options) do + vim.opt[k] = v +end + +vim.cmd "set whichwrap+=<,>,[,],h,l" +vim.cmd [[set iskeyword+=-]] diff --git a/dotfiles/.config/nvim/lua/custom/plugins.lua b/dotfiles/.config/nvim/lua/custom/plugins.lua new file mode 100644 index 0000000..1993205 --- /dev/null +++ b/dotfiles/.config/nvim/lua/custom/plugins.lua @@ -0,0 +1,27 @@ +local Plug = vim.fn['plug#'] +vim.call('plug#begin', '~/.local/share/nvim/plugged/') +Plug 'WhoIsSethDaniel/toggle-lsp-diagnostics.nvim' +Plug 'hrsh7th/cmp-buffer' +Plug 'hrsh7th/cmp-cmdline' +Plug 'hrsh7th/cmp-nvim-lsp' +Plug 'folke/which-key.nvim' +Plug 'hrsh7th/cmp-nvim-lua' +Plug 'hrsh7th/cmp-path' +Plug 'hrsh7th/cmp-vsnip' +Plug 'hrsh7th/nvim-cmp' +Plug 'hrsh7th/vim-vsnip' +Plug 'mrk21/yaml-vim' -- For hieradata +Plug 'neovim/nvim-lspconfig' +Plug 'nvim-lua/completion-nvim' +Plug 'nvim-lua/diagnostic-nvim' +Plug 'windwp/nvim-autopairs' +Plug 'kyazdani42/nvim-tree.lua' +Plug 'nvim-lua/plenary.nvim' -- Dependecy for telescope +Plug('nvim-treesitter/nvim-treesitter', {['do'] = ':TSUpdate'}) -- Dependecy for telescope +Plug 'nvim-lua/telescope.nvim' +Plug 'rodjek/vim-puppet' +Plug 'tpope/vim-surround' +Plug 'vim-ruby/vim-ruby' -- For Facts, Ruby functions, and custom providers +Plug 'williamboman/nvim-lsp-installer' +Plug('preservim/nerdtree', { on = {'NERDTreeToggle', 'NERDTree' }}) +vim.call('plug#end') diff --git a/dotfiles/.config/nvim/lua/custom/treesitter.lua b/dotfiles/.config/nvim/lua/custom/treesitter.lua new file mode 100644 index 0000000..5505950 --- /dev/null +++ b/dotfiles/.config/nvim/lua/custom/treesitter.lua @@ -0,0 +1,24 @@ +require'nvim-treesitter.configs'.setup { + -- One of "all", "maintained" (parsers with maintainers), or a list of languages + ensure_installed = "maintained", + + -- Install languages synchronously (only applied to `ensure_installed`) + sync_install = false, + + -- List of parsers to ignore installing + ignore_install = { "javascript" }, + + highlight = { + -- `false` will disable the whole extension + enable = true, + + -- list of language that will be disabled + disable = { "c", "rust" }, + + -- Setting this to true will run `:h syntax` and tree-sitter at the same time. + -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation). + -- Using this option may slow down your editor, and you may see some duplicate highlights. + -- Instead of true it can also be a list of languages + additional_vim_regex_highlighting = false, + }, +} diff --git a/dotfiles/.config/nvim/lua/custom/utils.lua b/dotfiles/.config/nvim/lua/custom/utils.lua new file mode 100644 index 0000000..f1908eb --- /dev/null +++ b/dotfiles/.config/nvim/lua/custom/utils.lua @@ -0,0 +1,3 @@ +vim.cmd([[if has("autocmd") + au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif +endif]]) diff --git a/dotfiles/.config/nvim/lua/custom/whichkey.lua b/dotfiles/.config/nvim/lua/custom/whichkey.lua new file mode 100644 index 0000000..13b9ca8 --- /dev/null +++ b/dotfiles/.config/nvim/lua/custom/whichkey.lua @@ -0,0 +1,188 @@ +local status_ok, which_key = pcall(require, "which-key") +if not status_ok then + return +end + +local setup = { + plugins = { + marks = true, -- shows a list of your marks on ' and ` + registers = true, -- shows your registers on " in NORMAL or in INSERT mode + spelling = { + enabled = true, -- enabling this will show WhichKey when pressing z= to select spelling suggestions + suggestions = 20, -- how many suggestions should be shown in the list? + }, + -- the presets plugin, adds help for a bunch of default keybindings in Neovim + -- No actual key bindings are created + presets = { + operators = false, -- adds help for operators like d, y, ... and registers them for motion / text object completion + motions = true, -- adds help for motions + text_objects = true, -- help for text objects triggered after entering an operator + windows = true, -- default bindings on + nav = true, -- misc bindings to work with windows + z = true, -- bindings for folds, spelling and others prefixed with z + g = true, -- bindings for prefixed with g + }, + }, + -- add operators that will trigger motion and text object completion + -- to enable all native operators, set the preset / operators plugin above + -- operators = { gc = "Comments" }, + key_labels = { + -- override the label used to display some keys. It doesn't effect WK in any other way. + -- For example: + -- [""] = "SPC", + -- [""] = "RET", + -- [""] = "TAB", + }, + icons = { + breadcrumb = "»", -- symbol used in the command line area that shows your active key combo + separator = "➜", -- symbol used between a key and it's label + group = "+", -- symbol prepended to a group + }, + popup_mappings = { + scroll_down = "", -- binding to scroll down inside the popup + scroll_up = "", -- binding to scroll up inside the popup + }, + window = { + border = "rounded", -- none, single, double, shadow + position = "bottom", -- bottom, top + margin = { 1, 0, 1, 0 }, -- extra window margin [top, right, bottom, left] + padding = { 2, 2, 2, 2 }, -- extra window padding [top, right, bottom, left] + winblend = 0, + }, + layout = { + height = { min = 4, max = 25 }, -- min and max height of the columns + width = { min = 20, max = 50 }, -- min and max width of the columns + spacing = 3, -- spacing between columns + align = "left", -- align columns left, center or right + }, + ignore_missing = true, -- enable this to hide mappings for which you didn't specify a label + hidden = { "", "", "", "", "call", "lua", "^:", "^ " }, -- hide mapping boilerplate + show_help = true, -- show help message on the command line when the popup is visible + triggers = "auto", -- automatically setup triggers + -- triggers = {""} -- or specify a list manually + triggers_blacklist = { + -- list of mode / prefixes that should never be hooked by WhichKey + -- this is mostly relevant for key maps that start with a native binding + -- most people should not need to change this + i = { "j", "k" }, + v = { "j", "k" }, + }, +} + +local opts = { + mode = "n", -- NORMAL mode + prefix = "", + buffer = nil, -- Global mappings. Specify a buffer number for buffer local mappings + silent = true, -- use `silent` when creating keymaps + noremap = true, -- use `noremap` when creating keymaps + nowait = true, -- use `nowait` when creating keymaps +} + +local mappings = { + ["a"] = { "Alpha", "Alpha" }, + ["b"] = { + "lua require('telescope.builtin').buffers(require('telescope.themes').get_dropdown{previewer = false})", + "Buffers", + }, + ["e"] = { "NvimTreeToggle", "Explorer" }, + ["w"] = { "w!", "Save" }, + ["q"] = { "q!", "Quit" }, + ["c"] = { "Bdelete!", "Close Buffer" }, + ["h"] = { "nohlsearch", "No Highlight" }, + ["f"] = { + "lua require('telescope.builtin').find_files(require('telescope.themes').get_dropdown{previewer = false})", + "Find files", + }, + ["F"] = { "Telescope live_grep theme=ivy", "Find Text" }, + ["P"] = { "lua require('telescope').extensions.projects.projects()", "Projects" }, + + p = { + name = "Packer", + c = { "PackerCompile", "Compile" }, + i = { "PackerInstall", "Install" }, + s = { "PackerSync", "Sync" }, + S = { "PackerStatus", "Status" }, + u = { "PackerUpdate", "Update" }, + }, + + g = { + name = "Git", + g = { "lua _LAZYGIT_TOGGLE()", "Lazygit" }, + j = { "lua require 'gitsigns'.next_hunk()", "Next Hunk" }, + k = { "lua require 'gitsigns'.prev_hunk()", "Prev Hunk" }, + l = { "lua require 'gitsigns'.blame_line()", "Blame" }, + p = { "lua require 'gitsigns'.preview_hunk()", "Preview Hunk" }, + r = { "lua require 'gitsigns'.reset_hunk()", "Reset Hunk" }, + R = { "lua require 'gitsigns'.reset_buffer()", "Reset Buffer" }, + s = { "lua require 'gitsigns'.stage_hunk()", "Stage Hunk" }, + u = { + "lua require 'gitsigns'.undo_stage_hunk()", + "Undo Stage Hunk", + }, + o = { "Telescope git_status", "Open changed file" }, + b = { "Telescope git_branches", "Checkout branch" }, + c = { "Telescope git_commits", "Checkout commit" }, + d = { + "Gitsigns diffthis HEAD", + "Diff", + }, + }, + + l = { + name = "LSP", + a = { "lua vim.lsp.buf.code_action()", "Code Action" }, + d = { + "Telescope lsp_document_diagnostics", + "Document Diagnostics", + }, + w = { + "Telescope lsp_workspace_diagnostics", + "Workspace Diagnostics", + }, + f = { "lua vim.lsp.buf.format{async=true}", "Format" }, + i = { "LspInfo", "Info" }, + I = { "LspInstallInfo", "Installer Info" }, + j = { + "lua vim.lsp.diagnostic.goto_next()", + "Next Diagnostic", + }, + k = { + "lua vim.lsp.diagnostic.goto_prev()", + "Prev Diagnostic", + }, + l = { "lua vim.lsp.codelens.run()", "CodeLens Action" }, + q = { "lua vim.lsp.diagnostic.set_loclist()", "Quickfix" }, + r = { "lua vim.lsp.buf.rename()", "Rename" }, + s = { "Telescope lsp_document_symbols", "Document Symbols" }, + S = { + "Telescope lsp_dynamic_workspace_symbols", + "Workspace Symbols", + }, + }, + s = { + name = "Search", + b = { "Telescope git_branches", "Checkout branch" }, + c = { "Telescope colorscheme", "Colorscheme" }, + h = { "Telescope help_tags", "Find Help" }, + M = { "Telescope man_pages", "Man Pages" }, + r = { "Telescope oldfiles", "Open Recent File" }, + R = { "Telescope registers", "Registers" }, + k = { "Telescope keymaps", "Keymaps" }, + C = { "Telescope commands", "Commands" }, + }, + + t = { + name = "Terminal", + n = { "lua _NODE_TOGGLE()", "Node" }, + u = { "lua _NCDU_TOGGLE()", "NCDU" }, + t = { "lua _HTOP_TOGGLE()", "Htop" }, + p = { "lua _PYTHON_TOGGLE()", "Python" }, + f = { "ToggleTerm direction=float", "Float" }, + h = { "ToggleTerm size=10 direction=horizontal", "Horizontal" }, + v = { "ToggleTerm size=80 direction=vertical", "Vertical" }, + }, +} + +which_key.setup(setup) +which_key.register(mappings, opts) +