1
Fork 0

Wasted way too much time on hydra.nvim shit

This commit is contained in:
Matei Adriel 2023-09-08 20:49:18 +03:00
parent fe3c267ca9
commit 8a54d25b3b
No known key found for this signature in database
17 changed files with 540 additions and 80 deletions

View file

@ -0,0 +1,42 @@
local env = require("my.helpers.env")
local K = require("my.keymaps")
local M = {
"mhartington/formatter.nvim",
event = "BufReadPre",
cond = env.vscode.not_active(),
}
function M.config()
local any = require("formatter.filetypes.any")
local formatters = {
markdown = {
require("formatter.filetypes.markdown").prettierd,
},
lua = {
require("formatter.filetypes.lua").stylua,
},
["*"] = {
any.remove_trailing_whitespace,
},
}
require("formatter").setup({ filetype = formatters })
local format = function()
if formatters[vim.bo.filetype] ~= nil then
vim.cmd([[Format]])
elseif next(vim.lsp.get_active_clients({ bufnr = 0 })) == nil then
vim.lsp.buf.format()
end
end
K.nmap("<leader>F", format, "[F]ormat file")
vim.api.nvim_create_autocmd("BufWritePre", {
group = vim.api.nvim_create_augroup("Formatting", { clear = false }),
callback = format,
})
end
return M

View file

@ -2,58 +2,361 @@ local M = {
-- keybinds where you only hit the head once
"anuvyklack/hydra.nvim",
dependencies = {
"jbyuki/venn.nvim", -- draw ascii diagrams
"mrjones2014/smart-splits.nvim", -- the name says it all
},
keys = { "<C-S-w>", "<leader>V" },
keys = { "<C-S-w>" },
}
local venn_hint = [[
Arrow^^^^^^ Select region with <C-v>
^ ^ _K_ ^ ^ _f_: surround it with box
_H_ ^ ^ _L_
^ ^ _J_ ^ ^ _<Esc>_
]]
-- {{{ Helpers
local function identity(x)
return x
end
local window_hint = [[
^^^^^^^^^^^^ Move ^^ Size ^^ ^^ Split
^^^^^^^^^^^^------------- ^^-----------^^ ^^---------------
local function len(x)
return #x
end
local function box(value, w, h)
return { value = value, width = w, height = h }
end
local function min(l, r)
if l < r then
return l
else
return r
end
end
local function max(l, r)
return -min(-l, -r)
end
local function zip_with(l, r, f, default)
local output = {}
for i = 1, max(#l, #r), 1 do
output[i] = f(l[i] or default, r[i] or default)
end
return output
end
local function flatten_list(list)
local output = {}
for i = 1, #list, 1 do
for j = 1, #list[i], 1 do
table.insert(output, list[i][j])
end
end
return output
end
local function map_list(list, f)
local output = {}
for i = 1, #list, 1 do
output[i] = f(list[i])
end
return output
end
local function l_repeat(v, times)
if times == 0 then
return {}
end
local o = {}
for i = 1, times, 1 do
o[i] = v
end
return o
end
local function s_repeat(text, times)
local o = ""
for _ = 1, times, 1 do
o = o .. text
end
return o
end
local function string_split(text, sep)
---@diagnostic disable-next-line: redefined-local
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
text:gsub(pattern, function(c)
fields[#fields + 1] = c
end)
return fields
end
local function table_max(list, f, default)
if #list == 0 then
return default
end
---@diagnostic disable-next-line: redefined-local
local f = f or identity
local c_max = list[1]
for i = 1, #list, 1 do
if f(list[i]) >= f(c_max) then
c_max = list[i]
end
end
return c_max
end
local function string_value(t)
if type(t) == "string" then
return t
end
return t.value
end
local function lines(text)
return string_split(string_value(text), "\n")
end
local function unlines(text)
return table.concat(text, "\n")
end
local function map_lines(text, f)
return unlines(map_list(lines(text), f))
end
local function string_width(t)
if type(t) == "string" then
return #table_max(lines(t), len, "")
end
return t.width
end
local function string_height(t)
if type(t) == "string" then
return #lines(t)
end
return t.height
end
local function half_down(num)
return math.floor(num / 2)
end
local function half_up(num)
return math.ceil(num / 2)
end
-- }}}
-- {{{ Hint helpers
local H = {}
function H.highlight(t)
return box("_" .. t .. "_", string_width(t), 1)
end
function H.spacing(amount)
return s_repeat(" ", amount)
end
function H.spacing_for(text)
return H.spacing(string_width(text))
end
function H.spacing_largest(values)
return H.spacing_for(table_max(values))
end
H.nojustify = { justify = "nojustify" }
function H.pad_left(text, length)
local spaces = length - string_width(text)
return box(
map_lines(text, function(line)
return H.spacing(spaces) .. line
end),
length,
string_height(text)
)
end
function H.pad_right(text, length)
local spaces = length - string_width(text)
return box(
map_lines(text, function(line)
return line .. H.spacing(spaces)
end),
length,
string_height(text)
)
end
function H.pad_around(text, length)
local spaces = length - string_width(text)
return box(
map_lines(text, function(line)
return H.spacing(half_up(spaces)) .. line .. H.spacing(half_down(spaces))
end),
length,
string_height(text)
)
end
function H.pad(text, length, justify)
if justify == "nojustify" then
return text
elseif justify == "center" then
return H.pad_around(text, length)
elseif justify == "right" then
return H.pad_left(text, length)
else
return H.pad_right(text, length)
end
end
function H.directional(h, j, k, l, spacing_amount)
---@diagnostic disable-next-line: redefined-local
local spacing_amount = spacing_amount or 1
return H.concat_many_w({
H.highlight(k),
H.concat_many_h({
H.highlight(h),
H.spacing(spacing_amount),
H.highlight(l),
}),
H.highlight(j),
})
end
function H.add_title(title, body)
local width = max(string_width(title), string_width(body))
return H.concat_many_w({
title,
s_repeat("-", width),
body,
})
end
function H.concat_h(left, right, spacing_amount)
---@diagnostic disable-next-line: redefined-local
local spacing_amount = spacing_amount or 0
return box(
unlines(zip_with(lines(left), lines(right), function(l, r)
return l .. H.spacing(spacing_amount) .. r
end, "")),
string_width(left) + string_width(right) + spacing_amount,
max(string_height(left), string_height(right))
)
end
function H.concat_w(above, below, opts)
---@diagnostic disable-next-line: redefined-local
local opts = opts or {}
local spacing_amount = opts.spacing_amount or 0
local justify = opts.justify or "center"
local width = max(string_width(above), string_width(below))
return box(
unlines(flatten_list({
{ H.pad(above, width, justify).value },
l_repeat(H.spacing(width), spacing_amount),
{ H.pad(below, width, justify).value },
})),
width,
spacing_amount + string_height(above) + string_height(below)
)
end
function H.concat_many_h(list, spacing_amount)
local result = list[1]
for i = 2, #list, 1 do
result = H.concat_h(result, list[i], spacing_amount)
end
return result
end
function H.concat_many_w(list, opts)
local result = list[1]
for i = 2, #list, 1 do
result = H.concat_w(result, list[i], opts)
end
return result
end
function H.pad_lengths_right(list)
local max_length = table_max(list, string_width)
return map_list(list, function(t)
return H.concat_h(t, H.spacing(max_length - string_width(t)))
end)
end
M.hint = H
-- }}}
local window_hint_old = [[
^^^^^^ Move ^^^^^^ ^^ Size ^^ ^^ Split
^^^^^^-------------^^^^^^ ^^-----------^^ ^^---------------
^ ^ _k_ ^ ^ ^ ^ _K_ ^ ^ ^ _<C-k>_ ^ _s_: horizontally
_h_ ^ ^ _l_ _H_ ^ ^ _L_ _<C-h>_ _<C-l>_ _v_: vertically
^ ^ _j_ ^ ^ ^ ^ _J_ ^ ^ ^ _<C-j>_ ^ _q_: close
focus^^^^^^ window^^^^^^ ^_=_: equalize^ _o_: close remaining
^^^focus^^^ ^^^window^^^ ^_=_: equalize^ _o_: close remaining
]]
local window_hint = H.concat_many_h({
H.add_title(
"Move",
H.concat_h(
H.concat_w(H.directional("h", "j", "k", "l", 3), "focus"),
H.concat_w(
H.directional("H", "J", "K", "L", 3),
"window",
{ justify = "left" }
),
2
)
),
H.add_title(
"Size",
H.concat_w(
H.directional("<C-h>", "<C-j>", "<C-k>", "<C-l>"),
H.concat_many_h({
H.highlight("="),
": equalize",
})
)
),
H.add_title(
"Split",
H.concat_many_w({
H.concat_h(H.highlight("s"), ": horizontally"),
H.concat_h(H.highlight("v"), ": vertical"),
H.concat_h(H.highlight("q"), ": close"),
H.concat_h(H.highlight("o"), ": close remaining"),
}, { justify = "left" })
),
}, 3).value
print(window_hint)
function M.config()
local Hydra = require("hydra")
local pcmd = require("hydra.keymap-util").pcmd
local splits = require("smart-splits")
-- {{{ Diagrams
Hydra({
name = "Draw Diagram",
hint = venn_hint,
config = {
invoke_on_body = true,
hint = {
border = "rounded",
},
on_enter = function()
vim.o.virtualedit = "all"
end,
},
mode = "n",
body = "<leader>V",
heads = {
{ "H", "<C-v>h:VBox<CR>" },
{ "J", "<C-v>j:VBox<CR>" },
{ "K", "<C-v>k:VBox<CR>" },
{ "L", "<C-v>l:VBox<CR>" },
{ "f", ":VBox<CR>", { mode = "v" } },
{ "<Esc>", nil, { exit = true } },
},
})
-- }}}
-- {{{ Windows
local resize = function(direction)
return function()

View file

@ -96,10 +96,20 @@ return {
},
-- Helper libs
"nvim-lua/plenary.nvim",
{
"nvim-lua/plenary.nvim",
-- Autoload when running tests
cmd = { "PlenaryBustedDirectory", "PlenaryBustedFile" },
},
"MunifTanjim/nui.nvim",
"nvim-tree/nvim-web-devicons", -- nice looking icons
"mateiadrielrafael/scrap.nvim", -- vim-abolish rewrite
{
"mateiadrielrafael/scrap.nvim",
event = "InsertEnter",
config = function()
require("my.abbreviations").setup()
end,
}, -- vim-abolish rewrite
{
"terrortylor/nvim-comment",

View file

@ -5,6 +5,7 @@ local M = {
event = "BufReadPre",
dependencies = "neovim/nvim-lspconfig",
cond = env.vscode.not_active(),
enable = false,
}
function M.config()

View file

@ -0,0 +1,31 @@
local env = require("my.helpers.env")
local vault = "/home/adrielus/Projects/stellar-sanctum"
return {
"epwalsh/obsidian.nvim",
event = "VeryLazy",
dependencies = {
"nvim-lua/plenary.nvim",
},
opts = {
dir = vault,
notes_subdir = "chaos",
daily_notes = {
folder = "daily",
date_format = "%Y-%m-%d",
},
completion = {
nvim_cmp = true,
min_chars = 2,
new_notes_location = "current_dir",
prepend_note_id = true,
},
mappings = {},
},
keys = {
{ "<C-O>", "<cmd>ObsidianQuickSwitch<cr>" },
},
cond = env.vscode.not_active()
and env.firenvim.not_active()
and vim.loop.cwd() == vault,
}

View file

@ -13,8 +13,6 @@ local M = {
--{{{Languages
ensure_installed = {
"bash",
"javascript",
"typescript",
"c",
"cpp",
"css",
@ -22,18 +20,22 @@ local M = {
"elixir",
"fish",
"html",
"javascript",
"json",
"jsonc",
"latex",
"lua",
"markdown",
"markdown_inline",
"nix",
"python",
"rust",
"scss",
"toml",
"tsx",
"typescript",
"vim",
"yaml",
"nix",
"lua",
},
sync_install = false,
--}}}

View file

@ -0,0 +1,52 @@
local M = {
"jbyuki/venn.nvim", -- draw ascii diagrams
dependencies = {
"anuvyklack/hydra.nvim",
},
keys = { "<leader>V" },
}
local venn_hint = [[
^^^Arrow^^^ Select region with <C-v>
^ ^ _K_ ^ ^ _f_: surround it with box
_H_ ^ ^ _L_
^ ^ _J_ ^ ^ _<Esc>_
]]
function M.config()
local Hydra = require("hydra")
Hydra({
name = "Draw Diagram",
hint = venn_hint,
config = {
color = "pink",
invoke_on_body = true,
hint = {
border = "single",
},
on_enter = function()
vim.o.virtualedit = "all"
end,
},
mode = "n",
desc = "[V]enn mode",
body = "<leader>V",
heads = {
{ "H", "<C-v>h:VBox<CR>" },
{ "J", "<C-v>j:VBox<CR>" },
{ "K", "<C-v>k:VBox<CR>" },
{ "L", "<C-v>l:VBox<CR>" },
{ "f", ":VBox<CR>", { mode = "v" } },
{ "<Esc>", nil, { exit = true } },
},
})
end
function M.init()
require("which-key").register({
["<leader>V"] = { name = "[V]enn mode" },
})
end
return M

View file

@ -1,4 +1,3 @@
local env = require("my.helpers.env")
local M = {
"folke/which-key.nvim",
event = "VeryLazy",
@ -7,15 +6,11 @@ local M = {
function M.config()
local wk = require("which-key")
local winblend = 0
if env.neovide.active() then
winblend = 30
end
wk.setup({
window = {
winblend = winblend,
winblend = 0,
pumblend = 0,
border = "single",
},
layout = { align = "center" },
})
@ -28,7 +23,6 @@ function M.config()
l = { name = "[L]ocal" },
w = { name = "[W]orkspace" },
y = { name = "[Y]ank" },
v = "which_key_ignore",
},
})
end