Skip to content

Commit

Permalink
feat: add "shown" to scope definition to allow whitelisting scopes in…
Browse files Browse the repository at this point in the history
… settings
  • Loading branch information
cbochs committed Apr 27, 2024
1 parent c5dd7b1 commit d9600cd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,8 @@ Used for defining new scopes.
- **`fallback?`**: `string` fallback scope name
- **`cache?`**: [`grapple.cache.options`](#grapplecacheoptions) | `boolean`
- **`priority?`**: `integer` scope priority, higher scopes are loaded first
- **`hidden?`**: `boolean` do not show the scope in the [Scopes Window](#scopes-window)
- **`hidden?`**: `boolean` _hide_ scopes which have this field set
- **`shown?`**: `boolean` _show_ scopes which have this field set (mutually exclusive with `hidden`)

**Note**: Scopes are given a `priority` based on their fallback ordering. By default, scopes without a fallback are given a priority of `1000`; scopes with a fallback, but are also fallbacks themselves, are given a priority of `100`; and all other scopes are given a priority of `1`. Higher priority scopes are loaded first. This can be overridden by setting a scope's `priority` manually in the [settings](#settings).

Expand Down
28 changes: 27 additions & 1 deletion lua/grapple/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ local DEFAULT_SETTINGS = {
---@field fallback? string name of scope to fall back on
---@field cache? grapple.cache.options | boolean
---@field priority? integer
---@field hidden? boolean
---@field hidden? boolean hide only scopes which have this set
---@field shown? boolean show only scopes which have this set
---@field delete? boolean

---Default scopes provided by Grapple
Expand Down Expand Up @@ -498,6 +499,11 @@ function Settings:scopes()
---@type table<string, boolean>
local fallback_lookup = {}

-- Detect how a user is configuring scopes. There are two options:
-- exclude: scopes set the "hidden" field and are excluded by default
-- include: scopes set the "shown" field and are included by default
local using_shown = false

-- Add default scopes
for name, definition in pairs(self.inner.default_scopes) do
if definition == false then
Expand All @@ -507,6 +513,14 @@ function Settings:scopes()
definition = vim.tbl_extend("keep", definition, { name = name })
assert(type(definition.name) == "string")

if definition.shown then
using_shown = true
end

if definition.fallback then
fallback_lookup[definition.fallback] = true
end

table.insert(scopes, definition)
end

Expand All @@ -515,6 +529,10 @@ function Settings:scopes()
definition = vim.tbl_extend("keep", definition, { name = name })
assert(type(definition.name) == "string")

if definition.shown then
using_shown = true
end

if definition.fallback then
fallback_lookup[definition.fallback] = true
end
Expand All @@ -535,6 +553,14 @@ function Settings:scopes()
end
end

-- Update to whitelist or blacklist scopes by default
if using_shown then
for _, scope in ipairs(scopes) do
scope.hidden = not scope.shown
scope.shown = nil
end
end

local function by_priority(scope_a, scope_b)
if scope_a.priority == scope_b.priority then
return string.lower(scope_a.name) < string.lower(scope_b.name)
Expand Down
2 changes: 1 addition & 1 deletion tests/grapple/scope_manager_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe("TagContent", function()
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()))
assert.is_same({ "a", "b", "c" }, vim.tbl_map(Util.pick("name"), sm:list()))
end)
end)
end)
20 changes: 20 additions & 0 deletions tests/grapple/settings_spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Settings = require("grapple.settings")
local Util = require("grapple.util")

describe("Settings", function()
describe("Defaults", function()
Expand Down Expand Up @@ -70,5 +71,24 @@ describe("Settings", function()
assert.same("cwd", deleted[1].name)
assert.same(true, deleted[1].delete)
end)

it("hides all scopes except those with 'shown'", function()
local settings = Settings:new()
settings:update({ default_scopes = { git = { shown = true } } })

-- stylua: ignore
local hidden = vim.tbl_map(
Util.pick("name"),
vim.tbl_filter(function(def) return def.hidden == true end, settings:scopes())
)
assert.same({ "cwd", "global", "static", "git_branch", "lsp" }, hidden)

-- stylua: ignore
local not_hidden = vim.tbl_map(
Util.pick("name"),
vim.tbl_filter(function(def) return def.hidden == false end, settings:scopes())
)
assert.same({ "git" }, not_hidden)
end)
end)
end)

0 comments on commit d9600cd

Please sign in to comment.