Skip to content

Commit

Permalink
feat: add :list_scopes to Grapple app
Browse files Browse the repository at this point in the history
  • Loading branch information
cbochs committed Apr 27, 2024
1 parent c0a6b04 commit 386b583
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
5 changes: 5 additions & 0 deletions lua/grapple/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions lua/grapple/scope_content.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions lua/grapple/scope_manager.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Scope = require("grapple.scope")
local Util = require("grapple.util")

---@class grapple.scope_manager
---@field cache grapple.cache
Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions lua/grapple/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugin/grapple.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down
14 changes: 14 additions & 0 deletions tests/grapple/scope_manager_spec.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 386b583

Please sign in to comment.