Skip to content

Commit 503a8f5

Browse files
Added navigation for tags in editor (#1433)
1 parent 8ab4f13 commit 503a8f5

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

packages/foam-vscode/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@
405405
"title": "Expand all",
406406
"icon": "$(expand-all)"
407407
},
408+
{
409+
"command": "foam-vscode.views.tags-explorer.focus",
410+
"title": "Focus on tag",
411+
"icon": "$(symbol-number)"
412+
},
408413
{
409414
"command": "foam-vscode.views.placeholders.show:for-current-file",
410415
"title": "Show placeholders in current file",

packages/foam-vscode/src/features/navigation-provider.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ export class NavigationProvider
157157
})
158158
);
159159

160-
return targets
160+
const links: vscode.DocumentLink[] = targets
161161
.filter(o => o.target.isPlaceholder()) // links to resources are managed by the definition provider
162162
.map(o => {
163163
const command = CREATE_NOTE_COMMAND.forPlaceholder(
@@ -180,5 +180,26 @@ export class NavigationProvider
180180
documentLink.tooltip = `Create note for '${o.target.path}'`;
181181
return documentLink;
182182
});
183+
184+
const tags: vscode.DocumentLink[] = resource.tags.map(tag => {
185+
const command = {
186+
name: 'foam-vscode.views.tags-explorer.focus',
187+
params: [tag.label, documentUri],
188+
};
189+
190+
const documentLink = new vscode.DocumentLink(
191+
new vscode.Range(
192+
tag.range.start.line,
193+
tag.range.start.character,
194+
tag.range.end.line,
195+
tag.range.end.character
196+
),
197+
commandAsURI(command)
198+
);
199+
documentLink.tooltip = `Explore tag '${tag.label}'`;
200+
return documentLink;
201+
});
202+
203+
return links.concat(tags);
183204
}
184205
}

packages/foam-vscode/src/features/panels/tags-explorer.ts

+45
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,51 @@ export default async function activate(
5454
provider,
5555
node => node.contextValue === 'tag' || node.contextValue === 'folder'
5656
)
57+
),
58+
vscode.commands.registerCommand(
59+
`foam-vscode.views.${provider.providerId}.focus`,
60+
async (tag?: string, source?: object) => {
61+
if (tag == null) {
62+
tag = await vscode.window.showQuickPick(
63+
Array.from(foam.tags.tags.keys()),
64+
{
65+
title: 'Select a tag to focus',
66+
}
67+
);
68+
}
69+
if (tag == null) {
70+
return;
71+
}
72+
const tagItem = (await provider.findTreeItemByPath(
73+
provider.valueToPath(tag)
74+
)) as TagItem;
75+
if (tagItem == null) {
76+
return;
77+
}
78+
await treeView.reveal(tagItem, {
79+
select: true,
80+
focus: true,
81+
expand: true,
82+
});
83+
const children = await provider.getChildren(tagItem);
84+
const sourceUri = source ? new URI(source) : undefined;
85+
const resourceItem = sourceUri
86+
? children.find(
87+
t =>
88+
t instanceof ResourceTreeItem &&
89+
sourceUri.isEqual(t.resource?.uri)
90+
)
91+
: undefined;
92+
// doing it as a two reveal process as revealing just the resource
93+
// was only working when the tag item was already expanded
94+
if (resourceItem) {
95+
treeView.reveal(resourceItem, {
96+
select: true,
97+
focus: true,
98+
expand: false,
99+
});
100+
}
101+
}
57102
)
58103
);
59104
}

packages/foam-vscode/src/features/preview/tag-highlight.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import markdownItRegex from 'markdown-it-regex';
44
import { FoamWorkspace } from '../../core/model/workspace';
55
import { Logger } from '../../core/utils/log';
66
import { isNone } from '../../core/utils';
7+
import { commandAsURI } from '../../utils/commands';
78

89
export const markdownItFoamTags = (
910
md: markdownit,
@@ -14,10 +15,7 @@ export const markdownItFoamTags = (
1415
regex: /(?<=^|\s)(#[0-9]*[\p{L}/_-][\p{L}\p{N}/_-]*)/u,
1516
replace: (tag: string) => {
1617
try {
17-
const resource = workspace.find(tag);
18-
if (isNone(resource)) {
19-
return getFoamTag(tag);
20-
}
18+
return getFoamTag(tag);
2119
} catch (e) {
2220
Logger.error(
2321
`Error while creating link for ${tag} in Preview panel`,
@@ -29,6 +27,8 @@ export const markdownItFoamTags = (
2927
});
3028
};
3129

30+
// Commands can't be run in the preview (see https://github.com/microsoft/vscode/issues/102532)
31+
// for we just return the tag as a span
3232
const getFoamTag = (content: string) =>
3333
`<span class='foam-tag'>${content}</span>`;
3434

0 commit comments

Comments
 (0)