Skip to content

Commit

Permalink
Tags explorer improvements (#1275)
Browse files Browse the repository at this point in the history
* Updated FolderTreeProvider to allow for values at any level of the tree

* Added utility method "walk" and passing node to createValueTreeItem

* Moved tag explorer to use FolderTreeProvider

* Added path in node info

* Added commands for showing tags in workspace or current file only

* Added support for grouping or flattening tags hierarchy

* Moved e2e test cleanup in the suite so that it also works when running the tests from VS Code debug task

* Added flag for registering commands in tags-explorer (needed for testing :( )
  • Loading branch information
riccardoferretti authored Aug 22, 2023
1 parent 86e2bb1 commit e773e1f
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 176 deletions.
56 changes: 56 additions & 0 deletions packages/foam-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@
"when": "view == foam-vscode.orphans && foam-vscode.views.orphans.group-by == 'folder'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.show:for-current-file",
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.show == 'all'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.show:all",
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.show == 'for-current-file'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:folder",
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.group-by == 'off'",
"group": "navigation"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:off",
"when": "view == foam-vscode.tags-explorer && foam-vscode.views.tags-explorer.group-by == 'folder'",
"group": "navigation"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.show == 'all'",
Expand Down Expand Up @@ -192,6 +212,22 @@
"command": "foam-vscode.views.orphans.group-by:off",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.show:for-current-file",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.show:all",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:folder",
"when": "false"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:off",
"when": "false"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"when": "false"
Expand Down Expand Up @@ -308,6 +344,26 @@
"title": "Flat list",
"icon": "$(list-flat)"
},
{
"command": "foam-vscode.views.tags-explorer.show:for-current-file",
"title": "Show tags in current file",
"icon": "$(file)"
},
{
"command": "foam-vscode.views.tags-explorer.show:all",
"title": "Show tags in workspace",
"icon": "$(files)"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:folder",
"title": "Group By Folder",
"icon": "$(list-tree)"
},
{
"command": "foam-vscode.views.tags-explorer.group-by:off",
"title": "Flat list",
"icon": "$(list-flat)"
},
{
"command": "foam-vscode.views.placeholders.show:for-current-file",
"title": "Show placeholders in current file",
Expand Down
4 changes: 0 additions & 4 deletions packages/foam-vscode/src/features/panels/notes-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ export class NotesProvider extends FolderTreeProvider<
return parts;
}

isValueType(value: Resource): value is Resource {
return value.uri != null;
}

createValueTreeItem(
value: Resource,
parent: FolderTreeItem<Resource>
Expand Down
42 changes: 20 additions & 22 deletions packages/foam-vscode/src/features/panels/tags-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteA);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);
provider.refresh();

const treeItems = (await provider.getChildren()) as TagItem[];
Expand All @@ -43,12 +43,12 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteA);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);
provider.refresh();

const parentTreeItems = (await provider.getChildren()) as TagItem[];
const parentTagItem = parentTreeItems.pop();
expect(parentTagItem.title).toEqual('parent');
expect(parentTagItem.label).toEqual('parent');

const childTreeItems = (await provider.getChildren(
parentTagItem
Expand All @@ -57,7 +57,7 @@ describe('Tags tree panel', () => {
childTreeItems.forEach(child => {
if (child instanceof TagItem) {
// eslint-disable-next-line jest/no-conditional-expect
expect(child.title).toEqual('child');
expect(child.label).toEqual('child');
}
});
});
Expand All @@ -73,15 +73,15 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteA).set(noteB);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);
provider.refresh();

const parentTreeItems = (await provider.getChildren()) as TagItem[];
const parentTagItem = parentTreeItems.filter(
item => item instanceof TagItem
)[0];

expect(parentTagItem.title).toEqual('parent');
expect(parentTagItem.label).toEqual('parent');
expect(parentTreeItems).toHaveLength(1);

const childTreeItems = (await provider.getChildren(
Expand All @@ -91,12 +91,12 @@ describe('Tags tree panel', () => {
childTreeItems.forEach(child => {
if (child instanceof TagItem) {
// eslint-disable-next-line jest/no-conditional-expect
expect(['child', 'subchild']).toContain(child.title);
expect(['child', 'subchild']).toContain(child.label);
// eslint-disable-next-line jest/no-conditional-expect
expect(child.title).not.toEqual('parent');
expect(child.label).not.toEqual('parent');
}
});
expect(childTreeItems).toHaveLength(3);
expect(childTreeItems).toHaveLength(2);
});

it('handles a parent and child tag in the same note', async () => {
Expand All @@ -107,7 +107,7 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteC);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);

provider.refresh();

Expand All @@ -116,7 +116,7 @@ describe('Tags tree panel', () => {
item => item instanceof TagItem
)[0];

expect(parentTagItem.title).toEqual('main');
expect(parentTagItem.label).toEqual('main');

const childTreeItems = (await provider.getChildren(
parentTagItem
Expand All @@ -132,10 +132,10 @@ describe('Tags tree panel', () => {
.filter(item => item instanceof TagItem)
.forEach(item => {
expect(['main/subtopic']).toContain(item.tag);
expect(item.title).toEqual('subtopic');
expect(item.label).toEqual('subtopic');
});

expect(childTreeItems).toHaveLength(3);
expect(childTreeItems).toHaveLength(2);
});

it('handles a tag with multiple levels of hierarchy - #1134', async () => {
Expand All @@ -145,28 +145,26 @@ describe('Tags tree panel', () => {
});
const workspace = new FoamWorkspace().set(noteA);
const foamTags = FoamTags.fromWorkspace(workspace);
const provider = new TagsProvider(foamTags, workspace);
const provider = new TagsProvider(foamTags, workspace, false);

provider.refresh();

const parentTreeItems = (await provider.getChildren()) as TagItem[];
const parentTagItem = parentTreeItems.pop();
expect(parentTagItem.title).toEqual('parent');
expect(parentTagItem.label).toEqual('parent');

const childTreeItems = (await provider.getChildren(
parentTagItem
)) as TagItem[];

expect(childTreeItems).toHaveLength(2);
expect(childTreeItems[0].label).toMatch(/^Search.*/);
expect(childTreeItems[1].label).toEqual('child');
expect(childTreeItems).toHaveLength(1);
expect(childTreeItems[0].label).toEqual('child');

const grandchildTreeItems = (await provider.getChildren(
childTreeItems[1]
childTreeItems[0]
)) as TagItem[];

expect(grandchildTreeItems).toHaveLength(2);
expect(grandchildTreeItems[0].label).toMatch(/^Search.*/);
expect(grandchildTreeItems[1].label).toEqual('second');
expect(grandchildTreeItems).toHaveLength(1);
expect(grandchildTreeItems[0].label).toEqual('second');
});
});
Loading

0 comments on commit e773e1f

Please sign in to comment.