From 386b5838096052db5ae9db26c4556ca08498490e Mon Sep 17 00:00:00 2001 From: Calvin Bochulak Date: Sat, 27 Apr 2024 10:22:44 -0600 Subject: [PATCH] feat: add :list_scopes to Grapple app --- lua/grapple/app.lua | 5 +++++ lua/grapple/scope_content.lua | 11 +---------- lua/grapple/scope_manager.lua | 13 +++++++++++++ lua/grapple/util.lua | 18 ++++++++++++++++++ plugin/grapple.lua | 2 +- tests/grapple/scope_manager_spec.lua | 14 ++++++++++++++ 6 files changed, 52 insertions(+), 11 deletions(-) create mode 100644 tests/grapple/scope_manager_spec.lua diff --git a/lua/grapple/app.lua b/lua/grapple/app.lua index 8102609..b6e2f61 100644 --- a/lua/grapple/app.lua +++ b/lua/grapple/app.lua @@ -459,6 +459,11 @@ function App:list_containers() return self.tag_manager:list() end +---@return grapple.scope[] +function App:list_scopes() + return self.scope_manager:list() +end + ---Convenience function to open content in a new floating window ---@param content grapple.tag_content | grapple.scope_content | grapple.container_content ---@return string? error diff --git a/lua/grapple/scope_content.lua b/lua/grapple/scope_content.lua index ce10466..148ee29 100644 --- a/lua/grapple/scope_content.lua +++ b/lua/grapple/scope_content.lua @@ -71,16 +71,7 @@ function ScopeContent:sync(original, parsed) end ---@return grapple.window.entity[] | nil, string? error function ScopeContent:entities() - ---@param scope_a grapple.scope - ---@param scope_b grapple.scope - local function by_name(scope_a, scope_b) - return string.lower(scope_a.name) < string.lower(scope_b.name) - end - - ---@type grapple.scope[] - local scopes = vim.tbl_values(self.app.scope_manager.scopes) - table.sort(scopes, by_name) - + local scopes = self.app:list_scopes() local entities = {} for _, scope in ipairs(scopes) do diff --git a/lua/grapple/scope_manager.lua b/lua/grapple/scope_manager.lua index 7189470..3ee0f21 100644 --- a/lua/grapple/scope_manager.lua +++ b/lua/grapple/scope_manager.lua @@ -1,4 +1,5 @@ local Scope = require("grapple.scope") +local Util = require("grapple.util") ---@class grapple.scope_manager ---@field cache grapple.cache @@ -15,10 +16,22 @@ function ScopeManager:new(cache) }, self) end +---@return boolean function ScopeManager:exists(name) return self.scopes[name] ~= nil end +---@return grapple.scope[] +function ScopeManager:list() + ---@param scope_a grapple.scope + ---@param scope_b grapple.scope + local function by_name(scope_a, scope_b) + return string.lower(scope_a.name) < string.lower(scope_b.name) + end + + return Util.sort(vim.tbl_values(self.scopes), by_name) +end + ---@param name string scope name ---@return grapple.scope | nil, string? error function ScopeManager:get(name) diff --git a/lua/grapple/util.lua b/lua/grapple/util.lua index f3e5ed4..1e0bc4c 100644 --- a/lua/grapple/util.lua +++ b/lua/grapple/util.lua @@ -103,6 +103,24 @@ function Util.with_prefix(prefix) end end +---Transformer that picks out the requested values +---@generic T, V +---@param keep T | T[] +---@return fun(value: table): V | V[] +function Util.pick(keep) + keep = type(keep) == "table" and keep or { keep } + + return function(value) + local kept = {} + for k, v in pairs(value) do + if vim.tbl_contains(keep, k) then + table.insert(kept, v) + end + end + return #keep == 1 and kept[1] or kept + end +end + ---Transformer adds a suffix to a string value ---@param suffix string ---@return fun(value: string): string diff --git a/plugin/grapple.lua b/plugin/grapple.lua index e112504..3f6f002 100644 --- a/plugin/grapple.lua +++ b/plugin/grapple.lua @@ -107,7 +107,7 @@ vim.api.nvim_create_user_command( local argument_lookup = { all = { "true", "false" }, direction = { "next", "prev" }, - scope = Util.sort(vim.tbl_keys(app.scope_manager.scopes), Util.as_lower), + scope = vim.tbl_map(Util.pick("name"), app:list_scopes()), style = Util.sort(vim.tbl_keys(app.settings.styles), Util.as_lower), } diff --git a/tests/grapple/scope_manager_spec.lua b/tests/grapple/scope_manager_spec.lua new file mode 100644 index 0000000..3054c02 --- /dev/null +++ b/tests/grapple/scope_manager_spec.lua @@ -0,0 +1,14 @@ +local ScopeManager = require("grapple.scope_manager") +local Util = require("grapple.util") + +describe("TagContent", function() + describe(".list", function() + it("returns a list of scopes, sorted by name", function() + local sm = ScopeManager:new() + sm:define("c", function() end) + sm:define("b", function() end) + sm:define("a", function() end) + assert.are.same({ "a", "b", "c" }, vim.tbl_map(Util.pick("name"), sm:list())) + end) + end) +end)