migrate to stow

This commit is contained in:
2022-03-20 00:37:41 +03:00
parent 8544e93367
commit 2ef5b95984
41 changed files with 351 additions and 93 deletions

View File

@@ -0,0 +1,3 @@
vim.cmd([[
au TextYankPost * silent! lua vim.highlight.on_yank({timeout = 200})
]])

View File

View File

@@ -0,0 +1,24 @@
require("Comment").setup({
pre_hook = function(ctx)
-- Only calculate commentstring for tsx filetypes
if vim.bo.filetype == "typescriptreact" or vim.bo.filetype == "javascriptreact" then
local U = require("Comment.utils")
-- Detemine whether to use linewise or blockwise commentstring
local type = ctx.ctype == U.ctype.line and "__default" or "__multiline"
-- Determine the location where to calculate commentstring from
local location = nil
if ctx.ctype == U.ctype.block then
location = require("ts_context_commentstring.utils").get_cursor_location()
elseif ctx.cmotion == U.cmotion.v or ctx.cmotion == U.cmotion.V then
location = require("ts_context_commentstring.utils").get_visual_start_location()
end
return require("ts_context_commentstring.internal").calculate_commentstring({
key = type,
location = location,
})
end
end,
})

View File

@@ -0,0 +1,74 @@
vim.g.nvim_tree_show_icons = {
git = 1,
files = 1,
folders = 1,
}
vim.g.nvim_tree_icons = {
default = "",
symlink = "",
git = {
unstaged = "U",
staged = "S",
unmerged = "M",
renamed = "R",
untracked = "N",
deleted = "D",
},
}
vim.g.nvim_tree_git_hl = 1
vim.g.nvim_tree_add_trailing = 1
vim.g.nvim_tree_indent_markers = 1
vim.g.nvim_tree_group_empty = 1
local function git_stage(node)
local cwd = vim.loop.cwd()
local relative_path = string.gsub(node.absolute_path, cwd .. "/", "")
local f = io.popen("git add " .. relative_path, "r")
f:close()
end
local function git_reset(node)
local cwd = vim.loop.cwd()
local relative_path = string.gsub(node.absolute_path, cwd .. "/", "")
local f = io.popen("git reset " .. relative_path, "r")
f:close()
end
-- TODO: add here keymap for git diff window
require("nvim-tree").setup({
disable_netrw = true,
hijack_netrw = true,
open_on_setup = true,
hijack_cursor = true,
open_on_tab = false,
update_cwd = true,
update_focused_file = {
enable = true,
},
view = {
width = 30,
side = "left",
auto_resize = true,
mappings = {
list = {
{ key = "tn", action = "tabnew" },
{ key = "gs", action = "git_stage", action_cb = git_stage },
{ key = "gr", action = "git_reset", action_cb = git_reset },
{ key = "l", action = "edit" },
{ key = "@", action = "cd" },
},
},
},
filters = {
custom = { ".git" },
},
git = {
ignore = true,
},
diagnostics = {
enable = true,
show_on_dirs = true,
icons = { hint = "", info = "", warning = "", error = "" },
},
})

View File

@@ -0,0 +1,26 @@
local nmap = require("user.utils").nmap
local vmap = require("user.utils").vmap
require("gitsigns").setup({
signcolumn = true,
attach_to_untracked = false,
current_line_blame = true,
current_line_blame_opts = {
delay = 1000,
},
current_line_blame_formatter_opts = {
relative_time = true,
},
on_attach = function(bufnr)
nmap("<leader>gs", ":Gitsigns stage_hunk<CR>")
nmap("<leader>gu", ":Gitsigns undo_stage_hunk<CR>")
nmap("<leader>gr", ":Gitsigns reset_hunk<CR>")
nmap("<leader>gp", ":Gitsigns preview_hunk<CR>")
nmap("<leader>gn", ":Gitsigns next_hunk<CR>")
nmap("<leader>gN", ":Gitsigns prev_hunk<CR>")
vmap("gs", ":Gitsigns stage_hunk<CR>")
vmap("gu", ":Gitsigns undo_stage_hunk<CR>")
vmap("gr", ":Gitsigns reset_hunk<CR>")
end,
})

View File

@@ -0,0 +1,225 @@
local lsp_installer = require("nvim-lsp-installer")
local cmp = require("cmp")
local null_ls = require("null-ls")
local list_includes_item = require("user.utils").list_includes_item
local kind_icons = {
Text = "",
Method = "",
Function = "",
Constructor = "",
Field = "",
Variable = "",
Class = "",
Interface = "",
Module = "",
Property = "",
Unit = "",
Value = "",
Enum = "",
Keyword = "",
Snippet = "",
Color = "",
File = "",
Reference = "",
Folder = "",
EnumMember = "",
Constant = "",
Struct = "",
Event = "",
Operator = "",
TypeParameter = "",
}
local formatting = null_ls.builtins.formatting
local diagnostics = null_ls.builtins.diagnostics
null_ls.setup({
sources = {
formatting.prettierd,
formatting.stylua,
formatting.black,
formatting.gofmt,
formatting.goimports,
formatting.shfmt,
},
on_attach = function()
vim.cmd([[
augroup LspFormatting
autocmd! * <buffer>
autocmd BufWritePre <buffer> lua vim.lsp.buf.formatting_sync()
augroup END
]])
end,
})
-- Other formats that work weird with null_ls
vim.cmd([[autocmd BufWritePre *.svelte lua vim.lsp.buf.formatting_sync(nil, 1000)]])
local completion_trigger = "<C-space>"
if vim.fn.has("win32") == 1 then
completion_trigger = "<C-y>"
end
cmp.setup({
snippet = {
expand = function(args)
require("luasnip").lsp_expand(args.body)
end,
},
mapping = {
["<C-j>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Select }),
["<C-k>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Select }),
[completion_trigger] = cmp.mapping(function(fallback)
if cmp.visible() then
cmp.close()
else
cmp.complete()
end
end),
["<TAB>"] = cmp.mapping.confirm({
select = true,
behavior = cmp.SelectBehavior.Insert,
}),
},
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "luasnip" },
}, {
{ name = "path" },
{ name = "buffer" },
}),
completion = {
completeopt = "menu,menuone,noselect,noinsert,preview",
},
experimental = {
ghost_text = true,
},
sorting = {
comparators = {
cmp.config.compare.score,
cmp.config.compare.sort_text,
cmp.config.compare.kind,
},
},
documentation = {
border = { "", "", "", "", "", "", "", "" },
zindex = 999,
format = { "markdown" },
},
formatting = {
format = function(entry, vim_item)
vim_item.kind = string.format("%s %s", kind_icons[vim_item.kind], vim_item.kind)
vim_item.menu = ({
buffer = "[Buffer]",
luasnip = "[Snippet]",
nvim_lua = "[Lua]",
path = "[File]",
})[entry.source.name]
return vim_item
end,
},
})
cmp.setup.cmdline("/", {
sources = {
{ name = "cmdline" },
},
})
cmp.setup.cmdline(":", {
sources = {
{ name = "cmdline" },
},
})
local capabilities = require("cmp_nvim_lsp").update_capabilities(vim.lsp.protocol.make_client_capabilities())
capabilities.textDocument.completion.completionItem.snippetSupport = true
local lsps_with_disabled_formatting = { "tsserver", "gopls", "jsonls", "html" }
local on_attach = function(client, bufnr)
if list_includes_item(lsps_with_disabled_formatting, client.name) then
client.resolved_capabilities.document_formatting = false
client.resolved_capabilities.document_range_formatting = false
end
if client.resolved_capabilities.document_highlight then
vim.cmd([[
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
]])
end
if client.name == "tailwindcss" then
require("tailwindcss-colors").buf_attach(bufnr)
end
require("lsp_signature").on_attach({
bind = true,
hint_enable = false,
}, bufnr)
end
lsp_installer.on_server_ready(function(server)
local opts = {
capabilities = capabilities,
on_attach = on_attach,
}
if server.name == "sumneko_lua" then
local runtime_path = vim.split(package.path, ";")
table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init/lua")
opts.settings = {
Lua = {
runtime = {
version = "LuaJIT",
path = runtime_path,
},
diagnostics = {
library = vim.api.nvim_get_runtime_file("", true),
globals = { "vim" },
},
telemetry = {
enable = false,
},
},
}
end
if server.name == "jsonls" then
opts.settings = {
json = {
schemas = require("schemastore").json.schemas({
select = {
".eslintrc",
"package.json",
"tsconfig.json",
"prettierrc.json",
},
}),
},
}
end
server:setup(opts)
end)
-- Populate local quickfix list with diagnostics from lsp
-- Thanks to ThePrimeagen
-- https://www.youtube.com/watch?v=IoyW8XYGqjM
vim.cmd([[
augroup PopulateLocalListWithLsp
autocmd!
autocmd! BufWritePre,BufEnter,InsertLeave * :lua vim.diagnostic.setloclist({ open = false })
augroup END
]])
vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(vim.lsp.diagnostic.on_publish_diagnostics, {
update_in_insert = false,
signs = false,
})

View File

@@ -0,0 +1,62 @@
local lualine = require("lualine")
local mode = {
"mode",
}
local branch = {
"branch",
icons_enabled = true,
icon = "",
}
local diagnostics = {
"diagnostics",
sections = { "error", "warn", "info", "hint" },
always_visible = true,
}
local filename = {
function()
local filename = vim.fn.expand("%:t")
local fileext = vim.fn.expand("%:e")
local icon = require("nvim-web-devicons").get_icon(filename, fileext)
return icon .. " " .. filename
end,
}
local fileformat = {
function()
return "[" .. vim.bo.fileformat .. "]"
end,
}
local tabstop = {
function()
local tabstop = vim.bo.tabstop
local expandtab = vim.bo.expandtab
local method = expandtab == true and "spaces" or "tabs"
local format = tabstop .. " " .. method
return format
end,
}
lualine.setup({
options = {
disabled_filetypes = { "NvimTree" },
component_separators = "",
section_separators = "",
globalstatus = true,
},
sections = {
lualine_a = { mode },
lualine_b = { branch },
lualine_c = { diagnostics },
lualine_x = { tabstop, fileformat },
lualine_y = { filename },
lualine_z = {},
},
})

View File

@@ -0,0 +1,46 @@
local opt = vim.opt
-- 2 tabs converted to spaces + autoindentation
opt.tabstop = 2
opt.shiftwidth = 2
opt.expandtab = true
opt.autoindent = true
-- What the **** is this
opt.shortmess = "filnxtToOFc"
-- Create splits like a normal human being
opt.splitbelow = true
opt.splitright = true
-- Other settings
opt.termguicolors = true
opt.number = true
opt.relativenumber = true
opt.wrap = true
opt.swapfile = false
opt.hidden = true
opt.writebackup = false
opt.encoding = "utf-8"
opt.updatetime = 300
opt.mouse = "a"
opt.cursorline = true
opt.clipboard = "unnamedplus"
opt.signcolumn = "yes"
opt.fillchars:append({ eob = " " })
opt.showtabline = 2
opt.showcmd = false
opt.guicursor = "a:block,i:ver25"
opt.fileformat = "unix"
opt.fileformats = { "unix", "dos", "mac" }
opt.laststatus = 3
-- if vim.fn.has("win32") == 1 then
-- opt.shell = "powershell.exe"
-- end
-- Settings for folds
-- wo.foldmethod = "expr"
-- wo.foldexpr = "nvim_treesitter#foldexpr()"
-- wo.foldcolumn = "3"
-- wo.foldenable = false

View File

@@ -0,0 +1,83 @@
local utils = require("user.utils")
local nmap = utils.nmap
local vmap = utils.vmap
local tmap = utils.tmap
local imap = utils.imap
-- General keymaps
nmap("<C-c>", ":nohl<CR>")
-- Disable PageUp and PageDown keys
nmap("<PageDown>", "<nop>")
imap("<PageDown>", "<nop>")
vmap("<PageDown>", "<nop>")
nmap("<PageUp>", "<nop>")
imap("<PageUp>", "<nop>")
vmap("<PageUp>", "<nop>")
-- Move focus between splits
nmap("<leader>h", "<C-w>h")
nmap("<leader>l", "<C-w>l")
nmap("<leader>j", "<C-w>j")
nmap("<leader>k", "<C-w>k")
-- Resize splits
nmap("<C-j>", ":resize -2<CR>")
nmap("<C-k>", ":resize +2<CR>")
nmap("<C-h>", ":vert resize -5<CR>")
nmap("<C-l>", ":vert resize +5<CR>")
-- Open splits
nmap("<leader>sv", ":vs<CR>")
nmap("<leader>sh", ":split<CR>")
-- Move lines easily
nmap("<A-k>", ":m .-2<CR>==")
nmap("<A-j>", ":m .+1<CR>==")
vmap("K", ":m '<-2<CR>gv=gv")
vmap("J", ":m '>+1<CR>gv=gv")
-- Leave selection when moving code left and right
vmap("<", "<gv")
vmap(">", ">gv")
-- Terminal
tmap("<ESC>", "<C-\\><C-n>")
nmap("Th", ":split | :term<CR>")
nmap("Tv", ":vs | :term<CR>")
-- Tabs
nmap("H", ":tabprev<CR>")
nmap("L", ":tabnext<CR>")
nmap("tn", ":tabnew<CR>")
nmap("tN", ":-tabnew<CR>")
nmap("tc", ":lua require('user.tabs').close_tab()<CR>")
nmap("tr", ":lua require('user.tabs').restore_tab()<CR>")
nmap("<A-h>", ":-tabmove<CR>")
nmap("<A-l>", ":+tabmove<CR>")
-- Nvim Tree
nmap("<leader><leader>", ":lua require('nvim-tree').toggle(true, false)<CR>")
-- Telescope
nmap("<leader>ff", ":Telescope find_files<CR>")
nmap("<leader>fo", ":Telescope lsp_document_symbols<CR>")
nmap("<leader>p", ":Telescope<CR>")
-- LSP
nmap("gd", ":lua vim.lsp.buf.definition()<CR>")
nmap("gr", ":lua vim.lsp.buf.references()<CR>")
nmap("K", ":lua vim.lsp.buf.hover()<CR>")
nmap("<F2>", ":lua vim.lsp.buf.rename()<CR>")
nmap("<leader>.", ":lua vim.lsp.buf.code_action()<CR>")
vmap("<leader>.", ":lua vim.lsp.buf.range_code_action()<CR>")
-- Diagnostics
nmap("<leader>dd", ':lua vim.diagnostic.open_float(nil, {focus = false, scope = "cursor"})<CR>')
nmap("<leader>dy", ":lua require('user.utils').copy_diagnostic_message()<CR>")
nmap("<leader>dn", ":lua vim.diagnostic.goto_next({ float = false })<CR>")
nmap("<leader>dp", ":lua vim.diagnostic.goto_prev({ float = false })<CR>")
nmap("<leader>do", ":lopen<CR>")
-- Git
nmap("<leader>gg", ":G<CR>")

View File

@@ -0,0 +1,27 @@
local ls = require("luasnip")
local s, i = ls.s, ls.insert_node
local fmt = require("luasnip.extras.fmt").fmt
local clg_snippet = s(
"clg",
fmt([[console.log({});]], {
i(0),
})
)
ls.snippets = {
javascript = {
clg_snippet,
},
typescript = {
clg_snippet,
},
javascriptreact = {
clg_snippet,
},
typescriptreact = {
clg_snippet,
},
}
require("luasnip.loaders.from_vscode").load()

View File

@@ -0,0 +1,37 @@
local M = {
history = {},
}
M.close_tab = function()
local wins = vim.api.nvim_tabpage_list_wins(0)
for _, win in ipairs(wins) do
local bufnr = vim.api.nvim_win_get_buf(win)
local is_modified = vim.api.nvim_buf_get_option(bufnr, "modified")
if is_modified then
local bufname = vim.fn.bufname(bufnr)
print(bufname .. " is not saved")
return
end
end
table.insert(M.history, vim.fn.bufnr("%"))
vim.cmd("tabclose")
end
M.restore_tab = function()
local buflen = #M.history
if buflen == 0 then
print("No buffers remaining")
return
end
local buf = M.history[buflen]
vim.cmd("tabnew +" .. tostring(buf) .. "buf")
table.remove(M.history, buflen)
end
return M

View File

@@ -0,0 +1,20 @@
local telescope = require("telescope")
telescope.setup({
defaults = {
sorting_strategy = "ascending",
file_ignore_patterns = { ".git/", "node_modules/" },
},
extensions = {
fzf = {
fuzzy = true,
override_generic_sorter = true,
override_file_sorter = true,
case_mode = "smart_case",
},
},
})
if vim.fn.has("win32") == 0 then
require("telescope").load_extension("fzf")
end

View File

@@ -0,0 +1,7 @@
vim.g.edge_diagnostic_virtual_text = "colored"
vim.g.edge_better_performance = 1
vim.cmd([[
colorscheme edge
hi SpellBad guifg=red
]])

View File

@@ -0,0 +1,12 @@
require("nvim-treesitter.configs").setup({
ensure_installed = "maintained",
highlight = {
enable = true,
},
context_commentstring = {
enable = true,
},
autotag = {
enable = true,
},
})

View File

@@ -0,0 +1,127 @@
local M = {}
local function map(mode, shortcut, command, additional_opts)
local opts = { noremap = true, silent = true }
if opts then
opts = vim.tbl_extend("force", opts, additional_opts)
end
vim.api.nvim_set_keymap(mode, shortcut, command, opts)
end
M.nmap = function(shortcut, command, opts)
opts = opts or {}
map("n", shortcut, command, opts)
end
M.vmap = function(shortcut, command, opts)
opts = opts or {}
map("v", shortcut, command, opts)
end
M.tmap = function(shortcut, command, opts)
opts = opts or {}
map("t", shortcut, command, opts)
end
M.imap = function(shortcut, command, opts)
opts = opts or {}
map("i", shortcut, command, opts)
end
M.list_includes_item = function(list, item)
for _, value in pairs(list) do
if value == item then
return true
end
end
return false
end
M.yank = function(message)
if vim.fn.has("win32") == 1 then
os.execute("echo '" .. message .. "' | win32yank -i")
else
-- i use wayland, so there is no xclip for X11
os.execute("echo -n '" .. message .. "' | wl-copy")
end
end
M.copy_diagnostic_message = function()
local diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
if #diagnostics == 0 then
print("No diagnostics to yank")
return
end
local message = ""
if #diagnostics == 1 then
message = diagnostics[1].message
elseif #diagnostics > 1 then
local d = {}
for _, diagnostic in ipairs(diagnostics) do
table.insert(d, diagnostic.message)
end
vim.ui.select(d, { prompt = "Pick a diagnostic to yank" }, function(item)
message = item
end)
end
M.yank(message)
print("Diagnostic message was yanked")
end
M.get_definitions = function()
local params = vim.lsp.util.make_position_params()
local resp = vim.lsp.buf_request_sync(0, "textDocument/definition", params, 1000)
local result = {}
for _, v in pairs(resp) do
table.insert(result, v)
end
result = result[1].result
return result
end
M.goto_local_definition = function()
local definitions = M.get_definitions()
local local_definition = unpack(definitions)
local target = local_definition.targetSelectionRange
local startLine = target.start.line + 1
local startCol = target.start.character
vim.api.nvim_win_set_cursor(0, { startLine, startCol })
end
M.goto_global_definition = function()
local definitions = M.get_definitions()
local global_definition = definitions[#definitions]
local target = global_definition.targetSelectionRange
local startLine = target.start.line + 1
local startCol = target.start.character
local fileuri = global_definition.targetUri
local filepath = string.gsub(fileuri, "file://", "")
vim.cmd("tabnew")
vim.cmd("e " .. filepath)
vim.api.nvim_win_set_cursor(0, { startLine, startCol })
end
return M