neovim: rename user dir to daniil
This commit is contained in:
55
neovim/.config/nvim/lua/daniil/git.lua
Normal file
55
neovim/.config/nvim/lua/daniil/git.lua
Normal file
@@ -0,0 +1,55 @@
|
||||
local u = require("daniil.utils")
|
||||
|
||||
local git = {}
|
||||
|
||||
--- @return string commit_hash
|
||||
function git.get_commit_hash_for_current_line()
|
||||
local fullpath = vim.api.nvim_buf_get_name(0)
|
||||
local cwd = vim.loop.cwd()
|
||||
local relative_path = string.gsub(fullpath, cwd .. "/", "")
|
||||
|
||||
local line = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
|
||||
local f = io.popen("git blame -L " .. line .. ",+1 " .. relative_path, "r")
|
||||
local data = f:read("*a")
|
||||
f:close()
|
||||
|
||||
local commit_hash = vim.split(data, " ")[1]
|
||||
|
||||
return commit_hash
|
||||
end
|
||||
|
||||
--- @return string git_remote_url
|
||||
function git.get_remote_url()
|
||||
local f = io.popen("git remote get-url origin", "r")
|
||||
local remote_url = f:read("*l")
|
||||
f:close()
|
||||
|
||||
if string.sub(remote_url, 0, 4) == "git@" then
|
||||
remote_url = string.gsub(remote_url, "git@", "")
|
||||
remote_url = string.gsub(remote_url, ".git", "")
|
||||
remote_url = string.gsub(remote_url, ":", "/")
|
||||
end
|
||||
|
||||
if string.sub(remote_url, 0, 5) ~= "https" then
|
||||
remote_url = "https://" .. remote_url
|
||||
end
|
||||
|
||||
return remote_url
|
||||
end
|
||||
|
||||
function git.open_commit_on_github()
|
||||
local commit_hash = git.get_commit_hash_for_current_line()
|
||||
local remote_url = git.get_remote_url()
|
||||
|
||||
if commit_hash == "00000000" then
|
||||
print("Not committed yet")
|
||||
return
|
||||
end
|
||||
|
||||
local commit_url = remote_url .. "/commit/" .. commit_hash
|
||||
|
||||
u.open_url_in_browser(commit_url)
|
||||
end
|
||||
|
||||
return git
|
||||
68
neovim/.config/nvim/lua/daniil/tabs.lua
Normal file
68
neovim/.config/nvim/lua/daniil/tabs.lua
Normal file
@@ -0,0 +1,68 @@
|
||||
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
|
||||
|
||||
M.find_tab = function()
|
||||
local tabpages = vim.api.nvim_list_tabpages()
|
||||
local tabs = {}
|
||||
for _, tabnr in ipairs(tabpages) do
|
||||
local winnr = vim.api.nvim_tabpage_get_win(tabnr)
|
||||
local bufnr = vim.api.nvim_win_get_buf(winnr)
|
||||
|
||||
local bufname = vim.api.nvim_buf_get_name(bufnr)
|
||||
local root = vim.loop.cwd()
|
||||
local relative_path = string.gsub(bufname, root .. "/", "")
|
||||
|
||||
table.insert(tabs, {
|
||||
bufname = relative_path,
|
||||
tabnr = tabnr,
|
||||
})
|
||||
end
|
||||
|
||||
vim.ui.select(tabs, {
|
||||
prompt = "Choose a tab: ",
|
||||
format_item = function(item)
|
||||
return item.tabnr .. " - " .. item.bufname
|
||||
end,
|
||||
}, function(item)
|
||||
if not item then
|
||||
return
|
||||
end
|
||||
|
||||
vim.api.nvim_set_current_tabpage(item.tabnr)
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
89
neovim/.config/nvim/lua/daniil/utils.lua
Normal file
89
neovim/.config/nvim/lua/daniil/utils.lua
Normal file
@@ -0,0 +1,89 @@
|
||||
local M = {}
|
||||
|
||||
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
|
||||
|
||||
--- @param diagnostics list
|
||||
--- @param prompt string
|
||||
--- @return string
|
||||
M.select_diagnostic = function(diagnostics, prompt)
|
||||
if #diagnostics == 0 then
|
||||
return ""
|
||||
end
|
||||
|
||||
local message = ""
|
||||
|
||||
if #diagnostics == 1 then
|
||||
message = diagnostics[1].message
|
||||
else
|
||||
local d = {}
|
||||
for _, diagnostic in ipairs(diagnostics) do
|
||||
table.insert(d, diagnostic.message)
|
||||
end
|
||||
|
||||
vim.ui.select(d, { prompt = prompt }, function(item)
|
||||
message = item
|
||||
end)
|
||||
end
|
||||
|
||||
return message
|
||||
end
|
||||
|
||||
M.copy_diagnostic_message = function()
|
||||
local diagnostics = vim.lsp.diagnostic.get_line_diagnostics()
|
||||
|
||||
local message = M.select_diagnostic(diagnostics, "Pick a diagnostic to yank")
|
||||
|
||||
M.yank(message)
|
||||
|
||||
print("Diagnostic message was yanked")
|
||||
end
|
||||
|
||||
M.lsp_organize_imports = function(bufnr, timeout)
|
||||
if not bufnr then
|
||||
bufnr = vim.api.nvim_get_current_buf()
|
||||
end
|
||||
|
||||
local params = {
|
||||
command = "_typescript.organizeImports",
|
||||
arguments = { vim.api.nvim_buf_get_name(bufnr) },
|
||||
title = "",
|
||||
}
|
||||
|
||||
vim.lsp.buf_request_sync(bufnr, "workspace/executeCommand", params, timeout or 500)
|
||||
end
|
||||
|
||||
M.open_url_in_browser = function(url)
|
||||
local f
|
||||
|
||||
if vim.fn.has("win32") == 1 then
|
||||
f = io.popen("explorer " .. url, "r")
|
||||
else
|
||||
f = io.popen("xdg-open " .. url, "r")
|
||||
end
|
||||
|
||||
f:close()
|
||||
end
|
||||
|
||||
function M.lsp_format(bufnr)
|
||||
bufnr = bufnr or 0
|
||||
|
||||
vim.lsp.buf.format({
|
||||
filter = function(client)
|
||||
if client.name == "tsserver" then
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end,
|
||||
bufnr = bufnr,
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user