@@ -245,11 +245,16 @@ end
245245--- @field action " merge"
246246--- @field value { [string] : unknown }
247247
248+ --- @class lls-addon.config-entry.remove-deleted-versions
249+ --- @field action " remove-deleted-versions"
250+ --- @field key string
251+
248252--- @alias lls-addon.config-entry
249253--- | lls-addon.config-entry.append
250254--- | lls-addon.config-entry.prepend
251255--- | lls-addon.config-entry.set
252256--- | lls-addon.config-entry.merge
257+ --- | lls-addon.config-entry.remove-deleted-versions
253258
254259--- @class lls-addon.install-entry
255260--- @field type " file" | " directory"
@@ -301,6 +306,11 @@ local function compileLuarc(rockspec, env)
301306 value = formattedLibraryDestination ,
302307 } --[[ @as lls-addon.config-entry.append]] )
303308
309+ table.insert (configEntries , {
310+ action = " remove-deleted-versions" ,
311+ key = " workspace.library" ,
312+ } --[[ @as lls-addon.config-entry.remove-deleted-versions]] )
313+
304314 table.insert (installEntries , {
305315 type = " directory" ,
306316 source = librarySource ,
@@ -338,6 +348,11 @@ local function compileLuarc(rockspec, env)
338348 value = formattedPluginDestination ,
339349 } --[[ @as lls-addon.config-entry.append]] )
340350
351+ table.insert (configEntries , {
352+ action = " remove-deleted-versions" ,
353+ key = " runtime.plugin" ,
354+ } --[[ @as lls-addon.config-entry.remove-deleted-versions]] )
355+
341356 table.insert (installEntries , {
342357 type = " file" ,
343358 source = pluginSource ,
@@ -450,13 +465,13 @@ local function tableFind(list, value)
450465 return nil
451466end
452467
468+ --- @param pointsToWrongVersion fun ( path : string ): boolean
453469--- @param config { [string] : any }
454470--- @param configEntries lls-addon.config-entry[]
455471--- @param luarcType " vscode settings" | " luarc"
456- local function applyConfigEntries (config , configEntries , luarcType )
472+ local function applyConfigEntries (pointsToWrongVersion , config , configEntries , luarcType )
457473 local prefix = " "
458474 local nested --- @type boolean
459- local transformMerge --- @type fun ( config : { [ string] : any }): { [ string] : any }
460475 if luarcType == " vscode settings" then
461476 prefix = " Lua."
462477 nested = false
@@ -487,7 +502,7 @@ local function applyConfigEntries(config, configEntries, luarcType)
487502 local oldValue2_isArray = json .isArray (oldValue2 )
488503 if oldValue1_isArray and oldValue2_isArray then
489504 secondary .set (config , key , nil )
490- extend (nested , oldValue1 , unnest2 (transformMerge ( oldValue2 ) ))
505+ extend (nested , oldValue1 , unnest2 (oldValue2 ))
491506 list = oldValue1
492507 elseif oldValue2_isArray then
493508 list = oldValue2
@@ -517,6 +532,36 @@ local function applyConfigEntries(config, configEntries, luarcType)
517532 else
518533 extend (nested , config , value )
519534 end
535+ elseif action == " remove-deleted-versions" then
536+ --- @cast entry lls-addon.config-entry.remove-deleted-versions
537+ local key = prefix .. entry .key
538+
539+ local list --- @type string[]
540+ do
541+ local oldValue1 = primary .get (config , key )
542+ local oldValue1_isArray = json .isArray (oldValue1 )
543+ local oldValue2 = secondary .get (config , key )
544+ local oldValue2_isArray = json .isArray (oldValue2 )
545+ if oldValue1_isArray and oldValue2_isArray then
546+ secondary .set (config , key , nil )
547+ extend (nested , oldValue1 , unnest2 (oldValue2 ))
548+ list = oldValue1
549+ elseif oldValue2_isArray then
550+ list = oldValue2
551+ elseif oldValue1_isArray then
552+ list = oldValue1
553+ else
554+ list = json .array ({})
555+ primary .set (config , key , list )
556+ end
557+ end
558+
559+ for i = # list , 1 , - 1 do
560+ local v = list [i ]
561+ if pointsToWrongVersion (v ) then
562+ table.remove (list , i )
563+ end
564+ end
520565 elseif action == " set" then
521566 --- @cast entry lls-addon.config-entry.set
522567 local key , value = prefix .. entry .key , entry .value
@@ -538,15 +583,31 @@ local function applyConfigEntries(config, configEntries, luarcType)
538583 end
539584end
540585
586+ --- @param rockspec luarocks.Rockspec
541587--- @param luarcFiles lls-addon.luarc-file[]
542588--- @param configEntries lls-addon.config-entry[]
543- local function installLuarcFiles (luarcFiles , configEntries )
589+ local function installLuarcFiles (rockspec , luarcFiles , configEntries )
590+ local pointsToWrongVersion
591+ do
592+ local projectDir = getProjectDir ()
593+ local packageDir = dir .path (path .rocks_dir (), rockspec .package )
594+ local packageDirLen = string.len (packageDir )
595+ local currentVersionDir = dir .path (packageDir , rockspec .version )
596+ local currentVersionDirLen = string.len (currentVersionDir )
597+
598+ function pointsToWrongVersion (installPath )
599+ local absPath = fs .absolute_name (installPath , projectDir )
600+ return string.sub (absPath , 1 , packageDirLen ) == packageDir
601+ and string.sub (absPath , 1 , currentVersionDirLen ) ~= currentVersionDir
602+ end
603+ end
604+
544605 for _ , luarcFile in ipairs (luarcFiles ) do
545606 local type = luarcFile .type
546607 local path = luarcFile .path
547608 log .info (string.format (" writing to %s: %s" , type , path ))
548609 local oldConfig = readOrCreateLuarc (path )
549- applyConfigEntries (oldConfig , configEntries , type )
610+ applyConfigEntries (pointsToWrongVersion , oldConfig , configEntries , type )
550611 json .write (path , oldConfig , { sortKeys = true })
551612 end
552613end
@@ -577,10 +638,10 @@ local function installAddon(rockspec, env, noInstall)
577638 return
578639 end
579640
580- installLuarcFiles (luarcFiles , configEntries )
581-
582641 -- for copying library, plugin.lua, and config.json
583642 installFiles (installEntries )
643+
644+ installLuarcFiles (rockspec , luarcFiles , configEntries )
584645end
585646M .installAddon = installAddon
586647
0 commit comments