Skip to content

Commit

Permalink
fix: adds a preset name to a scope when caching highlight name and in…
Browse files Browse the repository at this point in the history
…troduces restrictions to a preset name. Fixes #14

Now preset name should be validated through this regexp: [a-zA-Z0-9_-]
It had to be introduced before because it was initially planned to put a
preset name into a highlight group value.
  • Loading branch information
rasulomaroff committed Apr 11, 2024
1 parent 228386e commit 0a2cc41
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lua/reactive/snapshot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ local merge_handlers = {
---@param scope string
---@param constraints TriggerConstraints
function M:form_snapshot(preset_name, highlights, scope, constraints)
local opts = { scope = scope, preset_name = preset_name }
local opts = {
scope = string.format('@preset.%s.%s', preset_name, scope),
preset_name = preset_name,
}

Util.each(merge_handlers, function(value)
if not highlights[value] or constraints and constraints[value] then
Expand Down
27 changes: 22 additions & 5 deletions lua/reactive/state.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,29 @@ end

---@param preset Reactive.Preset
function M:add_preset(preset)
vim.validate { name = { preset.name, 'string' } }
vim.validate {
name = {
preset.name,
function(value)
if type(value) ~= 'string' then
return false, 'preset name has to be a string'
end

if preset.name:find ' ' then
vim.notify('reactive.nvim: whitespaces are not allowed in a preset name', vim.log.levels.ERROR)
return
end
local invalid_chars = value:match '[^a-zA-Z0-9_-]+'

if invalid_chars ~= nil then
return false,
[[You've used an invalid character(s) in a preset name.
Allowed: a-z A-Z 0-9 _ -
Whitespace is not allowed as well.
Your invalid characters:]] .. invalid_chars
end

return true
end,
'a valid string',
},
}

preset = Preset:parse(preset)

Expand Down

0 comments on commit 0a2cc41

Please sign in to comment.