Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Section+tag intersection searches #482

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions src/block-command-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,28 @@ interface Result {
document: Document;
root: ViewModelNode;
name: string;
description: string;
}

function intersectByKey<T, K extends keyof T>(lists: T[][], key: K): T[] {
if (lists.length === 0) return [];
if (lists.length === 1) return lists[0];

const keyCount = new Map<T[K], T>();

for (const item of lists[0]) {
keyCount.set(item[key], item);
}

for (let i = 1; i < lists.length; i++) {
const currentKeys = new Set(lists[i].map((item) => item[key]));
for (const [k] of keyCount.entries()) {
if (!currentKeys.has(k)) {
keyCount.delete(k);
}
}
}

return Array.from(keyCount.values());
}

export class BlockCommandBundle implements CommandBundle {
Expand All @@ -29,19 +50,28 @@ export class BlockCommandBundle implements CommandBundle {
const parts = input.split('/');
const constraints: Result[][] = [];
for (let i = 0; i < parts.length; i++) {
const filter = getFilter(parts[i]);
constraints[i] = (
const filters = parts[i].split(/(?=#)/).map(getFilter);
constraints[i] = intersectByKey(
await Promise.all(
names.filter(filter).map(async (name) => {
const blocks = await this.library.findAll(name);
return blocks.map((item) => ({
...item,
name: this.library.metadata.getNames(item.root)[0] ?? name,
description: this.library.metadata.getNames(item.root)[0] ?? name,
}));
}),
)
).flat();
filters.map(async (filter) =>
(
await Promise.all(
names.filter(filter).map(async (name) => {
const blocks = await this.library.findAll(name);
return blocks.map((item) => {
return {
...item,
name:
this.library.metadata.getNames(item.root)[0] ?? name,
};
});
}),
)
).flat(),
),
),
'root',
);
if (i > 0) {
constraints[i] = constraints[i].filter((item) => {
let next: ViewModelNode | undefined;
Expand All @@ -51,7 +81,6 @@ export class BlockCommandBundle implements CommandBundle {
const prev = constraints[i - 1].find(({root}) => root === next);
if (prev) {
item.name = prev.name + '/' + item.name;
item.description = prev.description + '/' + item.description;
return true;
}
}
Expand All @@ -70,7 +99,7 @@ export class BlockCommandBundle implements CommandBundle {
.at(-1)!
.filter(once)
.map((item) => ({
description: item.description,
description: item.name,
execute: async () => this.action(item),
icon: blockIcon(item),
preview: () => blockPreview(item),
Expand Down
Loading