Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Mar 27, 2024
1 parent 880ec88 commit e8810da
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lua/dotvim/.vim-template:*.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---@class dotvim
local M = {}


return M
8 changes: 8 additions & 0 deletions lua/dotvim/core/_types.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---@class dotvim.core.plugin.ExtraPluginOptions
---@field pname? string nix plugin name
---@field gui? "all"|string[] Can be used in which gui environment
---@field actions? dotvim.core.action.ActionOption[]|fun():dotvim.core.action.ActionOption[]

---@class dotvim.core.plugin.PluginOption: dotvim.core.plugin.ExtraPluginOptions,LazyPluginSpec

---@class dotvim.core.plugin.Plugin: dotvim.core.plugin.PluginOption,LazyPlugin
71 changes: 71 additions & 0 deletions lua/dotvim/core/action.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---@class dotvim.core.action
local M = {}

---@class dotvim.core.action.KeySpec: LazyKeysBase
---@field mode? string|string[]

---@alias dotvim.core.action.Condition fun(buf:dora.lib.vim.BufferWrapper):boolean

---@class dotvim.core.action.ActionOption
---@field id string Unique id
---@field title string
---@field callback string|function
---@field icon? string
---@field category? string
---@field description? string Shown description if action hovered
---@field keys? string|(string|dotvim.core.action.KeySpec)[]
---@field plugin? string
---@field condition? dotvim.core.action.Condition

---@class dotvim.core.action.Action: dotvim.core.action.ActionOption
local Action = {}

function Action:execute()
local callback = self.callback
if type(callback) == "string" then
vim.cmd(callback)
else
callback()
end
end

---@return LazyKeysSpec[]
function Action:into_lazy_keys_spec()
local keys = self.keys
if not keys then
return {}
end
local callback = function()
self:execute()
end
if type(keys) == "string" then
return {
{
keys,
callback,
},
}
end
if not vim.tbl_isarray(keys) then
keys = { keys }
end

local res = {}
for _, key in ipairs(keys) do
if type(key) == "string" then
res[#res + 1] = {
key,
callback,
}
else
---@type dotvim.core.action.KeySpec
local k = vim.deepcopy(key)
k[2] = callback
res[#res + 1] = k
end
end

return res
end

return M
19 changes: 19 additions & 0 deletions lua/dotvim/utils/fn.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---@class dotvim.utils.fn
local M = {}

---@generic T
---@param fun fun():T
---@return fun():T
function M.invoke_once(fun)
local res
local invoked = false
return function()
if not invoked then
res = fun()
invoked = true
end
return res
end
end

return M
25 changes: 25 additions & 0 deletions lua/dotvim/utils/fs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---@class dotvim.utils.fs
local M = {}

---@param file string filename
---@return string?
function M.read_file(file)
local fd = io.open(file, "r")
if fd == nil then
return nil
end
---@type string
local data = fd:read("*a")
fd:close()
return data
end

---@param file string filename
---@param contents string
function M.write_file(file, contents)
local fd = assert(io.open(file, "w+"))
fd:write(contents)
fd:close()
end

return M
3 changes: 3 additions & 0 deletions lua/dotvim/utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ M.tbl = require("dotvim.utils.tbl")
---@type dotvim.utils.value
M.value = require("dotvim.utils.value")

---@type dotvim.utils.nix
M.nix = require("dotvim.utils.nix")

return M
40 changes: 40 additions & 0 deletions lua/dotvim/utils/nix.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---@class dotvim.utils.nix
local M = {}

---@type dotvim.utils.fn
local fn = require("dotvim.utils.fn")
---@type dotvim.utils.fs
local fs = require("dotvim.utils.fs")

---@type fun():string[]
local get_plugin_packages = fn.invoke_once(function()
local obj = vim
.system(
{ "nix-store", "--query", "--requisites", "/run/current-system" },
{ text = true }
)
:wait()
if obj.code == 0 then
---@type string[]
local lines = vim.split(obj.stdout or "", "\n", { trimempty = true })
local res = {}
for _, value in ipairs(lines) do
if value:find("vimplugin-") then
res[#res + 1] = value
end
end
return res
else
return {}
end
end)

function M.update_nix_plugin_packages()
local packages = get_plugin_packages()
fs.write_file(
vim.fn.stdpath("data") .. "/nix-plugin-packages",
table.concat(packages, "\n")
)
end

return M
101 changes: 101 additions & 0 deletions lua/dotvim/utils/tbl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,105 @@ function M.merge_array(t1, t2)
return res
end

---Reverse given list-like table in place.
---NOTE: this mutates the given table.
---@generic T
---@param lst T[]
function M.list_reverse(lst)
for i = 1, math.floor(#lst / 2) do
local j = #lst - i + 1
lst[i], lst[j] = lst[j], lst[i]
end
end

local function normalize_search_table(tbl)
if vim.tbl_isarray(tbl) then
local ret = {}
for _, v in ipairs(tbl) do
ret[#ret + 1] = v
end
return ret
else
return tbl
end
end

---@param t table
---@param keys string[]
---@return table
function M.filter_out_keys(t, keys)
local res = {}
local _keys = normalize_search_table(keys)
for k, v in pairs(t) do
if _keys[k] ~= nil then
res[k] = v
end
end
return res
end

---@param tbl any
---@return any[]
function M.flatten_array(tbl)
if type(tbl) ~= "table" then
return { tbl }
end

if vim.tbl_isarray(tbl) then
local res = {}
for _, value in ipairs(tbl) do
local inner_value = M.flatten_array(value)
res = vim.list_extend(res, inner_value)
end
return res
else
return { tbl }
end
end

---@generic T
---@param tbl T[]
---@param fn fun(T, T):T
---@param acc T
function M.foldl(tbl, fn, acc)
for _, v in ipairs(tbl) do
acc = fn(acc, v)
end
return acc
end

---@generic T
---@param tbl T[]
---@param fn fun(T, T):T
---@param acc T
function M.foldr(tbl, fn, acc)
for i = #tbl, 1, -1 do
acc = fn(tbl[i], acc)
end
return acc
end

---@param tbl table
---@param value any
---@param ... string
function M.tbl_set(tbl, value, ...)
local len = select("#", ...)
local keys = { ... }
local now = tbl
for i, key in ipairs(keys) do
if i == len then
now[key] = value
else
if now[key] == nil then
now[key] = {}
end
now = now[key]
if type(now) ~= "table" then
error("tbl_set: key " .. key .. " is not a table")
end
end
end
return tbl
end

return M

0 comments on commit e8810da

Please sign in to comment.