forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu-sort-helpers.js
184 lines (164 loc) · 4.77 KB
/
menu-sort-helpers.js
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
// UTILS
function splitArray(arr, predicate) {
let lastArr = [];
const multiArr = [lastArr];
arr.forEach(item => {
if (predicate(item)) {
if (lastArr.length > 0) {
lastArr = [];
multiArr.push(lastArr);
}
} else {
lastArr.push(item);
}
});
return multiArr;
}
function joinArrays(arrays, joiner) {
const joinedArr = [];
arrays.forEach((arr, i) => {
if (i > 0 && arr.length > 0) {
joinedArr.push(joiner);
}
joinedArr.push(...arr);
});
return joinedArr;
}
const pushOntoMultiMap = (map, key, value) => {
if (!map.has(key)) {
map.set(key, []);
}
map.get(key).push(value);
};
function indexOfGroupContainingCommand(groups, command, ignoreGroup) {
return groups.findIndex(
candiateGroup =>
candiateGroup !== ignoreGroup &&
candiateGroup.some(candidateItem => candidateItem.command === command)
);
}
// Sort nodes topologically using a depth-first approach. Encountered cycles
// are broken.
function sortTopologically(originalOrder, edgesById) {
const sorted = [];
const marked = new Set();
function visit(id) {
if (marked.has(id)) {
// Either this node has already been placed, or we have encountered a
// cycle and need to exit.
return;
}
marked.add(id);
const edges = edgesById.get(id);
if (edges != null) {
edges.forEach(visit);
}
sorted.push(id);
}
originalOrder.forEach(visit);
return sorted;
}
function attemptToMergeAGroup(groups) {
for (let i = 0; i < groups.length; i++) {
const group = groups[i];
for (const item of group) {
const toCommands = [...(item.before || []), ...(item.after || [])];
for (const command of toCommands) {
const index = indexOfGroupContainingCommand(groups, command, group);
if (index === -1) {
// No valid edge for this command
continue;
}
const mergeTarget = groups[index];
// Merge with group containing `command`
mergeTarget.push(...group);
groups.splice(i, 1);
return true;
}
}
}
return false;
}
// Merge groups based on before/after positions
// Mutates both the array of groups, and the individual group arrays.
function mergeGroups(groups) {
let mergedAGroup = true;
while (mergedAGroup) {
mergedAGroup = attemptToMergeAGroup(groups);
}
return groups;
}
function sortItemsInGroup(group) {
const originalOrder = group.map((node, i) => i);
const edges = new Map();
const commandToIndex = new Map(group.map((item, i) => [item.command, i]));
group.forEach((item, i) => {
if (item.before) {
item.before.forEach(toCommand => {
const to = commandToIndex.get(toCommand);
if (to != null) {
pushOntoMultiMap(edges, to, i);
}
});
}
if (item.after) {
item.after.forEach(toCommand => {
const to = commandToIndex.get(toCommand);
if (to != null) {
pushOntoMultiMap(edges, i, to);
}
});
}
});
const sortedNodes = sortTopologically(originalOrder, edges);
return sortedNodes.map(i => group[i]);
}
function findEdgesInGroup(groups, i, edges) {
const group = groups[i];
for (const item of group) {
if (item.beforeGroupContaining) {
for (const command of item.beforeGroupContaining) {
const to = indexOfGroupContainingCommand(groups, command, group);
if (to !== -1) {
pushOntoMultiMap(edges, to, i);
return;
}
}
}
if (item.afterGroupContaining) {
for (const command of item.afterGroupContaining) {
const to = indexOfGroupContainingCommand(groups, command, group);
if (to !== -1) {
pushOntoMultiMap(edges, i, to);
return;
}
}
}
}
}
function sortGroups(groups) {
const originalOrder = groups.map((item, i) => i);
const edges = new Map();
for (let i = 0; i < groups.length; i++) {
findEdgesInGroup(groups, i, edges);
}
const sortedGroupIndexes = sortTopologically(originalOrder, edges);
return sortedGroupIndexes.map(i => groups[i]);
}
function isSeparator(item) {
return item.type === 'separator';
}
function sortMenuItems(menuItems) {
// Split the items into their implicit groups based upon separators.
const groups = splitArray(menuItems, isSeparator);
// Merge groups that contain before/after references to eachother.
const mergedGroups = mergeGroups(groups);
// Sort each individual group internally.
const mergedGroupsWithSortedItems = mergedGroups.map(sortItemsInGroup);
// Sort the groups based upon their beforeGroupContaining/afterGroupContaining
// references.
const sortedGroups = sortGroups(mergedGroupsWithSortedItems);
// Join the groups back
return joinArrays(sortedGroups, { type: 'separator' });
}
module.exports = { sortMenuItems };