Skip to content

Commit

Permalink
refactor: U.config
Browse files Browse the repository at this point in the history
  • Loading branch information
ueaner committed Jun 20, 2024
1 parent 6a023a9 commit aa0eac4
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 87 deletions.
2 changes: 1 addition & 1 deletion init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require("util.debug").setup()

require("config.options")
require("config.lazy")
require("config").setup()

require("tokyonight").load()
-- vim.cmd.colorscheme("catppuccin")
Expand Down
4 changes: 2 additions & 2 deletions lua/config/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ vim.api.nvim_create_autocmd("BufReadPost", {
-- close some filetypes with <q>
vim.api.nvim_create_autocmd("FileType", {
group = augroup("close_with_q"),
pattern = require("config").close_with_q,
pattern = U.config.close_with_q,
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.keymap.set("n", "q", "<cmd>close<cr>", { buffer = event.buf, silent = true })
Expand All @@ -65,7 +65,7 @@ vim.api.nvim_create_autocmd("FileType", {
-- close some filetypes (float window) with <esc>
vim.api.nvim_create_autocmd("FileType", {
group = augroup("close_with_esc"),
pattern = require("config").close_with_esc,
pattern = U.config.close_with_esc,
callback = function(event)
vim.bo[event.buf].buflisted = false
vim.keymap.set("n", "<esc>", "<cmd>close<cr>", { buffer = event.buf, silent = true })
Expand Down
9 changes: 2 additions & 7 deletions lua/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
-- https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/init.lua

_G.LazyUtil = require("lazy.core.util")
---@type util
_G.U = require("util")

---@class Config
local M = {}
U.config = M

M.excluded_filetypes = {
"",
Expand Down Expand Up @@ -65,6 +60,7 @@ M.icons = {
debugging = "",
},
-- https://microsoft.github.io/vscode-codicons/dist/codicon.html
-- https://microsoft.github.io/vscode-codicons/dist/codicon.csv
dap = {
Stopped = { "󰁕 ", "DiagnosticWarn", "DapStoppedLine" },
Breakpoint = " ",
Expand Down Expand Up @@ -177,8 +173,7 @@ function M.get_kind_filter(buf)
return type(M.kind_filter) == "table" and type(M.kind_filter.default) == "table" and M.kind_filter.default or nil
end

-- keys requires _G.U variable
M.keys = require("config.keymaps.keys")

function M.setup() end

return M
2 changes: 1 addition & 1 deletion lua/config/keymaps/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ local keys = {
{
"<S-Tab>",
function()
return vim.snippet.active({ direction = -1 }) and "<cmd>lua vim.snippet.jump(-1)<cr>" or "<Tab>"
return vim.snippet.active({ direction = -1 }) and "<cmd>lua vim.snippet.jump(-1)<cr>" or "<S-Tab>"
end,
expr = true,
silent = true,
Expand Down
50 changes: 10 additions & 40 deletions lua/config/lazy.lua
Original file line number Diff line number Diff line change
@@ -1,48 +1,18 @@
-- https://github.com/LazyVim/starter/blob/main/lua/config/lazy.lua
local lazier = require("util.lazier")

-- lazy.nvim v10.21.0
-- Triggered after LazyPlugins User Event
vim.api.nvim_create_autocmd("User", {
pattern = "LazyPlugins",
callback = function()
local keymaps = require("config").keys
for name, _ in pairs(require("lazy.core.config").spec.plugins) do
local keys = keymaps[name]
if keys then
-- keys for specific filetypes
if keys.ft then
local ft = keys.ft
keys.ft = nil
if type(ft) == "function" then
ft = ft()
end
for i, mapping in ipairs(keys) do
keys[i].ft = ft
end
end
---@type util
_G.U = require("util")
U.config = require("config")

require("lazy.core.config").spec.plugins[name].keys = keymaps[name]
end
end
end,
})
-- keys requires U variable
lazier.lazy_plugin_keymaps(U.config.keys)

-- Install lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable",
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
lazier.install()
_G.LazyUtil = require("lazy.core.util")

require("util.lazier").lazy_notify()
require("util.lazier").lazy_file()
lazier.lazy_notify()
lazier.lazy_file()

-- Configure lazy.nvim
require("lazy").setup({
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/coding/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ return {
item.menu = string.sub(item.menu, 1, 40) .. "..."
end

local icons = require("config").icons.kinds
local icons = U.config.icons.kinds
if icons[item.kind] then
item.kind = icons[item.kind] .. item.kind
end
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/dap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ return {
config = function()
vim.api.nvim_set_hl(0, "DapStoppedLine", { default = true, link = "Visual" })

for name, sign in pairs(require("config").icons.dap) do
for name, sign in pairs(U.config.icons.dap) do
---@cast sign string[]
sign = type(sign) == "table" and sign or { sign }
vim.fn.sign_define(
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/editor/fuzzyfinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ return {
pickers = {
buffers = { sort_mru = true, sort_lastused = true },
colorscheme = { enable_preview = true },
lsp_document_symbols = { symbols = require("config").get_kind_filter() },
lsp_document_symbols = { symbols = U.config.get_kind_filter() },
},
extensions = {
project = {
Expand Down
12 changes: 5 additions & 7 deletions lua/plugins/editor/outline.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local C = require("config")

return {
-- Code outline sidebar powered by Treesitter or LSP.
{
Expand All @@ -9,16 +7,16 @@ return {
opts = function()
vim.api.nvim_set_hl(0, "AerialLine", { bg = U.ui.bg("CursorLine"), fg = U.ui.fg("Title") })

local icons = vim.deepcopy(C.icons.kinds)
local icons = vim.deepcopy(U.config.icons.kinds)

-- HACK: fix lua's weird choice for `Package` for control
-- structures like if/else/for/etc.
icons.lua = { Package = icons.Control }

---@type table<string, string[]>|false
local filter_kind = false
if C.kind_filter then
filter_kind = assert(vim.deepcopy(C.kind_filter))
if U.config.kind_filter then
filter_kind = assert(vim.deepcopy(U.config.kind_filter))
filter_kind._ = filter_kind.default
filter_kind.default = nil
end
Expand Down Expand Up @@ -83,14 +81,14 @@ return {
unfold_all = nil,
},
}
local filter = C.kind_filter
local filter = U.config.kind_filter

if type(filter) == "table" then
filter = filter.default
if type(filter) == "table" then
for kind, symbol in pairs(defaults.symbols) do
opts.symbols[kind] = {
icon = C.icons.kinds[kind] or symbol.icon,
icon = U.config.icons.kinds[kind] or symbol.icon,
hl = symbol.hl,
}
if not vim.tbl_contains(filter, kind) then
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/extras/tools/codeium.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ return {
}
table.insert(opts.sections.lualine_x, 2, {
function()
return require("config").icons.kinds.Codeium
return U.config.icons.kinds.Codeium
end,
cond = function()
return status() ~= nil
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/extras/tools/navic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ return {
separator = " ",
highlight = true,
depth_limit = 5,
icons = require("config").icons.kinds,
icons = U.config.icons.kinds,
lazy_update_context = true,
}
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/formatting.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ return {
timeout_ms = 1000,
async = false, -- not recommended to change
quiet = false, -- not recommended to change
lsp_fallback = true,
lsp_format = true,
},
formatters_by_ft = {
lua = { "stylua" },
Expand Down
12 changes: 5 additions & 7 deletions lua/plugins/lsp/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
-- https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/plugins/lsp/init.lua

local icons = require("config").icons

return {
-- lspconfig
{
Expand Down Expand Up @@ -29,7 +27,7 @@ return {
spacing = 4,
source = "if_many",
prefix = function(diagnostic)
for name, icon in pairs(icons.diagnostics) do
for name, icon in pairs(U.config.icons.diagnostics) do
if diagnostic.severity == vim.diagnostic.severity[name:upper()] then
return icon
end
Expand All @@ -39,10 +37,10 @@ return {
severity_sort = true,
signs = {
text = {
[vim.diagnostic.severity.ERROR] = icons.diagnostics.Error,
[vim.diagnostic.severity.WARN] = icons.diagnostics.Warn,
[vim.diagnostic.severity.HINT] = icons.diagnostics.Hint,
[vim.diagnostic.severity.INFO] = icons.diagnostics.Info,
[vim.diagnostic.severity.ERROR] = U.config.icons.diagnostics.Error,
[vim.diagnostic.severity.WARN] = U.config.icons.diagnostics.Warn,
[vim.diagnostic.severity.HINT] = U.config.icons.diagnostics.Hint,
[vim.diagnostic.severity.INFO] = U.config.icons.diagnostics.Info,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/lsp/keymaps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function M.get()
if M._keys then
return M._keys
end
local keys = require("config").keys
local keys = U.config.keys
M._keys = keys["nvim-lspconfig keys"] and keys["nvim-lspconfig keys"] or {}
return M._keys
end
Expand Down
8 changes: 4 additions & 4 deletions lua/plugins/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ return {
textobjects = {
move = {
enable = true,
goto_next_start = { ["]f"] = "@function.outer", ["]c"] = "@class.outer" },
goto_next_end = { ["]F"] = "@function.outer", ["]C"] = "@class.outer" },
goto_previous_start = { ["[f"] = "@function.outer", ["[c"] = "@class.outer" },
goto_previous_end = { ["[F"] = "@function.outer", ["[C"] = "@class.outer" },
goto_next_start = { ["]f"] = "@function.outer", ["]c"] = "@class.outer", ["]a"] = "@parameter.inner" },
goto_next_end = { ["]F"] = "@function.outer", ["]C"] = "@class.outer", ["]A"] = "@parameter.inner" },
goto_previous_start = { ["[f"] = "@function.outer", ["[c"] = "@class.outer", ["[a"] = "@parameter.inner" },
goto_previous_end = { ["[F"] = "@function.outer", ["[C"] = "@class.outer", ["[A"] = "@parameter.inner" },
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/ui/indent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ return {
indent = { char = "│", tab_char = "│" },
scope = { show_start = false, show_end = false },
exclude = {
filetypes = require("config").excluded_filetypes,
filetypes = U.config.excluded_filetypes,
},
})
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/ui/scrollbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ return {
"lewis6991/satellite.nvim",
event = "LazyFile",
opts = {
excluded_filetypes = require("config").excluded_filetypes,
excluded_filetypes = U.config.excluded_filetypes,
current_only = true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/ui/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ return {
local lualine_require = require("lualine_require")
lualine_require.require = require

local icons = require("config").icons
local icons = U.config.icons

return {
options = {
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/ui/tabline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ return {
diagnostics = "nvim_lsp",
always_show_bufferline = false,
diagnostics_indicator = function(_, _, diag)
local icons = require("config").icons.diagnostics
local icons = U.config.icons.diagnostics
local ret = (diag.error and icons.Error .. diag.error .. " " or "")
.. (diag.warning and icons.Warn .. diag.warning or "")
return vim.trim(ret)
Expand Down
8 changes: 3 additions & 5 deletions lua/util/bufinfo.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
local icons = require("config").icons

local M = setmetatable({}, {
__call = function(m, ...)
return m.info(...)
end,
})

function M.langserver_names()
local info = icons.general.lsp .. " client(s): "
local info = U.config.icons.general.lsp .. " client(s): "
local clients = vim.lsp.get_active_clients()
if next(clients) == nil then
return info .. "<empty>"
Expand All @@ -23,15 +21,15 @@ function M.langserver_names()
end

function M.treesitter_has_parser()
local info = icons.general.treesitter .. " treesitter has parser(s): "
local info = U.config.icons.general.treesitter .. " treesitter has parser(s): "
if package.loaded["nvim-treesitter"] and require("nvim-treesitter.parsers").has_parser() then
return info .. "YES"
end
return info .. "NO"
end

function M.dap_has_adapter()
local info = icons.general.dap .. " dap has adapter(s): "
local info = U.config.icons.general.dap .. " dap has adapter(s): "
if require("dap").configurations[vim.bo.filetype] ~= nil then
return info .. "YES"
end
Expand Down
44 changes: 44 additions & 0 deletions lua/util/lazier.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,48 @@ function M.lazy_notify()
timer:start(500, 0, replay)
end

---@param keymaps { [string]: LazyKeysSpec[] }
function M.lazy_plugin_keymaps(keymaps)
-- lazy.nvim v10.21.0
-- Triggered after LazyPlugins User Event
vim.api.nvim_create_autocmd("User", {
pattern = "LazyPlugins",
callback = function()
for name, _ in pairs(require("lazy.core.config").spec.plugins) do
local keys = keymaps[name]
if keys then
-- keys for specific filetypes
if keys.ft then
local ft = keys.ft
keys.ft = nil
if type(ft) == "function" then
ft = ft()
end
for i, mapping in ipairs(keys) do
keys[i].ft = ft
end
end

require("lazy.core.config").spec.plugins[name].keys = keymaps[name]
end
end
end,
})
end

function M.install()
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
vim.fn.system({
"git",
"clone",
"--filter=blob:none",
"https://github.com/folke/lazy.nvim.git",
"--branch=stable",
lazypath,
})
end
vim.opt.rtp:prepend(lazypath)
end

return M
Loading

0 comments on commit aa0eac4

Please sign in to comment.