Skip to content

Commit

Permalink
feat: allowing users to pass in function to override instead of simpl…
Browse files Browse the repository at this point in the history
…y a list
  • Loading branch information
jay-babu committed Feb 14, 2023
1 parent 28d87f3 commit c202674
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/stylua.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ jobs:
stylua:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Run Stylua check
uses: JohnnyMorganz/stylua-action@v1.1.1
uses: JohnnyMorganz/stylua-action@v2
with:
# token is needed because the action allegedly downloads binary from github releases
token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
18 changes: 18 additions & 0 deletions lua/mason-nvim-dap/internal/overrides/func_or_extend.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--- Main configuration engine logic for extending a default configuration table with either a function override or a table to merge into the default option

-- @param overrides the override definition, either a table or a function that takes a single parameter of the original table
-- @param default the default configuration table
-- @return the new configuration table
local function func_or_extend(overrides, default)
-- if the override is a table, use vim.tbl_deep_extend
if type(overrides) == 'table' then
local opts = overrides or {}
default = default and vim.tbl_deep_extend('force', default, opts) or opts
-- if the override is a function, call it with the default and overwrite default with the return value
elseif type(overrides) == 'function' then
default = overrides(default)
end
-- return the modified default table
return default
end
return func_or_extend
2 changes: 1 addition & 1 deletion lua/mason-nvim-dap/mappings/adapters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ M.dart = {
args = { 'flutter' },
}

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

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

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

return M
3 changes: 1 addition & 2 deletions lua/mason-nvim-dap/mappings/filetypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ M.adapter_to_configs = {
['python'] = { 'python' },
}

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

return M
4 changes: 1 addition & 3 deletions lua/mason-nvim-dap/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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.
ensure_installed = {},

-- NOTE: this is left here for future porting in case needed
-- Whether adapters that are set up (via dap) should be automatically installed if they're not already installed.
-- This setting has no relation with the `ensure_installed` setting.
Expand All @@ -15,7 +14,6 @@ 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.
Expand All @@ -39,7 +37,7 @@ function M.set(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 },
automatic_setup = { M.current.automatic_setup, { 'boolean', 'table', 'function' }, true },
})
end

Expand Down

0 comments on commit c202674

Please sign in to comment.