Skip to content

Commit a94cace

Browse files
committed
feat: add types for plenary.context_manager
1 parent 403186a commit a94cace

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lua/plenary/context_manager.lua

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
--- I like context managers for Python
22
--- I want them in Lua.
33

4+
---@class PlenaryContextManager
45
local context_manager = {}
56

7+
---@class PlenaryContextManagerObject
8+
---@field enter fun(self: PlenaryContextManagerObject): any
9+
---@field exit fun(self: PlenaryContextManagerObject)
10+
11+
---@generic T, U
12+
---@param obj function|PlenaryContextManagerObject|thread
13+
---@param callable fun(arg: T): U?
14+
---@return U?
615
function context_manager.with(obj, callable)
716
-- Wrap functions for people since we're nice
817
if type(obj) == "function" then
@@ -36,15 +45,16 @@ function context_manager.with(obj, callable)
3645
return result
3746
end
3847
end
39-
40-
--- @param filename string|table -- If string, used as io.open(filename)
41-
--- Else, should be a table with `filename` as an attribute
48+
---If string, used as io.open(filename). Else, should be a table with `filename` as an attribute
49+
---@param filename string|{ filename: string }
50+
---@param mode? openmode
51+
---@return thread
4252
function context_manager.open(filename, mode)
4353
if type(filename) == "table" and filename.filename then
4454
filename = filename.filename
4555
end
4656

47-
local file_io = assert(io.open(filename, mode))
57+
local file_io = assert(io.open(filename --[[@as string]], mode))
4858

4959
return coroutine.create(function()
5060
coroutine.yield(file_io)

lua/plenary/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
-- Lazy load everything into plenary.
22
---@class Plenary
33
---@field async PlenaryAsync
4+
---@field context_manager PlenaryContextManager
45
---@field functional PlenaryFunctional
56
---@field job PlenaryJob
67
---@field path PlenaryPath

0 commit comments

Comments
 (0)