feat: refactor config to use lua modules

This commit is contained in:
max_richter 2021-08-12 13:12:30 +02:00
parent 1e65796eb8
commit c40c6dbeef
9 changed files with 535 additions and 195 deletions

View File

@ -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.softtabstop = 2 -- Sets the number of columns for a TAB
o.expandtab = false -- Expand TABs to spaces o.expandtab = false -- Expand TABs to spaces
vim.cmd [[set mouse=a]] 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" { require "paq" {
"savq/paq-nvim", -- Let Paq manage itself "savq/paq-nvim", -- Let Paq manage itself
@ -29,196 +49,37 @@ require "paq" {
"nvim-telescope/telescope.nvim", "nvim-telescope/telescope.nvim",
-- Syntax / Autocomplete -- Syntax / Autocomplete
"neovim/nvim-lspconfig", "neovim/nvim-lspconfig",
"nvim-lua/completion-nvim", "nvim-lua/lsp-status.nvim",
"hrsh7th/nvim-compe",
{"nvim-treesitter/nvim-treesitter", run = ":TSUpdate"}, {"nvim-treesitter/nvim-treesitter", run = ":TSUpdate"},
-- Formatting -- Formatting
"mhartington/formatter.nvim", "mhartington/formatter.nvim",
-- Git Interface -- Git Interface
"akinsho/nvim-toggleterm.lua" "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" require "nvim-treesitter.configs".setup {ensure_installed = "maintained", highlight = {enable = true}}
ts.setup {ensure_installed = "maintained", highlight = {enable = true}}
local Terminal = require("toggleterm.terminal").Terminal -- Toggleterm / Lazygit setup
local lazygit = require "lazy-git"
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", "<cmd>close<CR>", {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", "<C-g>", "<cmd>lua _lazygit_toggle()<CR>", {noremap = true, silent = true})
require("toggleterm").setup {}
-- KeyBindings
local map = vim.api.nvim_set_keymap
map("n", ",", "", {})
g.mapleader = ","
options = {noremap = true}
map("n", "<C-p>", ":Telescope find_files<CR>", options)
-- Navigate Buffers
map("n", "<C-h>", "<C-w>h", options)
map("n", "<C-j>", "<C-w>j", options)
map("n", "<C-k>", "<C-w>k", options)
map("n", "<C-l>", "<C-w>l", options)
-- Make ctrl+s work
map("n", "<C-s>", "<Esc>:w<CR>", options)
map("i", "<C-s>", "<Esc>:w<CR>i", options)
-- Update vim config
map("n", "<C-u>", "<Esc>:source $MYVIMRC<CR>", options)
-- Autocommands -- Autocommands
u.create_augroup( u.create_augroup(
{ {
{"VimEnter", "*", "NERDTree"}, {"VimEnter", "*", "if (@% == '') | NERDTree | endif"},
{"VimEnter", "*", "if &filetype !=# 'gitcommit' | NERDTree | wincmd p | endif"} {"BufEnter", "*", 'if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif'}
}, },
"Nerdtree" "Nerdtree"
) )
-- LSP Config -- Autocompletion Setup
local lsp = require "lspconfig" require "autocomplete"
local completion = require "completion"
lsp.svelte.setup {on_attach = completion.on_attach} -- LSP Config
-- lsp.sumneko_lua.setup {on_attach = completion.on_attach} local lsp_utils = require "lsp-utils"
lsp.tsserver.setup {on_attach = completion.on_attach}
opt.completeopt = {"menuone", "noinsert", "noselect"} opt.completeopt = {"menuone", "noinsert", "noselect"}
opt.shortmess:append({c = true}) opt.shortmess:append({c = true})
-- Config Formatter -- Autoformat
u.create_augroup( require "autoformatter"
{
{"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"
}
}
}

View File

@ -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
}
}

View File

@ -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
}
}
}
)

View File

@ -0,0 +1,39 @@
local map = vim.api.nvim_set_keymap
local g = vim.g
options = {noremap = true}
remap = {noremap = false}
map("n", "<Space>", "<Nop>", remap)
map("n", " ", "<Nop>", remap)
g.mapleader = " "
map("n", "<C-p>", ":Telescope find_files<CR>", options)
-- Navigate Buffers
map("n", "<C-h>", "<C-w>h", options)
map("n", "<C-j>", "<C-w>j", options)
map("n", "<C-k>", "<C-w>k", options)
map("n", "<C-l>", "<C-w>l", options)
-- I aint no weak boy
map("n", "<Left>", ":echo 'No Left for you'<CR><i><dw>", options)
map("n", "<Right>", ":echo 'No Right for you'<CR><dw>", options)
map("n", "<Up>", ":echo 'No Up for you'<CR><dw>", options)
map("n", "<Down>", ":echo 'No Down for you'<CR><dw>", options)
-- Close on q
map("n", "<Leader>q", "<Esc>:q<CR>", options)
-- Open Nerdtree
map("n", "<F6>", ":NERDTreeToggle<CR>", options)
-- Make ctrl+s work
map("n", "<C-s>", "<Esc>:w<CR>", options)
map("i", "<C-s>", "<Esc>:w<CR>i", options)
-- Update vim config
map("n", "<C-u>", "<Esc>:source $MYVIMRC<CR>", options)
-- Y yank until the end of line
map("n", "Y", "y$", {noremap = true})

67
configs/lua/lazy-git.lua Normal file
View File

@ -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", "<cmd>close<CR>", {noremap = true, silent = true})
end,
on_close = function(term)
Terminal:close()
end
}
)
function _lazygit_toggle()
lazygit:toggle()
end
vim.api.nvim_set_keymap("n", "<C-g>", "<cmd>lua _lazygit_toggle()<CR>", {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", "<Leader>d", "<cmd>lua _pnpm_toggle()<CR>", {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", "<Leader><C-i>", "<cmd>lua _nvimConfig_toggle()<CR>", {noremap = true, silent = true})
require("toggleterm").setup {
shade_terminals = true
}

304
configs/lua/lsp-utils.lua Normal file
View File

@ -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", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
buf_set_keymap("n", "<space>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
buf_set_keymap("n", "<space>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
-- Set some keybinds conditional on server capabilities
if client.resolved_capabilities.document_formatting then
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
elseif client.resolved_capabilities.document_range_formatting then
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
end
-- Set autocommands conditional on server_capabilities
if client.resolved_capabilities.document_highlight then
vim.api.nvim_exec(
[[
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> 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", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts)
buf_set_keymap("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opts)
buf_set_keymap("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opts)
buf_set_keymap("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts)
buf_set_keymap("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", opts)
buf_set_keymap("n", "<space>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opts)
buf_set_keymap("n", "<space>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", opts)
buf_set_keymap("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", opts)
buf_set_keymap("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<CR>", opts)
-- Set some keybinds conditional on server capabilities
if client.resolved_capabilities.document_formatting then
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
elseif client.resolved_capabilities.document_range_formatting then
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
end
-- Set autocommands conditional on server_capabilities
if client.resolved_capabilities.document_highlight then
vim.api.nvim_exec(
[[
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> 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
}
)

View File

@ -9,10 +9,10 @@ alias yoink="curl"
alias pls='sudo -E env "PATH=$PATH"' 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 zshc="cd ~/.dotfiles && vim configs/.zshrc && cd -"
alias zshu="source ~/.zshrc" alias zshu="source ~/.zshrc"
alias nano="nvim"
alias vim="nvim" alias vim="nvim"
alias v="nvim" alias v="nvim"
@ -23,6 +23,9 @@ alias gcm="git commit -m "
alias czi="commitizen init cz-conventional-changelog --yarn --dev --exact" alias czi="commitizen init cz-conventional-changelog --yarn --dev --exact"
alias cz="git-cz" alias cz="git-cz"
alias D="pnpm dev"
alias B="pnpm build"
alias T="pnpm test"
alias lt="tree -L 2 --filelimit 150 --dirsfirst" alias lt="tree -L 2 --filelimit 150 --dirsfirst"

View File

@ -2,7 +2,6 @@ source $(dirname "$0")/functions/co.zsh;
source $(dirname "$0")/functions/fx.zsh; source $(dirname "$0")/functions/fx.zsh;
source $(dirname "$0")/functions/start.zsh; source $(dirname "$0")/functions/start.zsh;
source $(dirname "$0")/functions/wp.zsh; source $(dirname "$0")/functions/wp.zsh;
source $(dirname "$0")/functions/y.zsh;
source $(dirname "$0")/functions/fp.zsh; source $(dirname "$0")/functions/fp.zsh;
source $(dirname "$0")/functions/rn.zsh; source $(dirname "$0")/functions/rn.zsh;
source $(dirname "$0")/functions/sum.zsh; source $(dirname "$0")/functions/sum.zsh;

View File

@ -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
}