Skip to content

Commit

Permalink
Improvements for the nixCats lua plugin (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: birdee <birdee@birdee.com>
  • Loading branch information
BirdeeHub and birdee authored Nov 13, 2024
1 parent d6dbf1c commit 0d9b264
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 125 deletions.
2 changes: 2 additions & 0 deletions builder/builder_error.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) 2023 BirdeeHub
# Licensed under the MIT license
builtins.throw ''
The following arguments are accepted:
Expand Down
21 changes: 10 additions & 11 deletions builder/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ in
# see :help nixCats
# this function gets passed all the way into the wrapper so that we can also add
# other dependencies that get resolved later in the process such as treesitter grammars.
nixCats = allPluginDeps:
pkgs.stdenv.mkDerivation (let
nixCats = allPluginDeps: pkgs.stdenv.mkDerivation (let
mkLuaFileWithMeta = pkgs.callPackage ncTools.mkLuaFileWithMeta {};
isUnwrappedCfgPath = settings.wrapRc == false && settings.unwrappedCfgPath != null && builtins.isString settings.unwrappedCfgPath;
isStdCfgPath = settings.wrapRc == false && ! isUnwrappedCfgPath;

Expand All @@ -114,10 +114,10 @@ in
};
all_def_names = ncTools.getCatSpace (builtins.attrValues final_cat_defs_set);

cats = pkgs.writeText "cats.lua" ''return ${ncTools.toLua categoriesPlus}'';
settingsTable = pkgs.writeText "settings.lua" ''return ${ncTools.toLua settingsPlus}'';
petShop = pkgs.writeText "petShop.lua" ''return ${ncTools.toLua all_def_names}'';
depsTable = pkgs.writeText "pawsible.lua" ''return ${ncTools.toLua allPluginDeps}'';
cats = mkLuaFileWithMeta "cats" categoriesPlus;
settingsTable = mkLuaFileWithMeta "settings" settingsPlus;
petShop = mkLuaFileWithMeta "petShop" all_def_names;
depsTable = mkLuaFileWithMeta "pawsible" allPluginDeps;
in {
name = "nixCats";
builder = pkgs.writeText "builder.sh" /*bash*/ ''
Expand Down Expand Up @@ -145,12 +145,11 @@ in
(pkgs.lib.unique (filterAndFlatten optionalLuaAdditions));
in/*lua*/''
${optLuaPre}
vim.g.configdir = require('nixCats').cats.nixCats_config_location
if vim.fn.filereadable(vim.g.configdir .. "/init.vim") == 1 then
vim.cmd.source(vim.g.configdir .. "/init.vim")
if vim.fn.filereadable(require('nixCats').configDir .. "/init.vim") == 1 then
vim.cmd.source(require('nixCats').configDir .. "/init.vim")
end
if vim.fn.filereadable(vim.g.configdir .. "/init.lua") == 1 then
dofile(vim.g.configdir .. "/init.lua")
if vim.fn.filereadable(require('nixCats').configDir .. "/init.lua") == 1 then
dofile(require('nixCats').configDir .. "/init.lua")
end
${optLuaAdditions}
'';
Expand Down
20 changes: 20 additions & 0 deletions builder/ncTools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@
# Licensed under the MIT license
{ lib, ... }: with builtins; rec {
# NIX CATS INTERNAL UTILS:

mkLuaFileWithMeta = { writeText }: modname: table: writeText "${modname}.lua" /*lua*/''
local ${modname} = ${toLua table};
return setmetatable(${modname}, {
__call = function(self, attrpath)
local strtable = {}
if type(attrpath) == "table" then
strtable = attrpath
elseif type(attrpath) == "string" then
for key in attrpath:gmatch("([^%.]+)") do
table.insert(strtable, key)
end
else
print("function requires a table of strings or a dot separated string")
return
end
return vim.tbl_get(${modname}, unpack(strtable));
end
})
'';

mkLuaInline = expr: { __type = "nix-to-lua-inline"; inherit expr; };

Expand Down
52 changes: 25 additions & 27 deletions builder/nixCats.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- Copyright (c) 2023 BirdeeHub
-- Licensed under the MIT license
---@type nixCats
---@type nixCats.main
local M = {}
M.cats = require('nixCats.cats')
M.pawsible = require('nixCats.pawsible')
Expand All @@ -13,48 +13,38 @@ M.nixCatsPath = require('nixCats.saveTheCats')
M.vimPackDir = vim.g[ [[nixCats-special-rtp-entry-vimPackDir]] ]
M.packageBinPath = os.getenv('NVIM_WRAPPER_PATH_NIX') or vim.v.progpath

---:h nixCats
---will return the nearest parent category value, unless the nearest
---parent is a table, in which case that means a different subcategory
---was enabled but this one was not. In that case it returns nil.
---@param category string|string[]
---@return any
function M.get(category)
local strtable
local strtable = {}
if type(category) == "table" then
strtable = category
elseif type(category) == "string" then
local keys = {}
for key in category:gmatch("([^%.]+)") do
table.insert(keys, key)
table.insert(strtable, key)
end
strtable = keys
else
print("get function requires a table of strings or a dot separated string")
return
end
---@type any
local cats = require('nixCats.cats')
local cats = M.cats
for _, key in ipairs(strtable) do
if type(cats) == "table" then
cats = cats[key]
else
return cats
end
end

return cats
end

function M.addGlobals()

---:h nixCats
---will return the nearest parent category value, unless the nearest
---This function will return the nearest parent category value, unless the nearest
---parent is a table, in which case that means a different subcategory
---was enabled but this one was not. In that case it returns nil.
---@type fun(category: string|string[]): any
function _G.nixCats(category)
return M.get(category)
end
---@type nixCats
_G.nixCats = M

local attributes = {
"cats",
Expand All @@ -69,22 +59,22 @@ function M.addGlobals()
-- command with debug info for nixCats setups
vim.api.nvim_create_user_command('NixCats', function(opts)
if #opts.fargs == 0 then
print(vim.inspect(require('nixCats.cats')))
print(vim.inspect(M.cats))
return
elseif #opts.fargs == 1 then
if vim.list_contains(attributes, opts.fargs[1]) then
print(vim.inspect(M[opts.fargs[1]]))
return
end
elseif #opts.fargs == 2 then
if opts.fargs[1] == 'cat' then
print(vim.inspect(nixCats(opts.fargs[2])))
if opts.fargs[1] == 'cat' or opts.fargs[1] == 'get' then
print(vim.inspect(M.get(opts.fargs[2])))
return
end
elseif #opts.fargs > 2 then
local first = table.remove(opts.fargs, 1)
if first == 'cat' then
print(vim.inspect(nixCats(opts.fargs)))
if first == 'cat' or first == 'get' then
print(vim.inspect(M.get(opts.fargs)))
return
end
end
Expand All @@ -101,7 +91,7 @@ function M.addGlobals()
for _ in cmdLineBeforeCursor:gmatch("([%s]+)") do
numSpaces = numSpaces + 1
end
local candidates = vim.list_extend({ "cat" }, attributes)
local candidates = vim.list_extend({ "cat", "get" }, attributes)
local matches = {}

if not (#argsTyped > 1) then
Expand All @@ -110,7 +100,7 @@ function M.addGlobals()
table.insert(matches, candidate)
end
end
elseif argsTyped[2] == 'cat' then
elseif argsTyped[2] == 'cat' or argsTyped[2] == 'get' then
table.remove(argsTyped, 1)
table.remove(argsTyped, 1)
local argsSoFar = {}
Expand All @@ -126,7 +116,8 @@ function M.addGlobals()
end
-- Walk table till end of argsSoFar,
-- and offer matching completion options
local cats = require('nixCats.cats')
---@type any
local cats = M.cats
for index, key in pairs(argsSoFar) do
if index == #argsSoFar then
for name, value in pairs(cats) do
Expand Down Expand Up @@ -185,4 +176,11 @@ function M.addGlobals()
]])
end

return M
M.addGlobals()

---@type nixCats
return setmetatable(M, {
__call = function(_, cat)
return M.get(cat)
end
})
32 changes: 24 additions & 8 deletions builder/nixCatsMeta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@
---@meta
error("Cannot import a meta module")

---@class nixCats
---@field cats table
---@field pawsible table
---@field settings table
---@field petShop table
---@class nixCats.main
---See :h nixCats.flake.outputs.packageDefinitions
---Function form will return vim.tml_get for the attrpath
---@field cats table|fun(attrpath: string|string[]): any
---See :h nixCats.flake.outputs.settings
---Function form will return vim.tml_get for the attrpath
---@field settings table|fun(attrpath: string|string[]): any
---Contains the final set of plugins added for this package
---Function form will return vim.tml_get for the attrpath
---@field pawsible table|fun(attrpath: string|string[]): any
---Contains all the defined categories that you COULD enable from nix
---Function form will return vim.tml_get for the attrpath
---@field petShop table|fun(attrpath: string|string[]): any
---@field nixCatsPath string
---@field vimPackDir string
---@field configDir string
---@field packageBinPath string
---internal: this creates the nixCats global commands
---@field addGlobals fun()
---internal: use the global alias, nixCats('path.to.value')
---for full compatibility with luaUtils template
---@field get fun(category: string|string[]): any --
---:h nixCats
---This function will return the nearest parent category value, unless the nearest
---parent is a table, in which case that means a different subcategory
---was enabled but this one was not. In that case it returns nil.
---@field get fun(category: string|string[]): any

---:h nixCats
---This function will return the nearest parent category value, unless the nearest
---parent is a table, in which case that means a different subcategory
---was enabled but this one was not. In that case it returns nil.
---@alias nixCats nixCats.main | fun(category: string|string[]): any
3 changes: 2 additions & 1 deletion builder/vim-pack-dir.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) 2023 BirdeeHub
# Licensed under the MIT license
# derived from:
# https://github.com/NixOS/nixpkgs/blob/ae5d2af73efa5e25bf9bf43672cd3d8d99c613d0/pkgs/applications/editors/vim/plugins/vim-utils.nix#L136-L207
{ lib, buildEnv, writeTextFile
Expand Down Expand Up @@ -44,7 +46,6 @@
start = builtins.listToAttrs (map mkEntryFromDrv startPlugins);
opt = builtins.listToAttrs (map mkEntryFromDrv opt);
inherit ts_grammar_path;
ts_grammar_plugin = ts_grammar_path;
};
python3Path = if (allPython3Dependencies python3.pkgs == []) then null
else ''${python3link}/pack/${packageName}/start/__python3_dependencies/python3'';
Expand Down
2 changes: 2 additions & 0 deletions builder/wrapNeovim.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) 2023 BirdeeHub
# Licensed under the MIT license
# derived from:
# https://github.com/NixOS/nixpkgs/blob/8564cb1517f118e1e90b8bc9ba052678f1aa4603/pkgs/applications/editors/neovim/utils.nix#L126-L164
{
Expand Down
2 changes: 2 additions & 0 deletions builder/wrapenvs.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) 2023 BirdeeHub
# Licensed under the MIT license
# derived from:
# https://github.com/NixOS/nixpkgs/blob/8564cb1517f118e1e90b8bc9ba052678f1aa4603/pkgs/applications/editors/neovim/utils.nix#L26-L122
{ withPython3 ? true
Expand Down
19 changes: 10 additions & 9 deletions builder/wrapper.nix
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Copyright (c) 2023 BirdeeHub
# Licensed under the MIT license
# Derived from:
# https://github.com/NixOS/nixpkgs/blob/master/pkgs/applications/editors/neovim/wrapper.nix
{ stdenv
Expand Down Expand Up @@ -96,15 +98,14 @@ let
vim.opt.runtimepath:prepend([[${finalPackDir}]])
vim.g[ [[nixCats-special-rtp-entry-vimPackDir]] ] = [[${finalPackDir}]]
vim.g[ [[nixCats-special-rtp-entry-nvimLuaEnv]] ] = [[${luaEnv}]]
require('nixCats').addGlobals()
vim.g.configdir = vim.fn.stdpath('config')
vim.opt.packpath:remove(vim.g.configdir)
vim.opt.runtimepath:remove(vim.g.configdir)
vim.opt.runtimepath:remove(vim.g.configdir .. "/after")
vim.g.configdir = require('nixCats').cats.nixCats_config_location
vim.opt.packpath:prepend(vim.g.configdir)
vim.opt.runtimepath:prepend(vim.g.configdir)
vim.opt.runtimepath:append(vim.g.configdir .. "/after")
local configdir = vim.fn.stdpath('config')
vim.opt.packpath:remove(configdir)
vim.opt.runtimepath:remove(configdir)
vim.opt.runtimepath:remove(configdir .. "/after")
configdir = require('nixCats').configDir
vim.opt.packpath:prepend(configdir)
vim.opt.runtimepath:prepend(configdir)
vim.opt.runtimepath:append(configdir .. "/after")
'';
in [
# vim accepts a limited number of commands so we join them all
Expand Down
43 changes: 30 additions & 13 deletions nixCatsHelp/nixCats_plugin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ Using:
>vim
:lua print(vim.inspect(require('nixCats').cats))
or
:lua print(vim.inspect(nixCats.cats))
or
:NixCats
or
:NixCats cats
Expand Down Expand Up @@ -181,16 +183,16 @@ Use this fact as you wish.
You could use it to pass information like port numbers or paths
Or whatever else you want.

In addition to `require('nixCats').cats`, nixCats also contains other info.
In addition to `nixCats.cats`, nixCats also contains other info.

>lua
require('nixCats').pawsible
-- contains a final list of all the plugins included
nixCats.pawsible
-- contains a final set of all the plugins included
-- in the package and their paths, and the path to treesitter parsers.
-- does not include lsps and runtime dependencies,
-- you can retrieve their paths with vim.fn.exepath if needed.

require('nixCats').settings
nixCats.settings
-- contains the settings set for the package
<

Expand All @@ -216,22 +218,37 @@ lua.
All of the following may be accessed by `require('nixCats').<item>`,
or :NixCats <item>, except the :NixCats user command also
has the `cat` subcommand to preview the value from `nixCats("catname")`,
and doesnt show the ones marked as internal
>lua
---@class nixCats
---@field cats table
---@field pawsible table
---@field settings table
---@field petShop table
---@class nixCats.main
---See :h nixCats.flake.outputs.packageDefinitions
---Function form will return vim.tml_get for the attrpath
---@field cats table|fun(attrpath: string|string[]): any
---See :h nixCats.flake.outputs.settings
---Function form will return vim.tml_get for the attrpath
---@field settings table|fun(attrpath: string|string[]): any
---Contains the final set of plugins added for this package
---Function form will return vim.tml_get for the attrpath
---@field pawsible table|fun(attrpath: string|string[]): any
---Contains all the defined categories that you COULD enable from nix
---Function form will return vim.tml_get for the attrpath
---@field petShop table|fun(attrpath: string|string[]): any
---@field nixCatsPath string
---@field vimPackDir string
---@field configDir string
---@field packageBinPath string
---internal: this creates the nixCats global commands
---@field addGlobals fun()
---internal: use the global alias, nixCats('path.to.value')
---for full compatibility with luaUtils template
---@field get fun(category: string|string[]): any --
---:h nixCats
---This function will return the nearest parent category value, unless the nearest
---parent is a table, in which case that means a different subcategory
---was enabled but this one was not. In that case it returns nil.
---@field get fun(category: string|string[]): any

---:h nixCats
---This function will return the nearest parent category value, unless the nearest
---parent is a table, in which case that means a different subcategory
---was enabled but this one was not. In that case it returns nil.
---@alias nixCats nixCats.main | fun(category: string|string[]): any
<
----------------------------------------------------------------------------------------
vim:tw=78:ts=8:ft=help:norl:
Loading

0 comments on commit 0d9b264

Please sign in to comment.