Skip to content

Commit

Permalink
Expand all button in tree views (foambubble#1276)
Browse files Browse the repository at this point in the history
* Added expand all util function

* Added expand-all command for notes explorer

* Added expand-all command for placeholder tree view

* Added expand-all in tags explorer

* Added id field to tree items to make `reveal` work for all items
  • Loading branch information
riccardoferretti committed Aug 30, 2023
1 parent 1e2b3b1 commit df4efc5
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 8 deletions.
42 changes: 42 additions & 0 deletions packages/foam-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.group-by == 'folder'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.expand-all",
"when": "view == foam-vscode.tags-explorer",
"group": "navigation"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.show == 'all'",
Expand All @@ -172,6 +177,11 @@
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.group-by == 'folder'",
"group": "navigation"
},
{
"command": "foam-vscode.views.placeholders.expand-all",
"when": "view == foam-vscode.placeholders",
"group": "navigation"
},
{
"command": "foam-vscode.views.notes-explorer.show:notes",
"when": "view == foam-vscode.notes-explorer && foam-vscode.views.notes-explorer.show == 'all'",
Expand All @@ -181,6 +191,11 @@
"command": "foam-vscode.views.notes-explorer.show:all",
"when": "view == foam-vscode.notes-explorer && foam-vscode.views.notes-explorer.show == 'notes-only'",
"group": "navigation"
},
{
"command": "foam-vscode.views.notes-explorer.expand-all",
"when": "view == foam-vscode.notes-explorer",
"group": "navigation"
}
],
"commandPalette": [
Expand Down Expand Up @@ -228,6 +243,10 @@
"command": "foam-vscode.views.tags-explorer.group-by:off",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.expand-all",
"when": "false"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"when": "false"
Expand All @@ -244,6 +263,10 @@
"command": "foam-vscode.views.placeholders.group-by:off",
"when": "false"
},
{
"command": "foam-vscode.views.placeholders.expand-all",
"when": "false"
},
{
"command": "foam-vscode.views.notes-explorer.show:all",
"when": "false"
Expand All @@ -252,6 +275,10 @@
"command": "foam-vscode.views.notes-explorer.show:notes",
"when": "false"
},
{
"command": "foam-vscode.views.notes-explorer.expand-all",
"when": "false"
},
{
"command": "foam-vscode.open-resource",
"when": "false"
Expand Down Expand Up @@ -364,6 +391,11 @@
"title": "Flat list",
"icon": "$(list-flat)"
},
{
"command": "foam-vscode.views.tags-explorer.expand-all",
"title": "Expand all",
"icon": "$(expand-all)"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"title": "Show placeholders in current file",
Expand All @@ -384,11 +416,21 @@
"title": "Flat list",
"icon": "$(list-flat)"
},
{
"command": "foam-vscode.views.placeholders.expand-all",
"title": "Expand all",
"icon": "$(expand-all)"
},
{
"command": "foam-vscode.views.notes-explorer.show:all",
"title": "Show all resources",
"icon": "$(files)"
},
{
"command": "foam-vscode.views.notes-explorer.expand-all",
"title": "Expand all",
"icon": "$(expand-all)"
},
{
"command": "foam-vscode.views.notes-explorer.show:notes",
"title": "Show only notes",
Expand Down
17 changes: 12 additions & 5 deletions packages/foam-vscode/src/features/panels/notes-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ResourceRangeTreeItem,
ResourceTreeItem,
createBacklinkItemsForResource as createBacklinkTreeItemsForResource,
expandAll,
} from './utils/tree-view-utils';
import { Resource } from '../../core/model/note';
import { FoamGraph } from '../../core/model/graph';
Expand Down Expand Up @@ -60,6 +61,11 @@ export default async function activate(
foam.graph.onDidUpdate(() => {
provider.refresh();
}),
vscode.commands.registerCommand(
`foam-vscode.views.notes-explorer.expand-all`,
(...args) =>
expandAll(treeView, provider, node => node.contextValue === 'folder')
),
vscode.window.onDidChangeActiveTextEditor(revealTextEditorItem),
treeView.onDidChangeVisibility(revealTextEditorItem)
);
Expand Down Expand Up @@ -138,30 +144,31 @@ export class NotesProvider extends FolderTreeProvider<
value: Resource,
parent: FolderTreeItem<Resource>
): NotesTreeItems {
const res = new ResourceTreeItem(value, this.workspace, {
const item = new ResourceTreeItem(value, this.workspace, {
parent,
collapsibleState:
this.graph.getBacklinks(value.uri).length > 0
? vscode.TreeItemCollapsibleState.Collapsed
: vscode.TreeItemCollapsibleState.None,
});
res.getChildren = async () => {
item.id = value.uri.toString();
item.getChildren = async () => {
const backlinks = await createBacklinkTreeItemsForResource(
this.workspace,
this.graph,
res.uri
item.uri
);
backlinks.forEach(item => {
item.description = item.label;
item.label = item.resource.title;
});
return backlinks;
};
res.description =
item.description =
value.uri.getName().toLocaleLowerCase() ===
value.title.toLocaleLowerCase()
? undefined
: value.uri.getBasename();
return res;
return item;
}
}
14 changes: 14 additions & 0 deletions packages/foam-vscode/src/features/panels/placeholders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { GroupedResourcesTreeDataProvider } from './utils/grouped-resources-tree
import {
UriTreeItem,
createBacklinkItemsForResource,
expandAll,
groupRangesByResource,
} from './utils/tree-view-utils';
import { IMatcher } from '../../core/services/datastore';
Expand Down Expand Up @@ -47,6 +48,17 @@ export default async function activate(
provider.onDidChangeTreeData(() => {
treeView.title = baseTitle + ` (${provider.nValues})`;
}),
vscode.commands.registerCommand(
`foam-vscode.views.placeholders.expand-all`,
() =>
expandAll(
treeView,
provider,
node =>
node.contextValue === 'placeholder' ||
node.contextValue === 'folder'
)
),
vscode.window.onDidChangeActiveTextEditor(() => {
if (provider.show.get() === 'for-current-file') {
provider.refresh();
Expand Down Expand Up @@ -92,6 +104,8 @@ export class PlaceholderTreeView extends GroupedResourcesTreeDataProvider {
parent,
collapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
});
item.contextValue = 'placeholder';
item.id = uri.toString();
item.getChildren = async () => {
return groupRangesByResource(
this.workspace,
Expand Down
22 changes: 19 additions & 3 deletions packages/foam-vscode/src/features/panels/tags-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FoamTags } from '../../core/model/tags';
import {
ResourceRangeTreeItem,
ResourceTreeItem,
expandAll,
groupRangesByResource,
} from './utils/tree-view-utils';
import {
Expand Down Expand Up @@ -44,12 +45,21 @@ export default async function activate(
if (provider.show.get() === 'for-current-file') {
provider.refresh();
}
})
}),
vscode.commands.registerCommand(
`foam-vscode.views.${provider.providerId}.expand-all`,
() =>
expandAll(
treeView,
provider,
node => node.contextValue === 'tag' || node.contextValue === 'folder'
)
)
);
}

export class TagsProvider extends FolderTreeProvider<TagTreeItem, string> {
private providerId = 'tags-explorer';
public providerId = 'tags-explorer';
public show = new ContextMemento<'all' | 'for-current-file'>(
new MapBasedMemento(),
`foam-vscode.views.${this.providerId}.show`,
Expand Down Expand Up @@ -178,7 +188,12 @@ export class TagsProvider extends FolderTreeProvider<TagTreeItem, string> {
);
return [...acc, ...items];
}, []);
const resources = await groupRangesByResource(this.workspace, resourceTags);
const resources = (
await groupRangesByResource(this.workspace, resourceTags)
).map(item => {
item.id = element.tag + ' / ' + item.uri.toString();
return item;
});

return [...subtags, ...resources];
}
Expand All @@ -197,6 +212,7 @@ export class TagItem extends FolderTreeItem<string> {
) {
super(node, node.path.slice(-1)[0], parentElement);
this.tag = node.path.join(TAG_SEPARATOR);
this.id = this.tag;
this.description = `${nResourcesInSubtree} reference${
nResourcesInSubtree !== 1 ? 's' : ''
}`;
Expand Down
55 changes: 55 additions & 0 deletions packages/foam-vscode/src/features/panels/utils/tree-view-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getNoteTooltip } from '../../../utils';
import { isSome } from '../../../core/utils';
import { getBlockFor } from '../../../core/services/markdown-parser';
import { Connection, FoamGraph } from '../../../core/model/graph';
import { Logger } from '../../../core/utils/log';

export class BaseTreeItem extends vscode.TreeItem {
resolveTreeItem(): Promise<vscode.TreeItem> {
Expand Down Expand Up @@ -227,3 +228,57 @@ export function createConnectionItemsForResource(
});
return Promise.all(backlinkItems);
}

/**
* Expands a node and its children in a tree view that match a given predicate
*
* @param treeView - The tree view to expand nodes in
* @param provider - The tree data provider for the view
* @param element - The element to expand
* @param when - A function that returns true if the node should be expanded
*/
export async function expandNode<T>(
treeView: vscode.TreeView<any>,
provider: vscode.TreeDataProvider<T>,
element: T,
when: (element: T) => boolean
) {
try {
if (when(element)) {
await treeView.reveal(element, {
select: false,
focus: false,
expand: true,
});
}
} catch (e) {
const obj = element as any;
const label = obj.label ?? obj.toString();
Logger.warn(
`Could not expand element: ${label}. Try setting the ID property of the TreeItem`
);
}

const children = await provider.getChildren(element);
for (const child of children) {
await expandNode(treeView, provider, child, when);
}
}

/**
* Expands all items in a tree view that match a given predicate
*
* @param treeView - The tree view to expand items in
* @param provider - The tree data provider for the view
* @param when - A function that returns true if the node should be expanded
*/
export async function expandAll<T>(
treeView: vscode.TreeView<T>,
provider: vscode.TreeDataProvider<T>,
when: (element: T) => boolean = () => true
) {
const elements = await provider.getChildren(undefined);
for (const element of elements) {
await expandNode(treeView, provider, element, when);
}
}

0 comments on commit df4efc5

Please sign in to comment.