Skip to content

Commit

Permalink
refactor: clean up feature toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
saecki committed Jun 30, 2024
1 parent 171cd0e commit b5228a0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
8 changes: 2 additions & 6 deletions lua/crates/edit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,10 @@ end

---@param buf integer
---@param crate TomlCrate
---@param feature ApiFeature
---@param feature string
---@return Span
function M.enable_feature(buf, crate, feature)
local name = feature.name
if feature.dep then
name = string.sub(name, 5)
end
local t = '"' .. name .. '"'
local t = '"' .. feature .. '"'

if crate.feat then
local last_feat = crate.feat.items[#crate.feat.items]
Expand Down
10 changes: 6 additions & 4 deletions lua/crates/popup/features.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ local function toggle_feature(ctx, line)
local parent_name = string.sub(feat_name, 5)
local parent_feat = features.map[parent_name]

if parent_feat and vim.tbl_contains(parent_feat.members, feat_name) then
if not parent_feat then
-- no direct explicit parent feature, so just toggle the implicit feature
elseif vim.tbl_contains(parent_feat.members, feat_name) then
if #parent_feat.members > 1 then
util.notify(vim.log.levels.INFO, "Cannot enable/disable '%s' directly; instead toggle its parent feature '%s'", feat_name, parent_name)
return
else
-- the explicit parent feature only contains the dependency feature, so toggle it instead
end
elseif not parent_feat then
-- no direct explicit parent feature, so just toggle the implicit feature
else
-- the parent feature named like the dependency, doesn't include the `dep:` feature,
-- so find other features, that include it.
Expand Down Expand Up @@ -103,7 +105,7 @@ local function toggle_feature(ctx, line)
if crate_feature then
line_span = edit.disable_feature(ctx.buf, ctx.crate, crate_feature)
else
line_span = edit.enable_feature(ctx.buf, ctx.crate, selected_feature)
line_span = edit.enable_feature(ctx.buf, ctx.crate, feat_name)
end
end

Expand Down

0 comments on commit b5228a0

Please sign in to comment.