Skip to content

Commit

Permalink
feat!: removing setup_handlers function. automatic_setup is now imp…
Browse files Browse the repository at this point in the history
…licitly true. (#59)

Migration Guide:

1. Move `setup_handlers` table to `setup({handlers = TABLE})`.
2. To disable `automatic_setup`, provide an empty function to `handlers`
   like `handlers = {function() end,}`
  • Loading branch information
jay-babu authored Apr 9, 2023
1 parent a991e76 commit a0c4dc1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 73 deletions.
60 changes: 16 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ Please read the whole README.md before jumping to [Setup](#setup).

# Installation

## [Packer](https://github.com/wbthomason/packer.nvim)

```lua
use {
"williamboman/mason.nvim",
"jose-elias-alvarez/null-ls.nvim",
"jay-babu/mason-null-ls.nvim",
}
```

## [lazy.nvim](https://github.com/folke/lazy.nvim)

```lua
Expand Down Expand Up @@ -128,15 +118,6 @@ local DEFAULT_SETTINGS = {
-- Can also be an exclusion list.
-- Example: `automatic_installation = { exclude = { "rust_analyzer", "solargraph" } }`
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:
-- - false: Null-ls is not automatically registered.
-- - true: Null-ls is automatically registered.
-- - { types = { SOURCE_NAME = {TYPES} } }. Allows overriding default configuration.
-- Ex: { types = { eslint_d = {'formatting'} } }
automatic_setup = false,
}
```

Expand All @@ -156,36 +137,29 @@ require("mason-null-ls").setup({

See the Default Configuration section to understand how the default dap configs can be overridden.

# Setup handlers usage
# Handlers usage

The `setup_handlers()` function provides a dynamic way of setting up sources and any other needed logic, It can also do that during runtime.
The `handlers` table provides a dynamic way of setting up sources and any other needed logic, It can also do that during runtime.

**NOTE:** When setting `automatic_setup = true`, the handler function needs to be called at a minimum like:
`require 'mason-null-ls'.setup_handlers()`. When passing in a custom handler function for the the default or a source,
then the automatic_setup function one won't be invoked. See below to keep original functionality inside the custom handler.
Providing an empty `handlers` will cause all sources to be automatically registered in `null-ls`. See below example on how to disable.

```lua
local null_ls = require 'null-ls'
null_ls.setup()

require ('mason-null-ls').setup({
ensure_installed = {'stylua', 'jq'}
ensure_installed = {'stylua', 'jq'},
handlers = {
function() end, -- disables automatic setup of all null-ls sources
stylua = function(source_name, methods)
null_ls.register(null_ls.builtins.formatting.stylua)
end,
shfmt = function(source_name, methods)
-- custom logic
require('mason-null-ls').default_setup(source_name, methods) -- to maintain default behavior
end,
},
})

require 'mason-null-ls'.setup_handlers {
function(source_name, methods)
-- all sources with no handler get passed here

-- To keep the original functionality of `automatic_setup = true`,
-- please add the below.
require("mason-null-ls.automatic_setup")(source_name, methods)
end,
stylua = function(source_name, methods)
null_ls.register(null_ls.builtins.formatting.stylua)
end,
}

-- will setup any installed and configured sources above
null_ls.setup()
```

# Setup
Expand All @@ -203,16 +177,14 @@ require("mason-null-ls").setup({
-- Opt to list sources here, when available in mason.
},
automatic_installation = false,
automatic_setup = true, -- Recommended, but optional
handlers = {},
})
require("null-ls").setup({
sources = {
-- Anything not supported by mason.
}
})

require 'mason-null-ls'.setup_handlers() -- If `automatic_setup` is true.

Note: This is my personal preference.
```

Expand Down
18 changes: 3 additions & 15 deletions lua/mason-null-ls/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ local M = {}
-- Currently this only needs to be evaluated for the same list passed in.
-- @param source string
-- @param types string[]
local default_setup = function(source, types)
local settings = require('mason-null-ls.settings')
if settings.current.automatic_setup then
local user_types = settings.current.automatic_setup.types or {}
local user_source_types = user_types[source]
require('mason-null-ls.automatic_setup')(source, user_source_types or types)
end
M.default_setup = function(source, types)
require('mason-null-ls.automatic_setup')(source, types)
end

---@param handlers table<string, fun(source_name: string, methods: string[])> | nil
Expand All @@ -25,7 +20,7 @@ local function setup_handlers(handlers)
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)))
local default_handler = Optional.of_nilable(handlers[1]):or_(_.always(Optional.of_nilable(M.default_setup)))

_.each(function(handler)
if type(handler) == 'string' and not source_mappings.getPackageFromNullLs(handler) then
Expand Down Expand Up @@ -106,13 +101,6 @@ function M.setup(config)
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
14 changes: 0 additions & 14 deletions lua/mason-null-ls/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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 = {
Expand All @@ -18,14 +17,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:
-- - false: Null-ls is not automatically registered.
-- - true: Null-ls is automatically registered.
-- - { types = { SOURCE_NAME = {TYPES} } }. Allows overriding default configuration.
-- Ex: { types = { eslint_d = {'formatting'} } }
automatic_setup = false,
handlers = nil,
}

Expand All @@ -34,15 +25,10 @@ M.current = M._DEFAULT_SETTINGS

---@param opts MasonNullLsSettings
function M.set(opts)
if opts.automatic_setup == true then
opts.automatic_setup = {}
end

M.current = vim.tbl_deep_extend('force', M.current, opts)
vim.validate({
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 a0c4dc1

Please sign in to comment.