Add pre-student

This commit is contained in:
venomade committed 2026-08-17 13:53:05 +01:00
1 parent b7b49d1bbe
commit 9cdc3c82dd
65 files changed
+6718

No files matched your search

@@ -0,0 +1,32 @@
local function map(mode, lhs, rhs, opts)
local options = { noremap = true, silent = true }
if opts then
options = vim.tbl_extend("force", options, opts)
end
vim.keymap.set(mode, lhs, rhs, options)
end
map("", "n", "j")
map("", "e", "k")
map("", "i", "l")
map("", "j", "n")
map("", "k", "e")
map("", "l", "i")
map("", "K", "E")
map("", "N", "J")
map("", "E", "K") -- As in 'Explore' documentation
map("", "L", "I")
map("", "I", "L")
map("", "J", "N")
map("n", "<c-w>h", "<c-w>h")
map("n", "<c-w>n", "<c-w>j")
map("n", "<c-w>e", "<c-w>k")
map("n", "<C-w>i", "<c-w>l")
map ("", "gn", "gj")
map ("", "ge", "gk")
-- Kakoune Holdover
map ("", "gh", "0")
map ("", "gi", "$")
@@ -0,0 +1,53 @@
vim.g.mapleader = " "
vim.keymap.set("n", "<leader>bk", vim.cmd.bdelete,
{ desc = "Kill Buffer" })
vim.keymap.set("n", "<leader>bn", vim.cmd.bnext,
{ desc = "Next Buffer" })
vim.keymap.set("n", "<leader>bp", vim.cmd.bprev,
{ desc = "Previous Buffer" })
vim.keymap.set("n", "E", vim.lsp.buf.hover)
vim.keymap.set("n", "<C-e>", vim.diagnostic.open_float)
vim.keymap.set("n", "<leader>ld", vim.lsp.buf.definition,
{ desc = "Go to Definition" })
vim.keymap.set("n", "<leader>lt", vim.lsp.buf.type_definition,
{ desc = "Go to Type Definition"} )
vim.keymap.set("n", "<leader>li", vim.lsp.buf.implementation,
{ desc = "Go to Implementation" })
vim.keymap.set("n", "<leader>la", vim.lsp.buf.code_action,
{ desc = "Show Code Actions" })
vim.keymap.set("n", "<leader>lf", vim.lsp.buf.format,
{ desc = "Format Document" })
vim.keymap.set("n", "<leader>lr", vim.lsp.buf.rename,
{ desc = "Rename" })
vim.keymap.set("n", "<leader>lk", function()
vim.diagnostic.jump({ count = 1, float = true })
end,
{ desc = "Next Error" })
-- vim.keymap.set("n", "<leader>lh", vim.lsp.buf.hover,
-- { desc = "Hover Diagnostics" })
vim.keymap.set("n", "<leader>wh", vim.cmd.split,
{ desc = "Split Window Horizontally" })
vim.keymap.set("n", "<leader>wv", vim.cmd.vsplit,
{ desc = "Split Window Vertically" })
vim.keymap.set("n", "<leader>wc", "<C-w>q",
{ desc = "Close Window" })
vim.keymap.set("n", "<leader>ww", "<C-w>w",
{ desc = "Next Window" })
vim.keymap.set("n", "<leader>foc", function()
vim.cmd('edit ~/.config/nvim/init.lua')
end, { desc = "Open Config File" })
vim.keymap.set("n", "<leader>fot", function()
vim.cmd('edit ~/Documents/TODO.org')
end, { desc = "Open Todo File" })
vim.keymap.set("n", "<leader>fon", function()
vim.cmd('edit ~/Documents/Notes.org')
end, { desc = "Open Notes File" })
vim.keymap.set('n', '<Tab>', '==', { noremap = true, silent = true })
vim.keymap.set('n', 'q:', ':q<CR>', { noremap = true })
@@ -0,0 +1,107 @@
vim.opt.nu = true
vim.opt.rnu = true
vim.opt.tabstop = 2
vim.opt.softtabstop = 2
vim.opt.shiftwidth = 2
vim.opt.expandtab = true
vim.opt.autoindent = true
vim.opt.smartindent = true
vim.opt.wrap = false
vim.opt.swapfile = false
vim.opt.backup = false
vim.opt.undodir = os.getenv("HOME") .. "/.nvim/undodir"
vim.opt.undofile = true
vim.opt.hlsearch = false
vim.opt.incsearch = true
vim.opt.termguicolors = true
vim.opt.scrolloff = 12
vim.opt.updatetime = 50
vim.opt.splitbelow = true
vim.opt.splitright = true
vim.opt.encoding = "utf-8"
vim.opt.shell = "zsh"
vim.opt.mouse = "a"
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.incsearch = true
-- vim.opt.cursorline = true
vim.opt.title = true
vim.opt.clipboard = "unnamedplus"
-- vim.opt.colorcolumn = "80"
vim.opt.signcolumn = 'no'
vim.cmd "set showtabline=0 | set laststatus=0"
vim.wo.fillchars='eob: '
vim.opt.conceallevel = 3
-- Window Borders
vim.o.winborder = 'rounded'
-- Remove Whitespaces on File Save
vim.api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = { "*" },
command = [[%s/\s\+$//e]],
})
-- Define highlight groups for line numbers with diagnostics
vim.api.nvim_set_hl(0, "LineNrDiagnosticError", { bg = "#3f1d1d", fg = "#ff6c6b" })
vim.api.nvim_set_hl(0, "LineNrDiagnosticWarn", { bg = "#3f311d", fg = "#ECBE7B" })
vim.api.nvim_set_hl(0, "LineNrDiagnosticInfo", { bg = "#1d2d3f", fg = "#51afef" })
vim.api.nvim_set_hl(0, "LineNrDiagnosticHint", { bg = "#1d3f2d", fg = "#98be65" })
local function set_line_number_highlight()
-- Clear existing highlights
vim.fn.sign_unplace("LineNrDiagnostics")
-- Get diagnostics per buffer
for _, diag in ipairs(vim.diagnostic.get(0)) do
local hl_group = nil
if diag.severity == vim.diagnostic.severity.ERROR then
hl_group = "LineNrDiagnosticError"
elseif diag.severity == vim.diagnostic.severity.WARN then
hl_group = "LineNrDiagnosticWarn"
elseif diag.severity == vim.diagnostic.severity.INFO then
hl_group = "LineNrDiagnosticInfo"
elseif diag.severity == vim.diagnostic.severity.HINT then
hl_group = "LineNrDiagnosticHint"
end
if hl_group then
vim.fn.sign_place(0, "LineNrDiagnostics", hl_group, vim.api.nvim_get_current_buf(), {
lnum = diag.lnum + 1, -- diagnostics are 0-indexed
priority = 10,
})
end
end
end
-- Create the sign definitions
vim.fn.sign_define("LineNrDiagnosticError", { text = "", texthl = "LineNrDiagnosticError", numhl = "LineNrDiagnosticError" })
vim.fn.sign_define("LineNrDiagnosticWarn", { text = "", texthl = "LineNrDiagnosticWarn", numhl = "LineNrDiagnosticWarn" })
vim.fn.sign_define("LineNrDiagnosticInfo", { text = "", texthl = "LineNrDiagnosticInfo", numhl = "LineNrDiagnosticInfo" })
vim.fn.sign_define("LineNrDiagnosticHint", { text = "", texthl = "LineNrDiagnosticHint", numhl = "LineNrDiagnosticHint" })
-- Update line numbers when diagnostics change
vim.api.nvim_create_autocmd({ "DiagnosticChanged", "BufEnter", "CursorHold" }, {
callback = set_line_number_highlight,
})
+26
View File
@@ -0,0 +1,26 @@
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
local lazyrepo = "https://github.com/folke/lazy.nvim.git"
local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
if vim.v.shell_error ~= 0 then
vim.api.nvim_echo({
{ "Failed to clone lazy.nvim:\n", "ErrorMsg" },
{ out, "WarningMsg" },
{ "\nPress any key to exit..." },
}, true, {})
vim.fn.getchar()
os.exit(1)
end
end
vim.opt.rtp:prepend(lazypath)
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"
require("lazy").setup({
spec = {
{ import = "plugins" },
},
install = { colorscheme = { "modus" } },
checker = { enabled = true, notify = false },
})
@@ -0,0 +1,8 @@
return {
"windwp/nvim-autopairs",
config = function()
require("nvim-autopairs").setup({
disable_filetype = { "TelescopePrompt" , "vim" }
})
end,
}
@@ -0,0 +1,15 @@
return {
'catppuccin/nvim',
name = 'catppuccin',
priority = 1000,
config = function()
require('catppuccin').setup({
flavour = 'mocha',
transparent_background = true,
float = {
transparent = true,
},
})
vim.cmd.colorscheme('catppuccin')
end,
}
@@ -0,0 +1,99 @@
return {
'hrsh7th/nvim-cmp',
dependencies = {
'neovim/nvim-lspconfig',
'hrsh7th/cmp-nvim-lsp',
'hrsh7th/cmp-buffer',
'hrsh7th/cmp-path',
'lukas-reineke/cmp-rg',
'onsails/lspkind.nvim'
},
config = function()
local cmp = require('cmp')
local lspkind = require('lspkind')
cmp.setup({
-- preselect = cmp.PreselectMode.None,
completion = {
completeopt = "menu,menuone,noinsert",
callSnippet = "Replace",
keywordSnippet = "Replace",
},
window = {
completion = cmp.config.window.bordered({}),
documentation = cmp.config.window.bordered({}),
},
snippet = {
expand = function(arg)
vim.snippet.expand(arg.body)
end
},
mapping = cmp.mapping.preset.insert({
['<Tab>'] = cmp.mapping.confirm({ select = true }),
['<C-n>'] = cmp.mapping.select_next_item(),
['<C-p>'] = cmp.mapping.select_prev_item(),
['<C-g>'] = cmp.mapping.abort(),
}),
sources = cmp.config.sources(
{
{ name = 'nvim_lsp' },
},
{
{ name = 'path' },
}
-- {
-- { name = 'buffer' },
-- { name = 'rg', keyword_length = 3 }
-- }
),
formatting = {
format = lspkind.cmp_format({
mode = 'symbol'
})
},
-- view = {
-- entries = 'native'
-- },
})
local capabilities = require('cmp_nvim_lsp').default_capabilities()
vim.lsp.config('lua_ls', {
-- cmd = { '/home/venomade/.local/share/lua-language-server/bin/lua-language-server' },
capabilities = capabilities,
root_markers = {
".luarc.json",
".luarc.jsonc",
".git",
},
settings = {
Lua = {
runtime = {
version = 'Lua 5.4',
path = vim.list_extend(
vim.split(package.path, ';'),
{
'?.lua',
'?/init.lua',
'./stubs/?.lua',
'./stubs/?/init.lua'
}),
},
workspace = {
library = {
vim.env.HOME .. '/.luarocks/share/lua/5.4',
'./stubs'
},
},
diagnostics = {
enable = true,
},
},
},
})
vim.lsp.enable('lua_ls')
vim.lsp.config('clangd', {
capabilities = capabilities
})
vim.lsp.enable('clangd')
end
}
@@ -0,0 +1,15 @@
return {
'norcalli/nvim-colorizer.lua',
config = function()
require("colorizer").setup({ "*" }, {
RGB = true,
RRGGBB = true,
names = false,
RRGGBBAA = true,
rgb_fn = true,
hsl_fn = true,
css = false,
css_fn = false,
})
end
}
@@ -0,0 +1,20 @@
return {
"ej-shafran/compile-mode.nvim",
branch = "latest",
dependencies = {
"nvim-lua/plenary.nvim",
},
config = function()
vim.g.compile_mode = {}
local compile_mode = require("compile-mode")
vim.keymap.set("n", "<leader>cc", compile_mode.compile,
{ desc = "Compile" })
vim.keymap.set("n", "<leader>cr", compile_mode.recompile,
{ desc = "Recompile" })
vim.keymap.set("n", "<leader>cq", compile_mode.interrupt,
{ desc = "Quit Compile (Interrupt)" })
end
}
@@ -0,0 +1,20 @@
return {
'3rd/image.nvim',
config = function()
require("image").setup({
backend = "sixel",
processor = "magick_cli",
integrations = {
markdown = {
enabled = true,
only_render_image_at_cursor = true,
only_render_image_at_cursor_mode = "popup", -- or "inline"
floating_windows = true, -- if true, images will be rendered in floating markdown windows
},
},
window_overlap_clear_ft_ignore = { "cmp_menu", "cmp_docs" },
editor_only_render_when_focused = true, -- auto show/hide images when the editor gains/looses focus
})
end,
build = false,
}
@@ -0,0 +1,20 @@
return {
"kdheepak/lazygit.nvim",
lazy = true,
cmd = {
"LazyGit",
"LazyGitConfig",
"LazyGitCurrentFile",
"LazyGitFilter",
"LazyGitFilterCurrentFile",
},
-- optional for floating window border decoration
dependencies = {
"nvim-lua/plenary.nvim",
},
-- setting the keybinding for LazyGit with 'keys' is recommended in
-- order to load the plugin when the command is run for the first time
keys = {
{ "<leader>g", "<cmd>LazyGit<cr>", desc = "LazyGit" }
}
}
@@ -0,0 +1,38 @@
return {
"nvim-lualine/lualine.nvim",
dependencies = { 'nvim-tree/nvim-web-devicons' },
config = function()
require("lualine").setup({
options = {
icons_enabled = true,
component_separators = { left = '|', right = '|'},
section_separators = { left = '', right = ''},
theme = {
normal = {
a = { bg = '#000000' },
b = { bg = '#000000' },
c = { bg = '#000000' },
},
inactive = {
a = { bg = '#000000' },
b = { bg = '#000000' },
c = { bg = '#000000' },
}
},
},
sections = {
lualine_a = {"mode", {
"filetype",
icon_only = true,
separator = "",
padding = { right = 0, left = 1 },
}, "filename" },
lualine_b = {},
lualine_c = {},
lualine_x = {"diagnostics"},
lualine_y = {},
lualine_z = {}
}
})
end,
}
@@ -0,0 +1,28 @@
return {
'stevearc/oil.nvim',
dependencies = { "nvim-tree/nvim-web-devicons" },
config = function()
function _G.get_oil_winbar()
local bufnr = vim.api.nvim_win_get_buf(vim.g.statusline_winid)
local dir = require("oil").get_current_dir(bufnr)
if dir then
return vim.fn.fnamemodify(dir, ":~")
else
-- If there is no current directory (e.g. over ssh), just show the buffer name
return vim.api.nvim_buf_get_name(0)
end
end
require('oil').setup({
win_options = {
winbar = "%!v:lua.get_oil_winbar()",
},
view_options = {
show_hidden = true
}
})
vim.keymap.set('n', '-', '<CMD>Oil<CR>', {desc = "Open Dir in Oil"})
end,
lazy = false,
}
@@ -0,0 +1,6 @@
return {
"hiphish/rainbow-delimiters.nvim",
config = function()
require('rainbow-delimiters.setup').setup({})
end,
}
@@ -0,0 +1,7 @@
return {
'vladdoster/remember.nvim',
config = function()
require'remember'.setup({})
end
}
@@ -0,0 +1,114 @@
return {
'MeanderingProgrammer/render-markdown.nvim',
dependencies = { 'nvim-treesitter/nvim-treesitter', 'nvim-tree/nvim-web-devicons' },
config = function()
require('render-markdown').setup({
link = {
enabled = true,
render_modes = false,
footnote = {
enabled = true,
superscript = true,
prefix = '',
suffix = '',
},
image = '󰥶 ',
email = '󰀓 ',
hyperlink = '󰌹 ',
highlight = 'RenderMarkdownLink',
wiki = {
icon = '󱗖 ',
body = function()
return nil
end,
highlight = 'RenderMarkdownWikiLink',
},
custom = {
web = { pattern = '^http', icon = '󰖟 ' },
github = { pattern = 'github%.com', icon = '󰊤 ' },
stackoverflow = { pattern = 'stackoverflow%.com', icon = '󰓌 ' },
wikipedia = { pattern = 'wikipedia%.org', icon = '󰖬 ' },
youtube = { pattern = 'youtube%.com', icon = '󰗃 ' },
reddit = { pattern = 'reddit%.com', icon = '󰑍 ' },
old_reddit = { pattern = 'old.reddit%.com', icon = '󰑍 ' },
gvc = { pattern = 'git%.venomade%.com', icon = '󰊢 ' },
catechism = { pattern = 'ccc%.directory', icon = '󰅄 ' },
bible = { pattern = 'biblenow%.net', icon = '󰂢 ' },
luarocks = { pattern = 'luarocks%.org', icon = '󰢱 ' },
},
},
callout = {
note = { raw = '[!NOTE]', rendered = '󰋽 Note', highlight = 'RenderMarkdownInfo' },
tip = { raw = '[!TIP]', rendered = '󰌶 Tip', highlight = 'RenderMarkdownSuccess' },
important = { raw = '[!IMPORTANT]', rendered = '󰅾 Important', highlight = 'RenderMarkdownHint' },
warning = { raw = '[!WARNING]', rendered = '󰀪 Warning', highlight = 'RenderMarkdownWarn' },
caution = { raw = '[!CAUTION]', rendered = '󰳦 Caution', highlight = 'RenderMarkdownError' },
abstract = { raw = '[!ABSTRACT]', rendered = '󰨸 Abstract', highlight = 'RenderMarkdownInfo' },
summary = { raw = '[!SUMMARY]', rendered = '󰨸 Summary', highlight = 'RenderMarkdownInfo' },
tldr = { raw = '[!TLDR]', rendered = '󰨸 Tldr', highlight = 'RenderMarkdownInfo' },
info = { raw = '[!INFO]', rendered = '󰋽 Info', highlight = 'RenderMarkdownInfo' },
todo = { raw = '[!TODO]', rendered = '󰗡 Todo', highlight = 'RenderMarkdownInfo' },
hint = { raw = '[!HINT]', rendered = '󰌶 Hint', highlight = 'RenderMarkdownSuccess' },
success = { raw = '[!SUCCESS]', rendered = '󰄬 Success', highlight = 'RenderMarkdownSuccess' },
check = { raw = '[!CHECK]', rendered = '󰄬 Check', highlight = 'RenderMarkdownSuccess' },
done = { raw = '[!DONE]', rendered = '󰄬 Done', highlight = 'RenderMarkdownSuccess' },
question = { raw = '[!QUESTION]', rendered = '󰘥 Question', highlight = 'RenderMarkdownWarn' },
help = { raw = '[!HELP]', rendered = '󰘥 Help', highlight = 'RenderMarkdownWarn' },
faq = { raw = '[!FAQ]', rendered = '󰘥 Faq', highlight = 'RenderMarkdownWarn' },
attention = { raw = '[!ATTENTION]', rendered = '󰀪 Attention', highlight = 'RenderMarkdownWarn' },
failure = { raw = '[!FAILURE]', rendered = '󰅖 Failure', highlight = 'RenderMarkdownError' },
fail = { raw = '[!FAIL]', rendered = '󰅖 Fail', highlight = 'RenderMarkdownError' },
missing = { raw = '[!MISSING]', rendered = '󰅖 Missing', highlight = 'RenderMarkdownError' },
danger = { raw = '[!DANGER]', rendered = '󱐌 Danger', highlight = 'RenderMarkdownError' },
error = { raw = '[!ERROR]', rendered = '󱐌 Error', highlight = 'RenderMarkdownError' },
bug = { raw = '[!BUG]', rendered = '󰨰 Bug', highlight = 'RenderMarkdownError' },
example = { raw = '[!EXAMPLE]', rendered = '󰉹 Example', highlight = 'RenderMarkdownHint' },
quote = { raw = '[!QUOTE]', rendered = '󱆨 Quote', highlight = 'RenderMarkdownQuote' },
cite = { raw = '[!CITE]', rendered = '󱆨 Cite', highlight = 'RenderMarkdownQuote' },
},
checkbox = {
enabled = true,
render_modes = false,
bullet = false,
right_pad = 1,
unchecked = {
icon = '󰄱 ',
highlight = 'RenderMarkdownUnchecked',
scope_highlight = nil,
},
checked = {
icon = '󰱒 ',
highlight = 'RenderMarkdownChecked',
scope_highlight = nil,
},
custom = {
todo = { raw = '[-]', rendered = '󰥔 ', highlight = 'RenderMarkdownTodo', scope_highlight = nil },
},
},
bullet = {
enabled = true,
render_modes = false,
icons = { '●', '○', '◆', '◇' },
ordered_icons = function(ctx)
local value = vim.trim(ctx.value)
local index = tonumber(value:sub(1, #value - 1))
return ('%d.'):format(index > 1 and index or ctx.index)
end,
left_pad = 0,
right_pad = 0,
highlight = 'RenderMarkdownBullet',
scope_highlight = {},
},
quote = { icon = '▋' },
anti_conceal = {
enabled = false,
},
heading = {
icons = { '󰫎 ', '󰫎 ', '󰫎 ', '󰫎 ', '󰫎 ', '󰫎 ' },
backgrounds = {},
},
nested = false,
})
end,
}
@@ -0,0 +1,40 @@
return {
"nvim-telescope/telescope.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
config = function()
require("telescope").setup({
-- defaults = {
-- borderchars = { "─", "│", "─", "│", "┌", "┐", "┘", "└" },
-- },
})
local tscope = require('telescope.builtin')
vim.keymap.set("n", "<leader>ff", function()
local cwd
if vim.bo.filetype == "oil" then
cwd = require("oil").get_current_dir()
else
cwd = vim.fn.expand("%:p:h")
end
tscope.find_files({ cwd = cwd })
end, { desc = "Find File" })
vim.keymap.set("n", "<leader>fp", function()
tscope.find_files({
cwd = vim.loop.cwd(),
})
end, { desc = "Find Project File" })
vim.keymap.set('n', '<leader>bb', tscope.buffers,
{ desc = "Find Buffer" })
vim.keymap.set('n', '<leader>fh', tscope.help_tags,
{ desc = "Find Help" })
vim.keymap.set('n', '<leader>fg', tscope.live_grep,
{ desc = "Find by Grep" })
vim.keymap.set('n', '<leader>fr', tscope.oldfiles,
{ desc = "Find Recent Files" })
vim.keymap.set('n', '<leader>lw', tscope.diagnostics,
{ desc = "Search Diagnostics" })
vim.keymap.set('n', '<leader>lg', tscope.lsp_references,
{ desc = "Search References" })
end,
}
@@ -0,0 +1,14 @@
return {
"folke/todo-comments.nvim",
dependencies = { "nvim-lua/plenary.nvim" },
opts = {
},
config = function()
require('todo-comments').setup({})
vim.keymap.set('n', '<leader>ft', function()
vim.cmd('TodoTelescope')
end,
{ desc = "Find Buffer" })
end,
}
@@ -0,0 +1,27 @@
return {
"OXY2DEV/ui.nvim",
lazy = false,
config = function()
require("ui").setup({
cmdline = {
enable = true,
row_offset = 0,
},
messages = {
enable = true,
},
popupmenu = {
enable = false,
},
});
local spec = require("ui.spec")
spec.config.message.showcmd.modifier = function(_, lines)
return {
lines = { "" },
extmarks = { {} },
}
end
end,
};
@@ -0,0 +1,17 @@
return {
'folke/which-key.nvim',
event = 'VeryLazy',
opts = {
icons = { mappings = false },
delay = 1000
},
keys = {
{
'<leader>?',
function()
require('which-key').show({ global = false })
end,
desc = 'Buffer Local Keymaps',
},
},
}
@@ -0,0 +1,26 @@
return {
"zk-org/zk-nvim",
lazy = false,
config = function()
require("zk").setup({
picker = "telescope",
lsp = {
config = {
name = "zk",
cmd = { "zk", "lsp" },
filetypes = { "markdown" },
},
auto_attach = {
enabled = true,
},
},
})
end,
keys = {
{ "<leader>zz", "<cmd>ZkNotes<cr>", desc = "Notes" },
{ "<leader>zl", "<cmd>ZkLinks<cr>", desc = "Links" },
{ "<leader>zb", "<cmd>ZkBacklinks<cr>", desc = "Backlinks" },
{ "<leader>zi", "<cmd>ZkInsertLink<cr>", desc = "Insert Link" },
{ "<leader>zd", "<cmd>ZkCd<cr>", desc = "Enter Directory" },
}
}
@@ -0,0 +1,760 @@
--[[
*Fancy* diagnostics for `Neovim`.
## Usage
```lua
require("diagnostics").setup();
```
You can then hit `D` to show diagnostics for the current line.
## Highlight groups
This script uses highlight groups provided by [highlights.lua](https://github.com/OXY2DEV/nvim/blob/main/lua/scripts/highlights.lua).
The used groups are,
- `FancyDiagnostic`, Default group for diagnostics.
- `FancyDiagnosticIcon`, Default group for the icons of diagnostics.
- `FancyDiagnosticInfo`, Group for `information` diagnostics.
- `FancyDiagnosticInfoIcon`, Group for the icons of `information` diagnostics.
- `FancyDiagnosticHint`, Group for `hint` diagnostics.
- `FancyDiagnosticHintIcon`, Group for the icons of `hint` diagnostics.
- `FancyDiagnosticWarn`, Group for `warning` diagnostics.
- `FancyDiagnosticWarnIcon`, Group for the icons of `warning` diagnostics.
- `FancyDiagnosticError`, Group for `error` diagnostics.
- `FancyDiagnosticErrorIcon`, Group for the icons of `error` diagnostics.
## Dependencies
- [beacon.lua](https://github.com/OXY2DEV/nvim/blob/main/lua/scripts/beacon.lua), For highlighting cursor.
- [highlights.lua](https://github.com/OXY2DEV/nvim/blob/main/lua/scripts/highlights.lua), For highlight groups.
- [types/diagnostics.lua](https://github.com/OXY2DEV/nvim/blob/main/lua/scripts/types/diagnostics.lua), For type definition.
## Configuration
Use `setup()` for setting configuration. See [types/diagnostics.lua#diagnostics.config](https://github.com/OXY2DEV/nvim/blob/main/lua/scripts/types/diagnostics.lua#L3-L19) for the type definition.
]]
local diagnostics = {};
---@param level string
---@param icon string
---@return table
local function handle_diagnostic_level (level, icon)
---|fS
local default = string.format("FancyDiagnostic%s", "Default");
local default_icon_hl = string.format("FancyDiagnostic%sIcon", "Default");
local bg = string.format("FancyDiagnostic%s", level);
local icon_hl = string.format("FancyDiagnostic%sIcon", level);
return {
width = 3,
line_hl_group = function (_, current)
return current and bg or default;
end,
icon = function (_, current)
return {
{ "▌", current and icon_hl or default_icon_hl },
{ icon, current and icon_hl or default_icon_hl },
{ " ", current and bg or default },
}
end,
padding = function (_, current)
return {
{ "▌", current and icon_hl or default_icon_hl },
{ " ", current and icon_hl or default_icon_hl },
{ " ", current and bg or default },
}
end
};
---|fE
end
---@class diagnostics.config
diagnostics.config = {
---|fS
keymap = "D",
decoration_width = 4,
width = function (items)
local max = math.floor(vim.o.columns * 0.4);
local use = 1
for _, item in ipairs(items) do
local width = vim.fn.strdisplaywidth(item.message or "");
use = math.min(
math.max(width, 0),
max
);
end
return use;
end,
max_height = function ()
return math.floor(vim.o.lines * 0.4);
end,
beacon = {
---|fS
default = {
from = function ()
local fg = vim.api.nvim_get_hl(0, { name = "@comment", link = false }).fg;
return fg and string.format("#%06x", fg) or "#9399b2";
end,
to = function ()
local bg = vim.api.nvim_get_hl(0, { name = vim.o.statusline and "Cursorline" or "Normal", link = false }).bg;
return bg and string.format("#%06x", bg) or "#1e1e2e";
end,
steps = 10,
interval = 100,
},
[vim.diagnostic.severity.INFO] = {
from = function ()
local fg = vim.api.nvim_get_hl(0, { name = "FancyDiagnosticInfoIcon", link = false }).fg;
return fg and string.format("#%06x", fg) or "#94e2d5";
end
},
[vim.diagnostic.severity.HINT] = {
from = function ()
local fg = vim.api.nvim_get_hl(0, { name = "FancyDiagnosticHintIcon", link = false }).fg;
return fg and string.format("#%06x", fg) or "#94e2d5";
end
},
[vim.diagnostic.severity.WARN] = {
from = function ()
local fg = vim.api.nvim_get_hl(0, { name = "FancyDiagnosticWarnIcon", link = false }).fg;
return fg and string.format("#%06x", fg) or "#f9e2af";
end
},
[vim.diagnostic.severity.ERROR] = {
from = function ()
local fg = vim.api.nvim_get_hl(0, { name = "FancyDiagnosticErrorIcon", link = false }).fg;
return fg and string.format("#%06x", fg) or "#f38ba8";
end
},
---|fE
},
decorations = {
---|fS
[vim.diagnostic.severity.INFO] = handle_diagnostic_level("Info", "󰀨 "),
[vim.diagnostic.severity.HINT] = handle_diagnostic_level("Hint", "󰁨 "),
[vim.diagnostic.severity.WARN] = handle_diagnostic_level("Warn", ""),
[vim.diagnostic.severity.ERROR] = handle_diagnostic_level("Error", "󰅙 "),
---|fE
},
---|fE
};
--[[ Evaluates `val`. ]]
---@param val any
---@param ... any
---@return any
local function eval(val, ...)
---|fS
if type(val) ~= "function" then
return val;
else
local can_call, new_val = pcall(val, ...);
if can_call and new_val ~= nil then
return new_val;
end
end
---|fE
end
--[[ Gets diagnostic item `decoration`. ]]
---@param level vim.diagnostic.Severity | "default" Diagnostic severity.
---@param ... any Extra arguments to be used for value evaluation.
---@return diagnostics.decorations__static decoration Static version of the diagnostic item's decoration.
local function get_decorations (level, ...)
---|fS
local output = {};
for k, v in pairs(diagnostics.config.decorations[level]) do
output[k] = eval(v, ...);
end
return output;
---|fE
end
--[[ Gets `beacon` configuration. ]]
---@param level? vim.diagnostic.Severity | "default" Diagnostic level.
---@param ... any Extra arguments to be used for value evaluation.
---@return table?
local function get_beacon_config (level, ...)
---|fS
if not level or not diagnostics.config.beacon then
return;
end
local output = {};
for k, v in pairs(diagnostics.config.beacon[level]) do
output[k] = eval(v, ...);
end
return output;
---|fE
end
--[[ Turns given **virtual text** into **format string** for the statusline. ]]
---@param virt_text diagnostics.decoration_fragment[] Virtual text.
---@return string sign Virtual text as a sign(use in `statuscolumn`, `statusline`, `tabline` or `winbar`)
local function virt_text_to_sign (virt_text)
---|fS
local output = "";
for _, item in ipairs(virt_text) do
if type(item[2]) == "string" then
output = output .. string.format("%%#%s#%s", item[2], item[1]) .. "%#Normal#";
else
output = output .. item[1];
end
end
return output;
---|fE
end
------------------------------------------------------------------------------
---@type integer Decoration namespace.
diagnostics.ns = vim.api.nvim_create_namespace("fancy_diagnostics");
---@type integer, integer Diagnostics buffer & window.
diagnostics.buffer, diagnostics.window = nil, nil;
--[[ Which *quadrant* the diagnostics window is currently using? ]]
---@type
---| "top_left"
---| "top_right"
---| "bottom_left"
---| "bottom_right"
---| "center"
diagnostics.quad = nil;
---@type diagnostics.signs.entry[]
diagnostics.sign_data = {};
--[[ Prepares the buffer for the diagnostics window. ]]
diagnostics.__prepare = function ()
---|fS
if not diagnostics.buffer or not vim.api.nvim_buf_is_valid(diagnostics.buffer) then
diagnostics.buffer = vim.api.nvim_create_buf(false, true);
end
---|fE
end
--[[ Updates the *quadrant* which is being used by the diagnostics window. ]]
---@param quad
---| "top_left"
---| "top_right"
---| "bottom_left"
---| "bottom_right"
---| "center"
---@param state boolean
diagnostics.update_quad = function (quad, state)
---|fS
if not _G.__used_quads then
-- NIT: Should this set a default state value?
return;
end
_G.__used_quads[quad] = state;
---|fE
end
--[[ Returns `{opts}` for `nvim_open_win()` based on the parameters. ]]
---@param window integer Window ID.
---@param w integer Window width.
---@param h integer Window height.
---@return string | string[] border Window border.
---@return "editor" | "cursor" relative Relative position for floating window.
---@return "NE" | "NW" | "SE" | "SW" anchor Anchor position.
---@return integer row Window position in Y-axis.
---@return integer col Window position in X-axis.
diagnostics.__win_args = function (window, w, h)
---|fS
---@type [ integer, integer ]
local cursor = vim.api.nvim_win_get_cursor(window);
---@type table<string, integer>
local screenpos = vim.fn.screenpos(window, cursor[1], cursor[2]);
local screen_width = vim.o.columns - 2;
local screen_height = vim.o.lines - vim.o.cmdheight - 2;
local quad_pref = { "bottom_right", "top_right", "bottom_left", "top_left" };
local quads = {
---|fS
center = {
relative = "editor",
anchor = "NW",
row = math.ceil((vim.o.lines - h) / 2),
col = math.ceil((vim.o.columns - w) / 2),
border = "rounded"
},
top_left = {
condition = function ()
if h >= screenpos.row then
-- Not enough space above.
return false;
elseif screenpos.curscol <= w then
-- Not enough space before.
return false;
end
return true;
end,
relative = "cursor",
border = { "╭", "─", "╮", "│", "┤", "─", "╰", "│" },
anchor = "SE",
row = 0,
col = 1
},
top_right = {
condition = function ()
if h >= screenpos.row then
-- Not enough space above.
return false;
elseif screenpos.curscol + w > screen_width then
-- Not enough space after.
return false;
end
return true;
end,
relative = "cursor",
border = { "╭", "─", "╮", "│", "╯", "─", "├", "│" },
anchor = "SW",
row = 0,
col = 0
},
bottom_left = {
condition = function ()
if screenpos.row + h > screen_height then
-- Not enough space below.
return false;
elseif screenpos.curscol <= w then
-- Not enough space before.
return false;
end
return true;
end,
relative = "cursor",
border = { "╭", "─", "┤", "│", "╯", "─", "╰", "│" },
anchor = "NE",
row = 1,
col = 1
},
bottom_right = {
condition = function ()
if screenpos.row + h > screen_height then
-- Not enough space below.
return false;
elseif screenpos.curscol + w > screen_width then
-- Nor enough space after.
return false;
end
return true;
end,
relative = "cursor",
border = { "├", "─", "╮", "│", "╯", "─", "╰", "│" },
anchor = "NW",
row = 1,
col = 0
}
---|fE
};
for _, pref in ipairs(quad_pref) do
if _G.__used_quads and _G.__used_quads[pref] == true then
goto continue;
elseif not quads[pref] then
goto continue;
end
local quad = quads[pref];
local ran_cond, cond = pcall(quad.condition);
if ran_cond and cond then
diagnostics.quad = pref;
return quad.border, quad.cursor, quad.anchor, quad.row, quad.col;
end
::continue::
end
diagnostics.quad = "center";
local fallback = quads.center;
return fallback.border, fallback.cursor, fallback.anchor, fallback.row, fallback.col;
---|fE
end
------------------------------------------------------------------------------
--[[ Closes diagnostics window. ]]
diagnostics.close = function ()
---|fS
if diagnostics.window and vim.api.nvim_win_is_valid(diagnostics.window) then
pcall(vim.api.nvim_win_close, diagnostics.window, true);
diagnostics.window = nil;
if diagnostics.quad then
diagnostics.update_quad(diagnostics.quad, false);
diagnostics.quad = nil;
end
end
---|fE
end
---@type beacon.instance Beacon instance to use for the Cursor.
diagnostics.__beacon = nil;
--[[ External integrations. ]]
---@param window integer Window Id.
---@param beacon_config beacon.instance.config Configuration for `beacon`.
diagnostics.__integration = function (window, beacon_config)
---|fS
-- Markdown rendering.
if package.loaded["markview"] then
package.loaded["markview"].render(diagnostics.buffer, {
enable = true,
hybrid_mode = false
}, {
markdown_inline = {
inline_codes = {
virtual = true
}
}
});
end
-- Beacon.
if package.loaded["scripts.beacon"] then
if not diagnostics.__beacon then
diagnostics.__beacon = require("scripts.beacon").new(window, beacon_config);
else
diagnostics.__beacon:update(window, beacon_config);
end
diagnostics.__beacon:start();
end
---|fE
end
--- Custom statuscolumn.
---@return string
_G.fancy_diagnostics_statuscolumn = function ()
---|fS
if vim.tbl_isempty(diagnostics.sign_data) then
return "";
end
local lnum = vim.v.lnum;
local data = diagnostics.sign_data[lnum];
local start = data.start_row;
if not data then
return "";
elseif vim.v.virtnum == 0 and start == lnum then
return virt_text_to_sign(data.icon);
else
return virt_text_to_sign(data.padding or data.icon);
end
---|fE
end
--- Hover function for diagnostics.
---@param window integer
diagnostics.hover = function (window)
---|fS
window = window or vim.api.nvim_get_current_win();
---@type integer Source buffer.
local buffer = vim.api.nvim_win_get_buf(window);
---@type [ integer, integer ]
local cursor = vim.api.nvim_win_get_cursor(window);
local items = vim.diagnostic.get(buffer, { lnum = cursor[1] - 1 });
---@type boolean Is the window already open?
local already_open = diagnostics.window and vim.api.nvim_win_is_valid(diagnostics.window);
if #items == 0 then
-- No diagnostics available.
diagnostics.close();
vim.api.nvim_echo({
{ " 󰾕 diagnostics.lua ", "DiagnosticVirtualTextWarn" },
{ ": ", "@comment" },
{ "No diagnostic under cursor", "@comment" },
}, true, {});
return;
elseif already_open then
vim.api.nvim_set_current_win(diagnostics.window);
return;
end
if diagnostics.quad then
-- If the old window's quadrant wasn't freed, we free it here.
diagnostics.update_quad(diagnostics.quad, false)
end
diagnostics.__prepare();
vim.bo[diagnostics.buffer].ft = "markdown";
-- Clear old decorations.
vim.api.nvim_buf_clear_namespace(diagnostics.buffer, diagnostics.ns, 0, -1);
vim.api.nvim_buf_set_lines(diagnostics.buffer, 0, -1, false, {});
local W = eval(diagnostics.config.width, items);
local D = eval(diagnostics.config.decoration_width, items);
---@type table Configuration used for calculating window height.
local height_calc_config = {
relative = "editor",
row = 0, col = 1,
width = W - D, height = 2,
style = "minimal",
hide = true,
};
if not diagnostics.window or not vim.api.nvim_win_is_valid(diagnostics.window) then
diagnostics.window = vim.api.nvim_open_win(diagnostics.buffer, false, height_calc_config);
else
vim.api.nvim_win_set_config(diagnostics.window, height_calc_config);
end
vim.wo[diagnostics.window].wrap = true;
vim.wo[diagnostics.window].linebreak = true;
vim.wo[diagnostics.window].breakindent = true;
local diagnostic_lines = 0;
---@type integer Line where the cursor should be placed.
local cursor_y = 1;
local ranges = {};
local start_row = 1;
---@type vim.diagnostic.Severity | nil
local level;
diagnostics.sign_data = {};
for i, item in ipairs(items) do
---|fS
local from = i == 1 and 0 or -1;
local lines = vim.split(item.message, "\n", { trimempty = true })
local start = item.col;
local stop = item.end_col;
local current = false;
if cursor[2] >= start and cursor[2] <= stop then
cursor_y = i;
current = true;
end
vim.api.nvim_buf_set_lines(diagnostics.buffer, from, -1, false, lines);
local decorations = get_decorations(item.severity, item, current);
ranges[i] = { item.lnum, item.col };
vim.api.nvim_buf_set_extmark(diagnostics.buffer, diagnostics.ns, diagnostic_lines, 0, {
end_row = diagnostic_lines + #lines,
line_hl_group = decorations.line_hl_group,
});
diagnostic_lines = diagnostic_lines + #lines;
if current == true and ( not level or item.severity < level ) then
level = item.severity;
end
for _ = 1, #lines do
-- Signs
table.insert(diagnostics.sign_data, {
start_row = start_row,
current = current,
width = decorations.width,
icon = decorations.icon,
line_hl_group = decorations.line_hl_group,
padding = decorations.padding,
});
end
start_row = start_row + #lines;
---|fE
end
local beacon_config = vim.tbl_extend("force",
get_beacon_config("default", {}, true),
get_beacon_config(level, {}, true) or {}
);
local H = vim.api.nvim_win_text_height(diagnostics.window, { start_row = 0, end_row = -1 }).all;
local _, relative, anchor, row, col = diagnostics.__win_args(window, W, H);
local win_config = {
relative = relative or "cursor",
row = row or 0, col = col or 0,
width = W, height = H,
anchor = anchor,
border = "none",
style = "minimal",
hide = false,
};
vim.api.nvim_win_set_config(diagnostics.window, win_config);
vim.api.nvim_win_set_cursor(diagnostics.window, { cursor_y, 0 });
-- Update quadrant state.
diagnostics.update_quad(diagnostics.quad, true);
-- Set necessary options.
vim.wo[diagnostics.window].signcolumn = "no";
vim.wo[diagnostics.window].statuscolumn = "%!v:lua.fancy_diagnostics_statuscolumn()";
vim.wo[diagnostics.window].conceallevel = 3;
vim.wo[diagnostics.window].concealcursor = "ncv";
vim.wo[diagnostics.window].winhl = "FloatBorder:@comment,Normal:Normal";
diagnostics.__integration(window, beacon_config);
---|fS
vim.api.nvim_buf_set_keymap(diagnostics.buffer, "n", "<CR>", "", {
desc = "Go to diagnostic location",
callback = function ()
---|fS
---@type [ integer, integer ] Selected item.
local _cursor = vim.api.nvim_win_get_cursor(diagnostics.window);
---@type [ integer, integer ]? Diagnostic location.
local location = ranges[_cursor[1]];
if location then
location[1] = location[1] + 1;
vim.api.nvim_win_set_cursor(window, location);
vim.api.nvim_set_current_win(window);
diagnostics.close();
end
---|fE
end
});
vim.api.nvim_buf_set_keymap(diagnostics.buffer, "n", "q", "", {
desc = "Exit diagnostics window",
callback = function ()
pcall(vim.api.nvim_set_current_win, window);
diagnostics.close();
end
});
---|fE
---|fE
end
--- Configuration for the diagnostics module.
---@param config? diagnostics.config
diagnostics.setup = function (config)
---|fS
if type(config) == "table" then
diagnostics.config = vim.tbl_extend("force", diagnostics.config, config);
end
if diagnostics.config.keymap then
vim.api.nvim_set_keymap("n", diagnostics.config.keymap, "", {
callback = diagnostics.hover
});
end
vim.api.nvim_create_autocmd({
"CursorMoved", "CursorMovedI"
}, {
callback = function ()
local win = vim.api.nvim_get_current_win();
if diagnostics.window and win ~= diagnostics.window then
diagnostics.close();
if diagnostics.quad then
diagnostics.update_quad(diagnostics.quad, false);
diagnostics.quad = nil;
end
end
end
});
---|fE
end
return diagnostics;
@@ -0,0 +1,4 @@
require("scripts.diagnostics").setup({
keymap = '<leader>le'
-- TODO: Find out how to which-key this
})