Skip to content

Commit

Permalink
feat!: 2.0 (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-babu authored Apr 9, 2023
1 parent aa7f489 commit 785265d
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 150 deletions.
102 changes: 34 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,8 @@ local DEFAULT_SETTINGS = {
-- Example: automatic_installation = { exclude = { "python", "delve" } }
automatic_installation = false,

-- Whether adapters that are installed in mason should be automatically set up in dap.
-- Removes the need to set up dap manually.
-- See mappings.adapters and mappings.configurations for settings.
-- Must invoke when set to true: `require 'mason-nvim-dap'.setup_handlers()`
-- Can either be:
-- - false: Dap is not automatically configured.
-- - true: Dap is automatically configured.
-- - {adapters: {ADAPTER: {}, }, configurations: {configuration: {}, }, filetypes: {filetype: {}, }}. Allows overriding default configuration.
-- - {adapters: function(default), configurations: function(default), filetypes: function(default), }. Allows modifying the default configuration passed in via function.
automatic_setup = false,
-- See below on usage
handlers = nil,
}
```

Expand All @@ -147,73 +139,47 @@ require("mason").setup()
require("mason-nvim-dap").setup({
automatic_setup = true,
})
require 'mason-nvim-dap'.setup_handlers {}
```

### Overriding Default Settings

```lua
require("mason").setup()
require("mason-nvim-dap").setup({
automatic_setup = {
-- modifies the default configurations table
-- pass in a function or a list to override with
-- the same can be done for adapters and filetypes
configurations = function(default)
default.php[1].port = 9003

return default
end,
}
})
require 'mason-nvim-dap'.setup_handlers {}
```

See the Default Configuration section to understand how the default dap configs can be overriden.
# Handlers usage

# Setup handlers usage
The `handlers` table provides a dynamic way of setting up sources and any other needed logic, It can also do that during runtime.

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 config table passed to each hander contains the below items.
```lua
local config = {
name -- adapter name

**NOTE:** When setting `automatic_setup = true`, the handler function needs to be called at a minimum like:
`require 'mason-nvim-dap'.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.
-- All the items below are looked up by the adapter name.
adapters -- https://github.com/jay-babu/mason-nvim-dap.nvim/blob/main/lua/mason-nvim-dap/mappings/adapters.lua
configurations -- https://github.com/jay-babu/mason-nvim-dap.nvim/blob/main/lua/mason-nvim-dap/mappings/configurations.lua
filetypes -- https://github.com/jay-babu/mason-nvim-dap.nvim/blob/main/lua/mason-nvim-dap/mappings/filetypes.lua
}
```

```lua
local dap = require("dap")

require ('mason-nvim-dap').setup({
ensure_installed = {'stylua', 'jq'}
})
ensure_installed = {'stylua', 'jq'},
handlers = {
function(config)
-- all sources with no handler get passed here

require 'mason-nvim-dap'.setup_handlers {
function(source_name)
-- all sources with no handler get passed here


-- Keep original functionality of `automatic_setup = true`
require('mason-nvim-dap.automatic_setup')(source_name)
end,
python = function(source_name)
dap.adapters.python = {
type = "executable",
command = "/usr/bin/python3",
args = {
"-m",
"debugpy.adapter",
},
}

dap.configurations.python = {
{
type = "python",
request = "launch",
name = "Launch file",
program = "${file}", -- This configuration will launch the current file if used.
},
}
end,
}
-- Keep original functionality
require('mason-nvim-dap').default_setup(config)
end,
python = function(config)
config.adapters = {
type = "executable",
command = "/usr/bin/python3",
args = {
"-m",
"debugpy.adapter",
},
}
require('mason-nvim-dap').default_setup(config)
end,
},
})
```

# Available Dap Adapters
Expand Down
16 changes: 7 additions & 9 deletions lua/mason-nvim-dap/automatic_setup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ local Optional = require('mason-core.optional')
local _ = require('mason-core.functional')

-- @param adapter string
return _.memoize(function(adapter)
local adapters = require('mason-nvim-dap.mappings.adapters')
local filetypes = require('mason-nvim-dap.mappings.filetypes').adapter_to_configs
local configurations = require('mason-nvim-dap.mappings.configurations')

return _.memoize(function(config)
local dap = require('dap')
Optional.of_nilable(adapters[adapter]):map(function(adapter_config)
dap.adapters[adapter] = adapter_config
local configuration = configurations[adapter] or {}

Optional.of_nilable(config.adapters):map(function(adapter_config)
dap.adapters[config.name] = adapter_config
local configuration = config.configurations or {}
if not vim.tbl_isempty(configuration) then
for _, filetype in ipairs(filetypes[adapter]) do
for _, filetype in ipairs(config.filetypes) do
dap.configurations[filetype] = vim.list_extend(dap.configurations[filetype] or {}, configuration)
end
end
end)
return config
end)
10 changes: 9 additions & 1 deletion lua/mason-nvim-dap/ensure_installed.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ local function resolve_package(nvim_dap_adapter_name)
end)
end

return function()
local function ensure_installed()
local Package = require('mason-core.package')
for _, source_identifier in ipairs(settings.current.ensure_installed) do
local source_name, version = Package.Parse(source_identifier)
Expand All @@ -37,3 +37,11 @@ return function()
)
end
end

if registry.refresh then
return function()
registry.refresh(vim.schedule_wrap(ensure_installed))
end
else
return ensure_installed
end
103 changes: 57 additions & 46 deletions lua/mason-nvim-dap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,14 @@ local _ = require('mason-core.functional')
local M = {}

-- Currently this only needs to be evaluated for the same list passed in.
-- @param adapters string
local default_setup = function(adapter)
local settings = require('mason-nvim-dap.settings')
if settings.current.automatic_setup then
require('mason-nvim-dap.automatic_setup')(adapter)
end
--- @param config table
M.default_setup = function(config)
return require('mason-nvim-dap.automatic_setup')(config)
end

function M.setup(config)
local settings = require('mason-nvim-dap.settings')

if config then
settings.set(config)
end

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

if settings.current.automatic_installation then
require('mason-nvim-dap.automatic_installation')()
end

require('mason-nvim-dap.api.command')
end

---@return string[]
function M.get_installed_sources()
local Optional = require('mason-core.optional')
local registry = require('mason-registry')
local source_mappings = require('mason-nvim-dap.mappings.source')

return _.filter_map(function(pkg_name)
return Optional.of_nilable(source_mappings.package_to_nvim_dap[pkg_name])
end, registry.get_installed_package_names())
end

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-nvim-dap.mappings.source')
Expand All @@ -64,13 +34,24 @@ function M.setup_handlers(handlers)

local installed_sources = _.filter_map(get_source_name, registry.get_installed_package_names())

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)))

local function call_handler(adapter_name)
log.fmt_trace('Checking handler for %s', adapter_name)
Optional.of_nilable(handlers[adapter_name]):or_(_.always(default_handler)):if_present(function(handler)
log.fmt_trace('Calling handler for %s', adapter_name)
local adapters = require('mason-nvim-dap.mappings.adapters')
local filetypes = require('mason-nvim-dap.mappings.filetypes')
local configurations = require('mason-nvim-dap.mappings.configurations')

local config = {
name = adapter_name,
adapters = adapters[adapter_name],
configurations = configurations[adapter_name],
filetypes = filetypes[adapter_name],
}

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)
local ok, err = pcall(handler, source_name)
local ok, err = pcall(handler, config)
if not ok then
vim.notify(err, vim.log.levels.ERROR)
end
Expand All @@ -86,6 +67,40 @@ function M.setup_handlers(handlers)
)
end

---@param config MasonNvimDapSettings | nil
function M.setup(config)
local settings = require('mason-nvim-dap.settings')

if config then
settings.set(config)
end

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

if settings.current.automatic_installation then
require('mason-nvim-dap.automatic_installation')()
end

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

require('mason-nvim-dap.api.command')
end

---@return string[]
function M.get_installed_sources()
local Optional = require('mason-core.optional')
local registry = require('mason-registry')
local source_mappings = require('mason-nvim-dap.mappings.source')

return _.filter_map(function(pkg_name)
return Optional.of_nilable(source_mappings.package_to_nvim_dap[pkg_name])
end, registry.get_installed_package_names())
end

---Get a list of available sources in mason-registry
---@param filter { filetype: string | string[] }?: (optional) Used to filter the list of source names.
--- The available keys are
Expand All @@ -98,10 +113,6 @@ function M.get_available_sources(filter)
filter = filter or {}
local predicates = {}

-- if filter.filetype then
-- table.insert(predicates, is_source_in_filetype(filter.filetype))
-- end

return _.filter_map(function(pkg_name)
return Optional.of_nilable(source_mappings.package_to_nvim_dap[pkg_name]):map(function(source_name)
if #predicates == 0 or _.all_pass(predicates, source_name) then
Expand Down
2 changes: 0 additions & 2 deletions lua/mason-nvim-dap/mappings/adapters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,4 @@ M.dart = {
args = { 'flutter' },
}

M = require('mason-nvim-dap.internal.overrides.func_or_extend')(settings.current.automatic_setup.adapters or {}, M)

return M
5 changes: 0 additions & 5 deletions lua/mason-nvim-dap/mappings/configurations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,4 @@ M.dart = {
},
}

M = require('mason-nvim-dap.internal.overrides.func_or_extend')(
settings.current.automatic_setup.configurations or {},
M
)

return M
6 changes: 1 addition & 5 deletions lua/mason-nvim-dap/mappings/filetypes.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
local _ = require('mason-core.functional')
local settings = require('mason-nvim-dap.settings')

local M = {}

M.adapter_to_configs = {
local M = {
['bash'] = { 'sh' },
['chrome'] = { 'javascriptreact', 'typescriptreact', 'typescript', 'javascript' },
['codelldb'] = { 'c', 'cpp', 'rust' },
Expand All @@ -19,6 +17,4 @@ M.adapter_to_configs = {
['python'] = { 'python' },
}

M = require('mason-nvim-dap.internal.overrides.func_or_extend')(settings.current.automatic_setup.filetypes or {}, M)

return M
19 changes: 5 additions & 14 deletions lua/mason-nvim-dap/settings.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local M = {}

---@class MasonNvimDapSettings
---@field handlers table | nil
---@field ensure_installed table
---@field automatic_installation boolean
local DEFAULT_SETTINGS = {
-- A list of adapters to automatically install if they're not already installed. Example: { "stylua" }
-- This setting has no relation with the `automatic_installation` setting.
Expand All @@ -14,31 +17,19 @@ local DEFAULT_SETTINGS = {
-- - { exclude: string[] }: All adapters set up via mason-nvim-dap, except the ones provided in the list, are automatically installed.
-- Example: automatic_installation = { exclude = { "python", "delve" } }
automatic_installation = false,
-- Whether adapters that are installed in mason should be automatically set up in dap.
-- Removes the need to set up dap manually.
-- See mappings.adapters and mappings.configurations for settings.
-- Can either be:
-- - false: Dap is not automatically configured.
-- - true: Dap is automatically configured.
-- - {adapters: {ADAPTER: {}, }, configurations: {configuration: {}, }, filetypes: {filetype: {}, }}. Allows overriding default configuration.
-- - {adapters: function(default), configurations: function(default), filetypes: function(default), }. Allows modifying the default configuration passed in via function.
automatic_setup = false,
handlers = nil,
}

M._DEFAULT_SETTINGS = DEFAULT_SETTINGS
M.current = M._DEFAULT_SETTINGS

---@param opts MasonNvimDapSettings
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', 'function' }, true },
handlers = { M.current.handlers, { 'table' }, true },
})
end

Expand Down

0 comments on commit 785265d

Please sign in to comment.