Skip to content

Commit c9907ec

Browse files
committed
feat: hide scopes in scopes window
1 parent 390ee69 commit c9907ec

10 files changed

+57
-30
lines changed

lua/grapple.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -577,12 +577,14 @@ function Grapple.toggle_scopes()
577577
end
578578

579579
---Open a floating window populated with all defined scopes
580-
function Grapple.open_scopes()
580+
---@param opts? { all: boolean }
581+
function Grapple.open_scopes(opts)
581582
local ScopeContent = require("grapple.scope_content")
582583
local App = require("grapple.app")
583584
local app = App.get()
584585

585-
local content = ScopeContent:new(app, app.settings.scope_hook, app.settings.scope_title)
586+
local show_all = opts and opts.all or false
587+
local content = ScopeContent:new(app, app.settings.scope_hook, app.settings.scope_title, show_all)
586588

587589
open(content)
588590
end
@@ -705,7 +707,7 @@ function Grapple.initialize()
705707
tag = { args = {}, kwargs = new_kwargs },
706708
toggle = { args = {}, kwargs = tag_kwargs },
707709
toggle_loaded = { args = {}, kwargs = { "all" } },
708-
toggle_scopes = { args = {}, kwargs = {} },
710+
toggle_scopes = { args = {}, kwargs = { "all" } },
709711
toggle_tags = { args = {}, kwargs = window_kwargs },
710712
unload = { args = {}, kwargs = scope_kwargs },
711713
untag = { args = {}, kwargs = use_kwargs },

lua/grapple/app.lua

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function App:define_scope(definition)
7575
desc = definition.desc,
7676
fallback = definition.fallback,
7777
cache = definition.cache,
78+
hidden = definition.hidden,
7879
})
7980
end
8081

lua/grapple/container_actions.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ end
2929

3030
---@param opts grapple.action.container_options
3131
function ContainerActions.toggle_all(opts)
32-
-- HACK: reduce window flickering for now by updating the content in-place
32+
-- HACK: reduce window flickering by updating the content in-place
3333
opts.window.content.show_all = not opts.show_all
3434
opts.window:render()
35-
-- require("grapple").open_loaded({ all = not opts.show_all })
3635
end
3736

3837
function ContainerActions.open_scopes()

lua/grapple/resolved_scope.lua

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ ResolvedScope.__index = ResolvedScope
1111
---@param name string
1212
---@param id string
1313
---@param path string | nil
14-
---@param tag_manager grapple.tag_manager
1514
---@return grapple.resolved_scope
1615
function ResolvedScope:new(app, name, id, path)
1716
return setmetatable({

lua/grapple/scope.lua

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local ResolvedScope = require("grapple.resolved_scope")
66
---@field desc string
77
---@field resolver grapple.scope_resolver
88
---@field fallback grapple.scope | nil
9+
---@field hidden boolean
910
local Scope = {}
1011
Scope.__index = Scope
1112

@@ -14,17 +15,18 @@ Scope.__index = Scope
1415
---@param app grapple.app
1516
---@param name string
1617
---@param resolver grapple.scope_resolver
17-
---@param opts? { desc?: string, fallback?: grapple.scope }
18+
---@param opts? { desc?: string, fallback?: grapple.scope, hidden?: boolean }
1819
---@return grapple.scope
1920
function Scope:new(app, name, resolver, opts)
2021
opts = opts or {}
2122

2223
return setmetatable({
2324
app = app,
2425
name = name,
25-
desc = opts.desc,
26+
desc = opts.desc or "",
2627
resolver = resolver,
2728
fallback = opts.fallback,
29+
hidden = opts.hidden,
2830
}, self)
2931
end
3032

@@ -36,9 +38,9 @@ function Scope:resolve()
3638
if not id then
3739
if self.fallback then
3840
return self.fallback:resolve()
39-
else
40-
return nil, err
4141
end
42+
43+
return nil, err
4244
end
4345

4446
return ResolvedScope:new(self.app, self.name, id, path), nil

lua/grapple/scope_actions.lua

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ local ScopeActions = {}
55
---Provided by Window
66
---@field window grapple.window
77
---
8+
---Provided by ScopeContent
9+
---@field show_all boolean
10+
---
811
---User-provided information
912
---@field name? string
1013

@@ -23,4 +26,10 @@ function ScopeActions.open_loaded()
2326
require("grapple").open_loaded()
2427
end
2528

29+
function ScopeActions.toggle_all(opts)
30+
-- HACK: reduce window flickering by updating the content in-place
31+
opts.window.content.show_all = not opts.show_all
32+
opts.window:render()
33+
end
34+
2635
return ScopeActions

lua/grapple/scope_content.lua

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,21 @@ local Util = require("grapple.util")
44
---@field app grapple.app
55
---@field hook_fn grapple.hook_fn
66
---@field title_fn grapple.title_fn
7+
---@field show_all boolean
78
local ScopeContent = {}
89
ScopeContent.__index = ScopeContent
910

1011
---@param app grapple.app
1112
---@param hook_fn? grapple.hook_fn
1213
---@param title_fn? grapple.title_fn
14+
---@param show_all boolean
1315
---@return grapple.scope_content
14-
function ScopeContent:new(app, hook_fn, title_fn)
16+
function ScopeContent:new(app, hook_fn, title_fn, show_all)
1517
return setmetatable({
1618
app = app,
1719
hook_fn = hook_fn,
1820
title_fn = title_fn,
21+
show_all = show_all,
1922
}, self)
2023
end
2124

@@ -77,19 +80,26 @@ function ScopeContent:entities()
7780
return string.lower(scope_a.name) < string.lower(scope_b.name)
7881
end
7982

83+
---@type grapple.scope[]
8084
local scopes = vim.tbl_values(self.app.scope_manager.scopes)
8185
table.sort(scopes, by_name)
8286

8387
local entities = {}
8488

8589
for _, scope in ipairs(scopes) do
90+
if not self.show_all and scope.hidden then
91+
goto continue
92+
end
93+
8694
---@class grapple.scope_content.entity
8795
local entity = {
8896
scope = scope,
8997
current = scope.name == app.settings.scope,
9098
}
9199

92100
table.insert(entities, entity)
101+
102+
::continue::
93103
end
94104

95105
return entities, nil
@@ -191,6 +201,10 @@ end
191201
---@param opts? grapple.action.options
192202
---@return string? error
193203
function ScopeContent:perform(action, opts)
204+
opts = vim.tbl_extend("force", opts or {}, {
205+
show_all = self.show_all,
206+
})
207+
194208
return action(opts)
195209
end
196210

lua/grapple/scope_manager.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ function ScopeManager:lookup(id)
7979
-- somewhere else? Looks like an opportunity for refactoring
8080
local ids = vim.tbl_map(to_id, self.app.tag_manager:list())
8181
if vim.tbl_contains(ids, id) then
82-
return ResolvedScope:new(self.app, "unknown", id, nil)
82+
return ResolvedScope:new(self.app, "unknown", id, nil), nil
8383
end
8484

8585
return nil, string.format("could not find resolved scope for id: %s", id)
8686
end
8787

8888
---@param name string
8989
---@param resolver grapple.scope_resolver
90-
---@param opts? { force?: boolean, desc?: string, fallback?: string, cache?: grapple.cache.options | boolean }
90+
---@param opts? { force?: boolean, desc?: string, fallback?: string, cache?: grapple.cache.options | boolean, hidden?: boolean }
9191
---@return string? error
9292
function ScopeManager:define(name, resolver, opts)
9393
opts = opts or {}
@@ -116,6 +116,7 @@ function ScopeManager:define(name, resolver, opts)
116116
local scope = Scope:new(self.app, name, resolver, {
117117
desc = opts.desc,
118118
fallback = fallback,
119+
hidden = opts.hidden,
119120
})
120121

121122
self.scopes[name] = scope

lua/grapple/settings.lua

+12-15
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ local DEFAULT_SETTINGS = {
281281
window:perform_close(ScopeActions.open_loaded)
282282
end, { desc = "Go to loaded scopes" })
283283

284+
-- Toggle
285+
window:map("n", "g.", function()
286+
window:perform_retain(ScopeActions.toggle_all)
287+
end, { desc = "Toggle show hidden" })
288+
284289
-- Help
285290
window:map("n", "?", function()
286291
local WindowActions = require("grapple.window_actions")
@@ -323,11 +328,6 @@ local DEFAULT_SETTINGS = {
323328
end, { desc = string.format("Quick select %d", i) })
324329
end
325330

326-
-- Toggle
327-
window:map("n", "<s-cr>", function()
328-
window:perform_retain(ContainerActions.toggle_all)
329-
end, { desc = "Toggle show all" })
330-
331331
-- Unload
332332
window:map("n", "x", function()
333333
local entry = window:current_entry()
@@ -347,6 +347,11 @@ local DEFAULT_SETTINGS = {
347347
window:perform_close(ContainerActions.open_scopes)
348348
end, { desc = "Go to scopes" })
349349

350+
-- Toggle
351+
window:map("n", "g.", function()
352+
window:perform_retain(ContainerActions.toggle_all)
353+
end, { desc = "Toggle show unloaded" })
354+
350355
-- Help
351356
window:map("n", "?", function()
352357
local WindowActions = require("grapple.window_actions")
@@ -499,23 +504,15 @@ function Settings:scopes()
499504
definition = { delete = true }
500505
end
501506

502-
definition = vim.tbl_extend("keep", definition, {
503-
name = name,
504-
desc = "",
505-
})
506-
507+
definition = vim.tbl_extend("keep", definition, { name = name })
507508
assert(type(definition.name) == "string")
508509

509510
table.insert(scopes, definition)
510511
end
511512

512513
-- Add user-defined scopes
513514
for name, definition in pairs(self.inner.scopes) do
514-
definition = vim.tbl_extend("keep", definition, {
515-
name = name,
516-
desc = "",
517-
})
518-
515+
definition = vim.tbl_extend("keep", definition, { name = name })
519516
assert(type(definition.name) == "string")
520517

521518
if definition.fallback then

lua/grapple/tag_manager.lua

+5-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ function TagManager:transaction(id, callback, opts)
7272
return nil
7373
end
7474

75-
---@alias grapple.tag_container_item { container: grapple.tag_container | nil, loaded: boolean}
76-
---
75+
---@class grapple.tag_container_item
76+
---@field id string
77+
---@field loaded boolean
78+
---@field container? grapple.tag_container
79+
7780
---@return grapple.tag_container_item[]
7881
function TagManager:list()
7982
local list = {}

0 commit comments

Comments
 (0)