From c40c6dbeefd93ecefc17f67a193dc901879b6c60 Mon Sep 17 00:00:00 2001 From: Jim Richter Date: Thu, 12 Aug 2021 13:12:30 +0200 Subject: [PATCH] feat: refactor config to use lua modules --- configs/init.lua | 205 ++++------------------- configs/lua/autocomplete.lua | 31 ++++ configs/lua/autoformatter.lua | 57 +++++++ configs/lua/keymappings.lua | 39 +++++ configs/lua/lazy-git.lua | 67 ++++++++ configs/lua/lsp-utils.lua | 304 ++++++++++++++++++++++++++++++++++ configs/zsh/aliases.sh | 5 +- configs/zsh/functions.zsh | 1 - configs/zsh/functions/y.zsh | 21 --- 9 files changed, 535 insertions(+), 195 deletions(-) create mode 100644 configs/lua/autocomplete.lua create mode 100644 configs/lua/autoformatter.lua create mode 100644 configs/lua/keymappings.lua create mode 100644 configs/lua/lazy-git.lua create mode 100644 configs/lua/lsp-utils.lua delete mode 100644 configs/zsh/functions/y.zsh diff --git a/configs/init.lua b/configs/init.lua index 8f6a2db..269b753 100644 --- a/configs/init.lua +++ b/configs/init.lua @@ -14,6 +14,26 @@ 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]] +vim.cmd [[set undofile]] + +-- Apply Theme +o.syntax = "enable" +o.termguicolors = true +g.NERDTreeShowHidden = true +g.NERDTreeAutoDeleteBuffer = true +g.NERDTreeMinimalUI = true +g.NERDTreeDirArrows = true +g.hidden = true +g.material_theme_style = "ocean_community" +vim.cmd [[colorscheme material]] + +-- KeyBindings +g.mapleader = " " +require "keymappings" + +-- Remove background color +vim.cmd [[highlight Normal guibg=none]] +vim.cmd [[highlight NonText guibg=none]] require "paq" { "savq/paq-nvim", -- Let Paq manage itself @@ -29,196 +49,37 @@ require "paq" { "nvim-telescope/telescope.nvim", -- Syntax / Autocomplete "neovim/nvim-lspconfig", - "nvim-lua/completion-nvim", + "nvim-lua/lsp-status.nvim", + "hrsh7th/nvim-compe", {"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}} +require "nvim-treesitter.configs".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) +-- Toggleterm / Lazygit setup +require "lazy-git" -- Autocommands u.create_augroup( { - {"VimEnter", "*", "NERDTree"}, - {"VimEnter", "*", "if &filetype !=# 'gitcommit' | NERDTree | wincmd p | endif"} + {"VimEnter", "*", "if (@% == '') | NERDTree | endif"}, + {"BufEnter", "*", 'if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif'} }, "Nerdtree" ) --- LSP Config -local lsp = require "lspconfig" -local completion = require "completion" +-- Autocompletion Setup +require "autocomplete" -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} +-- LSP Config +local lsp_utils = require "lsp-utils" 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" - } - } -} +-- Autoformat +require "autoformatter" diff --git a/configs/lua/autocomplete.lua b/configs/lua/autocomplete.lua new file mode 100644 index 0000000..9178459 --- /dev/null +++ b/configs/lua/autocomplete.lua @@ -0,0 +1,31 @@ +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 = "none", + 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, + spell = true, + treesitter = true + } +} diff --git a/configs/lua/autoformatter.lua b/configs/lua/autoformatter.lua new file mode 100644 index 0000000..157b633 --- /dev/null +++ b/configs/lua/autoformatter.lua @@ -0,0 +1,57 @@ +local u = require "utils" + +-- 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 + } + } + } +) diff --git a/configs/lua/keymappings.lua b/configs/lua/keymappings.lua new file mode 100644 index 0000000..50fce50 --- /dev/null +++ b/configs/lua/keymappings.lua @@ -0,0 +1,39 @@ +local map = vim.api.nvim_set_keymap +local g = vim.g + +options = {noremap = true} +remap = {noremap = false} + +map("n", "", "", remap) +map("n", " ", "", remap) +g.mapleader = " " + +map("n", "", ":Telescope find_files", options) + +-- Navigate Buffers +map("n", "", "h", options) +map("n", "", "j", options) +map("n", "", "k", options) +map("n", "", "l", options) + +-- I aint no weak boy +map("n", "", ":echo 'No Left for you'", options) +map("n", "", ":echo 'No Right for you'", options) +map("n", "", ":echo 'No Up for you'", options) +map("n", "", ":echo 'No Down for you'", options) + +-- Close on q +map("n", "q", ":q", options) + +-- Open Nerdtree +map("n", "", ":NERDTreeToggle", options) + +-- Make ctrl+s work +map("n", "", ":w", options) +map("i", "", ":wi", options) + +-- Update vim config +map("n", "", ":source $MYVIMRC", options) + +-- Y yank until the end of line +map("n", "Y", "y$", {noremap = true}) diff --git a/configs/lua/lazy-git.lua b/configs/lua/lazy-git.lua new file mode 100644 index 0000000..151b1cf --- /dev/null +++ b/configs/lua/lazy-git.lua @@ -0,0 +1,67 @@ +local Terminal = require("toggleterm.terminal").Terminal +local lazygit = + Terminal:new( + { + cmd = "lazygit", + dir = "git_dir", + direction = "float", + float_opts = { + winblend = 0, + border = "shadow" + }, + on_open = function(term) + vim.cmd("startinsert!") + vim.api.nvim_buf_set_keymap(term.bufnr, "n", "q", "close", {noremap = true, silent = true}) + end, + on_close = function(term) + Terminal:close() + end + } +) + +function _lazygit_toggle() + lazygit:toggle() +end + +vim.api.nvim_set_keymap("n", "", "lua _lazygit_toggle()", {noremap = true, silent = true}) + +local pnpm = + Terminal:new( + { + cmd = "pnpm dev", + dir = "git_dir", + size = 5, + direction = "vertical", + on_close = function(term) + Terminal:close() + end + } +) + +function _pnpm_toggle() + pnpm:toggle() +end + +vim.api.nvim_set_keymap("n", "d", "lua _pnpm_toggle()", {noremap = true, silent = true}) + +local nvimConfig = + Terminal:new( + { + cmd = "cd $HOME/.dotfiles && nvim configs/init.lua && cd -", + direction = "float", + on_close = function(term) + Terminal:close() + vim.api.nvim_command(":source $MYVIMRC") + end + } +) + +function _nvimConfig_toggle() + nvimConfig:toggle() +end + +vim.api.nvim_set_keymap("n", "", "lua _nvimConfig_toggle()", {noremap = true, silent = true}) + +require("toggleterm").setup { + shade_terminals = true +} diff --git a/configs/lua/lsp-utils.lua b/configs/lua/lsp-utils.lua new file mode 100644 index 0000000..3f64bd4 --- /dev/null +++ b/configs/lua/lsp-utils.lua @@ -0,0 +1,304 @@ +local nvim_lsp = require "lspconfig" +local lsp_status = require("lsp-status") + +-- function to attach completion when setting up lsp +local function 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") + + -- 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", "gr", "lua vim.lsp.buf.references()", opts) + buf_set_keymap("n", "gi", "lua vim.lsp.buf.implementation()", opts) + buf_set_keymap("n", "K", "lua vim.lsp.buf.hover()", opts) + buf_set_keymap("n", "rn", "lua vim.lsp.buf.rename()", 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) + + -- Set some keybinds conditional on server capabilities + if client.resolved_capabilities.document_formatting then + buf_set_keymap("n", "f", "lua vim.lsp.buf.formatting()", opts) + elseif client.resolved_capabilities.document_range_formatting then + buf_set_keymap("n", "f", "lua vim.lsp.buf.range_formatting()", opts) + end + + -- Set autocommands conditional on server_capabilities + if client.resolved_capabilities.document_highlight then + vim.api.nvim_exec( + [[ + augroup lsp_document_highlight + autocmd! * + autocmd CursorHold lua vim.lsp.buf.document_highlight() + autocmd CursorMoved lua vim.lsp.buf.clear_references() + " autocmd CursorHold *.* :lua vim.lsp.diagnostic.show_line_diagnostics() + autocmd BufWritePre * lua vim.lsp.buf.formatting_sync(nil, 300) + augroup END + ]], + false + ) + end +end +-- Use a loop to conveniently both setup defined servers +-- and map buffer local keybindings when the language server attaches +local servers = { + "tsserver", + "svelte" +} +for _, lsp in ipairs(servers) do + nvim_lsp[lsp].setup { + on_attach = on_attach, + capabilities = lsp_status.capabilities + } +end + +-- Setup diagnostics formaters and linters for non LSP provided files +nvim_lsp.diagnosticls.setup { + on_attach = on_attach, + capabilities = lsp_status.capabilities, + cmd = {"diagnostic-languageserver", "--stdio"}, + filetypes = { + "lua", + "sh", + "markdown", + "json", + "yaml", + "toml" + }, + init_options = { + linters = { + shellcheck = { + command = "shellcheck", + debounce = 100, + args = {"--format", "json", "-"}, + sourceName = "shellcheck", + parseJson = { + line = "line", + column = "column", + endLine = "endLine", + endColumn = "endColumn", + message = "${message} [${code}]", + security = "level" + }, + securities = { + error = "error", + warning = "warning", + info = "info", + style = "hint" + } + }, + markdownlint = { + command = "markdownlint", + isStderr = true, + debounce = 100, + args = {"--stdin"}, + offsetLine = 0, + offsetColumn = 0, + sourceName = "markdownlint", + formatLines = 1, + formatPattern = { + "^.*?:\\s?(\\d+)(:(\\d+)?)?\\s(MD\\d{3}\\/[A-Za-z0-9-/]+)\\s(.*)$", + { + line = 1, + column = 3, + message = {4} + } + } + } + }, + filetypes = { + sh = "shellcheck", + markdown = "markdownlint" + }, + formatters = { + shfmt = { + command = "shfmt", + args = {"-i", "2", "-bn", "-ci", "-sr"} + }, + prettier = { + command = "prettier", + args = {"--stdin-filepath", "%filepath"} + } + }, + formatFiletypes = { + sh = "shfmt", + json = "prettier", + yaml = "prettier", + toml = "prettier", + markdown = "prettier", + lua = "prettier" + } + } +} + +-- Enable diagnostics +vim.lsp.handlers["textDocument/publishDiagnostics"] = + vim.lsp.with( + vim.lsp.diagnostic.on_publish_diagnostics, + { + underline = true, + virtual_text = false, + signs = true, + update_in_insert = true + } +) +local lsp_status = require("lsp-status") + +-- function to attach completion when setting up lsp +local function 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") + + -- 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", "gr", "lua vim.lsp.buf.references()", opts) + buf_set_keymap("n", "gi", "lua vim.lsp.buf.implementation()", opts) + buf_set_keymap("n", "K", "lua vim.lsp.buf.hover()", opts) + buf_set_keymap("n", "rn", "lua vim.lsp.buf.rename()", 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) + + -- Set some keybinds conditional on server capabilities + if client.resolved_capabilities.document_formatting then + buf_set_keymap("n", "f", "lua vim.lsp.buf.formatting()", opts) + elseif client.resolved_capabilities.document_range_formatting then + buf_set_keymap("n", "f", "lua vim.lsp.buf.range_formatting()", opts) + end + + -- Set autocommands conditional on server_capabilities + if client.resolved_capabilities.document_highlight then + vim.api.nvim_exec( + [[ + augroup lsp_document_highlight + autocmd! * + autocmd CursorHold lua vim.lsp.buf.document_highlight() + autocmd CursorMoved lua vim.lsp.buf.clear_references() + " autocmd CursorHold *.* :lua vim.lsp.diagnostic.show_line_diagnostics() + autocmd BufWritePre * lua vim.lsp.buf.formatting_sync(nil, 300) + augroup END + ]], + false + ) + end +end +-- Use a loop to conveniently both setup defined servers +-- and map buffer local keybindings when the language server attaches +local servers = { + "tsserver", + "svelte" +} +for _, lsp in ipairs(servers) do + nvim_lsp[lsp].setup { + on_attach = on_attach, + capabilities = lsp_status.capabilities + } +end + +-- Setup diagnostics formaters and linters for non LSP provided files +nvim_lsp.diagnosticls.setup { + on_attach = on_attach, + capabilities = lsp_status.capabilities, + cmd = {"diagnostic-languageserver", "--stdio"}, + filetypes = { + "lua", + "sh", + "markdown", + "json", + "yaml", + "toml" + }, + init_options = { + linters = { + shellcheck = { + command = "shellcheck", + debounce = 100, + args = {"--format", "json", "-"}, + sourceName = "shellcheck", + parseJson = { + line = "line", + column = "column", + endLine = "endLine", + endColumn = "endColumn", + message = "${message} [${code}]", + security = "level" + }, + securities = { + error = "error", + warning = "warning", + info = "info", + style = "hint" + } + }, + markdownlint = { + command = "markdownlint", + isStderr = true, + debounce = 100, + args = {"--stdin"}, + offsetLine = 0, + offsetColumn = 0, + sourceName = "markdownlint", + formatLines = 1, + formatPattern = { + "^.*?:\\s?(\\d+)(:(\\d+)?)?\\s(MD\\d{3}\\/[A-Za-z0-9-/]+)\\s(.*)$", + { + line = 1, + column = 3, + message = {4} + } + } + } + }, + filetypes = { + sh = "shellcheck", + markdown = "markdownlint" + }, + formatters = { + shfmt = { + command = "shfmt", + args = {"-i", "2", "-bn", "-ci", "-sr"} + }, + prettier = { + command = "prettier", + args = {"--stdin-filepath", "%filepath"} + } + }, + formatFiletypes = { + sh = "shfmt", + json = "prettier", + yaml = "prettier", + toml = "prettier", + markdown = "prettier", + lua = "prettier" + } + } +} + +-- Enable diagnostics +vim.lsp.handlers["textDocument/publishDiagnostics"] = + vim.lsp.with( + vim.lsp.diagnostic.on_publish_diagnostics, + { + underline = true, + virtual_text = false, + signs = true, + update_in_insert = true + } +) + diff --git a/configs/zsh/aliases.sh b/configs/zsh/aliases.sh index 40f0e8a..9516319 100644 --- a/configs/zsh/aliases.sh +++ b/configs/zsh/aliases.sh @@ -9,10 +9,10 @@ alias yoink="curl" alias pls='sudo -E env "PATH=$PATH"' +alias zshi="cd ~/.dotfiles && vim configs/init.lua && cd -" alias zshc="cd ~/.dotfiles && vim configs/.zshrc && cd -" alias zshu="source ~/.zshrc" -alias nano="nvim" alias vim="nvim" alias v="nvim" @@ -23,6 +23,9 @@ alias gcm="git commit -m " alias czi="commitizen init cz-conventional-changelog --yarn --dev --exact" alias cz="git-cz" +alias D="pnpm dev" +alias B="pnpm build" +alias T="pnpm test" alias lt="tree -L 2 --filelimit 150 --dirsfirst" diff --git a/configs/zsh/functions.zsh b/configs/zsh/functions.zsh index 29c218e..35c633f 100644 --- a/configs/zsh/functions.zsh +++ b/configs/zsh/functions.zsh @@ -2,7 +2,6 @@ source $(dirname "$0")/functions/co.zsh; source $(dirname "$0")/functions/fx.zsh; source $(dirname "$0")/functions/start.zsh; source $(dirname "$0")/functions/wp.zsh; -source $(dirname "$0")/functions/y.zsh; source $(dirname "$0")/functions/fp.zsh; source $(dirname "$0")/functions/rn.zsh; source $(dirname "$0")/functions/sum.zsh; diff --git a/configs/zsh/functions/y.zsh b/configs/zsh/functions/y.zsh deleted file mode 100644 index b5b34c9..0000000 --- a/configs/zsh/functions/y.zsh +++ /dev/null @@ -1,21 +0,0 @@ -function y(){ - word=$(echo "$1" | fold -w 1) - for char in $(echo $word) - do - if [ $char = "i" ]; then - pnpm i -r - elif [ $char = "d" ]; then - pnpm dev - elif [ $char = "b" ]; then - pnpm build - elif [ $char = "a" ]; then - pnpm add - elif [ $char = "u" ]; then - pnpm upgrade - elif [ $char = "t" ]; then - pnpm test - elif [ $char = "c" ]; then - pnpm coverage - fi - done -} \ No newline at end of file