vim: better structure for utils

This commit is contained in:
Fernando Schauenburg 2024-02-18 02:31:02 +01:00
parent 5f2dd220d9
commit e34671ddaa
15 changed files with 34 additions and 37 deletions

View file

@ -1,6 +1,6 @@
vim.bo.tabstop = 2
local lua = require('fschauen.util').lua
local lua = require('fschauen.util.lua')
local opts = function(desc)
return { desc = desc, buffer = true }

View file

@ -1,6 +1,6 @@
local M = {}
local icons = require('fschauen.icons')
local icons = require('fschauen.util.icons')
-- Show/navigate warning and errors by default.
M.severity = vim.diagnostic.severity.WARN

View file

@ -89,7 +89,7 @@ M.config = function(--[[plugin]]_, --[[opts]]_)
path = '',
},
symbol_map = require('fschauen.icons').kind,
symbol_map = require('fschauen.util.icons').kind,
},
},

View file

@ -6,7 +6,7 @@ M.event = 'LspAttach'
M.opts = {
text = {
done = require('fschauen.icons').ui.Checkmark,
done = require('fschauen.util.icons').ui.Checkmark,
spinner = {
'▱▱▱▱▱▱▱',
'▰▱▱▱▱▱▱',

View file

@ -17,7 +17,7 @@ M.keys = {
M.main = 'ibl'
M.opts = function(--[[plugin]]_, opts)
local icons = require('fschauen.icons')
local icons = require('fschauen.util.icons')
return vim.tbl_deep_extend('force', opts, {
enabled = false,
indent = {

View file

@ -1,6 +1,6 @@
local M = { 'nvim-lualine/lualine.nvim' }
local icons = require('fschauen.icons')
local icons = require('fschauen.util.icons')
local orange = '#d65d0e'
local bright = '#ffffff' -- alternative: '#f9f5d7'

View file

@ -9,7 +9,7 @@ M.keys = {
}
M.opts = function(--[[plugin]]_, opts)
local icons = require('fschauen.icons')
local icons = require('fschauen.util.icons')
return vim.tbl_deep_extend('force', opts, {
disable_hint = true,
signs = {

View file

@ -21,7 +21,7 @@ M.keys = {
M.lazy = false
M.opts = function(--[[plugin]]_, opts)
local icons = require('fschauen.icons')
local icons = require('fschauen.util.icons')
return vim.tbl_deep_extend('force', opts, {
icons = {
ERROR = icons.diagnostics_bold.Error,

View file

@ -10,7 +10,7 @@ M.keys = {
}
M.opts = function(--[[plugin]]_, opts)
local icons = require('fschauen.icons')
local icons = require('fschauen.util.icons')
return vim.tbl_deep_extend('force', opts, {
disable_netrw = true, -- replace netrw with nvim-tree
hijack_cursor = true, -- keep the cursor on begin of the filename

View file

@ -125,7 +125,7 @@ M.keys = {
M.opts = function(--[[plugin]]_, opts)
local actions = require('telescope.actions')
local layout = require('telescope.actions.layout')
local icons = require('fschauen.icons')
local icons = require('fschauen.util.icons')
local trouble = vim.F.npcall(require, 'trouble.providers.telescope') or {}
local mappings = {

View file

@ -12,7 +12,7 @@ M.keys = {
}
M.opts = function(--[[plugin]]_, opts)
local icons = require('fschauen.icons')
local icons = require('fschauen.util.icons')
return vim.tbl_deep_extend('force', opts, {
keywords = {
TODO = { icon = icons.ui.Checkbox },

View file

@ -16,7 +16,7 @@ M.keys = {
M.config = function(--[[plugin]]_, --[[opts]]_)
require('virt-column').setup {
char = require('fschauen.icons').ui.LineMiddle,
char = require('fschauen.util.icons').ui.LineMiddle,
}
end

View file

@ -0,0 +1,9 @@
local M = {}
M.exists = function(path)
local stat = vim.loop.fs_stat(path)
return (stat and stat.type) or false
end
return M

View file

@ -1,17 +1,9 @@
local M = {}
M.file_exists = function(path)
local stat = vim.loop.fs_stat(path)
return stat and stat.type == 'file'
end
local util = require('fschauen.util')
local exists = util.exists
M.edit_file = function(path)
if not pcall(vim.api.nvim_command, string.format('edit %s', path)) then
vim.notify('Could not open ' .. path, vim.log.levels.ERROR)
end
end
local find_lua_module_sources = function(modname)
local find_module_sources = function(modname)
modname = modname:gsub('^%.+', ''):gsub('/', '.')
local base = 'lua/' .. modname:gsub('%.', '/')
local candidates = { base .. '.lua', base .. '/init.lua' }
@ -20,7 +12,7 @@ local find_lua_module_sources = function(modname)
for _, directory in ipairs(vim.opt.runtimepath:get()) do
for _, candidate in ipairs(candidates) do
local path = directory .. '/' .. candidate
if M.file_exists(path) then
if exists(path) then
table.insert(results, path)
end
end
@ -28,47 +20,43 @@ local find_lua_module_sources = function(modname)
return results
end
local lua = {}
lua.execute_lines = function(first, last)
M.execute_lines = function(first, last)
first = first or vim.fn.line('.')
last = last or first
local code = vim.fn.join(vim.fn.getline(first, last), '\n')
loadstring(code)()
end
lua.execute_selection = function()
M.execute_selection = function()
local selection = { vim.fn.line('v'), vim.fn.line('.') }
table.sort(selection)
lua.execute_lines(unpack(selection))
M.execute_lines(unpack(selection))
end
lua.execute_file = function(path)
M.execute_file = function(path)
if path then
vim.cmd.luafile(path)
else
lua.execute_lines(1, vim.fn.line('$'))
M.execute_lines(1, vim.fn.line('$'))
end
end
lua.go_to_module = function(modname)
M.go_to_module = function(modname)
modname = modname or vim.fn.expand('<cfile>')
local sources = find_lua_module_sources(modname)
local sources = find_module_sources(modname)
if #sources == 0 then
vim.notify('Not found: ' .. modname, vim.log.levels.WARN)
elseif #sources == 1 then
M.edit_file(sources[1])
vim.cmd.edit(sources[1])
else
vim.ui.select(sources, { prompt = 'Which one?' }, function(choice)
if choice then
M.edit_file(choice)
vim.cmd.edit(choice)
end
end)
end
end
M.lua = lua
return M