1
0
Fork 0
mirror of https://github.com/pnx/dotfiles synced 2026-06-16 19:30:01 +02:00

new nvim config

This commit is contained in:
Henrik Hautakoski 2024-08-25 16:49:58 +02:00
parent f087422bbf
commit 7d14948480
66 changed files with 1771 additions and 1719 deletions

View file

@ -0,0 +1,58 @@
-- Helper functions for dealing with buffers
local M = {}
function M.Close(buf)
-- check if mini is installed.
local hasmini, mini = pcall(require,"mini.bufremove")
if hasmini then
local bd = mini.delete
if vim.bo[buf].modified then
local bufname = vim.fn.bufname(buf)
local choice = vim.fn.confirm(("Save changes to %q?"):format(bufname), "&Yes\n&No\n&Cancel")
if choice == 1 then -- Yes
vim.cmd(("%i,%ibufdo w"):format(buf, buf))
bd(buf)
elseif choice == 2 then -- No
bd(buf, true)
end
else
bd(buf)
end
-- fallback to native nvim api
else
vim.api.nvim_buf_delete(buf, {})
end
end
-- Close current buffer
function M.CloseCurrent()
M.Close(vim.api.nvim_get_current_buf())
end
-- Close all but current buffer
function M.CloseOthers()
for _, i in ipairs(vim.api.nvim_list_bufs()) do
if i ~= vim.api.nvim_get_current_buf() then
M.Close(i)
end
end
end
-- Close all open buffers
function M.CloseAll()
for _, i in ipairs(vim.api.nvim_list_bufs()) do
M.Close(i)
end
end
function M.GetLoaded()
local loaded = {}
for i, hnd in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_loaded(hnd) then
loaded[i] = hnd
end
end
return loaded
end
return M

View file

@ -0,0 +1,44 @@
local hasluansp, luasnip = pcall(require, "luasnip")
local cmp = require("cmp")
local M = {}
function M.selectNext(opts)
return cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_next_item(opts or {})
elseif hasluansp and luasnip.locally_jumpable(1) then
luasnip.jump(1)
else
fallback()
end
end, { "i", "s" })
end
function M.selectPrev(opts)
return cmp.mapping(function(fallback)
if cmp.visible() then
cmp.select_prev_item(opts or {})
elseif hasluansp and luasnip.locally_jumpable(-1) then
luasnip.jump(-1)
else
fallback()
end
end, { "i", "s" })
end
function M.confirm(opts)
return cmp.mapping(function(fallback)
if cmp.visible() then
-- if luasnip.expand_or_jumpable() then
-- luasnip.expand_or_jump()
-- else
cmp.confirm(opts or {})
-- end
else
fallback()
end
end)
end
return M

View file

@ -0,0 +1,72 @@
---@param text string
---@param width number
---@param ellipsis string
---@return string
local function format_fixed_length(text, width, ellipsis)
text = text or ""
if width < 1 then
return text
end
local len = vim.fn.strdisplaywidth(text)
if text and len > width then
text = vim.fn.strcharpart(text, 0, width - vim.fn.strdisplaywidth(ellipsis)) .. ellipsis
elseif width > 0 then
text = text .. string.rep(' ', width - len)
end
return text
end
---@param entry any
---@param kind any
---@param map any
---@return {icon: string, hl_group: string}|string
local function get_icon(entry, kind, map)
if vim.tbl_contains({ 'path' }, entry.source.name) then
local icon, hl_group = require('nvim-web-devicons').get_icon(entry:get_completion_item().label)
if icon then
return {
icon = icon,
hl_group = hl_group
}
end
end
if entry.source.name == "snippet" then
return map["Snippet"] or "Snippet"
end
if entry.source.name == "spell" then
return map["Spell"] or "Spell"
end
return map[kind] or "?"
end
return function (opts)
return function(entry, item)
local icon = get_icon(entry, item.kind, opts.symbol_map)
if type(icon) == "table" then
item.kind = icon.icon
item.kind_hl_group = icon.hl_group
else
item.kind = icon
end
local widths = {
abbr = opts.widths and opts.widths.abbr or 20,
menu = opts.widths and opts.widths.menu or 15,
}
local ellipsis = opts.ellipsis or '...'
for key, width in pairs(widths) do
item[key] = format_fixed_length(item[key], width, ellipsis)
end
return item
end
end

View file

@ -0,0 +1,18 @@
local M = {}
function M.setHardtabs()
-- vim.bo.expandtab = false
-- vim.bo.tabstop = 8
-- vim.bo.shiftwidth = 0
-- vim.bo.softtabstop = 0
vim.cmd([[setlocal noet ts=8 sts=0 sw=0]])
end
---@param width number
function M.setSofttabs(width, isLocal)
isLocal = (isLocal or false) and "local" or ""
vim.cmd(string.format("set%s et ts=%s sts=0 sw=%s", isLocal, width, width))
end
return M

View file

@ -0,0 +1,32 @@
local M = {}
function M.document_highlight(bufnr)
local group = vim.api.nvim_create_augroup('lsp_document_highlight', { clear = false })
vim.api.nvim_clear_autocmds({
buffer = bufnr,
group = group,
})
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
group = group,
buffer = bufnr,
callback = vim.lsp.buf.document_highlight,
})
vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, {
group = group,
buffer = bufnr,
callback = vim.lsp.buf.clear_references,
})
end
function M.signature_help_on_hover(bufnr)
local group = vim.api.nvim_create_augroup('lsp_hover', { clear = false })
vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, {
group = group,
buffer = bufnr,
callback = vim.lsp.buf.hover,
})
end
return M

View file

@ -0,0 +1,15 @@
local M = {}
M.highlight_yank = function(opts)
vim.api.nvim_create_autocmd("TextYankPost", {
group = vim.api.nvim_create_augroup("highlight_yank", {}),
desc = "Hightlight selection on yank",
pattern = "*",
callback = function()
vim.highlight.on_yank(opts)
end,
})
end
return M

View file

@ -0,0 +1,11 @@
local M = {}
function M.delimiter()
if vim.fn.has('win32') then
return '\\'
end
return '/'
end
return M

View file

@ -0,0 +1,99 @@
local M = {}
function M.all_files() require("telescope.builtin").find_files({no_ignore=true, prompt_title = "Find All Files"}) end
function M.buffer_view(opts)
local devicons = require("nvim-web-devicons")
local entry_display = require("telescope.pickers.entry_display")
local filter = vim.tbl_filter
local map = vim.tbl_map
local defaults = {
indicators = {
modified = {
icon = "+",
higroup = "TelescopeIndicatorModified",
},
readonly = {
icon = "=",
higroup = "TelescopeIndicatorReadonly",
},
hidden = {
icon = "h",
higroup = "TelescopeIndicatorHidden",
}
}
}
opts = vim.tbl_deep_extend('force', defaults, opts)
local default_icons, _ = devicons.get_icon("file", "", {default = true})
-- local bufnrs = filter(function(b)
-- return 1 == vim.fn.buflisted(b)
-- end, vim.api.nvim_list_bufs())
--
-- local max_bufnr = math.max(unpack(bufnrs))
-- local bufnr_width = #tostring(max_bufnr)
local displayer = entry_display.create {
separator = "",
items = {
-- { width = bufnr_width },
{ width = 2 },
{ width = 2 },
-- { width = 2 },
{ width = vim.fn.strwidth(default_icons) + 1 },
{ remaining = true },
},
}
local make_display = function(entry)
return displayer {
-- {entry.bufnr, "TelescopeResultsNumber"},
{entry.indicator.readonly.icon, entry.indicator.readonly.highlight},
{entry.indicator.changed.icon, entry.indicator.changed.highlight},
-- {entry.indicator.hidden.icon, entry.indicator.hidden.highlight},
{entry.file_icon, entry.file_highlight},
entry.file_name,
}
end
return function(entry)
local bufname = entry.info.name ~= "" and entry.info.name or "[No Name]"
local file_name = vim.fn.fnamemodify(bufname, ":.")
local file_icon, file_highlight = devicons.get_icon(bufname, string.match(bufname, "%a+$"), { default = true })
return {
valid = true,
value = bufname,
ordinal = entry.bufnr .. " : " .. bufname,
display = make_display,
bufnr = entry.bufnr,
lnum = entry.info.lnum ~= 0 and entry.info.lnum or 1,
indicator = {
-- hidden = {
-- icon = entry.info.hidden == 1 and opts.indicators.hidden.icon or " ",
-- highlight = opts.indicators.hidden.higroup
-- },
changed = {
icon = entry.info.changed == 1 and opts.indicators.modified.icon or " ",
highlight = opts.indicators.modified.higroup
},
readonly = {
icon = vim.api.nvim_buf_get_option(entry.bufnr, "readonly") and opts.indicators.readonly.icon or " ",
highlight = opts.indicators.readonly.higroup
}
},
file_icon = file_icon,
file_highlight = file_highlight,
file_name = file_name,
}
end
end
return M