Skip to content

Commit 2b7e2cf

Browse files
committed
add: support for multi-folder workspace
1 parent 1808089 commit 2b7e2cf

File tree

5 files changed

+209
-104
lines changed

5 files changed

+209
-104
lines changed
Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
import * as vscode from "vscode";
22
import addonManager from "../services/addonManager.service";
3+
import { createChildLogger } from "../services/logging.service";
34

45
type Message = {
56
data: {
67
name: string;
78
};
89
};
910

11+
const localLogger = createChildLogger("Disable Addon");
12+
1013
export default async (context: vscode.ExtensionContext, message: Message) => {
1114
const addon = addonManager.addons.get(message.data.name);
12-
await addon.disable();
13-
await addon.setLock(false);
15+
const workspaceFolders = vscode.workspace.workspaceFolders;
16+
17+
const folderOptions = await (
18+
await addon.getEnabled()
19+
)
20+
.filter((entry) => entry.enabled === true)
21+
.map((entry) => {
22+
return {
23+
label: entry.folder.name,
24+
detail: entry.folder.uri.path,
25+
};
26+
});
27+
28+
try {
29+
if (workspaceFolders.length === 1) {
30+
await addon.disable(workspaceFolders[0]);
31+
} else {
32+
const targetFolders = await vscode.window.showQuickPick(
33+
folderOptions,
34+
{
35+
canPickMany: true,
36+
ignoreFocusOut: true,
37+
title: `Disable ${addon.name} in which folders?`,
38+
}
39+
);
40+
if (!targetFolders) {
41+
localLogger.warn("User did not pick workspace folder");
42+
await addon.setLock(false);
43+
return;
44+
}
45+
for (const target of targetFolders) {
46+
await addon.disable(
47+
workspaceFolders.find(
48+
(folder) => folder.name === target.label
49+
)
50+
);
51+
}
52+
}
53+
} catch (e) {
54+
localLogger.error(e);
55+
return;
56+
} finally {
57+
await addon.setLock(false);
58+
}
1459
};
Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
import * as vscode from "vscode";
22
import addonManager from "../services/addonManager.service";
3+
import { createChildLogger } from "../services/logging.service";
34

45
type Message = {
56
data: {
67
name: string;
78
};
89
};
910

11+
const localLogger = createChildLogger("Enable Addon");
12+
1013
export default async (context: vscode.ExtensionContext, message: Message) => {
1114
const addon = addonManager.addons.get(message.data.name);
12-
await addon.enable();
13-
await addon.setLock(false);
15+
const workspaceFolders = vscode.workspace.workspaceFolders;
16+
17+
const folderOptions = await (
18+
await addon.getEnabled()
19+
)
20+
.filter((entry) => entry.enabled === false)
21+
.map((entry) => {
22+
return {
23+
label: entry.folder.name,
24+
detail: entry.folder.uri.path,
25+
};
26+
});
27+
28+
try {
29+
if (workspaceFolders.length === 1) {
30+
await addon.enable(workspaceFolders[0]);
31+
} else {
32+
const targetFolders = await vscode.window.showQuickPick(
33+
folderOptions,
34+
{
35+
canPickMany: true,
36+
ignoreFocusOut: true,
37+
title: `Enable ${addon.name} in which folders?`,
38+
}
39+
);
40+
if (!targetFolders) {
41+
localLogger.warn("User did not pick workspace folder");
42+
await addon.setLock(false);
43+
return;
44+
}
45+
for (const target of targetFolders) {
46+
await addon.enable(
47+
workspaceFolders.find(
48+
(folder) => folder.name === target.label
49+
)
50+
);
51+
}
52+
}
53+
} catch (e) {
54+
localLogger.error(e);
55+
return;
56+
} finally {
57+
await addon.setLock(false);
58+
}
1459
};

client/src/addon_manager/models/addon.ts

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { AddonConfig, AddonInfo } from "../types/addon";
55
import { WebVue } from "../panels/WebVue";
66
import {
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

client/src/addon_manager/panels/WebVue.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as vscode from "vscode";
22

33
import { createChildLogger } from "../services/logging.service";
44
import { commands } from "../commands";
5-
import { getWorkspace } from "../services/settings.service";
65
import { WebVueMessage } from "../types/webvue";
76
import { DEVELOPMENT_IFRAME_URL } from "../config";
87

@@ -81,7 +80,7 @@ export class WebVue {
8180
WebVue.currentPanel = new WebVue(context, panel);
8281
}
8382

84-
const workspaceOpen = getWorkspace() !== undefined;
83+
const workspaceOpen = vscode.workspace.workspaceFolders.length > 0;
8584
const clientVersion = context.extension.packageJSON.version;
8685

8786
WebVue.sendMessage("appStore", {

0 commit comments

Comments
 (0)