yes there's a bit of java in my nvim config why do you ask?

This commit is contained in:
2024-08-09 02:45:31 -04:00
parent ad76983d96
commit c489d39369
104 changed files with 2058 additions and 2796 deletions

View File

@ -1,81 +0,0 @@
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local previewers = require("telescope.previewers")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local M = {}
local function genmenu()
local list = {}
local function add(name, plug)
if not plug then
table.insert(list, name)
elseif package.loaded[plug] then
table.insert(list, name)
end
end
add('Edit Config', nil)
add('Update Plugins', 'dep')
add('Keybinds', 'telescope')
add('Colorscheme', 'telescope')
return list
end
function M.configmenu()
pickers.new({
prompt_title = "Nvim Config Menu",
finder = finders.new_table { results = genmenu() },
sorter = conf.generic_sorter(),
previewer = previewers.new_buffer_previewer {
define_preview = function(self, entry)
local lines = {
'a'
}
if entry.value == "Edit Config" then
lines = misc.readf(vim.fn.stdpath('config')..'/init.lua')
local ft = vim.filetype.match({
filename = os.getenv('XDG_CONFIG_HOME')..'/nvim/init.lua' })
require("telescope.previewers.utils").highlighter(self.state.bufnr, ft)
elseif entry.value == "Colorscheme" then
lines = vim.fn.getcompletion('', 'color')
for k, v in pairs(lines) do
if v.find(v, '.ext') then
table.remove(lines, k)
break
end
end
end
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
end
},
attach_mappings = function(bufnr, map)
actions.select_default:replace(function()
actions.close(bufnr)
local selection = action_state.get_selected_entry()
if selection[1] == 'Edit Config' then
vim.cmd('e '..vim.fn.stdpath('config')..'/init.lua')
elseif selection[1] == 'Update Plugins' then
require('dep').sync()
if package.loaded['nvim-treesitter'] then
vim.cmd('TSUpdate')
end
if package.loaded['mason'] then
require('mason.api.command').MasonUpdate()
end
elseif selection[1] == 'Keybinds' then
require('telescope.builtin').keymaps()
elseif selection[1] == 'Colorscheme' then
require('core.theme').switcher()
end
end)
return true
end,
}):find()
end
return M

View File

@ -1,19 +0,0 @@
-- clear menu
vim.cmd([[:aunmenu PopUp]])
-- add menu items
vim.cmd([[:amenu PopUp.Save\ Buffer <cmd>:w<CR>]])
vim.cmd([[:amenu PopUp.Save\ All\ Buffers <cmd>:wa<CR>]])
vim.cmd([[:nmenu PopUp.Select\ All ggVG]])
vim.cmd([[:nmenu PopUp.Undo u]])
vim.cmd([[:nmenu PopUp.Redo <C-r>]])
vim.cmd([[:amenu PopUp.-1- <nop>]]) -- dividers
vim.cmd([[:nmenu PopUp.Copy\ Line yy]])
vim.cmd([[:vmenu PopUp.Copy\ Selection y]])
vim.cmd([[:amenu PopUp.Paste p]])
vim.cmd([[:vmenu PopUp.Cut d]])
vim.cmd([[:nmenu PopUp.Cut dd]])
vim.cmd([[:vmenu PopUp.-2- <nop>]]) -- dividers
vim.cmd([[:nmenu PopUp.-2- <nop>]]) -- dividers
vim.cmd([[:vmenu PopUp.Format\ Selection =]])
vim.cmd([[:nmenu PopUp.Format\ Line ==]])

View File

@ -1,61 +0,0 @@
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_set = require("telescope.actions.set")
local action_state = require("telescope.actions.state")
local harpoon = require('harpoon')
local M = {}
function M.switcher()
local filepaths = {}
for _, item in ipairs(harpoon:list().items) do
table.insert(filepaths, item.value)
end
pickers.new({
prompt_title = "Harpoon",
finder = finders.new_table { results = filepaths },
sorter = conf.generic_sorter(),
previewer = conf.file_previewer {},
attach_mappings = function(prompt_bufnr, map)
actions.move_selection_previous:replace(function()
action_set.shift_selection(prompt_bufnr, -1)
end)
actions.move_selection_next:replace(function()
action_set.shift_selection(prompt_bufnr, 1)
end)
-- remove harpoon item
vim.keymap.set("i", "<C-a>", function()
if action_state.get_selected_entry() then
for i, v in ipairs(filepaths) do
if v == action_state.get_selected_entry()[1] then
harpoon:list():removeAt(i)
actions.close(prompt_bufnr)
M.switcher()
end
end
end
end)
-- select items, and open buffer
vim.keymap.set("i", "<C-s>", function()
if action_state.get_selected_entry() then
actions.close(prompt_bufnr)
vim.cmd("split "..action_state.get_selected_entry()[1])
end
end, { buffer = true, remap = true })
actions.select_default:replace(function()
if action_state.get_selected_entry() then
actions.close(prompt_bufnr)
vim.cmd("e "..action_state.get_selected_entry()[1])
end
end)
return true
end
}):find()
end
return M

View File

@ -1,43 +1,184 @@
local M = {}
--- vim.notify title
M.appid = "Nvim Config"
--- safe version of require
---@param fn string name of file to include
---@return any
function M.include(fn)
local ok, r = pcall(require, fn)
if not ok then
vim.notify('Could not find "'..fn..'": '..r, vim.log.levels.WARN, { title = M.appid })
return ok
end
return r
end
--- loop through files in directory
---@param path string absolute path of directory to be looped through
---@param body function function to use on every recursion of the loop
---@param ext string? desired file extension of files to be returned from directory
function M.loopf(path, body, ext)
ext = ext and [[v:val =~ '\.]]..ext..[[$']] or nil
for _, file in ipairs(vim.fn.readdir(path, ext)) do
body(file)
end
end
--- set colorscheme
---@param name string name of colorscheme
function M.colorscheme(name)
vim.cmd('colorscheme '..name)
for k, v in pairs(vim.fn.getcompletion('', 'color')) do
for _, v in pairs(vim.fn.getcompletion('', 'color')) do
if v == name..'.ext' then
vim.cmd('colorscheme '..name..'.ext')
end
end
end
function M.replaceword(old, new)
local conf = vim.fn.stdpath("config").."/lua/conf/".."opts.lua"
local f = io.open(conf, "r")
if not f then return end
local new_content = f:read("*all"):gsub(old, new)
f:close()
--- extend vim.kemap.set
---@param mode string|table mode for the keymap
---@param bind string|table keymap
---@param cmd function|string command to run
---@param opts table? keymap options
function M.map(mode, bind, cmd, opts)
opts = opts or {}
opts['noremap'] = true
opts['silent'] = true
f = io.open(conf, "w")
if not f then return end
f:write(new_content)
f:close()
end
function M.include(fn)
if not pcall(require, fn) then
vim.notify('Could not find "'..fn, vim.log.levels.WARN..'"', { title = M.appid })
if type(bind) == 'table' then
for i in pairs(bind) do
vim.keymap.set(mode, bind[i], cmd, opts)
end
elseif type(bind) == 'string' then
vim.keymap.set(mode, bind, cmd, opts)
end
end
function M.readf(fn)
local f = io.open(fn, "r")
if not f then return nil end
local tab = {}
for l in f:lines() do
table.insert(tab, l)
--- a small map wrapper to easily create local buffer mappings
---@param mode string|table mode for the keymap
---@param bind string|table keymap
---@param cmd function|string command to run
---@param opts table? keymap options
function M.map_local(mode, bind, cmd, opts)
opts = opts or {}
opts["buffer"] = 0
M.map(mode, bind, cmd, opts)
end
--- extend vim.api.nvim_create_autocmd
---@param event string|table event or events
---@param opts table options
function M.auto(event, opts)
vim.api.nvim_create_autocmd(event, opts)
end
--- extend auto group
---@param name string name of the autogroup
---@param opts table? table of options
function M.augroup(name, opts)
opts = opts or {}
vim.api.nvim_create_augroup(name, opts)
end
--- extend vim.api.nvim_set_hl
---@param group string|table highlight group
---@param opts table highlight options
---@param namespace? number highlight space
function M.highlight(group, opts, namespace)
namespace = namespace or 0
if type(group) == 'table' then
for i in pairs(group) do
vim.api.nvim_set_hl(namespace, group[i], opts)
end
elseif type(group) == 'string' then
vim.api.nvim_set_hl(namespace, group, opts)
end
return tab
end
--- copy highlight group
---@param hlgroup string highlight group to copy
---@param namespace? number highlight space
---@return table
function M.cpyhl(hlgroup, namespace)
namespace = namespace or 0
local ok, hl = pcall(vim.api.nvim_get_hl, namespace, {
name = hlgroup,
create = false
})
if not ok then
return {}
end
for _, key in pairs({"foreground", "background", "special"}) do
if hl[key] then
hl[key] = string.format("#%06x", hl[key])
end
end
return hl
end
--- highlight something with some highlight group for a certain amount of time
---@param opts table? options
--- example:
--- ```lua
--- {
--- -- highlight group to use
--- hl = "IncSearch",
---
--- -- # of ms to stay highlighted for
--- timeout = 250,
---
--- -- line to highlight
--- line = vim.api.nvim_win_get_cursor(0)[1],
---
--- -- range to highlight if this is used line will be ignored
--- range = {
--- { 0, 0 },
--- { 0, 0 }
--- }
--- }
--- ```
--- opts is optional and if empty will simply highlight the current line for
--- 250ms using IncSearch as the highlight group
function M.timeout_highlight(opts)
opts = opts or {}
opts.hl = opts.hl or "IncSearch"
opts.timeout = opts.timeout or 250
if type(opts.range) ~= "table" or type(opts.range[1]) ~= "table" or
type(opts.range[2]) ~= "table" then
local curline = opts.line or vim.api.nvim_win_get_cursor(0)[1]
opts.range = {
{ curline - 1, 0 },
{ curline, 0 }
}
end
local namespaceid = vim.api.nvim_create_namespace("timeout_highlight")
-- timer code was happily stolen from neovim/runtime/lua/vim/highlight.lua :)
local timer, timer_cancel
if timer then
timer:close()
assert(timer_cancel)
timer_cancel()
end
-- set the highlight
vim.highlight.range(0, namespaceid, opts.hl, opts.range[1], opts.range[2], {})
timer_cancel = function()
timer = nil
timer_cancel = nil
pcall(vim.api.nvim_buf_clear_namespace, 0, namespaceid, 0, -1)
pcall(vim.api.nvim_win_remove_ns, 0, namespaceid)
end
timer = vim.defer_fn(timer_cancel, opts.timeout)
end
return M

View File

@ -0,0 +1,22 @@
luasnip = require('luasnip')
s = luasnip.snippet
sn = luasnip.snippet_node
t = luasnip.text_node
i = luasnip.insert_node
f = luasnip.function_node
c = luasnip.choice_node
d = luasnip.dynamic_node
r = luasnip.restore_node
l = require("luasnip.extras").lambda
rep = require("luasnip.extras").rep
p = require("luasnip.extras").partial
m = require("luasnip.extras").match
n = require("luasnip.extras").nonempty
dl = require("luasnip.extras").dynamic_lambda
fmt = require("luasnip.extras.fmt").fmt
fmta = require("luasnip.extras.fmt").fmta
types = require("luasnip.util.types")
conds = require("luasnip.extras.conditions")
conds_expand = require("luasnip.extras.conditions.expand")
ts_postfix = require("luasnip.extras.treesitter_postfix").treesitter_postfix

View File

@ -1,325 +0,0 @@
if not pcall(require, "el") then return end
local job = require "plenary.job"
local el_sub = require "el.subscribe"
local M = {}
function M.extract_hl(spec)
if not spec or vim.tbl_isempty(spec) then return end
local hl_name, hl_opts = { "El" }, {}
for attr, val in pairs(spec) do
if type(val) == "table" then
table.insert(hl_name, attr)
assert(vim.tbl_count(val) == 1)
local hl, what = next(val)
local hlID = vim.fn.hlID(hl)
if hlID > 0 then
table.insert(hl_name, hl)
local col = vim.fn.synIDattr(hlID, what)
if col and #col > 0 then
table.insert(hl_name, what)
hl_opts[attr] = col
end
end
else
-- bold, underline, etc
hl_opts[attr] = val
end
end
hl_name = table.concat(hl_name, "_")
-- if highlight exists, verify it has
-- the correct colorscheme highlights
local newID = vim.fn.hlID(hl_name)
if newID > 0 then
for what, expected in pairs(hl_opts) do
local res = vim.fn.synIDattr(newID, what)
if type(expected) == "boolean" then
-- synIDattr returns '1' for boolean
res = res and res == "1" and true
end
if res ~= expected then
-- need to regen the highlight
-- print("color mismatch", hl_name, what, "e:", expected, "c:", res)
newID = 0
end
end
end
if newID == 0 then
vim.api.nvim_set_hl(0, hl_name, hl_opts)
end
return hl_name
end
local function set_hl(hls, s)
if not hls or not s then return s end
hls = type(hls) == "string" and { hls } or hls
for _, hl in ipairs(hls) do
if vim.fn.hlID(hl) > 0 then
return ("%%#%s#%s%%0*"):format(hl, s)
end
end
return s
end
local function wrap_fnc(opts, fn)
return function(window, buffer)
-- buf_autocmd doesn't send win
if not window and buffer then
window = { win_id = vim.fn.bufwinid(buffer.bufnr) }
end
if opts.hide_inactive and window and
window.win_id ~= vim.api.nvim_get_current_win() then
return ""
end
return fn(window, buffer)
end
end
function M.mode(opts)
opts = opts or {}
return wrap_fnc(opts, function(_, _)
local fmt = opts.fmt or "%s%s"
local mode = vim.api.nvim_get_mode().mode
local mode_data = opts.modes and opts.modes[mode]
local hls = mode_data and mode_data[3]
local icon = opts.hl_icon_only and set_hl(hls, opts.icon) or opts.icon
mode = mode_data and mode_data[1]:upper() or mode
mode = (fmt):format(icon or "", mode)
return not opts.hl_icon_only and set_hl(hls, mode) or mode
end)
end
function M.git_branch(opts)
opts = opts or {}
return el_sub.buf_autocmd("el_git_branch", "BufEnter",
wrap_fnc(opts, function(_, buffer)
-- Try fugitive first as it's most reliable
local branch = vim.g.loaded_fugitive == 1 and
vim.fn.FugitiveHead() or nil
-- buffer can be null and code will crash with:
-- E5108: Error executing lua ... 'attempt to index a nil value'
if not buffer or not (buffer.bufnr > 0) then
return
end
-- fugitive is empty or not loaded, try gitsigns
if not branch or #branch == 0 then
local ok, res = pcall(vim.api.nvim_buf_get_var,
buffer.bufnr, "gitsigns_head")
if ok then branch = res end
end
-- last resort run git command
if not branch then
local j = job:new {
command = "git",
args = { "branch", "--show-current" },
cwd = vim.fn.fnamemodify(buffer.name, ":h"),
}
local ok, result = pcall(function()
return vim.trim(j:sync()[1])
end)
if ok then
branch = result
end
end
if branch and #branch > 0 then
local fmt = opts.fmt or "%s %s"
local icon = opts.icon or ""
return set_hl(opts.hl, (fmt):format(icon, branch))
end
end))
end
local function git_changes_formatter(opts)
local specs = {
insert = {
regex = "(%d+) insertions?",
icon = opts.icon_insert or "+",
hl = opts.hl_insert,
},
change = {
regex = "(%d+) files? changed",
icon = opts.icon_change or "~",
hl = opts.hl_change,
},
delete = {
regex = "(%d+) deletions?",
icon = opts.icon_delete or "-",
hl = opts.hl_delete,
},
}
return function(_, _, s)
local result = {}
for k, v in pairs(specs) do
local count = nil
if type(s) == "string" then
-- 'git diff --shortstat' output
-- from 'git_changes_all'
count = tonumber(string.match(s, v.regex))
else
-- map from 'git_changes_buf'
count = s[k]
end
if count and count > 0 then
table.insert(result, set_hl(v.hl, ("%s%d"):format(v.icon, count)))
end
end
return table.concat(result, " ")
end
end
-- requires gitsigns
function M.git_changes_buf(opts)
opts = opts or {}
local formatter = opts.formatter or git_changes_formatter(opts)
return wrap_fnc(opts, function(window, buffer)
local stats = {}
if buffer and buffer.bufnr > 0 then
local ok, res = pcall(vim.api.nvim_buf_get_var,
buffer.bufnr, "vgit_status")
if ok then stats = res end
end
if buffer and buffer.bufnr > 0 then
local ok, res = pcall(vim.api.nvim_buf_get_var,
buffer.bufnr, "gitsigns_status_dict")
if ok then stats = res end
end
local counts = {
insert = stats.added > 0 and stats.added or nil,
change = stats.changed > 0 and stats.changed or nil,
delete = stats.removed > 0 and stats.removed or nil,
}
if not vim.tbl_isempty(counts) then
local fmt = opts.fmt or "%s"
local out = formatter(window, buffer, counts)
return out and fmt:format(out) or nil
else
-- el functions that return a
-- string must not return nil
return ""
end
end)
end
function M.git_changes_all(opts)
opts = opts or {}
local formatter = opts.formatter or git_changes_formatter(opts)
return el_sub.buf_autocmd("el_git_changes", "BufWritePost",
wrap_fnc(opts, function(window, buffer)
if not buffer or
not (buffer.bufnr > 0) or
vim.bo[buffer.bufnr].bufhidden ~= "" or
vim.bo[buffer.bufnr].buftype == "nofile" or
vim.fn.filereadable(buffer.name) ~= 1 then
return
end
local j = job:new {
command = "git",
args = { "diff", "--shortstat" },
-- makes no sense to run for one file as
-- 'file(s) changed' will always be 1
-- args = { "diff", "--shortstat", buffer.name },
cwd = vim.fn.fnamemodify(buffer.name, ":h"),
}
local ok, git_changes = pcall(function()
return formatter(window, buffer, vim.trim(j:sync()[1]))
end)
if ok then
local fmt = opts.fmt or "%s"
return git_changes and fmt:format(git_changes) or nil
end
end))
end
function M.lsp_srvname(opts)
local fmt = opts.fmt or "%s"
local icon = opts.icon or ""
local names = ""
vim.lsp.for_each_buffer_client(0, function(client)
if names ~= "" then names = names..", " end
names = names..client.name
end)
return icon..fmt:format(names)
end
local function diag_formatter(opts)
return function(_, buffer, counts)
local items = {}
local icons = {
["errors"] = { opts.icon_err or "x", opts.hl_err or "DiagnosticError"},
["warnings"] = { opts.icon_warn or "!", opts.hl_warn or "DiagnosticWarn"},
["infos"] = { opts.icon_info or "i", opts.hl_info or "DiagnosticInfo"},
["hints"] = { opts.icon_hint or "h", opts.hl_hint or "DiagnosticHint"},
}
for _, k in ipairs({ "errors", "warnings", "infos", "hints" }) do
if counts[k] > 0 then
table.insert(items,
set_hl(icons[k][2], ("%s:%s"):format(icons[k][1], counts[k])))
end
end
local fmt = opts.fmt or "%s"
if vim.tbl_isempty(items) then
return ""
else
return fmt:format(table.concat(items, " "))
end
end
end
local function get_buffer_counts(diagnostic, _, buffer)
local counts = { 0, 0, 0, 0 }
local diags = diagnostic.get(buffer.bufnr)
if diags and not vim.tbl_isempty(diags) then
for _, d in ipairs(diags) do
if tonumber(d.severity) then
counts[d.severity] = counts[d.severity] + 1
end
end
end
return {
errors = counts[1],
warnings = counts[2],
infos = counts[3],
hints = counts[4],
}
end
function M.diagnostics(opts)
opts = opts or {}
local formatter = opts.formatter or diag_formatter(opts)
return el_sub.buf_autocmd("el_buf_diagnostic", "LspAttach,DiagnosticChanged",
wrap_fnc(opts, function(window, buffer)
return formatter(window, buffer, get_buffer_counts(vim.diagnostic, window, buffer))
end))
end
function M.line(opts)
opts = opts or {}
local fmt = opts.fmt or "%s"
return wrap_fnc(opts, function(_, _)
local al = vim.api.nvim_buf_line_count(0)
local cl = vim.api.nvim_win_get_cursor(0)[1]
return (fmt):format(cl.."/"..al.." "..math.floor((cl / al) * 100).."%%")
end)
end
function M.fn_tail(opts)
opts = opts or {}
local fmt = opts.fmt or "%s"
local hl_exec = opts.hl_exec or "Character"
local fn = vim.fn.expand("%:t")
if vim.fn.getftype(fn) == "file" then
if string.match(vim.fn.getfperm(fn), 'x', 3) then
return (fmt):format(set_hl(hl_exec, fn))
end
end
return (fmt):format(fn)
end
return M

View File

@ -1,80 +0,0 @@
local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local previewers = require("telescope.previewers")
local conf = require("telescope.config").values
local actions = require("telescope.actions")
local action_set = require("telescope.actions.set")
local action_state = require("telescope.actions.state")
local misc = require('core.misc')
local M = {}
function M.switcher()
local bufnr = vim.api.nvim_get_current_buf()
-- show current buffer content in previewer
local colors = vim.fn.getcompletion('', 'color')
for k, v in pairs(colors) do
if v.find(v, '.ext') then
table.remove(colors, k)
break
end
end
-- our picker function: colors
pickers.new({
prompt_title = "Set Nvim Colorscheme",
finder = finders.new_table { results = colors },
sorter = conf.generic_sorter(),
previewer = previewers.new_buffer_previewer {
define_preview = function(self, entry)
-- add content
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
-- add syntax highlighting in previewer
local ft = (vim.filetype.match { buf = bufnr } or "diff"):match "%w+"
require("telescope.previewers.utils").highlighter(self.state.bufnr, ft)
end
},
attach_mappings = function(prompt_bufnr, map)
-- reload theme while typing
vim.schedule(function()
vim.api.nvim_create_autocmd("TextChangedI", {
buffer = prompt_bufnr,
callback = function()
if action_state.get_selected_entry() then
misc.colorscheme(action_state.get_selected_entry()[1])
end
end,
})
end)
-- reload theme on cycling
actions.move_selection_previous:replace(function()
action_set.shift_selection(prompt_bufnr, -1)
misc.colorscheme(action_state.get_selected_entry()[1])
end)
actions.move_selection_next:replace(function()
action_set.shift_selection(prompt_bufnr, 1)
misc.colorscheme(action_state.get_selected_entry()[1])
end)
-- reload theme on selection
actions.select_default:replace(function()
if action_state.get_selected_entry() then
actions.close(prompt_bufnr)
misc.colorscheme(action_state.get_selected_entry()[1])
-- make the colorscheme swap persistant
misc.replaceword("misc.colorscheme%(.+%)",
"misc.colorscheme('"..action_state.get_selected_entry()[1].."')")
end
end)
return true
end,
}):find()
end
return M