Skip to content

Commit

Permalink
feat: allowing handlers to be passed in via setup function
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-babu committed Mar 27, 2023
1 parent 4070ec7 commit 35ce897
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
76 changes: 46 additions & 30 deletions lua/mason-null-ls/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,18 @@ local default_setup = function(source, types)
end
end

---@param config MasonNullLsSettings | nil
function M.setup(config)
local settings = require('mason-null-ls.settings')

if config then
settings.set(config)
end

-- NOTE: this is left here for future porting in case needed
-- local ok, err = pcall(function()
-- require "mason-lspconfig.lspconfig_hook"()
-- require "mason-lspconfig.server_config_extensions"()
-- end)
-- if not ok then
-- log.error("Failed to set up lspconfig integration.", err)
-- end

if #settings.current.ensure_installed > 0 then
require('mason-null-ls.ensure_installed')()
end

if settings.current.automatic_installation then
require('mason-null-ls.automatic_installation')()
end

require('mason-null-ls.api.command')
end

---@param handlers table<string, fun(source_name: string, methods: string[])>
function M.setup_handlers(handlers)
---@param handlers table<string, fun(source_name: string, methods: string[])> | nil
---@return nil
local function setup_handlers(handlers)
handlers = handlers or {}

local Optional = require('mason-core.optional')
local source_mappings = require('mason-null-ls.mappings.source')
local registry = require('mason-registry')
local notify = require('mason-core.notify')

local default_handler = Optional.of_nilable(handlers[1]):or_(_.always(Optional.of_nilable(default_setup)))

_.each(function(handler)
if type(handler) == 'string' and not source_mappings.getPackageFromNullLs(handler) then
notify(
Expand All @@ -68,6 +43,7 @@ function M.setup_handlers(handlers)

local function call_handler(source_name)
log.fmt_trace('Checking handler for %s', source_name)

Optional.of_nilable(handlers[source_name]):or_(_.always(default_handler)):if_present(function(handler)
log.fmt_trace('Calling handler for %s', source_name)

Expand All @@ -81,6 +57,7 @@ function M.setup_handlers(handlers)
end, { 'diagnostics', 'formatting', 'code_actions', 'completion', 'hover' })

local ok, err = pcall(handler, source_name, types)

if not ok then
vim.notify(err, vim.log.levels.ERROR)
end
Expand All @@ -97,6 +74,45 @@ function M.setup_handlers(handlers)
)
end

---@param config MasonNullLsSettings | nil
function M.setup(config)
local settings = require('mason-null-ls.settings')

if config then
settings.set(config)
end

-- NOTE: this is left here for future porting in case needed
-- local ok, err = pcall(function()
-- require "mason-lspconfig.lspconfig_hook"()
-- require "mason-lspconfig.server_config_extensions"()
-- end)
-- if not ok then
-- log.error("Failed to set up lspconfig integration.", err)
-- end

if #settings.current.ensure_installed > 0 then
require('mason-null-ls.ensure_installed')()
end

if settings.current.automatic_installation then
require('mason-null-ls.automatic_installation')()
end

if settings.current.handlers then
setup_handlers(settings.current.handlers)
end

require('mason-null-ls.api.command')
end

---@param handlers table<string, fun(source_name: string, methods: string[])> | nil
---@deprecated
---@return nil
function M.setup_handlers(handlers)
return setup_handlers(handlers)
end

---@return string[]
function M.get_installed_sources()
local Optional = require('mason-core.optional')
Expand Down
8 changes: 6 additions & 2 deletions lua/mason-null-ls/settings.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
local M = {}

---@class MasonNullLsSettings
---@field handlers table | nil
---@field automatic_setup boolean | table
---@field ensure_installed table
---@field automatic_installation boolean | table
local DEFAULT_SETTINGS = {
-- A list of sources to automatically install if they're not already installed. Example: { "stylua" }
-- This setting has no relation with the `automatic_installation` setting.
ensure_installed = {},

-- NOTE: this is left here for future porting in case needed
-- Whether sources that are set up (via null-ls) should be automatically installed if they're not already installed.
-- This setting has no relation with the `ensure_installed` setting.
Expand All @@ -15,7 +18,6 @@ local DEFAULT_SETTINGS = {
-- - { exclude: string[] }: All servers set up via mason-null-ls, except the ones provided in the list, are automatically installed.
-- Example: automatic_installation = { exclude = { "stylua", "eslint", } }
automatic_installation = false,

-- Whether sources that are installed in mason should be automatically set up in null-ls.
-- Removes the need to set up null-ls manually.
-- Can either be:
Expand All @@ -24,6 +26,7 @@ local DEFAULT_SETTINGS = {
-- - { types = { SOURCE_NAME = {TYPES} } }. Allows overriding default configuration.
-- Ex: { types = { eslint_d = {'formatting'} } }
automatic_setup = false,
handlers = nil,
}

M._DEFAULT_SETTINGS = DEFAULT_SETTINGS
Expand All @@ -40,6 +43,7 @@ function M.set(opts)
ensure_installed = { M.current.ensure_installed, 'table', true },
automatic_installation = { M.current.automatic_installation, { 'boolean', 'table' }, true },
automatic_setup = { M.current.automatic_setup, { 'boolean', 'table' }, true },
handlers = { M.current.handlers, { 'table' }, true },
})
end

Expand Down

0 comments on commit 35ce897

Please sign in to comment.