refactor(nvim): better dir structure

This commit is contained in:
2021-11-10 13:09:54 +01:00
parent 3c81fd59fe
commit 59b5bdce99
21 changed files with 358 additions and 148 deletions

View File

@ -0,0 +1,13 @@
local cmd = vim.cmd;
cmd [[
augroup auto_format
au!
au BufWritePre * silent! lua vim.lsp.buf.formatting_sync(nil, 300)<CR>
augroup END
augroup highlight_yank
au!
au TextYankPost * silent! lua vim.highlight.on_yank { timeout = 150 }
augroup END
]]

View File

@ -0,0 +1,51 @@
-- luasnip setup
local luasnip = require "luasnip"
local cmp = require "cmp"
cmp.setup {
completion = {
completeopt = "menu,menuone,noselect"
},
preselect = "none",
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end
},
mapping = {
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.close(),
["<CR>"] = cmp.mapping.confirm {
behavior = cmp.ConfirmBehavior.Replace,
select = true
},
["<Tab>"] = function(fallback)
if vim.fn.pumvisible() == 1 then
vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<C-n>", true, true, true), "n")
elseif luasnip.expand_or_jumpable() then
vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-expand-or-jump", true, true, true), "")
else
fallback()
end
end,
["<S-Tab>"] = function(fallback)
if vim.fn.pumvisible() == 1 then
vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<C-p>", true, true, true), "n")
elseif luasnip.jumpable(-1) then
vim.fn.feedkeys(vim.api.nvim_replace_termcodes("<Plug>luasnip-jump-prev", true, true, true), "")
else
fallback()
end
end
},
sources = {
{name = "nvim_lsp"},
{name = "luasnip"},
{name = "path"}
}
}
-- The nvim-cmp almost supports LSP's capabilities so You should advertise it to LSP servers..
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities = require("cmp_nvim_lsp").update_capabilities(capabilities)

View File

@ -0,0 +1,7 @@
local fn = vim.fn
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', '--depth=1', 'https://github.com/wbthomason/packer.nvim', install_path})
end

View File

@ -0,0 +1,63 @@
local map = vim.api.nvim_set_keymap
local g = vim.g
local options = {noremap = true}
local remap = {noremap = false}
--map("n", "<Space>", "<Nop>", remap)
--map("n", " ", "<Nop>", remap)
g.mapleader = " "
map("n", "<C-p>", ":Telescope find_files<CR>", options)
--map("n", "<C-f>", ":Telescope grep_string<CR>", options)
map("n", "<C-f>", ":Telescope live_grep<CR>", options)
map("n", "<Leader>c", ":CodeActionMenu<CR>", remap)
-- LSP Functionality
map("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", options)
map("n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", options)
map("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", options)
map("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", options)
map("n", "K", "<Cmd>lua vim.lsp.buf.hover()<CR>", options)
map("n", "<Leader>e", "<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>", options)
map("n", "<Leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", options)
map("n", "<Leader><C-f>", "<cmd>lua vim.lsp.buf.code_action()<CR>", options)
map("n", "[d", "<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>", options)
map("n", "]d", "<cmd>lua vim.lsp.diagnostic.goto_next()<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)
-- Find file in NvimTree
map("n", "<Leader>f", ":NvimTreeFindFile<CR><c-w>", 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)
-- Run Requests
map("n", "<Leader>r", "<cmd>lua require('rest-nvim').run()<CR>", options)
-- Close on q
map("n", "<Leader>q", "<Esc>:q<CR>", options)
-- Open Nerdtree
map("n", "<C-n>", ":NvimTreeToggle<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})

View File

@ -0,0 +1,68 @@
local Terminal = require("toggleterm.terminal").Terminal
local u = require("utils")
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()
u.ReloadConfig()
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
}

View File

@ -0,0 +1,224 @@
local nvim_lsp = require "lspconfig"
local lsp_status = require("lsp-status")
local utils = require("utils")
-- 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}
-- Set some keybinds conditional on server capabilities
if client.resolved_capabilities.document_formatting then
vim.cmd [[command Format :lua vim.lsp.buf.formatting()<CR>]]
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
elseif client.resolved_capabilities.document_range_formatting then
vim.cmd [[command Format :lua vim.lsp.buf.range_formatting()<CR>]]
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()
augroup END
]],
false
)
end
end
local system_name = ""
-- Lua Language Server
if vim.fn.has("mac") == 1 then
system_name = "macOS"
elseif vim.fn.has("unix") == 1 then
system_name = "Linux"
elseif vim.fn.has("win32") == 1 then
system_name = "Windows"
else
print("Unsupported system for sumneko")
end
local sumneko_root_path = "/home/jim/.local/share/lua-language-server"
local sumneko_binary = sumneko_root_path .. "/bin/" .. system_name .. "/lua-language-server"
local runtime_path = vim.split(package.path, ";")
table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init.lua")
nvim_lsp.sumneko_lua.setup {
on_attach = on_attach,
capabilities = lsp_status.capabilities,
cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"},
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
-- Setup your lua path
path = runtime_path
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = {"vim"}
},
workspace = {
-- Make the server aware of Neovim runtime files
library = vim.api.nvim_get_runtime_file("", true)
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = {
enable = false
}
}
}
}
-- Go Language Server
nvim_lsp.gopls.setup {
on_attach = on_attach,
capabilities = lsp_status.capabilities
}
-- Html Setup
nvim_lsp.html.setup {
on_attach = on_attach,
capabilities = lsp_status.capabilities,
filetypes = {"html"}
}
-- Svelte Language Server
nvim_lsp.svelte.setup {
on_attach = on_attach,
capabilities = lsp_status.capabilities
}
-- Typescript Language Server
local function organize_imports()
local params = {
command = "_typescript.organizeImports",
arguments = {vim.api.nvim_buf_get_name(0)},
title = ""
}
vim.lsp.buf.execute_command(params)
end
nvim_lsp.tsserver.setup {
on_attach = on_attach,
capabilities = lsp_status.capabilities,
commands = {
OrganizeImports = {
organize_imports,
description = "Organize Imports"
}
}
}
-- 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",
"jsonc",
"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

@ -0,0 +1,14 @@
local lsp_installer = require("nvim-lsp-installer")
lsp_installer.on_server_ready(function(server)
local opts = {}
-- (optional) Customize the options passed to the server
-- if server.name == "tsserver" then
-- opts.root_dir = function() ... end
-- end
-- This setup() function is exactly the same as lspconfig's setup function (:help lspconfig-quickstart)
server:setup(opts)
vim.cmd [[ do User LspAttachBuffers ]]
end)

View File

@ -0,0 +1,68 @@
local Terminal = require("toggleterm.terminal").Terminal
local u = require("utils")
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()
u.ReloadConfig()
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
}

View File

@ -0,0 +1,67 @@
return require("packer").startup(function()
-- Let packer manage itself
use "wbthomason/packer.nvim"
-- General Helper Functions
use "nvim-lua/plenary.nvim"
use "alexghergh/nvim-tmux-navigation"
-- Theming Section
use "kaicataldo/material.vim"
use "xiyaowong/nvim-transparent"
-- Layout Plugins
use "kyazdani42/nvim-web-devicons"
use "kyazdani42/nvim-tree.lua"
use "nvim-lua/popup.nvim"
use "mhinz/vim-startify"
use "lukas-reineke/indent-blankline.nvim"
use "karb94/neoscroll.nvim"
use "tpope/vim-fugitive"
-- Code Navigation
use "dense-analysis/ale"
use "nathanmsmith/nvim-ale-diagnostic"
use "junegunn/fzf"
use "nvim-telescope/telescope.nvim"
-- Postman like features
use "NTBBloodbath/rest.nvim"
-- Obsidian / Roam like features
-- use "lervag/wiki.vim"
-- Syntax / Autocomplete
use "preservim/nerdcommenter"
use "neovim/nvim-lspconfig"
use "williamboman/nvim-lsp-installer"
use "nvim-lua/lsp-status.nvim"
use "hrsh7th/nvim-cmp"
use "hrsh7th/cmp-nvim-lsp"
use "hrsh7th/cmp-path"
use "weilbith/nvim-code-action-menu" -- Need to find better alternative
use "L3MON4D3/LuaSnip"
use "windwp/nvim-autopairs"
use "neoclide/jsonc.vim"
use {
'nvim-treesitter/nvim-treesitter',
run = ':TSUpdate'
}
-- Preview Markdown
use "ellisonleao/glow.nvim"
-- Autoformat
--use "sbdchd/neoformat"
-- General Popup Window
use "akinsho/nvim-toggleterm.lua"
-- Database Feature
use "tpope/vim-dadbod"
use "kristijanhusak/vim-dadbod-ui"
end)

View File

@ -0,0 +1,26 @@
local parser_configs = require("nvim-treesitter.parsers").get_parser_configs()
parser_configs.http = {
install_info = {
url = "https://github.com/NTBBloodbath/tree-sitter-http",
files = {"src/parser.c"},
branch = "main"
}
}
require "nvim-treesitter.configs".setup {
ensure_installed = {
"bash",
"yaml",
"http",
"svelte",
"css",
"svelte",
"typescript",
"javascript",
"go",
"lua",
"yaml"
},
highlight = {enable = true}
}

View File

@ -0,0 +1,41 @@
local M = {}
local cmd = vim.cmd
function M.create_augroup(autocmds, name)
cmd('augroup ' .. name)
cmd('autocmd!')
for _, autocmd in ipairs(autocmds) do
cmd('autocmd ' .. table.concat(autocmd, ' '))
end
cmd('augroup END')
end
function M.ReloadConfig()
for name,_ in pairs(package.loaded) do
if name:match('^cnull') then
package.loaded[name] = nil
end
end
dofile(vim.env.MYVIMRC)
end
local function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
function M.has_plugin(pluginName)
local status = pcall(require, pluginName);
return status
end
return M