-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkspace_registry.js
More file actions
190 lines (157 loc) · 5.31 KB
/
Copy pathworkspace_registry.js
File metadata and controls
190 lines (157 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const { moveArrayItem } = require("./list_reorder");
function normalizeWorkspaceFolderSnapshot(snapshot) {
if (typeof snapshot?.rootPath !== "string" || snapshot.rootPath.length === 0) {
throw new Error("Workspace folder snapshot requires a root path.");
}
return {
rootPath: snapshot.rootPath,
rootName: typeof snapshot.rootName === "string" && snapshot.rootName.length > 0
? snapshot.rootName
: snapshot.rootPath,
entries: Array.isArray(snapshot.entries) ? snapshot.entries.map((entry) => ({ ...entry })) : [],
loadedDirectoryPaths: Array.isArray(snapshot.loadedDirectoryPaths)
? snapshot.loadedDirectoryPaths.filter((directoryPath) => typeof directoryPath === "string")
: [""],
isTruncated: snapshot.isTruncated === true,
lastError: typeof snapshot.lastError === "string" ? snapshot.lastError : ""
};
}
function cloneFolderRecord(folderRecord) {
return {
...folderRecord,
entries: folderRecord.entries.map((entry) => ({ ...entry })),
loadedDirectoryPaths: Array.isArray(folderRecord.loadedDirectoryPaths)
? [...folderRecord.loadedDirectoryPaths]
: [""]
};
}
function serializeWorkspaceRegistry(registry) {
return {
importedFolders: [...registry.importedFolders.values()].map(cloneFolderRecord),
activeFolderId: registry.activeFolderId
};
}
function createWorkspaceRegistry() {
return {
importedFolders: new Map(),
activeFolderId: null,
nextFolderNumber: 0
};
}
function getWorkspaceFolder(registry, folderId) {
return registry.importedFolders.get(folderId) ?? null;
}
function createWorkspaceFolderRecord(registry, snapshot) {
registry.nextFolderNumber += 1;
return {
id: `workspace-folder-${registry.nextFolderNumber}`,
...normalizeWorkspaceFolderSnapshot(snapshot)
};
}
function importWorkspaceFolder(registry, snapshot) {
const normalizedSnapshot = normalizeWorkspaceFolderSnapshot(snapshot);
for (const folderRecord of registry.importedFolders.values()) {
if (folderRecord.rootPath === normalizedSnapshot.rootPath) {
registry.activeFolderId = folderRecord.id;
return {
folderId: folderRecord.id,
deduplicated: true,
state: serializeWorkspaceRegistry(registry)
};
}
}
const folderRecord = createWorkspaceFolderRecord(registry, normalizedSnapshot);
registry.importedFolders.set(folderRecord.id, folderRecord);
registry.activeFolderId = folderRecord.id;
return {
folderId: folderRecord.id,
deduplicated: false,
state: serializeWorkspaceRegistry(registry)
};
}
function activateWorkspaceFolder(registry, folderId) {
if (!registry.importedFolders.has(folderId)) {
throw new Error("Workspace folder not found.");
}
registry.activeFolderId = folderId;
return {
folderId,
state: serializeWorkspaceRegistry(registry)
};
}
function getNextActiveFolderIdAfterRemoval(registry, folderId) {
const folderIds = [...registry.importedFolders.keys()];
const currentIndex = folderIds.indexOf(folderId);
if (currentIndex === -1) {
return registry.activeFolderId;
}
return folderIds[currentIndex + 1] ?? folderIds[currentIndex - 1] ?? null;
}
function removeWorkspaceFolder(registry, folderId) {
if (!registry.importedFolders.has(folderId)) {
throw new Error("Workspace folder not found.");
}
const nextActiveFolderId = registry.activeFolderId === folderId
? getNextActiveFolderIdAfterRemoval(registry, folderId)
: registry.activeFolderId;
registry.importedFolders.delete(folderId);
registry.activeFolderId = nextActiveFolderId;
return {
removedFolderId: folderId,
state: serializeWorkspaceRegistry(registry)
};
}
function updateWorkspaceFolderSnapshot(registry, folderId, snapshot) {
const currentFolder = registry.importedFolders.get(folderId);
if (currentFolder == null) {
throw new Error("Workspace folder not found.");
}
const normalizedSnapshot = normalizeWorkspaceFolderSnapshot(snapshot);
registry.importedFolders.set(folderId, {
...currentFolder,
...normalizedSnapshot,
id: folderId
});
return {
folderId,
state: serializeWorkspaceRegistry(registry)
};
}
function reorderWorkspaceFolder(registry, folderId, targetIndex) {
const folderEntries = [...registry.importedFolders.entries()];
const sourceIndex = folderEntries.findIndex(([currentFolderId]) => currentFolderId === folderId);
if (sourceIndex < 0) {
throw new Error("Workspace folder not found.");
}
const reorderedEntries = moveArrayItem(folderEntries, sourceIndex, targetIndex);
registry.importedFolders = new Map(reorderedEntries);
return {
folderId,
state: serializeWorkspaceRegistry(registry)
};
}
function setWorkspaceFolderError(registry, folderId, errorMessage) {
const currentFolder = registry.importedFolders.get(folderId);
if (currentFolder == null) {
throw new Error("Workspace folder not found.");
}
registry.importedFolders.set(folderId, {
...currentFolder,
lastError: typeof errorMessage === "string" ? errorMessage : ""
});
return {
folderId,
state: serializeWorkspaceRegistry(registry)
};
}
module.exports = {
activateWorkspaceFolder,
createWorkspaceRegistry,
getWorkspaceFolder,
importWorkspaceFolder,
reorderWorkspaceFolder,
removeWorkspaceFolder,
serializeWorkspaceRegistry,
setWorkspaceFolderError,
updateWorkspaceFolderSnapshot
};