Skip to content

Commit 28d0d54

Browse files
committed
feat: basic config validation
1 parent 3b79d9d commit 28d0d54

File tree

8 files changed

+99
-22
lines changed

8 files changed

+99
-22
lines changed

.stylua.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
column_width = 100
1+
column_width = 110
22
line_endings = "Unix"
33
indent_type = "Spaces"
44
indent_width = 4

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ request](https://github.com/igorlfs/nvim-dap-view/issues/new?assignees=&labels=e
245245
for the inspiration for handling breakpoint exceptions;
246246
- [Kulala](https://github.com/mistweaverco/kulala.nvim) for the creative usage
247247
of neovim's `'winbar'` to handle multiple views.
248+
- [blink.cmp](https://github.com/Saghen/blink.cmp/blob/main/lua/blink/cmp/config/utils.lua) for the config validation (which is partialy taken from a PR to [indent-blankline](https://github.com/lukas-reineke/indent-blankline.nvim/pull/934/files#diff-09ebcaa8c75cd1e92d25640e377ab261cfecaf8351c9689173fd36c2d0c23d94R16))
248249

249250
[^1]: Filters depend on the debug adapter's capabilities
250251
[^2]: From treesitter and extmarks (e.g., semantic highlighting from LSP)

lua/dap-view/setup.lua

Lines changed: 0 additions & 21 deletions
This file was deleted.

lua/dap-view/setup/init.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
local M = {}
2+
3+
M.config = require("dap-view.config").config
4+
5+
---@param config Config?
6+
M.setup = function(config)
7+
M.config = vim.tbl_deep_extend("force", M.config, config or {})
8+
9+
require("dap-view.setup.validate").validate(M.config)
10+
end
11+
12+
return M
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
local M = {}
2+
3+
---@param config Config
4+
function M.validate(config)
5+
require("dap-view.setup.validate.util").validate("config", {
6+
windows = { config.windows, "table" },
7+
winbar = { config.winbar, "table" },
8+
}, config)
9+
10+
require("dap-view.setup.validate.winbar").validate(config.winbar)
11+
require("dap-view.setup.validate.windows").validate(config.windows)
12+
end
13+
14+
return M
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
local M = {}
2+
3+
-- Code taken from @MariaSolOs in a indent-blankline.nvim PR:
4+
-- https://github.com/lukas-reineke/indent-blankline.nvim/pull/934/files#diff-09ebcaa8c75cd1e92d25640e377ab261cfecaf8351c9689173fd36c2d0c23d94R16
5+
6+
--- @param spec table<string, {[1]:any, [2]:function|string, [3]:string|true|nil}>
7+
--- NOTE: We disable some Lua diagnostics here since lua_ls isn't smart enough to
8+
--- realize that we're using an overloaded function.
9+
local _validate = function(spec)
10+
for key, key_spec in pairs(spec) do
11+
local message = type(key_spec[3]) == "string" and key_spec[3] or nil --[[@as string?]]
12+
local optional = type(key_spec[3]) == "boolean" and key_spec[3] or nil --[[@as boolean?]]
13+
---@diagnostic disable-next-line:param-type-mismatch, redundant-parameter
14+
vim.validate(key, key_spec[1], key_spec[2], optional, message)
15+
end
16+
end
17+
18+
--- @param path string The path to the field being validated
19+
--- @param tbl table The table to validate
20+
--- @param source table The original table that we're validating against
21+
--- @see vim.validate
22+
function M.validate(path, tbl, source)
23+
-- Validate
24+
local _, err = pcall(_validate, tbl)
25+
if err then
26+
error(path .. "." .. err)
27+
end
28+
29+
-- Check for erroneous fields
30+
for k, _ in pairs(source) do
31+
if tbl[k] == nil then
32+
error(path .. "." .. k .. ": unexpected field found in configuration")
33+
end
34+
end
35+
end
36+
37+
return M
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local M = {}
2+
3+
---@param config WinbarConfig
4+
function M.validate(config)
5+
require("dap-view.setup.validate.util").validate("winbar", {
6+
show = { config.show, "boolean" },
7+
sections = { config.sections, "table" },
8+
default_section = { config.default_section, "string" },
9+
}, config)
10+
11+
local sections = config.sections
12+
local default = config.default_section
13+
14+
-- Also check the "semantics"
15+
if #sections == 0 then
16+
error("There must be at least one section")
17+
end
18+
if not vim.tbl_contains(sections, default) then
19+
local pretty_sections = vim.inspect(sections)
20+
error("Default section (" .. default .. ") not listed as one of the sections " .. pretty_sections)
21+
end
22+
end
23+
24+
return M
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local M = {}
2+
3+
---@param config WindowsConfig
4+
function M.validate(config)
5+
require("dap-view.setup.validate.util").validate("windows", {
6+
height = { config.height, "number" },
7+
}, config)
8+
end
9+
10+
return M

0 commit comments

Comments
 (0)