diff options
Diffstat (limited to 'lua/mason-core')
| -rw-r--r-- | lua/mason-core/EventEmitter.lua | 8 | ||||
| -rw-r--r-- | lua/mason-core/async/control.lua | 58 | ||||
| -rw-r--r-- | lua/mason-core/async/init.lua | 12 | ||||
| -rw-r--r-- | lua/mason-core/installer/context/cwd.lua | 14 | ||||
| -rw-r--r-- | lua/mason-core/installer/context/fs.lua | 8 | ||||
| -rw-r--r-- | lua/mason-core/installer/context/init.lua | 10 | ||||
| -rw-r--r-- | lua/mason-core/installer/context/spawn.lua | 10 | ||||
| -rw-r--r-- | lua/mason-core/installer/handle.lua | 46 | ||||
| -rw-r--r-- | lua/mason-core/installer/location.lua | 12 | ||||
| -rw-r--r-- | lua/mason-core/installer/runner.lua | 16 | ||||
| -rw-r--r-- | lua/mason-core/optional.lua | 14 | ||||
| -rw-r--r-- | lua/mason-core/package.lua | 31 | ||||
| -rw-r--r-- | lua/mason-core/receipt.lua | 26 | ||||
| -rw-r--r-- | lua/mason-core/result.lua | 22 | ||||
| -rw-r--r-- | lua/mason-core/ui/display.lua | 2 |
15 files changed, 174 insertions, 115 deletions
diff --git a/lua/mason-core/EventEmitter.lua b/lua/mason-core/EventEmitter.lua index 7d95827f..8a936cb9 100644 --- a/lua/mason-core/EventEmitter.lua +++ b/lua/mason-core/EventEmitter.lua @@ -5,8 +5,12 @@ local log = require "mason-core.log" local EventEmitter = {} EventEmitter.__index = EventEmitter -function EventEmitter.new() - return EventEmitter.init(setmetatable({}, EventEmitter)) +function EventEmitter:new() + local instance = {} + setmetatable(instance, self) + instance.__event_handlers = {} + instance.__event_handlers_once = {} + return instance end ---@generic T diff --git a/lua/mason-core/async/control.lua b/lua/mason-core/async/control.lua index 57aa88db..2388bce5 100644 --- a/lua/mason-core/async/control.lua +++ b/lua/mason-core/async/control.lua @@ -4,8 +4,12 @@ local a = require "mason-core.async" local Condvar = {} Condvar.__index = Condvar -function Condvar.new() - return setmetatable({ handles = {} }, Condvar) +function Condvar:new() + ---@type Condvar + local instance = {} + setmetatable(instance, self) + instance.handles = {} + return instance end ---@async @@ -31,8 +35,12 @@ end local Permit = {} Permit.__index = Permit -function Permit.new(semaphore) - return setmetatable({ semaphore = semaphore }, Permit) +function Permit:new(semaphore) + ---@type Permit + local instance = {} + setmetatable(instance, self) + instance.semaphore = semaphore + return instance end function Permit:forget() @@ -42,7 +50,7 @@ function Permit:forget() if semaphore.permits > 0 and #semaphore.handles > 0 then semaphore.permits = semaphore.permits - 1 local release = table.remove(semaphore.handles, 1) - release(Permit.new(semaphore)) + release(Permit:new(semaphore)) end end @@ -51,8 +59,13 @@ local Semaphore = {} Semaphore.__index = Semaphore ---@param permits integer -function Semaphore.new(permits) - return setmetatable({ permits = permits, handles = {} }, Semaphore) +function Semaphore:new(permits) + ---@type Semaphore + local instance = {} + setmetatable(instance, self) + instance.permits = permits + instance.handles = {} + return instance end ---@async @@ -65,7 +78,7 @@ function Semaphore:acquire() end) end - return Permit.new(self) + return Permit:new(self) end ---@class OneShotChannel @@ -75,12 +88,14 @@ end local OneShotChannel = {} OneShotChannel.__index = OneShotChannel -function OneShotChannel.new() - return setmetatable({ - has_sent = false, - value = nil, - condvar = Condvar.new(), - }, OneShotChannel) +function OneShotChannel:new() + ---@type OneShotChannel + local instance = {} + setmetatable(instance, self) + instance.has_sent = false + instance.value = nil + instance.condvar = Condvar:new() + return instance end function OneShotChannel:is_closed() @@ -108,12 +123,15 @@ end ---@field is_closed boolean local Channel = {} Channel.__index = Channel -function Channel.new() - return setmetatable({ - condvar = Condvar.new(), - buffer = nil, - is_closed = false, - }, Channel) + +function Channel:new() + ---@type Channel + local instance = {} + setmetatable(instance, self) + instance.condvar = Condvar:new() + instance.buffer = nil + instance.is_closed = false + return instance end function Channel:close() diff --git a/lua/mason-core/async/init.lua b/lua/mason-core/async/init.lua index 63d4ec94..56d86170 100644 --- a/lua/mason-core/async/init.lua +++ b/lua/mason-core/async/init.lua @@ -6,8 +6,12 @@ local exports = {} local Promise = {} Promise.__index = Promise -function Promise.new(resolver) - return setmetatable({ resolver = resolver, has_resolved = false }, Promise) +function Promise:new(resolver) + local instance = {} + setmetatable(instance, self) + instance.resolver = resolver + instance.has_resolved = false + return instance end ---@param success boolean @@ -27,7 +31,7 @@ function Promise:__call(callback) end local function await(resolver) - local ok, value = co.yield(Promise.new(resolver)) + local ok, value = co.yield(Promise:new(resolver)) if not ok then error(value[1], 0) end @@ -156,7 +160,7 @@ end ---@param suspend_fns async fun()[] ---@param mode '"first"' | '"all"' local function wait(suspend_fns, mode) - local channel = require("mason-core.async.control").OneShotChannel.new() + local channel = require("mason-core.async.control").OneShotChannel:new() if #suspend_fns == 0 then return end diff --git a/lua/mason-core/installer/context/cwd.lua b/lua/mason-core/installer/context/cwd.lua index cb2e70ec..2b74bf55 100644 --- a/lua/mason-core/installer/context/cwd.lua +++ b/lua/mason-core/installer/context/cwd.lua @@ -11,13 +11,15 @@ InstallContextCwd.__index = InstallContextCwd ---@param handle InstallHandle ---@param location InstallLocation -function InstallContextCwd.new(handle, location) +function InstallContextCwd:new(handle, location) assert(location, "location not provided") - return setmetatable({ - location = location, - handle = handle, - cwd = nil, - }, InstallContextCwd) + ---@type InstallContextCwd + local instance = {} + setmetatable(instance, self) + instance.location = location + instance.handle = handle + instance.cwd = nil + return instance end function InstallContextCwd:initialize() diff --git a/lua/mason-core/installer/context/fs.lua b/lua/mason-core/installer/context/fs.lua index 5c51fb56..93379017 100644 --- a/lua/mason-core/installer/context/fs.lua +++ b/lua/mason-core/installer/context/fs.lua @@ -8,8 +8,12 @@ local InstallContextFs = {} InstallContextFs.__index = InstallContextFs ---@param cwd InstallContextCwd -function InstallContextFs.new(cwd) - return setmetatable({ cwd = cwd }, InstallContextFs) +function InstallContextFs:new(cwd) + ---@type InstallContextFs + local instance = {} + setmetatable(instance, InstallContextFs) + instance.cwd = cwd + return instance end ---@async diff --git a/lua/mason-core/installer/context/init.lua b/lua/mason-core/installer/context/init.lua index f2cedb42..425bf39c 100644 --- a/lua/mason-core/installer/context/init.lua +++ b/lua/mason-core/installer/context/init.lua @@ -26,10 +26,10 @@ InstallContext.__index = InstallContext ---@param handle InstallHandle ---@param location InstallLocation ---@param opts PackageInstallOpts -function InstallContext.new(handle, location, opts) - local cwd = InstallContextCwd.new(handle, location) - local spawn = InstallContextSpawn.new(handle, cwd, false) - local fs = InstallContextFs.new(cwd) +function InstallContext:new(handle, location, opts) + local cwd = InstallContextCwd:new(handle, location) + local spawn = InstallContextSpawn:new(handle, cwd, false) + local fs = InstallContextFs:new(cwd) return setmetatable({ cwd = cwd, spawn = spawn, @@ -37,7 +37,7 @@ function InstallContext.new(handle, location, opts) location = location, package = handle.package, -- for convenience fs = fs, - receipt = receipt.InstallReceiptBuilder.new(), + receipt = receipt.InstallReceiptBuilder:new(), stdio_sink = handle.stdio.sink, links = { bin = {}, diff --git a/lua/mason-core/installer/context/spawn.lua b/lua/mason-core/installer/context/spawn.lua index 0a73ff3a..f2ce8df2 100644 --- a/lua/mason-core/installer/context/spawn.lua +++ b/lua/mason-core/installer/context/spawn.lua @@ -10,8 +10,14 @@ local InstallContextSpawn = {} ---@param handle InstallHandle ---@param cwd InstallContextCwd ---@param strict_mode boolean -function InstallContextSpawn.new(handle, cwd, strict_mode) - return setmetatable({ cwd = cwd, handle = handle, strict_mode = strict_mode }, InstallContextSpawn) +function InstallContextSpawn:new(handle, cwd, strict_mode) + ---@type InstallContextSpawn + local instance = {} + setmetatable(instance, self) + instance.cwd = cwd + instance.handle = handle + instance.strict_mode = strict_mode + return instance end ---@param cmd string diff --git a/lua/mason-core/installer/handle.lua b/lua/mason-core/installer/handle.lua index 96acbdd1..62da5bae 100644 --- a/lua/mason-core/installer/handle.lua +++ b/lua/mason-core/installer/handle.lua @@ -23,9 +23,19 @@ local uv = vim.loop local InstallHandleSpawnHandle = {} InstallHandleSpawnHandle.__index = InstallHandleSpawnHandle ----@param fields InstallHandleSpawnHandle -function InstallHandleSpawnHandle.new(fields) - return setmetatable(fields, InstallHandleSpawnHandle) +---@param luv_handle luv_handle +---@param pid integer +---@param cmd string +---@param args string[] +function InstallHandleSpawnHandle:new(luv_handle, pid, cmd, args) + ---@type InstallHandleSpawnHandle + local instance = {} + setmetatable(instance, InstallHandleSpawnHandle) + instance.uv_handle = luv_handle + instance.pid = pid + instance.cmd = cmd + instance.args = args + return instance end function InstallHandleSpawnHandle:__tostring() @@ -38,8 +48,9 @@ end ---@field stdio { buffers: { stdout: string[], stderr: string[] }, sink: StdioSink } ---@field is_terminated boolean ---@field private spawn_handles InstallHandleSpawnHandle[] -local InstallHandle = setmetatable({}, { __index = EventEmitter }) -local InstallHandleMt = { __index = InstallHandle } +local InstallHandle = {} +InstallHandle.__index = InstallHandle +setmetatable(InstallHandle, { __index = EventEmitter }) ---@param handle InstallHandle local function new_sink(handle) @@ -60,14 +71,14 @@ local function new_sink(handle) end ---@param pkg Package -function InstallHandle.new(pkg) - local self = EventEmitter.init(setmetatable({}, InstallHandleMt)) - self.state = "IDLE" - self.package = pkg - self.spawn_handles = {} - self.stdio = new_sink(self) - self.is_terminated = false - return self +function InstallHandle:new(pkg) + local instance = EventEmitter.new(self) + instance.state = "IDLE" + instance.package = pkg + instance.spawn_handles = {} + instance.stdio = new_sink(instance) + instance.is_terminated = false + return instance end ---@param luv_handle luv_handle @@ -75,12 +86,7 @@ end ---@param cmd string ---@param args string[] function InstallHandle:register_spawn_handle(luv_handle, pid, cmd, args) - local spawn_handles = InstallHandleSpawnHandle.new { - uv_handle = luv_handle, - pid = pid, - cmd = cmd, - args = args, - } + local spawn_handles = InstallHandleSpawnHandle:new(luv_handle, pid, cmd, args) log.fmt_trace("Pushing spawn_handles stack for %s: %s (pid: %s)", self, spawn_handles, pid) self.spawn_handles[#self.spawn_handles + 1] = spawn_handles self:emit "spawn_handles:change" @@ -211,7 +217,7 @@ function InstallHandle:close() self:__clear_event_handlers() end -function InstallHandleMt:__tostring() +function InstallHandle:__tostring() return ("InstallHandle(package=%s, state=%s)"):format(self.package, self.state) end diff --git a/lua/mason-core/installer/location.lua b/lua/mason-core/installer/location.lua index cc069cc2..9cdf097f 100644 --- a/lua/mason-core/installer/location.lua +++ b/lua/mason-core/installer/location.lua @@ -8,14 +8,16 @@ local InstallLocation = {} InstallLocation.__index = InstallLocation ---@param dir string -function InstallLocation.new(dir) - return setmetatable({ - dir = dir, - }, InstallLocation) +function InstallLocation:new(dir) + ---@type InstallLocation + local instance = {} + setmetatable(instance, self) + instance.dir = dir + return instance end function InstallLocation.global() - return InstallLocation.new(settings.current.install_root_dir) + return InstallLocation:new(settings.current.install_root_dir) end function InstallLocation:get_dir() diff --git a/lua/mason-core/installer/runner.lua b/lua/mason-core/installer/runner.lua index 3e3580e5..64aa605d 100644 --- a/lua/mason-core/installer/runner.lua +++ b/lua/mason-core/installer/runner.lua @@ -20,12 +20,14 @@ InstallRunner.__index = InstallRunner ---@param location InstallLocation ---@param handle InstallHandle ---@param semaphore Semaphore -function InstallRunner.new(location, handle, semaphore) - return setmetatable({ - location = location, - semaphore = semaphore, - handle = handle, - }, InstallRunner) +function InstallRunner:new(location, handle, semaphore) + ---@type InstallRunner + local instance = {} + setmetatable(instance, self) + instance.location = location + instance.semaphore = semaphore + instance.handle = handle + return instance end ---@param opts PackageInstallOpts @@ -34,7 +36,7 @@ function InstallRunner:execute(opts, callback) local handle = self.handle log.fmt_info("Executing installer for %s %s", handle.package, opts) - local context = InstallContext.new(handle, self.location, opts) + local context = InstallContext:new(handle, self.location, opts) local tailed_output = {} diff --git a/lua/mason-core/optional.lua b/lua/mason-core/optional.lua index d5055c9c..bb168324 100644 --- a/lua/mason-core/optional.lua +++ b/lua/mason-core/optional.lua @@ -4,18 +4,22 @@ local Optional = {} Optional.__index = Optional ---@param value any -function Optional.new(value) - return setmetatable({ _value = value }, Optional) +function Optional:new(value) + ---@type Optional + local instance = {} + setmetatable(instance, self) + instance._value = value + return instance end -local EMPTY = Optional.new(nil) +local EMPTY = Optional:new(nil) ---@param value any function Optional.of_nilable(value) if value == nil then return EMPTY else - return Optional.new(value) + return Optional:new(value) end end @@ -25,7 +29,7 @@ end ---@param value any function Optional.of(value) - return Optional.new(value) + return Optional:new(value) end ---@param mapper_fn fun(value: any): any diff --git a/lua/mason-core/package.lua b/lua/mason-core/package.lua index 58ad9b46..a8a7ac79 100644 --- a/lua/mason-core/package.lua +++ b/lua/mason-core/package.lua @@ -17,7 +17,9 @@ local Semaphore = require("mason-core.async.control").Semaphore ---@field name string ---@field spec RegistryPackageSpec ---@field private handle InstallHandle The currently associated handle. -local Package = setmetatable({}, { __index = EventEmitter }) +local Package = {} +Package.__index = Package +setmetatable(Package, { __index = EventEmitter }) ---@param package_identifier string ---@return string, string? @@ -56,8 +58,6 @@ Package.License = setmetatable({}, { end, }) -local PackageMt = { __index = Package } - ---@class RegistryPackageSourceVersionOverride : RegistryPackageSource ---@field constraint string @@ -108,12 +108,12 @@ local function validate_spec(spec) end ---@param spec RegistryPackageSpec -function Package.new(spec) +function Package:new(spec) validate_spec(spec) - return EventEmitter.init(setmetatable({ - name = spec.name, -- for convenient access - spec = spec, - }, PackageMt)) + local instance = EventEmitter.new(self) --[[@as Package]] + instance.name = spec.name -- for convenient access + instance.spec = spec + return instance end function Package:new_handle() @@ -122,7 +122,7 @@ function Package:new_handle() end) log.fmt_trace("Creating new handle for %s", self) local InstallationHandle = require "mason-core.installer.handle" - local handle = InstallationHandle.new(self) + local handle = InstallationHandle:new(self) self.handle = handle -- Ideally we'd decouple this and leverage Mason's event system, but to allow loading as little as possible during @@ -139,7 +139,7 @@ end ---@alias PackageInstallOpts { version?: string, debug?: boolean, target?: string, force?: boolean, strict?: boolean } -- TODO this needs to be elsewhere -local semaphore = Semaphore.new(settings.current.max_concurrent_installers) +local semaphore = Semaphore:new(settings.current.max_concurrent_installers) function Package:is_installing() return self:get_handle() @@ -159,7 +159,7 @@ function Package:install(opts, callback) opts = opts or {} assert(not self:is_installing(), "Package is already installing.") local handle = self:new_handle() - local runner = InstallRunner.new(InstallLocation.global(), handle, semaphore) + local runner = InstallRunner:new(InstallLocation.global(), handle, semaphore) runner:execute(opts, callback) return handle end @@ -263,13 +263,12 @@ function Package:get_lsp_settings_schema() end return Optional.empty() end - -function PackageMt.__tostring(self) - return ("Package(name=%s)"):format(self.name) -end - function Package:get_aliases() return require("mason-registry").get_package_aliases(self.name) end +function Package:__tostring() + return ("Package(name=%s)"):format(self.name) +end + return Package diff --git a/lua/mason-core/receipt.lua b/lua/mason-core/receipt.lua index 748cab38..63403503 100644 --- a/lua/mason-core/receipt.lua +++ b/lua/mason-core/receipt.lua @@ -25,12 +25,12 @@ local M = {} local InstallReceipt = {} InstallReceipt.__index = InstallReceipt -function InstallReceipt.new(data) - return setmetatable(data, InstallReceipt) +function InstallReceipt:new(data) + return setmetatable(data, self) end function InstallReceipt.from_json(json) - return InstallReceipt.new(json) + return InstallReceipt:new(json) end function InstallReceipt:get_name() @@ -72,14 +72,16 @@ end local InstallReceiptBuilder = {} InstallReceiptBuilder.__index = InstallReceiptBuilder -function InstallReceiptBuilder.new() - return setmetatable({ - links = { - bin = vim.empty_dict(), - share = vim.empty_dict(), - opt = vim.empty_dict(), - }, - }, InstallReceiptBuilder) +function InstallReceiptBuilder:new() + ---@type InstallReceiptBuilder + local instance = {} + setmetatable(instance, self) + instance.links = { + bin = vim.empty_dict(), + share = vim.empty_dict(), + opt = vim.empty_dict(), + } + return instance end ---@param name string @@ -130,7 +132,7 @@ function InstallReceiptBuilder:build() assert(self.start_time, "start_time is required") assert(self.completion_time, "completion_time is required") assert(self.source, "source is required") - return InstallReceipt.new { + return InstallReceipt:new { name = self.name, schema_version = "1.2", metrics = { diff --git a/lua/mason-core/result.lua b/lua/mason-core/result.lua index 1491c31f..e98a11b3 100644 --- a/lua/mason-core/result.lua +++ b/lua/mason-core/result.lua @@ -3,8 +3,12 @@ local Failure = {} Failure.__index = Failure -function Failure.new(error) - return setmetatable({ error = error }, Failure) +function Failure:new(error) + ---@type Failure + local instance = {} + setmetatable(instance, self) + instance.error = error + return instance end ---@class Result @@ -12,18 +16,20 @@ end local Result = {} Result.__index = Result -function Result.new(value) - return setmetatable({ - value = value, - }, Result) +function Result:new(value) + ---@type Result + local instance = {} + setmetatable(instance, self) + instance.value = value + return instance end function Result.success(value) - return Result.new(value) + return Result:new(value) end function Result.failure(error) - return Result.new(Failure.new(error)) + return Result:new(Failure:new(error)) end function Result:get_or_nil() diff --git a/lua/mason-core/ui/display.lua b/lua/mason-core/ui/display.lua index ebd33203..a9e63910 100644 --- a/lua/mason-core/ui/display.lua +++ b/lua/mason-core/ui/display.lua @@ -229,7 +229,7 @@ function M.new_view_only_win(name, filetype) ---@type WindowOpts local window_opts = {} - local events = EventEmitter.new() + local events = EventEmitter:new() vim.diagnostic.config({ virtual_text = { |
