Skip to content

Commit

Permalink
feat: package for luarocks
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Nov 21, 2024
1 parent 3ac471b commit 8246a17
Show file tree
Hide file tree
Showing 20 changed files with 206 additions and 58 deletions.
14 changes: 14 additions & 0 deletions .busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
return {
_all = {
coverage = false,
lpath = "lua/?.lua;lua/?/init.lua",
cpath = ".luarocks/lib/lua/5.1/?.so",
lua = "nlua",
},
default = {
verbose = true,
},
tests = {
verbose = true,
},
}
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[Makefile]
indent_style = tab
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
3 changes: 3 additions & 0 deletions .github/workflows/nix-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ name: Test Nix Build

on:
push:
branches:
- main
pull_request:

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dual/
result
.direnv
.devenv
.luarocks
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ version = "0.1.0"
edition = "2021"

[lib]
path = "lua/blink/cmp/fuzzy/lib.rs"
crate-type = ["cdylib"]

[features]
luajit = ["mlua/luajit"]
lua51 = ["mlua/lua51"]

[dependencies]
regex = "1.10.5"
lazy_static = "1.5.0"
frizbee = { git = "https://github.com/saghen/frizbee" }
serde = { version = "1.0.204", features = ["derive"] }
heed = "0.20.3"
mlua = { version = "0.10.0", features = ["module", "luajit"] }
mlua = { version = "0.10.0", features = ["module"] }
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
./test.sh
46 changes: 46 additions & 0 deletions blink.cmp-scm-1.rockspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local MODREV, SPECREV = "scm", "-1"
rockspec_format = "3.0"
package = "blink.cmp"
version = MODREV .. SPECREV

description = {
summary = "Performant, batteries-included completion plugin for Neovim",
labels = { "neovim" },
homepage = "https://github.com/Saghen/blink.cmp",
license = "MIT",
}

source = {
url = "http://github.com/Saghen/blink-cmp/archive/v" .. MODREV .. ".zip",
}

if MODREV == "scm" then
source = {
url = "git://github.com/Saghen/blink-cmp",
}
end

dependencies = {
"lua == 5.1",
}

test_dependencies = {
"nlua",
}

build_dependencies = {
"luarocks-build-rust-mlua",
}

build = {
type = "rust-mlua",
modules = {
"blink_cmp_fuzzy",
},
install = {
lua = {
["blink-cmp.init"] = "lua/blink-cmp.lua",
},
},
features = { "lua51" }
}
6 changes: 3 additions & 3 deletions flake.lock

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

136 changes: 84 additions & 52 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,24 @@
fenix.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = inputs@{ flake-parts, nixpkgs, ... }:
outputs = inputs@{ self, flake-parts, nixpkgs, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems =
[ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];

perSystem = { self, config, self', inputs', pkgs, system, lib, ... }: {
perSystem = { config, self', pkgs, system, lib, ... }: {
# use fenix overlay
_module.args.pkgs = import nixpkgs {
inherit system;
overlays = [ inputs.fenix.overlays.default ];
overlays = [
inputs.fenix.overlays.default
self.overlays.default
];
};

# define the packages provided by this flake
packages = let
inherit (inputs.fenix.packages.${system}.minimal) toolchain;
inherit (pkgs.stdenv) hostPlatform;

rustPlatform = pkgs.makeRustPlatform {
cargo = toolchain;
rustc = toolchain;
};

src = ./.;
version = "2024-11-11";

blink-fuzzy-lib = rustPlatform.buildRustPackage {
pname = "blink-fuzzy-lib";
inherit src version;
cargoLock = {
lockFile = ./Cargo.lock;
outputHashes = {
"frizbee-0.1.0" =
"sha256-pt6sMsRyjXrbrTK7t/YvWeen/n3nU8UUaiNYTY1LczE=";
};
};
};

libExt = if hostPlatform.isDarwin then
"dylib"
else if hostPlatform.isWindows then
"dll"
else
"so";
in {
blink-cmp = pkgs.vimUtils.buildVimPlugin {
pname = "blink-cmp";
inherit src version;
preInstall = ''
mkdir -p target/release
ln -s ${blink-fuzzy-lib}/lib/libblink_cmp_fuzzy.${libExt} target/release/libblink_cmp_fuzzy.${libExt}
'';

meta = {
description =
"Performant, batteries-included completion plugin for Neovim ";
homepage = "https://github.com/saghen/blink.cmp";
license = lib.licenses.mit;
maintainers = with lib.maintainers; [ redxtech ];
};
};

packages = {
blink-cmp = pkgs.vimPlugins.blink-cmp;
default = self'.packages.blink-cmp;
};

Expand All @@ -92,7 +49,82 @@
# define the default dev environment
devShells.default = pkgs.mkShell {
name = "blink";
packages = with pkgs; [ fenix.minimal.toolchain ];
packages = with pkgs; [
fenix.minimal.toolchain
rust-analyzer
rustfmt
clippy
lua-language-server
luarocks
(lua5_1.withPackages (ps: with ps; [nlua busted]))
];
};
};
flake = {
overlays.default = final: prev: let
lib = final.lib;
inherit (inputs.fenix.packages.${final.system}.minimal) toolchain cargo rustc;

rustPlatform = final.makeRustPlatform {
cargo = toolchain;
rustc = toolchain;
};
luaPackage-override = luafinal: luaprev: {
blink-cmp = luafinal.callPackage ({
buildLuarocksPackage,
fetchzip,
fetchurl,
lua,
luaOlder,
luarocks-build-rust-mlua,
busted,
nlua,
}:
buildLuarocksPackage {
pname = "blink.cmp";
version = "scm-1";
knownRockspec = "${self}/blink.cmp-scm-1.rockspec";
src = self;
disabled = luaOlder "5.1";
cargoDeps = final.rustPlatform.importCargoLock {
lockFile = self + "/Cargo.lock";
outputHashes = {
"frizbee-0.1.0" = "sha256-pt6sMsRyjXrbrTK7t/YvWeen/n3nU8UUaiNYTY1LczE=";
};
};
NIX_LDFLAGS = lib.optionalString final.stdenv.hostPlatform.isDarwin
(if lua.pkgs.isLuaJIT then "-lluajit-${lua.luaversion}" else "-llua");
buildInputs = [
cargo
rustc
rustPlatform.cargoSetupHook
luarocks-build-rust-mlua
];
propagatedBuildInputs = [
# HACK: These packages shouldn't be propagated, but without this,
# luarocks make fails in the preCheck (needs to be fixed in nixpkgs).
luarocks-build-rust-mlua
busted
nlua
];
doCheck = true;
preCheck = ''
export HOME="$(mktemp -d)"
mkdir -p .luarocks
luarocks $LUAROCKS_EXTRA_ARGS make --tree=.luarocks --deps-mode=all
'';
}) {};
};
in {
lua5_1 = prev.lua5_1.override {
packageOverrides = luaPackage-override;
};
lua51Packages = prev.lua51Packages // final.lua5_1.pkgs;
vimPlugins = prev.vimPlugins // {
blink-cmp = final.neovimUtils.buildNeovimPlugin {
luaAttr = final.lua51Packages.blink-cmp;
};
};
};
};
};
Expand Down
5 changes: 4 additions & 1 deletion lua/blink/cmp/fuzzy/download.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ local version_path = root_dir .. '../../../../target/release/version.txt'
function download.ensure_downloaded(callback)
callback = vim.schedule_wrap(callback)

if not download_config.download then return callback() end
if not download_config.download or pcall(require, 'blink_cmp_fuzzy') then
-- download disabled or already loaded (built with luarocks)
return callback()
end

download.get_git_tag(function(git_version_err, git_version)
if git_version_err then return callback(git_version_err) end
Expand Down
8 changes: 8 additions & 0 deletions lua/blink/cmp/fuzzy/rust.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
local ok, rust = pcall(require, 'blink_cmp_fuzzy')

if ok then
return rust
end

-- Set up the path do use the downloaded binary

--- @return string
local function get_lib_extension()
if jit.os:lower() == 'mac' or jit.os:lower() == 'osx' then return '.dylib' end
Expand Down
5 changes: 5 additions & 0 deletions spec/blink_cmp_fuzzy_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("rust-mlua library", function()
it("can load blink_cmp_fuzzy module", function()
require("blink_cmp_fuzzy")
end)
end)
17 changes: 17 additions & 0 deletions spec/fuzzy_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local tempdir = vim.fn.tempname()
vim.system({ "rm", "-r", tempdir }):wait()
vim.system({ "mkdir", "-p", tempdir }):wait()

describe("blink.cmp.fuzzy", function()
local fuzzy = require("blink.cmp.fuzzy")
it("init_db", function()
local db_path = vim.fs.joinpath(tempdir, "fuzzy.db")
fuzzy.init_db(db_path)
assert.is_not_nil(vim.uv.fs_stat(db_path))
end)
it("get_words", function()
local result = fuzzy.get_words("first\nsecond\tthird")
table.sort(result) -- make sure it's not flaky
assert.same({"first", "second", "third"}, result)
end)
end)
5 changes: 5 additions & 0 deletions spec/setup_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("setup", function()
it("can setup", function()
require("blink.cmp").setup {};
end)
end)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

bash -c "mkdir -p .luarocks"
luarocks make --tree=.luarocks --deps-mode=all
eval "$(luarocks --tree=.luarocks path --append)"
luarocks test

0 comments on commit 8246a17

Please sign in to comment.