@@ -2,21 +2,24 @@ local Path = require("grapple.path")
2
2
local Util = require (" grapple.util" )
3
3
4
4
--- @class grapple.container_content
5
- --- @field tag_manager grapple.tag_manager
5
+ --- @field app grapple.app
6
6
--- @field hook_fn grapple.hook_fn
7
7
--- @field title_fn grapple.title_fn
8
+ --- @field show_all boolean
8
9
local ContainerContent = {}
9
10
ContainerContent .__index = ContainerContent
10
11
11
- --- @param tag_manager grapple.tag_manager
12
+ --- @param app grapple.app
12
13
--- @param hook_fn ? grapple.hook_fn
13
14
--- @param title_fn ? grapple.title_fn
15
+ --- @param show_all boolean
14
16
--- @return grapple.container_content
15
- function ContainerContent :new (tag_manager , hook_fn , title_fn )
17
+ function ContainerContent :new (app , hook_fn , title_fn , show_all )
16
18
return setmetatable ({
17
- tag_manager = tag_manager ,
19
+ app = app ,
18
20
hook_fn = hook_fn ,
19
21
title_fn = title_fn ,
22
+ show_all = show_all ,
20
23
}, self )
21
24
end
22
25
@@ -69,34 +72,45 @@ function ContainerContent:sync(original, parsed) end
69
72
70
73
--- @return grapple.window.entity[] | nil , string ? error
71
74
function ContainerContent :entities ()
72
- local App = require (" grapple.app" )
73
- local app = App .get ()
74
-
75
- local current_scope , err = app :current_scope ()
75
+ local current_scope , err = self .app :current_scope ()
76
76
if not current_scope then
77
77
return nil , err
78
78
end
79
79
80
- --- @param cont_a grapple.tag_container
81
- --- @param cont_b grapple.tag_container
82
- local function by_id (cont_a , cont_b )
83
- return string.lower (cont_a .id ) < string.lower (cont_b .id )
80
+ --- @param item_a grapple.tag_container_item
81
+ --- @param item_b grapple.tag_container_item
82
+ --- @return boolean
83
+ local function by_loaded_then_id (item_a , item_b )
84
+ local loaded_a = item_a .loaded and 1 or 0
85
+ local loaded_b = item_b .loaded and 1 or 0
86
+ if loaded_a ~= loaded_b then
87
+ return loaded_a > loaded_b
88
+ else
89
+ return string.lower (item_a .id ) < string.lower (item_b .id )
90
+ end
84
91
end
85
92
86
- --- @type grapple.tag_container[]
87
- local containers = vim .tbl_values (self .tag_manager .containers )
88
- table.sort (containers , by_id )
93
+ local container_list = self .app .tag_manager :list ()
94
+ table.sort (container_list , by_loaded_then_id )
89
95
90
96
local entities = {}
91
97
92
- for _ , container in ipairs (containers ) do
98
+ for _ , item in ipairs (container_list ) do
99
+ if not self .show_all and not item .loaded then
100
+ goto continue
101
+ end
102
+
93
103
--- @class grapple.container_content.entity
94
104
local entity = {
95
- container = container ,
96
- current = container .id == current_scope .id ,
105
+ id = item .id ,
106
+ container = item .container ,
107
+ loaded = item .loaded ,
108
+ current = item .id == current_scope .id ,
97
109
}
98
110
99
111
table.insert (entities , entity )
112
+
113
+ :: continue::
100
114
end
101
115
102
116
return entities , nil
@@ -106,51 +120,72 @@ end
106
120
--- @param index integer
107
121
--- @return grapple.window.entry
108
122
function ContainerContent :create_entry (entity , index )
109
- local App = require (" grapple.app" )
110
- local app = App .get ()
111
-
112
123
local container = entity .container
113
124
114
125
-- A string representation of the index
115
126
local id = string.format (" /%03d" , index )
116
127
117
128
-- Don't try to modify IDs which are not paths, like "global"
118
- local rel_id
119
- if Path .is_absolute (container .id ) then
120
- rel_id = vim .fn .fnamemodify (container .id , " :~" )
129
+ local container_id
130
+ if Path .is_absolute (entity .id ) then
131
+ container_id = vim .fn .fnamemodify (entity .id , " :~" )
121
132
else
122
- rel_id = container .id
133
+ container_id = entity .id
123
134
end
124
135
125
136
-- In compliance with "grapple" syntax
126
- local line = string.format (" %s %s" , id , rel_id )
137
+ local line = string.format (" %s %s" , id , container_id )
127
138
local min_col = assert (string.find (line , " %s" )) -- width of id
128
139
140
+ -- Define line highlights for line and extmarks
141
+ --- @type grapple.vim.highlight[]
142
+ local highlights = {}
143
+
129
144
local sign_highlight
130
- if app .settings .status and entity .current then
145
+ if self . app .settings .status and entity .current then
131
146
sign_highlight = " GrappleCurrent"
147
+ elseif not entity .loaded then
148
+ sign_highlight = " GrappleHint"
149
+ end
150
+
151
+ local loaded_highlight
152
+ if not entity .loaded then
153
+ local col_start , col_end = assert (string.find (line , container_id ))
154
+ loaded_highlight = {
155
+ hl_group = " GrappleHint" ,
156
+ line = index - 1 ,
157
+ col_start = col_start - 1 ,
158
+ col_end = col_end ,
159
+ }
132
160
end
133
161
162
+ highlights = vim .tbl_filter (Util .not_nil , {
163
+ loaded_highlight ,
164
+ })
165
+
134
166
-- Define line extmarks
135
167
--- @type grapple.vim.extmark[]
136
168
local extmarks = {}
137
169
138
170
--- @type grapple.vim.mark
139
171
local sign_mark
140
- local quick_select = app .settings :quick_select ()[index ]
172
+ local quick_select = self . app .settings :quick_select ()[index ]
141
173
if quick_select then
142
174
sign_mark = {
143
175
sign_text = string.format (" %s" , quick_select ),
144
176
sign_hl_group = sign_highlight ,
145
177
}
146
178
end
147
179
148
- local count = container :len ()
149
- local count_text = count == 1 and " tag" or " tags"
150
- local count_mark = {
151
- virt_text = { { string.format (" [%d %s]" , count , count_text ) } },
152
- virt_text_pos = " eol" ,
153
- }
180
+ local count_mark
181
+ if container then
182
+ local count = container :len ()
183
+ local count_text = count == 1 and " tag" or " tags"
184
+ count_mark = {
185
+ virt_text = { { string.format (" [%d %s]" , count , count_text ) } },
186
+ virt_text_pos = " eol" ,
187
+ }
188
+ end
154
189
155
190
extmarks = vim .tbl_filter (Util .not_nil , { sign_mark , count_mark })
156
191
extmarks = vim .tbl_map (function (mark )
@@ -165,15 +200,15 @@ function ContainerContent:create_entry(entity, index)
165
200
local entry = {
166
201
--- @class grapple.scope_content.data
167
202
data = {
168
- id = container .id ,
203
+ id = entity .id ,
169
204
},
170
205
171
206
line = line ,
172
207
index = index ,
173
208
min_col = min_col ,
174
209
175
210
--- @type grapple.vim.highlight[]
176
- highlights = {} ,
211
+ highlights = highlights ,
177
212
178
213
--- @type grapple.vim.extmark[]
179
214
extmarks = extmarks ,
202
237
--- @param opts ? grapple.action.options
203
238
--- @return string ? error
204
239
function ContainerContent :perform (action , opts )
240
+ opts = vim .tbl_extend (" force" , opts or {}, {
241
+ show_all = self .show_all ,
242
+ })
243
+
205
244
return action (opts )
206
245
end
207
246
0 commit comments