Skip to content

Commit a533c68

Browse files
committed
doc(generics): Added missing type definitions
fix(generics): `Toggle` now works as expected
1 parent 04992ab commit a533c68

2 files changed

Lines changed: 74 additions & 91 deletions

File tree

lua/bars/generic.lua

Lines changed: 19 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,7 @@
1-
--[[
2-
# 🧬 Generics
3-
4-
Base class for various bars & lines. All bars/lines extends from it.
5-
6-
## 📦 Variables
7-
8-
| Name | Kind | Description |
9-
|--------------------|---------|-----------------------------------------------|
10-
| `state` | table | Bars state. |
11-
| `config` | table | Bars configuration. |
12-
| `use_blank_output` | boolean | Uses blank output for windows without styles. |
13-
14-
## 🚧 Functions
15-
16-
1. `set_default_state`, Sets the config & state for a bar. **MUST BE USED WHEN CREATING A NEW BAR.**
17-
2. `current`, Gets current bar value. **MUST BE SET AFTER CREATION.**
18-
3. `should_attach`, Should a bar attach to a `window`?
19-
4. `should_detach`, Should a bar detach from a `window`?
20-
5. `set`, Sets bar/line for a `window`. **MUST BE SET AFTER CREATION.**
21-
6. `set`, Removes bar/line for a `window`. **MUST BE SET AFTER CREATION.**
22-
7. `attach`, Attaches to a `window`.
23-
8. `detach`, Detaches from a `window`.
24-
9. `enable`, Enables bar/line for a `window`.
25-
10. `disable`, Disables bar/line for a `window`.
26-
11. `toggle`, Toggles bar/line for a `window`.
27-
12. `update`, Update bar/line style for a `window`.
28-
13. `Toggle`, Toggle bar/line `globally`.
29-
14. `Enable`, Enable bar/line `globally`.
30-
15. `Disable`, Disable bar/line `globally`.
31-
16. `handle_new_window`, Handle events that causes a window to be attached/detached. Mainly `WinNew`..
32-
17. `update_style`, Updates the bar/line style for a `window`.
33-
18. `styled_component`, Gets output of a styled component of a bar/line.
34-
19. `get_styled_output`, Gets the styled bar/line for a window from a source.
35-
36-
## 📚 Usage
37-
38-
```lua
39-
local bar = require("bars.generics").new();
40-
bar:set_default_state();
41-
42-
bar:attach(0);
43-
bar:enable(0);
44-
```
45-
46-
]]
1+
---@type bars.generic
2+
---@diagnostic disable-next-line: missing-fields
473
local generic = {};
484

49-
---@class bars.statusline.state
50-
---
51-
---@field enable boolean
52-
---@field window_state table<integer, boolean|nil>
53-
545
function generic:set_default_state ()
556
self.state = {
567
enable = true,
@@ -77,13 +28,8 @@ generic.var_name = "bars_style";
7728

7829
--------------------------------------------------------------------------------
7930

80-
--- Current value for a `bar`.
81-
---@param _ integer
8231
function generic:current (_) return ""; end
8332

84-
--- Should we attach to `win`?
85-
---@param win integer
86-
---@return boolean
8733
function generic:should_attach (win)
8834
if not self.state.enable then
8935
return false
@@ -120,9 +66,6 @@ function generic:should_attach (win)
12066
return true;
12167
end
12268

123-
--- Should we detach from `win`?
124-
---@param win integer
125-
---@return boolean
12669
function generic:should_detach (win)
12770
if not self.state.enable then
12871
return false
@@ -161,16 +104,9 @@ end
161104

162105
--------------------------------------------------------------------------------
163106

164-
--- Sets options for a specific bar.
165-
---@param _ integer
166107
function generic:set (_) end
167-
168-
--- Removes/resets options for a specific bar.
169-
---@param _ integer
170108
function generic:remove (_) end
171109

172-
--[[ Attaches a bar to `win`. ]]
173-
---@param win integer
174110
function generic:attach (win)
175111
if self.state.window_state[win] == true then return; end
176112
self.state.window_state[win] = true;
@@ -179,17 +115,13 @@ function generic:attach (win)
179115
self:set(win);
180116
end
181117

182-
--[[ Detaches a bar from `win`. ]]
183-
---@param win integer
184118
function generic:detach (win)
185119
if self.state.window_state[win] == false then return; end
186120
self.state.window_state[win] = nil;
187121

188122
self:remove(win);
189123
end
190124

191-
--[[ Enables a bar of `win`. ]]
192-
---@param win integer
193125
function generic:enable (win)
194126
if not self.state.enable then
195127
return;
@@ -203,8 +135,6 @@ function generic:enable (win)
203135
self:set(win);
204136
end
205137

206-
--[[ Disables a bar of `win`. ]]
207-
---@param win integer
208138
function generic:disable (win)
209139
if not self.state.enable then
210140
return;
@@ -216,8 +146,6 @@ function generic:disable (win)
216146
self:remove(win);
217147
end
218148

219-
--[[ Toggles a bar of `win`. ]]
220-
---@param win integer
221149
function generic:toggle (win)
222150
if self.state.window_state[win] then
223151
self:disable(win);
@@ -226,19 +154,22 @@ function generic:toggle (win)
226154
end
227155
end
228156

229-
--[[ Update a bar style of `win`. ]]
230-
---@param win integer
231157
function generic:update (win)
232158
self:update_style(win);
233159
end
234160

235161
function generic:Toggle ()
236-
for win, _ in pairs(self.state.window_state) do
237-
self:toggle(win);
238-
end
162+
self:Enable();
163+
self:Disable();
239164
end
240165

241166
function generic:Enable ()
167+
if self.state.enable then
168+
return;
169+
end
170+
171+
self.state.enable = true;
172+
242173
for win, state in pairs(self.state.window_state) do
243174
if state == false then
244175
self:enable(win);
@@ -247,15 +178,19 @@ function generic:Enable ()
247178
end
248179

249180
function generic:Disable ()
181+
if not self.state.enable then
182+
return;
183+
end
184+
250185
for win, state in pairs(self.state.window_state) do
251186
if state then
252187
self:disable(win);
253188
end
254189
end
190+
191+
self.state.enable = false;
255192
end
256193

257-
--[[ Handles events that cause a bar to be `attached/detached`. ]]
258-
---@param win integer
259194
function generic:handle_new_window (win)
260195
if not self.state.enable then
261196
return;
@@ -268,7 +203,6 @@ function generic:handle_new_window (win)
268203
end
269204
end
270205

271-
---@param win integer
272206
function generic:update_style(win)
273207
local buf = vim.api.nvim_win_get_buf(win);
274208

@@ -299,12 +233,6 @@ function generic:update_style(win)
299233
vim.api.nvim_win_set_var(win, "_" .. self.var_name, style);
300234
end
301235

302-
---@param win integer
303-
---@param buf integer
304-
---@param components table<string, function>
305-
---@param entry table
306-
---@param last string
307-
---@return string
308236
function generic:styled_component (win, buf, components, entry, last)
309237
if entry.condition then
310238
if entry.condition == false then
@@ -346,9 +274,6 @@ function generic:styled_component (win, buf, components, entry, last)
346274
end
347275
end
348276

349-
---@param win integer
350-
---@param components table<string, function>
351-
---@return string
352277
function generic:get_styled_output (win, components)
353278
local buf = vim.api.nvim_win_get_buf(win);
354279

@@ -391,7 +316,10 @@ generic.__index = generic;
391316

392317
local builder = {};
393318

319+
--[[ Creates a new `bar`. ]]
320+
---@return bars.generic
394321
builder.new = function ()
322+
---@type bars.generic
395323
local out = setmetatable({}, generic);
396324
out:set_default_state();
397325

lua/bars/types/generics.lua

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---@meta
2+
3+
--[[
4+
# 🧬 Generics
5+
6+
Base class for various bars & lines. All bars/lines extends from it.
7+
8+
## 📚 Usage
9+
10+
```lua
11+
local bar = require("bars.generics").new();
12+
13+
bar:attach(0);
14+
bar:enable(0);
15+
```
16+
17+
]]
18+
---@class bars.generic
19+
---
20+
---@field config table Bars configuration.
21+
---@field default string Default value for a `bar@` used by the plugin.
22+
---@field state bars.state Bars state.
23+
---@field use_blank_output? boolean Uses blank output for windows without styles.
24+
---@field var_name string Variable name for storing current style name.
25+
---
26+
---@field set_default_state fun(self: bars.generic): nil Sets the config & state for a bar.
27+
---@field current fun(self: bars.generic, win: integer): string Gets current bar value. **MUST BE SET AFTER CREATION.**
28+
---@field should_attach fun(self: bars.generic, win: integer): boolean Should a bar attach to a `window`?
29+
---@field should_detach fun(self: bars.generic, win: integer): boolean Should a bar detach from a `window`?
30+
---@field set fun(self: bars.generic, win: integer): nil Sets bar/line for a `window`. **MUST BE SET AFTER CREATION.**
31+
---@field remove fun(self: bars.generic, win: integer): nil Removes bar/line for a `window`. **MUST BE SET AFTER CREATION.**
32+
---@field attach fun(self: bars.generic, win: integer): nil Attaches to a `window`.
33+
---@field detach fun(self: bars.generic, win: integer): nil Detaches from a `window`.
34+
---@field handle_new_window fun(self: bars.generic, win: integer): nil Handle events that causes a window to be attached/detached. Mainly `WinNew`..
35+
---@field update_style fun(self: bars.generic, win: integer): nil Updates the bar/line style for a `window`.
36+
---
37+
---@field enable fun(self: bars.generic, win: integer): nil Enables bar/line for a `window`.
38+
---@field disable fun(self: bars.generic, win: integer): nil Disables bar/line for a `window`.
39+
---@field toggle fun(self: bars.generic, win: integer): nil Toggles bar/line for a `window`.
40+
---@field update fun(self: bars.generic, win: integer): nil Update bar/line style for a `window`.
41+
---@field Enable fun(self: bars.generic): nil Enable bar/line `globally`.
42+
---@field Disable fun(self: bars.generic): nil Disable bar/line `globally`.
43+
---@field Toggle fun(self: bars.generic): nil Toggle bar/line `globally`.
44+
---
45+
---@field styled_component fun(self: bars.generic, win: integer, buf: integer, components: table<string, function>, entry: table, last: string): string Gets output of a styled component of a bar/line.
46+
---@field get_styled_output fun(self: bars.generic, win: integer, components: table<string, function>): string Gets the styled bar/line for a window from a source.
47+
---
48+
---@field __index table
49+
50+
51+
---@class bars.state
52+
---
53+
---@field enable boolean
54+
---@field window_state table<integer, boolean>
55+

0 commit comments

Comments
 (0)