Skip to content

Commit b5228a0

Browse files
committed
refactor: clean up feature toggling
1 parent 171cd0e commit b5228a0

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

lua/crates/edit.lua

+2-6
Original file line numberDiff line numberDiff line change
@@ -376,14 +376,10 @@ end
376376

377377
---@param buf integer
378378
---@param crate TomlCrate
379-
---@param feature ApiFeature
379+
---@param feature string
380380
---@return Span
381381
function M.enable_feature(buf, crate, feature)
382-
local name = feature.name
383-
if feature.dep then
384-
name = string.sub(name, 5)
385-
end
386-
local t = '"' .. name .. '"'
382+
local t = '"' .. feature .. '"'
387383

388384
if crate.feat then
389385
local last_feat = crate.feat.items[#crate.feat.items]

lua/crates/popup/features.lua

+6-4
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ local function toggle_feature(ctx, line)
6565
local parent_name = string.sub(feat_name, 5)
6666
local parent_feat = features.map[parent_name]
6767

68-
if parent_feat and vim.tbl_contains(parent_feat.members, feat_name) then
68+
if not parent_feat then
69+
-- no direct explicit parent feature, so just toggle the implicit feature
70+
elseif vim.tbl_contains(parent_feat.members, feat_name) then
6971
if #parent_feat.members > 1 then
7072
util.notify(vim.log.levels.INFO, "Cannot enable/disable '%s' directly; instead toggle its parent feature '%s'", feat_name, parent_name)
7173
return
74+
else
75+
-- the explicit parent feature only contains the dependency feature, so toggle it instead
7276
end
73-
elseif not parent_feat then
74-
-- no direct explicit parent feature, so just toggle the implicit feature
7577
else
7678
-- the parent feature named like the dependency, doesn't include the `dep:` feature,
7779
-- so find other features, that include it.
@@ -103,7 +105,7 @@ local function toggle_feature(ctx, line)
103105
if crate_feature then
104106
line_span = edit.disable_feature(ctx.buf, ctx.crate, crate_feature)
105107
else
106-
line_span = edit.enable_feature(ctx.buf, ctx.crate, selected_feature)
108+
line_span = edit.enable_feature(ctx.buf, ctx.crate, feat_name)
107109
end
108110
end
109111

0 commit comments

Comments
 (0)