Skip to content

Commit

Permalink
feat: namedEnum parameter type (#217)
Browse files Browse the repository at this point in the history
* Add namedEnum parameter

* fix: remove editorconfig

* refactor: display value in virtual text

* lint: styling and type errors

* fix: error handling for mixed enum types

* Rollback to value default

---------

Co-authored-by: Steven Arcangeli <[email protected]>
  • Loading branch information
eaglesemanation and stevearc authored Oct 6, 2023
1 parent 3258e2a commit c14d9f3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
15 changes: 13 additions & 2 deletions lua/overseer/form/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,24 @@ function Builder:on_cursor_move()
cur[2] = name_end
end
local schema = self.schema[name]
local vtext = {}
if schema.type == "namedEnum" then
local value = schema.choices[text]
if value then
table.insert(vtext, { string.format("[%s] ", value), "Comment" })
end
end
if schema.desc then
table.insert(vtext, { schema.desc, "Comment" })
end
if not vim.tbl_isempty(vtext) then
vim.api.nvim_buf_set_extmark(self.bufnr, ns, cur[1] - 1, 0, {
virt_text = { { schema.desc, "Comment" } },
virt_text = vtext,
})
end
local completion_schema = schema.subtype and schema.subtype or schema
local choices = completion_schema.type == "boolean" and { "true", "false" }
local choices = (completion_schema.type == "boolean" and { "true", "false" })
or (completion_schema.type == "namedEnum" and vim.tbl_keys(completion_schema.choices))
or completion_schema.choices
vim.api.nvim_buf_set_var(0, "overseer_choices", choices)
end
Expand Down
38 changes: 33 additions & 5 deletions lua/overseer/form/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local log = require("overseer.log")
local util = require("overseer.util")
local M = {}

---@alias overseer.Param overseer.StringParam|overseer.BoolParam|overseer.NumberParam|overseer.IntParam|overseer.ListParam|overseer.EnumParam|overseer.OpaqueParam
---@alias overseer.Param overseer.StringParam|overseer.BoolParam|overseer.NumberParam|overseer.IntParam|overseer.ListParam|overseer.EnumParam|overseer.NamedEnumParam|overseer.OpaqueParam

---@class overseer.BaseParam
---@field name? string
Expand All @@ -29,22 +29,27 @@ local M = {}
---@field default? number

---@class overseer.IntParam : overseer.BaseParam
---@field type? "integer"
---@field type "integer"
---@field default? number

---@class overseer.ListParam : overseer.BaseParam
---@field type? "list"
---@field type "list"
---@field subtype? overseer.Param
---@field delimiter? string
---@field default? table

---@class overseer.EnumParam : overseer.BaseParam
---@field type? "enum"
---@field type "enum"
---@field default? string
---@field choices string[]

---@class overseer.NamedEnumParam : overseer.BaseParam
---@field type "namedEnum"
---@field default? string
---@field choices? table<string, string>

---@class overseer.OpaqueParam : overseer.BaseParam
---@field type? "opaque"
---@field type "opaque"
---@field default? any

local default_schema = {
Expand Down Expand Up @@ -89,6 +94,15 @@ M.render_value = function(schema, value)
end
if schema.type == "opaque" then
return "<opaque>"
elseif schema.type == "namedEnum" then
local label
for k, v in pairs(schema.choices) do
if v == value then
label = k
break
end
end
return label or value
elseif type(value) == "table" then
local rendered_values = {}
for _, v in ipairs(value) do
Expand Down Expand Up @@ -120,6 +134,8 @@ local function validate_type(schema, value)
return true
elseif ptype == "enum" then
return vim.tbl_contains(schema.choices, value)
elseif ptype == "namedEnum" then
return vim.tbl_contains(vim.tbl_values(schema.choices), value)
elseif ptype == "list" then
return type(value) == "table" and vim.tbl_islist(value)
elseif ptype == "number" then
Expand Down Expand Up @@ -199,6 +215,18 @@ M.parse_value = function(schema, value)
end
end
return best ~= nil, best
elseif schema.type == "namedEnum" then
local key = "^" .. value:lower()
local best
---@cast schema overseer.NamedEnumParam
for k, v in pairs(schema.choices) do
if k == value then
return true, v
elseif k:lower():match(key) then
best = v
end
end
return best ~= nil, best
elseif schema.type == "number" then
local num = tonumber(value)
if num then
Expand Down
18 changes: 16 additions & 2 deletions lua/overseer/template/vscode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,31 @@ local function extract_params(params, str, inputs)
if schema then
if schema.type == "pickString" then
local choices = {}
local paramType = nil
for _, v in ipairs(schema.options) do
local choiceType
if type(v) == "table" then
table.insert(choices, v.value)
choiceType = "namedEnum"
-- NOTE: There is an assumption that labels are unique
-- VSCode does not seem to require that, but it's too much of a hassle to deal with it
choices[v.label] = v.value
else
choiceType = "enum"
table.insert(choices, v)
end

if paramType == nil or choiceType == paramType then
paramType = choiceType
else
log.error("VS Code task input %s mixes labeled and unlabeled options", name)
break
end
end

params[name] = {
desc = schema.desc,
default = schema.default,
type = "enum",
type = paramType,
choices = choices,
}
elseif schema.type == "promptString" then
Expand Down

0 comments on commit c14d9f3

Please sign in to comment.