Skip to content

Commit

Permalink
draft: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Apr 23, 2024
1 parent ac72bb5 commit e79dc33
Show file tree
Hide file tree
Showing 15 changed files with 699 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
result
.pre-commit-config.yaml
.direnv
.luarc.json
103 changes: 79 additions & 24 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 21 additions & 68 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@
url = "github:cachix/pre-commit-hooks.nix";
};

neorocks = {
url = "github:nvim-neorocks/neorocks";
};
neorocks.url = "github:nvim-neorocks/neorocks";

neodev-nvim = {
url = "github:folke/neodev.nvim";
flake = false;
};
gen-luarc.url = "github:mrcjkb/nix-gen-luarc-json";
};

outputs = inputs @ {
Expand All @@ -26,7 +21,7 @@
flake-parts,
pre-commit-hooks,
neorocks,
neodev-nvim,
gen-luarc,
...
}: let
name = "lz.n";
Expand All @@ -49,77 +44,33 @@
...
}: let
ci-overlay = import ./nix/ci-overlay.nix {
inherit
self
neodev-nvim
;
inherit self;
plugin-name = name;
};

pkgs = import nixpkgs {
inherit system;
overlays = [
ci-overlay
gen-luarc.overlays.default
neorocks.overlays.default
ci-overlay
plugin-overlay
];
};

mkTypeCheck = {
nvim-api ? [],
disabled-diagnostics ? [],
}:
pre-commit-hooks.lib.${system}.run {
src = self;
hooks = {
lua-ls.enable = true;
};
settings = {
lua-ls = {
config = {
runtime.version = "LuaJIT";
Lua = {
workspace = {
library =
nvim-api
++ [
"\${3rd}/busted/library"
"\${3rd}/luassert/library"
];
ignoreDir = [
".git"
".github"
".direnv"
"result"
"nix"
"doc"
];
};
diagnostics = {
libraryFiles = "Disable";
disable = disabled-diagnostics;
};
};
};
};
};
};

type-check-stable = mkTypeCheck {
nvim-api = [
"${pkgs.neovim}/share/nvim/runtime/lua"
"${pkgs.neodev-plugin}/types/stable"
];
disabled-diagnostics = [
# For compatibility with nightly, some diagnostics may have to be disabled here.
];
luarc = pkgs.mk-luarc {
nvim = pkgs.neovim-nightly;
neodev-types = "nightly";
};

type-check-nightly = mkTypeCheck {
nvim-api = [
"${pkgs.neovim-nightly}/share/nvim/runtime/lua"
"${pkgs.neodev-plugin}/types/nightly"
];
type-check-nightly = pre-commit-hooks.lib.${system}.run {
src = self;
hooks = {
lua-ls.enable = true;
};
settings = {
lua-ls.config = luarc;
};
};

pre-commit-check = pre-commit-hooks.lib.${system}.run {
Expand All @@ -135,7 +86,10 @@

devShell = pkgs.nvim-nightly-tests.overrideAttrs (oa: {
name = "lz.n devShell";
inherit (pre-commit-check) shellHook;
shellHook = ''
${pre-commit-check.shellHook}
ln -fs ${pkgs.luarc-to-json luarc} .luarc.json
'';
buildInputs = with pre-commit-hooks.packages.${system};
[
alejandra
Expand All @@ -162,7 +116,6 @@
checks = {
inherit
pre-commit-check
type-check-stable
type-check-nightly
;
inherit
Expand Down
75 changes: 75 additions & 0 deletions lua/lz/n/handler/cmd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local loader = require('lz.n.loader')

---@class LzCmdHandler: LzHandler
local M = {
---@type table<string, LzPlugin>
active = {},
managed = {},
type = 'cmd',
}

---@param cmd string
local function load(cmd)
vim.api.nvim_del_user_command(cmd)
loader.load(M.active[cmd])
end

---@param cmd string
function M.add(cmd)
vim.api.nvim_create_user_command(cmd, function(event)
---@cast event vim.api.keyset.user_command
local command = {
cmd = cmd,
bang = event.bang or nil,
---@diagnostic disable-next-line: undefined-field
mods = event.smods,
---@diagnostic disable-next-line: undefined-field
args = event.fargs,
count = event.count >= 0 and event.range == 0 and event.count or nil,
}

if event.range == 1 then
---@diagnostic disable-next-line: undefined-field
command.range = { event.line1 }
elseif event.range == 2 then
---@diagnostic disable-next-line: undefined-field
command.range = { event.line1, event.line2 }
end

---@type string
local plugins = '`' .. table.concat(vim.tbl_values(M.active[cmd]), ', ') .. '`'

load(cmd)

local info = vim.api.nvim_get_commands({})[cmd] or vim.api.nvim_buf_get_commands(0, {})[cmd]
if not info then
vim.schedule(function()
vim.notify('Command `' .. cmd .. '` not found after loading ' .. plugins, vim.log.levels.ERROR)
end)
return
end

command.nargs = info.nargs
---@diagnostic disable-next-line: undefined-field
if event.args and event.args ~= '' and info.nargs and info.nargs:find('[1?]') then
---@diagnostic disable-next-line: undefined-field
command.args = { event.args }
end
vim.cmd(command)
end, {
bang = true,
range = true,
nargs = '*',
complete = function(_, line)
load(cmd)
return vim.fn.getcompletion(line, 'cmdline')
end,
})
end

---@param cmd string
function M.del(cmd)
pcall(vim.api.nvim_del_user_command, cmd)
end

return M
Loading

0 comments on commit e79dc33

Please sign in to comment.