Skip to content

Commit

Permalink
feat(commands): fuzzy completions (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Dec 6, 2023
1 parent 86a8d80 commit ffb7f20
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 51 deletions.
1 change: 1 addition & 0 deletions .github/workflows/luarocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
toml-edit
toml
nui.nvim
fzy
labels: |
neovim
summary: "Neovim plugin management inspired by `Cargo`."
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Arguments:

> [!NOTE]
>
> - The command provides completions for rocks and versions on luarocks.org.
> - The command provides fuzzy completions for rocks and versions on luarocks.org.
> - Installs the latest version if `version` is omitted.
> - This plugin keeps track of installed plugins in a `rocks.toml` file,
> which you can commit to version control.
Expand All @@ -110,7 +110,7 @@ that are no longer needed, run the `:Rocks prune [rock]` command.

> [!NOTE]
>
> - The command provides completions for rocks that can safely
> - The command provides fuzzy completions for rocks that can safely
> be pruned without breaking dependencies.
### Editing `rocks.toml`
Expand Down
55 changes: 27 additions & 28 deletions flake.lock

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

3 changes: 2 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
};

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
nixpkgs.url = "github:nixos/nixpkgs";

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

Expand Down Expand Up @@ -79,6 +79,7 @@
"${toml-edit}/lib/lua/5.1/"
"${toml}/lib/lua/5.1/"
"${nui-nvim}/share/lua/5.1"
"${fzy}/share/lua/5.1"
])
++ [
"\${3rd}/busted/library"
Expand Down
8 changes: 3 additions & 5 deletions lua/rocks/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ function commands.create_commands()
nargs = "+",
desc = "Interacts with currently installed rocks",
complete = function(arg_lead, cmdline, _)
local fzy = require("rocks.fzy")
local search = require("rocks.search")

local rocks_commands = vim.tbl_keys(rocks_command_tbl)

local name, version_query = cmdline:match("^Rocks install%s([^%s]+)%s(.+)$")
Expand All @@ -99,11 +101,7 @@ function commands.create_commands()
return rocks_list
end
if cmdline:match("^Rocks%s+%w*$") then
return vim.iter(rocks_commands)
:filter(function(command)
return command:find(arg_lead) ~= nil
end)
:totable()
return fzy.fuzzy_filter_sort(arg_lead, rocks_commands)
end
end,
})
Expand Down
51 changes: 51 additions & 0 deletions lua/rocks/fzy.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---@mod rocks.fzy fzy helpers
---
---@brief [[
---
--- Provides an adapter to fzy
---
---@brief ]]
---
---
-- Copyright (C) 2023 Neorocks Org.
--
-- Version: 0.1.0
-- License: GPLv3
-- Created: 05 Dec 2023
-- Updated: 05 Dec 2023
-- Homepage: https://github.com/nvim-neorocks/rocks.nvim
-- Maintainer: NTBBloodbath <[email protected]>

local fzy_adapter = {}

local fzy = require("fzy")

---@alias index integer
---@alias fzy_position integer
---@alias score number
---@alias FzyResult { [1]: index, [2]: fzy_position[], [3]: score }
---@alias FzyResults FzyResult[]

---Fuzzy-filter a list of items.
---@param query string The query to fuzzy-match.
---@param items string[] The items to search.
---@return string[] Matching items, sorted by score (higher score first).
function fzy_adapter.fuzzy_filter_sort(query, items)
local fzy_results = fzy.filter(query, items) or {}
table.sort(fzy_results, function(a, b)
---@cast a FzyResult
---@cast b FzyResult
local score_a = a[3]
local score_b = b[3]
return score_a > score_b
end)
return vim.iter(fzy_results)
:map(function(fzy_result)
---@cast fzy_result FzyResult
local idx = fzy_result[1]
return items[idx]
end)
:totable()
end

return fzy_adapter
14 changes: 6 additions & 8 deletions lua/rocks/search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
local search = {}

local luarocks = require("rocks.luarocks")
local fzy = require("rocks.fzy")
local nio = require("nio")

---@type Rock[] | nil
Expand All @@ -44,7 +45,7 @@ local populate_cache = nio.create(function()
_cache = nil
return
end
for name, version in result:gmatch("([^%s]+)%s+(%S+)%s+") do
for name, version in result:gmatch("(%S+)%s+(%S+)%s+[^\n]+") do
table.insert(_cache, { name = name, version = version })
end
if #_cache == 0 then
Expand Down Expand Up @@ -89,16 +90,13 @@ search.complete_names = function(query)
if not query then
return {}
end
local matching_rocks = vim.tbl_filter(function(rock)
---@cast rock Rock
return vim.startswith(rock.name, query)
end, _cache)
---@type {[string]: Rock}
---@type { [string]: Rock }
local unique_rocks = {}
for _, rock in pairs(matching_rocks) do
for _, rock in pairs(_cache) do
unique_rocks[rock.name] = rock
end
return vim.tbl_keys(unique_rocks)
local rock_names = vim.tbl_keys(unique_rocks)
return fzy.fuzzy_filter_sort(query, rock_names)
end

return search
7 changes: 2 additions & 5 deletions lua/rocks/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ local state = {}
local _removable_rock_cache = nil

local luarocks = require("rocks.luarocks")
local fzy = require("rocks.fzy")
local nio = require("nio")

---@async
Expand Down Expand Up @@ -165,11 +166,7 @@ state.complete_removable_rocks = function(query)
if not query then
return {}
end
return vim.iter(_removable_rock_cache)
:filter(function(rock_name)
return vim.startswith(rock_name, query)
end)
:totable()
return fzy.fuzzy_filter_sort(query, _removable_rock_cache)
end

state.invalidate_cache = function()
Expand Down
20 changes: 19 additions & 1 deletion nix/plugin-overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
toml,
toml-edit,
nui-nvim,
fzy,
}:
buildLuarocksPackage {
pname = name;
version = "scm-1";
knownRockspec = "${self}/rocks.nvim-scm-1.rockspec";
src = self;
disabled = luaOlder "5.1";
propagatedBuildInputs = [toml toml-edit nui-nvim];
propagatedBuildInputs = [
toml
toml-edit
nui-nvim
fzy
];
}) {};
};
lua5_1 = prev.lua5_1.override {
Expand Down Expand Up @@ -59,11 +65,23 @@ in {
+ " "
+ ''--set NVIM_APPNAME "nvimrocks"''
+ " "
# XXX: Luarocks packages need to be added manaully,
# using LUA_PATH and LUA_CPATH.
# It looks like buildNeovimPlugin is broken?
+ ''--suffix LUA_CPATH ";" "${
lib.concatMapStringsSep ";" lua51Packages.getLuaCPath
(with lua51Packages; [
toml
toml-edit
nui-nvim
fzy
])
}"''
+ " "
+ ''--suffix LUA_PATH ";" "${
lib.concatMapStringsSep ";" lua51Packages.getLuaPath
(with lua51Packages; [
fzy
])
}"''
+ " "
Expand Down
1 change: 1 addition & 0 deletions nix/test-overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
toml
plenary-nvim
nui-nvim
fzy
];

extraPackages = [
Expand Down
4 changes: 3 additions & 1 deletion rocks.nvim-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ dependencies = {
"toml-edit",
"toml",
"nui.nvim",
"fzy",
}

test_dependencies = {
"lua >= 5.1",
"toml-edit",
"toml",
"nui.nvim",
"fzy",
}

source = {
Expand All @@ -27,7 +29,7 @@ source = {
build = {
type = "builtin",
copy_directories = {
-- 'doc',
'doc',
"plugin",
},
}

0 comments on commit ffb7f20

Please sign in to comment.