@@ -5,7 +5,8 @@ import { AddonConfig, AddonInfo } from "../types/addon";
55import { WebVue } from "../panels/WebVue" ;
66import {
77 applyAddonSettings ,
8- getLibraries ,
8+ getSetting ,
9+ getLibraryPaths ,
910 revokeAddonSettings ,
1011 setSetting ,
1112} from "../services/settings.service" ;
@@ -19,29 +20,18 @@ export class Addon {
1920 readonly name : string ;
2021 readonly uri : vscode . Uri ;
2122
22- #displayName?: string ;
23- /** The description defined in the addon's `config.json`. */
24- #description?: string ;
25- /** The size of the addon in bytes. */
26- #size?: number ;
27- /** Whether or not this addon has a `plugin.lua`. */
28- #hasPlugin?: boolean ;
2923 /** Whether or not this addon is currently processing an operation. */
3024 #processing?: boolean ;
31-
32- /** Whether or not this addon is enabled. */
33- #enabled?: boolean ;
34- /** A unix timestamp (milliseconds) of when this addon was installed. */
35- #installTimestamp?: number ;
36- /** Whether or not this addon has an update available from GitHub. */
25+ /** The workspace folders that this addon is enabled in. */
26+ #enabled?: boolean [ ] ;
27+ /** Whether or not this addon has an update available from git. */
3728 #hasUpdate?: boolean ;
38- /** The settings to apply when this addon is enabled. */
39- #settings?: Record < string , unknown > ;
4029
4130 constructor ( name : string , path : vscode . Uri ) {
4231 this . name = name ;
4332 this . uri = path ;
4433
34+ this . #enabled = [ ] ;
4535 this . #hasUpdate = false ;
4636 }
4737
@@ -51,11 +41,6 @@ export class Addon {
5141 const rawInfo = await filesystem . readFile ( path ) ;
5242 const info = JSON . parse ( rawInfo ) as AddonInfo ;
5343
54- this . #displayName = info . name ;
55- this . #description = info . description ;
56- this . #size = info . size ;
57- this . #hasPlugin = info . hasPlugin ;
58-
5944 return {
6045 name : info . name ,
6146 description : info . description ,
@@ -64,6 +49,7 @@ export class Addon {
6449 } ;
6550 }
6651
52+ /** Get the `config.json` for this addon. */
6753 public async getConfig ( ) {
6854 const configURI = vscode . Uri . joinPath (
6955 this . uri ,
@@ -83,31 +69,56 @@ export class Addon {
8369 }
8470 }
8571
72+ /** Update this addon using git. */
8673 public async update ( ) {
8774 const path = this . uri . path . substring ( 1 ) ;
8875 return git
8976 . submoduleUpdate ( [ path ] )
9077 . then ( ( message ) => localLogger . debug ( message ) ) ;
9178 }
9279
93- public async getIsEnabled ( libraryPaths ?: string [ ] ) {
80+ /** Check whether this addon is enabled, given an array of enabled library paths.
81+ * @param libraryPaths An array of paths from the `Lua.workspace.library` setting.
82+ */
83+ public checkIfEnabled ( libraryPaths : string [ ] ) {
9484 const regex = new RegExp (
9585 `/sumneko.lua/addonManager/addons/${ this . name } ` ,
9686 "g"
9787 ) ;
9888
99- if ( ! libraryPaths ) libraryPaths = await getLibraries ( ) ;
10089 const index = libraryPaths . findIndex ( ( path ) => regex . test ( path ) ) ;
10190 return index !== - 1 ;
10291 }
10392
104- public async enable ( ) {
105- const libraryPaths = await getLibraries ( ) ;
93+ /** Get the enabled state for this addon in all opened workspace folders */
94+ public async getEnabled ( ) {
95+ const folders = await getLibraryPaths ( ) ;
96+
97+ // Check all workspace folders for a path that matches this addon
98+ const folderStates = folders . map ( ( entry ) => {
99+ return {
100+ folder : entry . folder ,
101+ enabled : this . checkIfEnabled ( entry . paths ) ,
102+ } ;
103+ } ) ;
104+
105+ folderStates . forEach (
106+ ( entry ) => ( this . #enabled[ entry . folder . index ] = entry . enabled )
107+ ) ;
106108
107- const enabled = await this . getIsEnabled ( libraryPaths ) ;
109+ return folderStates ;
110+ }
111+
112+ public async enable ( folder : vscode . WorkspaceFolder ) {
113+ const librarySetting = ( await getSetting (
114+ LIBRARY_SETTING ,
115+ folder
116+ ) ) as string [ ] ;
117+
118+ const enabled = await this . checkIfEnabled ( librarySetting ) ;
108119 if ( enabled ) {
109120 localLogger . warn ( `${ this . name } is already enabled` ) ;
110- this . #enabled = true ;
121+ this . #enabled[ folder . index ] = true ;
111122 return ;
112123 }
113124
@@ -129,76 +140,80 @@ export class Addon {
129140 return ;
130141 }
131142
132- // Apply setting for Language Server
143+ // Apply addon settings
133144 const libraryUri = vscode . Uri . joinPath ( this . uri , "module" , "library" ) ;
134145 const libraryPath = libraryUri . path . substring ( 1 ) ;
135- libraryPaths . push ( libraryPath ) ;
146+ librarySetting . push ( libraryPath ) ;
136147
137148 const configValues = await this . getConfig ( ) ;
138149
139150 try {
140- await setSetting ( LIBRARY_SETTING , libraryPaths ) ;
141- await applyAddonSettings ( configValues . settings ) ;
151+ await setSetting ( folder , LIBRARY_SETTING , librarySetting ) ;
152+ if ( configValues . settings )
153+ await applyAddonSettings ( folder , configValues . settings ) ;
142154 } catch ( e ) {
143155 localLogger . warn ( `Failed to apply settings of "${ this . name } "` ) ;
144156 return ;
145157 }
146158
147- this . #enabled = true ;
159+ this . #enabled[ folder . index ] = true ;
148160 localLogger . info ( `Enabled "${ this . name } "` ) ;
149-
150- return this . setLock ( false ) ;
151161 }
152162
153- public async disable ( ) {
154- const libraryPaths = await getLibraries ( ) ;
163+ public async disable ( folder : vscode . WorkspaceFolder ) {
164+ const librarySetting = ( await getSetting (
165+ LIBRARY_SETTING ,
166+ folder
167+ ) ) as string [ ] ;
168+
155169 const regex = new RegExp (
156170 `/sumneko.lua/addonManager/addons/${ this . name } ` ,
157171 "g"
158172 ) ;
159- const index = libraryPaths . findIndex ( ( path ) => regex . test ( path ) ) ;
173+ const index = librarySetting . findIndex ( ( path ) => regex . test ( path ) ) ;
160174
161175 if ( index === - 1 ) {
162176 localLogger . warn ( `"${ this . name } " is already disabled` ) ;
163- this . #enabled = false ;
177+ this . #enabled[ folder . index ] = false ;
164178 return ;
165179 }
166180
167181 // Remove setting for Language Server
168- libraryPaths . splice ( index ) ;
182+ librarySetting . splice ( index ) ;
169183 const configValues = await this . getConfig ( ) ;
170184
171185 // Revoke settings
172186 try {
173- await setSetting ( LIBRARY_SETTING , libraryPaths ) ;
174- await revokeAddonSettings ( configValues . settings ) ;
187+ await setSetting ( folder , LIBRARY_SETTING , librarySetting ) ;
188+ if ( configValues . settings )
189+ await revokeAddonSettings ( folder , configValues . settings ) ;
175190 } catch ( e ) {
176191 localLogger . error ( `Failed to revoke settings of "${ this . name } "` ) ;
177192 return ;
178193 }
179194
180195 // Remove submodule
181- try {
182- const moduleURI = vscode . Uri . joinPath ( this . uri , "module" ) ;
183- await filesystem . deleteFile ( moduleURI , {
184- recursive : true ,
185- useTrash : false ,
186- } ) ;
187- } catch ( e ) {
188- localLogger . error ( `Failed to uninstall "${ this . name } "` ) ;
189- return ;
190- }
191-
192- this . #enabled = false ;
196+ // try {
197+ // const moduleURI = vscode.Uri.joinPath(this.uri, "module");
198+ // await filesystem.deleteFile(moduleURI, {
199+ // recursive: true,
200+ // useTrash: false,
201+ // });
202+ // } catch (e) {
203+ // localLogger.error(`Failed to uninstall "${this.name}"`);
204+ // return;
205+ // }
206+
207+ this . #enabled[ folder . index ] = false ;
193208 localLogger . info ( `Disabled "${ this . name } "` ) ;
194-
195- return this . setLock ( false ) ;
196209 }
197210
198211 /** Convert this addon to an object ready for sending to WebVue. */
199212 public async toJSON ( ) {
213+ await this . getEnabled ( ) ;
214+
200215 const { name, description, size, hasPlugin } = await this . fetchInfo ( ) ;
201- const enabled = await this . getIsEnabled ( ) ;
216+ const enabled = this . #enabled ;
202217 const installTimestamp = ( await git . log ( ) ) . latest . date ;
203218 const hasUpdate = this . #hasUpdate;
204219
0 commit comments