mirror of
https://github.com/foambubble/foam.git
synced 2026-01-10 22:48:09 -05:00
Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1b15eceed | ||
|
|
96f410a453 | ||
|
|
95a14d5dd6 | ||
|
|
10905fd703 | ||
|
|
f4eaf5c5ff | ||
|
|
b4830eaf30 | ||
|
|
0cda6aed50 | ||
|
|
89c9bb5a7f | ||
|
|
941e870a65 | ||
|
|
c6655c33ff | ||
|
|
c94fb18f8a | ||
|
|
cbd55bac74 | ||
|
|
83a90177b9 | ||
|
|
37aec28af6 | ||
|
|
447f7fc068 | ||
|
|
ad1243665a | ||
|
|
f07de73bc4 | ||
|
|
c431ccfb62 | ||
|
|
f31ef897cc | ||
|
|
7a5f45c0ce | ||
|
|
df32d9e708 | ||
|
|
b3d4691bfa | ||
|
|
f5260f7d3f | ||
|
|
9b4b7ec84d | ||
|
|
52b7f86a9f | ||
|
|
2db7060124 | ||
|
|
a4f04b3b6b | ||
|
|
b5a8a5d7c7 | ||
|
|
f5a29e431c | ||
|
|
5a7a1ba89f | ||
|
|
b054bafc78 | ||
|
|
8acb60253a | ||
|
|
3c69508dcb | ||
|
|
a368be9b47 | ||
|
|
b1f76bb653 | ||
|
|
d4bc16b9bd | ||
|
|
882b0b6012 |
@@ -1004,6 +1004,24 @@
|
||||
"contributions": [
|
||||
"doc"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "Skakerman",
|
||||
"name": "Scott Akerman",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/15224439?v=4",
|
||||
"profile": "http://scottakerman.com",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
},
|
||||
{
|
||||
"login": "jimgraham",
|
||||
"name": "Jim Graham",
|
||||
"avatar_url": "https://avatars.githubusercontent.com/u/430293?v=4",
|
||||
"profile": "http://www.jim-graham.net/",
|
||||
"contributions": [
|
||||
"code"
|
||||
]
|
||||
}
|
||||
],
|
||||
"contributorsPerLine": 7,
|
||||
|
||||
43
.github/workflows/update-docs.yml
vendored
Normal file
43
.github/workflows/update-docs.yml
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
name: Update Docs
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
paths:
|
||||
- docs/user/**/*
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
update-docs:
|
||||
runs-on: ubuntu-22.04
|
||||
timeout-minutes: 10
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
repository: foambubble/foam-template
|
||||
path: foam-template
|
||||
- uses: actions/checkout@v3
|
||||
with:
|
||||
path: foam
|
||||
- name: Copy and fixup user docs files
|
||||
id: copy
|
||||
run: |
|
||||
rm -r foam-template/docs
|
||||
cp -r foam/docs/user foam-template/docs
|
||||
|
||||
# Strip autogenerated wikileaks references because
|
||||
# they are not an appropriate default user experience.
|
||||
(cd foam-template/docs; sed -i '/\[\/\/begin\]/,/\[\/\/end\]/d' $(find . -type f -name \*.md))
|
||||
|
||||
# Set the commit message format
|
||||
echo "message=Docs sync @ $(cd foam; git log --pretty='format:%h %s')" >> $GITHUB_OUTPUT
|
||||
- uses: peter-evans/create-pull-request@v4
|
||||
with:
|
||||
token: ${{ secrets.FOAM_DOCS_SYNC_TOKEN }}
|
||||
path: foam-template
|
||||
commit-message: ${{ steps.copy.outputs.message }}
|
||||
branch: bot/foam-docs-sync
|
||||
delete-branch: true
|
||||
title: Sync docs from foam
|
||||
body: Copy docs from main foam repo
|
||||
@@ -247,6 +247,8 @@ If that sounds like something you're interested in, I'd love to have you along o
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://jonathanpberger.com/"><img src="https://avatars.githubusercontent.com/u/41085?v=4?s=60" width="60px;" alt="jonathan berger"/><br /><sub><b>jonathan berger</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=jonathanpberger" title="Documentation">📖</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/badsketch"><img src="https://avatars.githubusercontent.com/u/8953212?v=4?s=60" width="60px;" alt="Daniel Wang"/><br /><sub><b>Daniel Wang</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=badsketch" title="Code">💻</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://yongliangliu.com"><img src="https://avatars.githubusercontent.com/u/41845017?v=4?s=60" width="60px;" alt="Liu YongLiang"/><br /><sub><b>Liu YongLiang</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=tlylt" title="Documentation">📖</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://scottakerman.com"><img src="https://avatars.githubusercontent.com/u/15224439?v=4?s=60" width="60px;" alt="Scott Akerman"/><br /><sub><b>Scott Akerman</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=Skakerman" title="Code">💻</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://www.jim-graham.net/"><img src="https://avatars.githubusercontent.com/u/430293?v=4?s=60" width="60px;" alt="Jim Graham"/><br /><sub><b>Jim Graham</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=jimgraham" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -9,16 +9,19 @@ In particular, some commands can be very customizible and can help with custom w
|
||||
This command creates a note.
|
||||
Although it works fine on its own, it can be customized to achieve various use cases.
|
||||
Here are the settings available for the command:
|
||||
- notePath: The path of the note to create. If relative it will be resolved against the workspace root.
|
||||
|
||||
- notePath: The path of the note to create. If relative it will be resolved against the workspace root.
|
||||
- templatePath: The path of the template to use. If relative it will be resolved against the workspace root.
|
||||
- title: The title of the note (that is, the `FOAM_TITLE` variable)
|
||||
- text: The text to use for the note. If also a template is provided, the template has precedence
|
||||
- variables: Variables to use in the text or template (e.g. `FOAM_TITLE`)
|
||||
- date: The date used to resolve the FOAM_DATE_* variables. in `YYYY-MM-DD` format
|
||||
- variables: Variables to use in the text or template
|
||||
- date: The date used to resolve the FOAM*DATE*\* variables. in `YYYY-MM-DD` format
|
||||
- onFileExists?: 'overwrite' | 'open' | 'ask' | 'cancel': What to do in case the target file already exists
|
||||
|
||||
To customize a command and associate a key binding to it, open the key binding settings and add the appropriate configuration, here are some examples:
|
||||
|
||||
- Create a note called `test note.md` with some text. If the note already exists, ask for a new name
|
||||
|
||||
```
|
||||
{
|
||||
"key": "alt+f",
|
||||
@@ -32,6 +35,7 @@ To customize a command and associate a key binding to it, open the key binding s
|
||||
```
|
||||
|
||||
- Create a note following the `weekly-note.md` template. If the note already exists, open it
|
||||
|
||||
```
|
||||
{
|
||||
"key": "alt+g",
|
||||
@@ -43,3 +47,27 @@ To customize a command and associate a key binding to it, open the key binding s
|
||||
}
|
||||
```
|
||||
|
||||
## foam-vscode.open-resource command
|
||||
|
||||
This command opens a resource.
|
||||
|
||||
Normally it receives a `URI`, which identifies the resource to open.
|
||||
|
||||
It is also possible to pass in a filter, which will be run against the workspace resources to find one or more matches.
|
||||
|
||||
- If there is one match, it will be opened
|
||||
- If there is more than one match, a quick pick will show up allowing the user to select the desired resource
|
||||
|
||||
Examples:
|
||||
|
||||
```
|
||||
{
|
||||
"key": "alt+f",
|
||||
"command": "foam-vscode.open-resource",
|
||||
"args": {
|
||||
"filter": {
|
||||
"title": "Weekly Note*"
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
- [Frequently Asked Questions](#frequently-asked-questions)
|
||||
- [Links/Graphs/BackLinks don't work. How do I enable them?](#linksgraphsbacklinks-dont-work-how-do-i-enable-them)
|
||||
- [I don't want Foam enabled for all my workspaces](#i-dont-want-foam-enabled-for-all-my-workspaces)
|
||||
- [I want to publish the graph view to GitHub pages or Vercel](#i-want-to-publish-the-graph-view-to-github-pages-or-vercel)
|
||||
|
||||
## Links/Graphs/BackLinks don't work. How do I enable them?
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
# Using Foam
|
||||
|
||||
Foam is a collection VS Code extensions and recipes that power up the editor into a full-blown note taking system.
|
||||
This folder contains user documentation describing how to get started using Foam, what its main features are, and strategies for getting the most out of Foam.
|
||||
The full docs are included in the `foam-template` repo that most users start from.
|
||||
Foam is a collection VS Code extensions and recipes that power up the editor
|
||||
into a full-blown note taking system. This folder contains user documentation
|
||||
describing how to get started using Foam, what its main features are, and
|
||||
strategies for getting the most out of Foam. The full docs are included in the
|
||||
`foam-template` repo that most users start from.
|
||||
|
||||
> See also [[frequently-asked-questions]].
|
||||
|
||||
|
||||
@@ -4,5 +4,5 @@
|
||||
],
|
||||
"npmClient": "yarn",
|
||||
"useWorkspaces": true,
|
||||
"version": "0.20.8"
|
||||
"version": "0.22.2"
|
||||
}
|
||||
|
||||
@@ -4,6 +4,67 @@ All notable changes to the "foam-vscode" extension will be documented in this fi
|
||||
|
||||
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
|
||||
|
||||
## [0.22.2] - 2023-04-20
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Support to show placeholders only for open file in panel (#1201, #988)
|
||||
- Show note block in panels on hover preview (#1201, #800)
|
||||
- Show tag references within tag explorer (#1201)
|
||||
- Improved structure of view related commands (#1201)
|
||||
- Ignore `.foam` directory
|
||||
|
||||
## [0.22.1] - 2023-04-15
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Allow the `#` char to trigger tag autocompletion (#1192, #1189 - thanks @jimgraham)
|
||||
|
||||
## [0.22.0] - 2023-04-15
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Added support for deep tag hierarchy in Tag Explorer panel (#1134, #1194)
|
||||
- Consolidated and improved Backlinks, Placeholders and Orphans panels (#1196)
|
||||
- Fixed note resolution when using template without defined path (#1197)
|
||||
|
||||
## [0.21.4] - 2023-04-14
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed issue with generated daily note template due to path escape (#1188, #1190)
|
||||
|
||||
## [0.21.3] - 2023-04-12
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed relative path from workspace root in templates (#1188)
|
||||
|
||||
## [0.21.2] - 2023-04-11
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed embed with relative paths (#1168, #1170)
|
||||
- Improved multi-root folder support for daily notes (#1126, #1175)
|
||||
- Improved use of tag completion (#1183 - thanks @jimgraham)
|
||||
- Fixed relative path use in note creation when using templates (#1170)
|
||||
|
||||
Internal:
|
||||
|
||||
- Sync user docs with foam-template docs (#1180 - thanks @infogulch)
|
||||
|
||||
## [0.21.1] - 2023-02-24
|
||||
|
||||
Fixes and Improvements:
|
||||
|
||||
- Fixed note creation from placeholder (#1172)
|
||||
|
||||
## [0.21.0] - 2023-02-16
|
||||
|
||||
Features:
|
||||
|
||||
- Added support for filters for the `foam-vscode.open-resource` command (#1161)
|
||||
|
||||
## [0.20.8] - 2023-02-10
|
||||
|
||||
Internal:
|
||||
@@ -64,7 +125,7 @@ Fixes and Improvements:
|
||||
|
||||
## [0.20.0] - 2022-09-30
|
||||
|
||||
New Features:
|
||||
Features:
|
||||
|
||||
- Added `foam-vscode.create-note` command, which can be very customized for several use cases (#1076)
|
||||
|
||||
@@ -120,7 +181,7 @@ Internal:
|
||||
|
||||
## [0.19.0] - 2022-07-07
|
||||
|
||||
New Features:
|
||||
Features:
|
||||
|
||||
- Support for attachments (PDF) and images (#1027)
|
||||
- Support for opening day notes for other days as well (#1026, thanks @alper)
|
||||
@@ -600,7 +661,7 @@ Fixes and Improvements:
|
||||
|
||||
## [0.7.1] - 2020-11-27
|
||||
|
||||
New Feature:
|
||||
Features:
|
||||
|
||||
- Foam logging can now be inspected in VsCode Output panel (#377)
|
||||
|
||||
@@ -612,7 +673,7 @@ Fixes and Improvements:
|
||||
|
||||
## [0.7.0] - 2020-11-25
|
||||
|
||||
New Features:
|
||||
Features:
|
||||
|
||||
- Foam stays in sync with changes in notes
|
||||
- Dataviz: Added multiple selection in graph (shift+click on node)
|
||||
@@ -624,7 +685,7 @@ Fixes and Improvements:
|
||||
|
||||
## [0.6.0] - 2020-11-19
|
||||
|
||||
New features:
|
||||
Features:
|
||||
|
||||
- Added command to create notes from templates (#115 - Thanks @ingalless)
|
||||
|
||||
@@ -639,7 +700,7 @@ Fixes and Improvements:
|
||||
|
||||
## [0.5.0] - 2020-11-09
|
||||
|
||||
New features:
|
||||
Features:
|
||||
|
||||
- Added tags panel (#311)
|
||||
|
||||
@@ -653,7 +714,7 @@ Fixes and Improvements:
|
||||
|
||||
## [0.4.0] - 2020-10-28
|
||||
|
||||
New features:
|
||||
Features:
|
||||
|
||||
- Added `Foam: Show Graph` command
|
||||
- Added date snippets (/+1d, ...) to create wikilinks to dates in daily note format
|
||||
@@ -681,7 +742,7 @@ Fixes and improvements:
|
||||
|
||||
## [0.3.0] - 2020-07-25
|
||||
|
||||
New features:
|
||||
Features:
|
||||
|
||||
- [Daily Notes](https://foambubble.github.io/foam/daily-notes)
|
||||
- [Janitor](https://foambubble.github.io/foam/workspace-janitor) for updating headings and link references across your workspace
|
||||
|
||||
33
packages/foam-vscode/LICENSE
Normal file
33
packages/foam-vscode/LICENSE
Normal file
@@ -0,0 +1,33 @@
|
||||
The MIT Licence (MIT)
|
||||
|
||||
Copyright 2020 - present Jani Eväkallio <jani.evakallio@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Where noted, some code uses the following license:
|
||||
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2015 - present Microsoft Corporation
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
@@ -8,7 +8,7 @@
|
||||
"type": "git"
|
||||
},
|
||||
"homepage": "https://github.com/foambubble/foam",
|
||||
"version": "0.20.8",
|
||||
"version": "0.22.2",
|
||||
"license": "MIT",
|
||||
"publisher": "foam",
|
||||
"engines": {
|
||||
@@ -96,29 +96,39 @@
|
||||
},
|
||||
{
|
||||
"view": "foam-vscode.placeholders",
|
||||
"contents": "No placeholders found. Pending links and notes without content will show up here."
|
||||
"contents": "No placeholders found for selected resource or workspace."
|
||||
}
|
||||
],
|
||||
"menus": {
|
||||
"view/title": [
|
||||
{
|
||||
"command": "foam-vscode.group-orphans-by-folder",
|
||||
"when": "view == foam-vscode.orphans && foam-vscode.orphans-grouped-by-folder == false",
|
||||
"command": "foam-vscode.views.orphans.group-by:folder",
|
||||
"when": "view == foam-vscode.orphans && foam-vscode.views.orphans.group-by == 'off'",
|
||||
"group": "navigation"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-orphans-off",
|
||||
"when": "view == foam-vscode.orphans && foam-vscode.orphans-grouped-by-folder == true",
|
||||
"command": "foam-vscode.views.orphans.group-by:off",
|
||||
"when": "view == foam-vscode.orphans && foam-vscode.views.orphans.group-by == 'folder'",
|
||||
"group": "navigation"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-placeholders-by-folder",
|
||||
"when": "view == foam-vscode.placeholders && foam-vscode.placeholders-grouped-by-folder == false",
|
||||
"command": "foam-vscode.views.placeholders.show:for-current-file",
|
||||
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.show == 'all'",
|
||||
"group": "navigation"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-placeholders-off",
|
||||
"when": "view == foam-vscode.placeholders && foam-vscode.placeholders-grouped-by-folder == true",
|
||||
"command": "foam-vscode.views.placeholders.show:all",
|
||||
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.show == 'for-current-file'",
|
||||
"group": "navigation"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.views.placeholders.group-by:folder",
|
||||
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.group-by == 'off'",
|
||||
"group": "navigation"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.views.placeholders.group-by:off",
|
||||
"when": "view == foam-vscode.placeholders && foam-vscode.views.placeholders.group-by == 'folder'",
|
||||
"group": "navigation"
|
||||
}
|
||||
],
|
||||
@@ -132,19 +142,27 @@
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-orphans-by-folder",
|
||||
"command": "foam-vscode.views.orphans.group-by:folder",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-orphans-off",
|
||||
"command": "foam-vscode.views.orphans.group-by:off",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-placeholders-by-folder",
|
||||
"command": "foam-vscode.views.placeholders.show:for-current-file",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-placeholders-off",
|
||||
"command": "foam-vscode.views.placeholders.show:all",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.views.placeholders.group-by:folder",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.views.placeholders.group-by:off",
|
||||
"when": "false"
|
||||
},
|
||||
{
|
||||
@@ -215,23 +233,33 @@
|
||||
"title": "Foam: Open Resource"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-orphans-by-folder",
|
||||
"title": "Foam: Group Orphans By Folder",
|
||||
"command": "foam-vscode.views.orphans.group-by:folder",
|
||||
"title": "Group By Folder",
|
||||
"icon": "$(list-tree)"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-orphans-off",
|
||||
"title": "Foam: Don't Group Orphans",
|
||||
"command": "foam-vscode.views.orphans.group-by:off",
|
||||
"title": "Flat list",
|
||||
"icon": "$(list-flat)"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-placeholders-by-folder",
|
||||
"title": "Foam: Group Placeholders By Folder",
|
||||
"command": "foam-vscode.views.placeholders.show:for-current-file",
|
||||
"title": "Show placeholders in current file",
|
||||
"icon": "$(file)"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.views.placeholders.show:all",
|
||||
"title": "Show placeholders in workspace",
|
||||
"icon": "$(files)"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.views.placeholders.group-by:folder",
|
||||
"title": "Group By Folder",
|
||||
"icon": "$(list-tree)"
|
||||
},
|
||||
{
|
||||
"command": "foam-vscode.group-placeholders-off",
|
||||
"title": "Foam: Don't Group Placeholders",
|
||||
"command": "foam-vscode.views.placeholders.group-by:off",
|
||||
"title": "Flat list",
|
||||
"icon": "$(list-flat)"
|
||||
},
|
||||
{
|
||||
@@ -375,21 +403,6 @@
|
||||
"default": [],
|
||||
"markdownDescription": "Specifies the list of glob patterns that will be excluded from the orphans report. To ignore the all the content of a given folder, use `**<folderName>/**/*`"
|
||||
},
|
||||
"foam.orphans.groupBy": {
|
||||
"type": [
|
||||
"string"
|
||||
],
|
||||
"enum": [
|
||||
"off",
|
||||
"folder"
|
||||
],
|
||||
"enumDescriptions": [
|
||||
"Disable grouping",
|
||||
"Group by folder"
|
||||
],
|
||||
"default": "folder",
|
||||
"markdownDescription": "Group orphans report entries by."
|
||||
},
|
||||
"foam.placeholders.exclude": {
|
||||
"type": [
|
||||
"array"
|
||||
@@ -397,21 +410,6 @@
|
||||
"default": [],
|
||||
"markdownDescription": "Specifies the list of glob patterns that will be excluded from the placeholders report. To ignore the all the content of a given folder, use `**<folderName>/**/*`"
|
||||
},
|
||||
"foam.placeholders.groupBy": {
|
||||
"type": [
|
||||
"string"
|
||||
],
|
||||
"enum": [
|
||||
"off",
|
||||
"folder"
|
||||
],
|
||||
"enumDescriptions": [
|
||||
"Disable grouping",
|
||||
"Group by folder"
|
||||
],
|
||||
"default": "folder",
|
||||
"markdownDescription": "Group blank note report entries by."
|
||||
},
|
||||
"foam.dateSnippets.afterCompletion": {
|
||||
"type": "string",
|
||||
"default": "createNote",
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import os from 'os';
|
||||
import detectNewline from 'detect-newline';
|
||||
import { Position } from '../model/position';
|
||||
import { Range } from '../model/range';
|
||||
|
||||
export interface TextEdit {
|
||||
range: Range;
|
||||
newText: string;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param text text on which the textEdit will be applied
|
||||
* @param textEdit
|
||||
* @returns {string} text with the applied textEdit
|
||||
*/
|
||||
export const applyTextEdit = (text: string, textEdit: TextEdit): string => {
|
||||
const eol = detectNewline(text) || os.EOL;
|
||||
const lines = text.split(eol);
|
||||
const characters = text.split('');
|
||||
const startOffset = getOffset(lines, textEdit.range.start, eol);
|
||||
const endOffset = getOffset(lines, textEdit.range.end, eol);
|
||||
const deleteCount = endOffset - startOffset;
|
||||
|
||||
const textToAppend = `${textEdit.newText}`;
|
||||
characters.splice(startOffset, deleteCount, textToAppend);
|
||||
return characters.join('');
|
||||
};
|
||||
|
||||
const getOffset = (
|
||||
lines: string[],
|
||||
position: Position,
|
||||
eol: string
|
||||
): number => {
|
||||
const eolLen = eol.length;
|
||||
let offset = 0;
|
||||
let i = 0;
|
||||
while (i < position.line && i < lines.length) {
|
||||
offset = offset + lines[i].length + eolLen;
|
||||
i++;
|
||||
}
|
||||
return offset + Math.min(position.character, lines[i]?.length ?? 0);
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
import matter from 'gray-matter';
|
||||
import { TextEdit } from './apply-text-edit';
|
||||
import { Resource } from '../model/note';
|
||||
import { Range } from '../model/range';
|
||||
import { TextEdit } from '../services/text-edit';
|
||||
import { getHeadingFromFileName } from '../utils';
|
||||
|
||||
export const generateHeading = async (
|
||||
|
||||
@@ -5,8 +5,8 @@ import {
|
||||
stringifyMarkdownLinkReferenceDefinition,
|
||||
} from '../services/markdown-provider';
|
||||
import { FoamWorkspace } from '../model/workspace';
|
||||
import { TextEdit } from './apply-text-edit';
|
||||
import { Position } from '../model/position';
|
||||
import { TextEdit } from '../services/text-edit';
|
||||
|
||||
export const LINK_REFERENCE_DEFINITION_HEADER = `[//begin]: # "Autogenerated link references for markdown compatibility"`;
|
||||
export const LINK_REFERENCE_DEFINITION_FOOTER = `[//end]: # "Autogenerated link references"`;
|
||||
|
||||
@@ -5,6 +5,7 @@ export interface ResourceLink {
|
||||
type: 'wikilink' | 'link';
|
||||
rawText: string;
|
||||
range: Range;
|
||||
isEmbed: boolean;
|
||||
}
|
||||
|
||||
export interface NoteLinkDefinition {
|
||||
|
||||
@@ -69,4 +69,8 @@ export abstract class Range {
|
||||
static isBefore(a: Range, b: Range): number {
|
||||
return a.start.line - b.start.line || a.start.character - b.start.character;
|
||||
}
|
||||
|
||||
static toString(range: Range): string {
|
||||
return `${range.start.line}:${range.start.character} - ${range.end.line}:${range.end.character}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,6 +87,13 @@ describe('Workspace resources', () => {
|
||||
const res = ws.find('test-file#my-section');
|
||||
expect(res.uri.fragment).toEqual('my-section');
|
||||
});
|
||||
|
||||
it('should find absolute files even when no basedir is provided', () => {
|
||||
const noteA = createTestNote({ uri: '/a/path/to/file.md' });
|
||||
const ws = createTestWorkspace().set(noteA);
|
||||
|
||||
expect(ws.find('/a/path/to/file.md').uri.path).toEqual(noteA.uri.path);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Identifier computation', () => {
|
||||
|
||||
@@ -121,14 +121,16 @@ export class FoamWorkspace implements IDisposable {
|
||||
if (FoamWorkspace.isIdentifier(path)) {
|
||||
resource = this.listByIdentifier(path)[0];
|
||||
} else {
|
||||
if (isAbsolute(path) || isSome(baseUri)) {
|
||||
if (getExtension(path) !== '.md') {
|
||||
const uri = baseUri.resolve(path + '.md');
|
||||
resource = uri ? this._resources.get(normalize(uri.path)) : null;
|
||||
}
|
||||
if (!resource) {
|
||||
const uri = baseUri.resolve(path);
|
||||
resource = uri ? this._resources.get(normalize(uri.path)) : null;
|
||||
const candidates = [path, path + '.md'];
|
||||
for (const candidate of candidates) {
|
||||
const searchKey = isAbsolute(candidate)
|
||||
? candidate
|
||||
: isSome(baseUri)
|
||||
? baseUri.resolve(candidate).path
|
||||
: null;
|
||||
resource = this._resources.get(normalize(searchKey));
|
||||
if (resource) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,6 +107,7 @@ describe('MarkdownLink', () => {
|
||||
type: 'link',
|
||||
rawText: '[link](#section)',
|
||||
range: Range.create(0, 0),
|
||||
isEmbed: false,
|
||||
};
|
||||
const parsed = MarkdownLink.analyzeLink(link);
|
||||
expect(parsed.target).toEqual('');
|
||||
@@ -161,7 +162,7 @@ describe('MarkdownLink', () => {
|
||||
target: 'new-link',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[[new-link#section]]`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
it('should rename the section only', () => {
|
||||
const link = parser.parse(
|
||||
@@ -172,7 +173,7 @@ describe('MarkdownLink', () => {
|
||||
section: 'new-section',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[[wikilink#new-section]]`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
it('should rename both target and section', () => {
|
||||
const link = parser.parse(
|
||||
@@ -184,7 +185,7 @@ describe('MarkdownLink', () => {
|
||||
section: 'new-section',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[[new-link#new-section]]`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
it('should be able to remove the section', () => {
|
||||
const link = parser.parse(
|
||||
@@ -195,7 +196,7 @@ describe('MarkdownLink', () => {
|
||||
section: '',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[[wikilink]]`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
it('should be able to rename the alias', () => {
|
||||
const link = parser.parse(getRandomURI(), `this is a [[wikilink|alias]]`)
|
||||
@@ -204,7 +205,7 @@ describe('MarkdownLink', () => {
|
||||
alias: 'new-alias',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[[wikilink|new-alias]]`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -216,7 +217,7 @@ describe('MarkdownLink', () => {
|
||||
target: 'to/another-path.md',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[link](to/another-path.md)`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
it('should rename the section only', () => {
|
||||
const link = parser.parse(
|
||||
@@ -227,7 +228,7 @@ describe('MarkdownLink', () => {
|
||||
section: 'section2',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[link](to/path.md#section2)`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
it('should rename both target and section', () => {
|
||||
const link = parser.parse(
|
||||
@@ -239,7 +240,7 @@ describe('MarkdownLink', () => {
|
||||
section: 'section2',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[link](to/another-path.md#section2)`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
it('should be able to remove the section', () => {
|
||||
const link = parser.parse(
|
||||
@@ -250,7 +251,7 @@ describe('MarkdownLink', () => {
|
||||
section: '',
|
||||
});
|
||||
expect(edit.newText).toEqual(`[link](to/path.md)`);
|
||||
expect(edit.selection).toEqual(link.range);
|
||||
expect(edit.range).toEqual(link.range);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -46,16 +46,17 @@ export abstract class MarkdownLink {
|
||||
const newAlias = delta.alias ?? alias ?? '';
|
||||
const sectionDivider = newSection ? '#' : '';
|
||||
const aliasDivider = newAlias ? '|' : '';
|
||||
const embed = link.isEmbed ? '!' : '';
|
||||
if (link.type === 'wikilink') {
|
||||
return {
|
||||
newText: `[[${newTarget}${sectionDivider}${newSection}${aliasDivider}${newAlias}]]`,
|
||||
selection: link.range,
|
||||
newText: `${embed}[[${newTarget}${sectionDivider}${newSection}${aliasDivider}${newAlias}]]`,
|
||||
range: link.range,
|
||||
};
|
||||
}
|
||||
if (link.type === 'link') {
|
||||
return {
|
||||
newText: `[${newAlias}](${newTarget}${sectionDivider}${newSection})`,
|
||||
selection: link.range,
|
||||
newText: `${embed}[${newAlias}](${newTarget}${sectionDivider}${newSection})`,
|
||||
range: link.range,
|
||||
};
|
||||
}
|
||||
throw new Error(
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
import { createMarkdownParser, ParserPlugin } from './markdown-parser';
|
||||
import {
|
||||
createMarkdownParser,
|
||||
getBlockFor,
|
||||
ParserPlugin,
|
||||
} from './markdown-parser';
|
||||
import { Logger } from '../utils/log';
|
||||
import { URI } from '../model/uri';
|
||||
import { Range } from '../model/range';
|
||||
@@ -39,6 +43,7 @@ describe('Markdown parsing', () => {
|
||||
const link = note.links[0];
|
||||
expect(link.type).toEqual('link');
|
||||
expect(link.rawText).toEqual('[link to page b](../doc/page-b.md)');
|
||||
expect(link.isEmbed).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should detect links that have formatting in label', () => {
|
||||
@@ -48,6 +53,15 @@ describe('Markdown parsing', () => {
|
||||
expect(note.links.length).toEqual(1);
|
||||
const link = note.links[0];
|
||||
expect(link.type).toEqual('link');
|
||||
expect(link.isEmbed).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should detect embed links', () => {
|
||||
const note = createNoteFromMarkdown('this is ');
|
||||
expect(note.links.length).toEqual(1);
|
||||
const link = note.links[0];
|
||||
expect(link.type).toEqual('link');
|
||||
expect(link.isEmbed).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should detect wikilinks', () => {
|
||||
@@ -61,6 +75,16 @@ describe('Markdown parsing', () => {
|
||||
link = note.links[1];
|
||||
expect(link.type).toEqual('wikilink');
|
||||
expect(link.rawText).toEqual('[[a file]]');
|
||||
expect(link.isEmbed).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should detect wikilink embeds', () => {
|
||||
const note = createNoteFromMarkdown('Some content and ![[an embed]]');
|
||||
expect(note.links.length).toEqual(1);
|
||||
const link = note.links[0];
|
||||
expect(link.type).toEqual('wikilink');
|
||||
expect(link.rawText).toEqual('![[an embed]]');
|
||||
expect(link.isEmbed).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should detect wikilinks that have aliases', () => {
|
||||
@@ -74,6 +98,7 @@ describe('Markdown parsing', () => {
|
||||
link = note.links[1];
|
||||
expect(link.type).toEqual('wikilink');
|
||||
expect(link.rawText).toEqual('[[other link | spaced]]');
|
||||
expect(link.isEmbed).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should skip wikilinks in codeblocks', () => {
|
||||
@@ -438,3 +463,48 @@ But with some content.
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Block detection', () => {
|
||||
const md = `
|
||||
- this is block 1
|
||||
- this is [[block]] 2
|
||||
- this is block 2.1
|
||||
- this is block 3
|
||||
- this is block 3.1
|
||||
- this is block 3.1.1
|
||||
- this is block 3.2
|
||||
- this is block 4
|
||||
this is a simple line
|
||||
this is another simple line
|
||||
`;
|
||||
|
||||
it('can detect block', () => {
|
||||
const { block } = getBlockFor(md, 1);
|
||||
expect(block).toEqual('- this is block 1');
|
||||
});
|
||||
|
||||
it('supports nested blocks 1', () => {
|
||||
const { block } = getBlockFor(md, 2);
|
||||
expect(block).toEqual(`- this is [[block]] 2
|
||||
- this is block 2.1`);
|
||||
});
|
||||
|
||||
it('supports nested blocks 2', () => {
|
||||
const { block } = getBlockFor(md, 5);
|
||||
expect(block).toEqual(` - this is block 3.1
|
||||
- this is block 3.1.1`);
|
||||
});
|
||||
|
||||
it('returns the line if no block is detected', () => {
|
||||
const { block } = getBlockFor(md, 9);
|
||||
expect(block).toEqual(`this is a simple line`);
|
||||
});
|
||||
|
||||
it('is compatible with Range object', () => {
|
||||
const note = parser.parse(URI.file('/path/to/a'), md);
|
||||
const { start } = note.links[0].range;
|
||||
const { block } = getBlockFor(md, start);
|
||||
expect(block).toEqual(`- this is [[block]] 2
|
||||
- this is block 2.1`);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -299,18 +299,33 @@ const wikilinkPlugin: ParserPlugin = {
|
||||
name: 'wikilink',
|
||||
visit: (node, note, noteSource) => {
|
||||
if (node.type === 'wikiLink') {
|
||||
const isEmbed =
|
||||
noteSource.charAt(node.position!.start.offset - 1) === '!';
|
||||
|
||||
const literalContent = noteSource.substring(
|
||||
node.position!.start.offset!,
|
||||
isEmbed
|
||||
? node.position!.start.offset! - 1
|
||||
: node.position!.start.offset!,
|
||||
node.position!.end.offset!
|
||||
);
|
||||
|
||||
const range = isEmbed
|
||||
? Range.create(
|
||||
node.position.start.line - 1,
|
||||
node.position.start.column - 2,
|
||||
node.position.end.line - 1,
|
||||
node.position.end.column - 1
|
||||
)
|
||||
: astPositionToFoamRange(node.position!);
|
||||
|
||||
note.links.push({
|
||||
type: 'wikilink',
|
||||
rawText: literalContent,
|
||||
range: astPositionToFoamRange(node.position!),
|
||||
range,
|
||||
isEmbed,
|
||||
});
|
||||
}
|
||||
if (node.type === 'link') {
|
||||
if (node.type === 'link' || node.type === 'image') {
|
||||
const targetUri = (node as any).url;
|
||||
const uri = note.uri.resolve(targetUri);
|
||||
if (uri.scheme !== 'file' || uri.path === note.uri.path) {
|
||||
@@ -324,6 +339,7 @@ const wikilinkPlugin: ParserPlugin = {
|
||||
type: 'link',
|
||||
rawText: literalContent,
|
||||
range: astPositionToFoamRange(node.position!),
|
||||
isEmbed: literalContent.startsWith('!'),
|
||||
});
|
||||
}
|
||||
},
|
||||
@@ -408,3 +424,28 @@ const astPositionToFoamRange = (pos: AstPosition): Range =>
|
||||
pos.end.line - 1,
|
||||
pos.end.column - 1
|
||||
);
|
||||
|
||||
const blockParser = unified().use(markdownParse, { gfm: true });
|
||||
export const getBlockFor = (
|
||||
markdown: string,
|
||||
line: number | Position
|
||||
): { block: string; nLines: number } => {
|
||||
const searchLine = typeof line === 'number' ? line : line.line;
|
||||
const tree = blockParser.parse(markdown);
|
||||
const lines = markdown.split('\n');
|
||||
let block = null;
|
||||
let nLines = 0;
|
||||
visit(tree, ['listItem'], (node: any) => {
|
||||
if (node.position.start.line === searchLine + 1) {
|
||||
block = lines
|
||||
.slice(node.position.start.line - 1, node.position.end.line)
|
||||
.join('\n');
|
||||
nLines = node.position.end.line - node.position.start.line;
|
||||
return visit.EXIT;
|
||||
}
|
||||
});
|
||||
if (block == null) {
|
||||
block = lines[searchLine];
|
||||
}
|
||||
return { block, nLines };
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Range } from '../model/range';
|
||||
import { Logger } from '../utils/log';
|
||||
import { applyTextEdit } from './apply-text-edit';
|
||||
import { TextEdit } from './text-edit';
|
||||
|
||||
Logger.setLevel('error');
|
||||
|
||||
@@ -23,7 +23,7 @@ describe('applyTextEdit', () => {
|
||||
3. this is third line
|
||||
4. this is fourth line`;
|
||||
|
||||
const actual = applyTextEdit(text, textEdit);
|
||||
const actual = TextEdit.apply(text, textEdit);
|
||||
|
||||
expect(actual).toBe(expected);
|
||||
});
|
||||
@@ -45,7 +45,7 @@ describe('applyTextEdit', () => {
|
||||
3. this is third line
|
||||
`;
|
||||
|
||||
const actual = applyTextEdit(text, textEdit);
|
||||
const actual = TextEdit.apply(text, textEdit);
|
||||
|
||||
expect(actual).toBe(expected);
|
||||
});
|
||||
@@ -68,7 +68,7 @@ describe('applyTextEdit', () => {
|
||||
3. this is third line
|
||||
`;
|
||||
|
||||
const actual = applyTextEdit(text, textEdit);
|
||||
const actual = TextEdit.apply(text, textEdit);
|
||||
|
||||
expect(actual).toBe(expected);
|
||||
});
|
||||
44
packages/foam-vscode/src/core/services/text-edit.ts
Normal file
44
packages/foam-vscode/src/core/services/text-edit.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import detectNewline from 'detect-newline';
|
||||
import { Position } from '../model/position';
|
||||
import { Range } from '../model/range';
|
||||
|
||||
export interface TextEdit {
|
||||
range: Range;
|
||||
newText: string;
|
||||
}
|
||||
|
||||
export abstract class TextEdit {
|
||||
/**
|
||||
*
|
||||
* @param text text on which the textEdit will be applied
|
||||
* @param textEdit
|
||||
* @returns {string} text with the applied textEdit
|
||||
*/
|
||||
public static apply(text: string, textEdit: TextEdit): string {
|
||||
const eol = detectNewline.graceful(text);
|
||||
const lines = text.split(eol);
|
||||
const characters = text.split('');
|
||||
const startOffset = getOffset(lines, textEdit.range.start, eol);
|
||||
const endOffset = getOffset(lines, textEdit.range.end, eol);
|
||||
const deleteCount = endOffset - startOffset;
|
||||
|
||||
const textToAppend = `${textEdit.newText}`;
|
||||
characters.splice(startOffset, deleteCount, textToAppend);
|
||||
return characters.join('');
|
||||
}
|
||||
}
|
||||
|
||||
const getOffset = (
|
||||
lines: string[],
|
||||
position: Position,
|
||||
eol: string
|
||||
): number => {
|
||||
const eolLen = eol.length;
|
||||
let offset = 0;
|
||||
let i = 0;
|
||||
while (i < position.line && i < lines.length) {
|
||||
offset = offset + lines[i].length + eolLen;
|
||||
i++;
|
||||
}
|
||||
return offset + Math.min(position.character, lines[i]?.length ?? 0);
|
||||
};
|
||||
@@ -1,5 +1,5 @@
|
||||
import { isSome } from './core';
|
||||
const HASHTAG_REGEX =
|
||||
export const HASHTAG_REGEX =
|
||||
/(?<=^|\s)#([0-9]*[\p{L}\p{Emoji_Presentation}/_-][\p{L}\p{Emoji_Presentation}\p{N}/_-]*)/gmu;
|
||||
const WORD_REGEX =
|
||||
/(?<=^|\s)([0-9]*[\p{L}\p{Emoji_Presentation}/_-][\p{L}\p{Emoji_Presentation}\p{N}/_-]*)/gmu;
|
||||
|
||||
@@ -86,9 +86,7 @@ export async function createDailyNoteIfNotExists(targetDate: Date) {
|
||||
|
||||
const templateFallbackText = `---
|
||||
foam_template:
|
||||
filepath: "${workspace.asRelativePath(
|
||||
toVsCodeUri(pathFromLegacyConfiguration)
|
||||
)}"
|
||||
filepath: "${pathFromLegacyConfiguration.toFsPath().replace(/\\/g, '\\\\')}"
|
||||
---
|
||||
# ${dateFormat(targetDate, titleFormat, false)}
|
||||
`;
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
showInEditor,
|
||||
} from '../../test/test-utils-vscode';
|
||||
import { fromVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { CREATE_NOTE_COMMAND } from './create-note';
|
||||
|
||||
describe('create-note command', () => {
|
||||
afterEach(() => {
|
||||
@@ -188,3 +189,17 @@ describe('create-note command', () => {
|
||||
await deleteFile(base);
|
||||
});
|
||||
});
|
||||
|
||||
describe('factories', () => {
|
||||
describe('forPlaceholder', () => {
|
||||
it('adds the .md extension to notes created for placeholders', async () => {
|
||||
await closeEditors();
|
||||
const command = CREATE_NOTE_COMMAND.forPlaceholder('my-placeholder');
|
||||
await commands.executeCommand(command.name, command.params);
|
||||
|
||||
const doc = window.activeTextEditor.document;
|
||||
expect(doc.uri.path).toMatch(/my-placeholder.md$/);
|
||||
expect(doc.getText()).toMatch(/^# my-placeholder/);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ import { Foam } from '../../core/model/foam';
|
||||
import { Resolver } from '../../services/variable-resolver';
|
||||
import { asAbsoluteWorkspaceUri, fileExists } from '../../services/editor';
|
||||
import { isSome } from '../../core/utils';
|
||||
import { CommandDescriptor } from '../../utils/commands';
|
||||
|
||||
interface CreateNoteArgs {
|
||||
/**
|
||||
@@ -34,11 +35,15 @@ interface CreateNoteArgs {
|
||||
/**
|
||||
* Variables to use in the text or template
|
||||
*/
|
||||
variables?: Map<string, string>;
|
||||
variables?: { [key: string]: string };
|
||||
/**
|
||||
* The date used to resolve the FOAM_DATE_* variables. in YYYY-MM-DD format
|
||||
*/
|
||||
date?: string;
|
||||
/**
|
||||
* The title of the note (translates into the FOAM_TITLE variable)
|
||||
*/
|
||||
title?: string;
|
||||
/**
|
||||
* What to do in case the target file already exists
|
||||
*/
|
||||
@@ -64,6 +69,9 @@ async function createNote(args: CreateNoteArgs) {
|
||||
new Map(Object.entries(args.variables ?? {})),
|
||||
date
|
||||
);
|
||||
if (args.title) {
|
||||
resolver.define('FOAM_TITLE', args.title);
|
||||
}
|
||||
const text = args.text ?? DEFAULT_NEW_NOTE_TEXT;
|
||||
const noteUri = args.notePath && URI.file(args.notePath);
|
||||
let templateUri: URI;
|
||||
@@ -101,12 +109,26 @@ async function createNote(args: CreateNoteArgs) {
|
||||
|
||||
export const CREATE_NOTE_COMMAND = {
|
||||
command: 'foam-vscode.create-note',
|
||||
title: 'Foam: Create Note',
|
||||
|
||||
asURI: (args: CreateNoteArgs) =>
|
||||
vscode.Uri.parse(`command:${CREATE_NOTE_COMMAND.command}`).with({
|
||||
query: encodeURIComponent(JSON.stringify(args)),
|
||||
}),
|
||||
forPlaceholder: (
|
||||
placeholder: string,
|
||||
extra: Partial<CreateNoteArgs> = {}
|
||||
): CommandDescriptor<CreateNoteArgs> => {
|
||||
const title = placeholder.endsWith('.md')
|
||||
? placeholder.replace(/\.md$/, '')
|
||||
: placeholder;
|
||||
const notePath = placeholder.endsWith('.md')
|
||||
? placeholder
|
||||
: placeholder + '.md';
|
||||
return {
|
||||
name: CREATE_NOTE_COMMAND.command,
|
||||
params: {
|
||||
title,
|
||||
notePath,
|
||||
...extra,
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const feature: FoamFeature = {
|
||||
|
||||
@@ -20,8 +20,8 @@ import { Foam } from '../../core/model/foam';
|
||||
import { Resource } from '../../core/model/note';
|
||||
import { generateHeading, generateLinkReferences } from '../../core/janitor';
|
||||
import { Range } from '../../core/model/range';
|
||||
import { applyTextEdit } from '../../core/janitor/apply-text-edit';
|
||||
import detectNewline from 'detect-newline';
|
||||
import { TextEdit } from '../../core/services/text-edit';
|
||||
|
||||
const feature: FoamFeature = {
|
||||
activate: (context: ExtensionContext, foamPromise: Promise<Foam>) => {
|
||||
@@ -130,8 +130,8 @@ async function runJanitor(foam: Foam) {
|
||||
// Note: The ordering matters. Definitions need to be inserted
|
||||
// before heading, since inserting a heading changes line numbers below
|
||||
let text = noteText;
|
||||
text = definitions ? applyTextEdit(text, definitions) : text;
|
||||
text = heading ? applyTextEdit(text, heading) : text;
|
||||
text = definitions ? TextEdit.apply(text, definitions) : text;
|
||||
text = heading ? TextEdit.apply(text, heading) : text;
|
||||
|
||||
return workspace.fs.writeFile(toVsCodeUri(note.uri), Buffer.from(text));
|
||||
});
|
||||
|
||||
100
packages/foam-vscode/src/features/commands/open-resource.spec.ts
Normal file
100
packages/foam-vscode/src/features/commands/open-resource.spec.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import dateFormat from 'dateformat';
|
||||
import { commands, window } from 'vscode';
|
||||
import { CommandDescriptor } from '../../utils/commands';
|
||||
import { OpenResourceArgs, OPEN_COMMAND } from './open-resource';
|
||||
import * as filter from '../../core/services/resource-filter';
|
||||
import { URI } from '../../core/model/uri';
|
||||
import { closeEditors, createFile } from '../../test/test-utils-vscode';
|
||||
import { deleteFile } from '../../services/editor';
|
||||
import waitForExpect from 'wait-for-expect';
|
||||
|
||||
describe('open-resource command', () => {
|
||||
beforeEach(async () => {
|
||||
await jest.resetAllMocks();
|
||||
await closeEditors();
|
||||
});
|
||||
|
||||
it('URI param has precedence over filter', async () => {
|
||||
const spy = jest.spyOn(filter, 'createFilter');
|
||||
const noteA = await createFile('Note A for open command');
|
||||
|
||||
const command: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
uri: noteA.uri,
|
||||
filter: { title: 'note 1' },
|
||||
},
|
||||
};
|
||||
await commands.executeCommand(command.name, command.params);
|
||||
|
||||
waitForExpect(() => {
|
||||
expect(window.activeTextEditor.document.uri.path).toEqual(noteA.uri.path);
|
||||
});
|
||||
expect(spy).not.toHaveBeenCalled();
|
||||
|
||||
await deleteFile(noteA.uri);
|
||||
});
|
||||
|
||||
it('URI param accept URI object, or path', async () => {
|
||||
const noteA = await createFile('Note A for open command');
|
||||
|
||||
const uriCommand: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
uri: URI.file('path/to/file.md'),
|
||||
},
|
||||
};
|
||||
await commands.executeCommand(uriCommand.name, uriCommand.params);
|
||||
waitForExpect(() => {
|
||||
expect(window.activeTextEditor.document.uri.path).toEqual(noteA.uri.path);
|
||||
});
|
||||
|
||||
await closeEditors();
|
||||
|
||||
const pathCommand: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
uri: URI.file('path/to/file.md'),
|
||||
},
|
||||
};
|
||||
await commands.executeCommand(pathCommand.name, pathCommand.params);
|
||||
waitForExpect(() => {
|
||||
expect(window.activeTextEditor.document.uri.path).toEqual(noteA.uri.path);
|
||||
});
|
||||
await deleteFile(noteA.uri);
|
||||
});
|
||||
|
||||
it('User is notified if no resource is found', async () => {
|
||||
const spy = jest.spyOn(window, 'showInformationMessage');
|
||||
|
||||
const command: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
filter: { title: 'note 1 with no existing title' },
|
||||
},
|
||||
};
|
||||
await commands.executeCommand(command.name, command.params);
|
||||
|
||||
waitForExpect(() => {
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('filter with multiple results will show a quick pick', async () => {
|
||||
const spy = jest
|
||||
.spyOn(window, 'showQuickPick')
|
||||
.mockImplementationOnce(jest.fn(() => Promise.resolve(undefined)));
|
||||
|
||||
const command: CommandDescriptor<OpenResourceArgs> = {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
filter: { title: '.*' },
|
||||
},
|
||||
};
|
||||
await commands.executeCommand(command.name, command.params);
|
||||
|
||||
waitForExpect(() => {
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,68 +1,119 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { FoamFeature } from '../../types';
|
||||
import { URI } from '../../core/model/uri';
|
||||
import { fromVsCodeUri, toVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { NoteFactory } from '../../services/templates';
|
||||
import { toVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { Foam } from '../../core/model/foam';
|
||||
import {
|
||||
createFilter,
|
||||
FilterDescriptor,
|
||||
} from '../../core/services/resource-filter';
|
||||
import { CommandDescriptor } from '../../utils/commands';
|
||||
import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { Resource } from '../../core/model/note';
|
||||
import { isSome, isNone } from '../../core/utils';
|
||||
import { Logger } from '../../core/utils/log';
|
||||
|
||||
export interface OpenResourceArgs {
|
||||
/**
|
||||
* The URI of the resource to open.
|
||||
* If present the `filter` param is ignored
|
||||
*/
|
||||
uri?: URI | string | vscode.Uri;
|
||||
|
||||
/**
|
||||
* The filter object that describes which notes to consider
|
||||
* for opening
|
||||
*/
|
||||
filter?: FilterDescriptor;
|
||||
}
|
||||
|
||||
export const OPEN_COMMAND = {
|
||||
command: 'foam-vscode.open-resource',
|
||||
title: 'Foam: Open Resource',
|
||||
|
||||
asURI: (uri: URI) =>
|
||||
vscode.Uri.parse(`command:${OPEN_COMMAND.command}`).with({
|
||||
query: encodeURIComponent(JSON.stringify({ uri })),
|
||||
}),
|
||||
forURI: (uri: URI): CommandDescriptor<OpenResourceArgs> => {
|
||||
return {
|
||||
name: OPEN_COMMAND.command,
|
||||
params: {
|
||||
uri: uri,
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
async function openResource(workspace: FoamWorkspace, args?: OpenResourceArgs) {
|
||||
args = args ?? {};
|
||||
|
||||
let item: { uri: URI } | null = null;
|
||||
|
||||
if (args.uri) {
|
||||
const path = typeof args.uri === 'string' ? args.uri : args.uri.path;
|
||||
item = workspace.find(path);
|
||||
}
|
||||
|
||||
if (isNone(item) && args.filter) {
|
||||
const resources = workspace.list();
|
||||
const candidates = resources.filter(
|
||||
createFilter(args.filter, vscode.workspace.isTrusted)
|
||||
);
|
||||
|
||||
if (candidates.length === 0) {
|
||||
vscode.window.showInformationMessage(
|
||||
'Foam: No note matches given filters.'
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
item =
|
||||
candidates.length === 1
|
||||
? candidates[0]
|
||||
: await vscode.window.showQuickPick(
|
||||
candidates.map(createQuickPickItemForResource)
|
||||
);
|
||||
}
|
||||
|
||||
if (isSome(item)) {
|
||||
const targetUri =
|
||||
item.uri.path === vscode.window.activeTextEditor?.document.uri.path
|
||||
? vscode.window.activeTextEditor?.document.uri
|
||||
: toVsCodeUri(item.uri.asPlain());
|
||||
return vscode.commands.executeCommand('vscode.open', targetUri);
|
||||
}
|
||||
}
|
||||
|
||||
const feature: FoamFeature = {
|
||||
activate: (context: vscode.ExtensionContext, foamPromise: Promise<Foam>) => {
|
||||
activate: async (
|
||||
context: vscode.ExtensionContext,
|
||||
foamPromise: Promise<Foam>
|
||||
) => {
|
||||
const foam = await foamPromise;
|
||||
context.subscriptions.push(
|
||||
vscode.commands.registerCommand(
|
||||
OPEN_COMMAND.command,
|
||||
async (params: { uri: URI }) => {
|
||||
const uri = new URI(params.uri);
|
||||
switch (uri.scheme) {
|
||||
case 'file': {
|
||||
const targetUri =
|
||||
uri.path === vscode.window.activeTextEditor?.document.uri.path
|
||||
? vscode.window.activeTextEditor?.document.uri
|
||||
: toVsCodeUri(uri.asPlain());
|
||||
// if the doc is already open, reuse the same colunm
|
||||
const targetEditor = vscode.window.visibleTextEditors.find(
|
||||
ed => targetUri.path === ed.document.uri.path
|
||||
);
|
||||
const column = targetEditor?.viewColumn;
|
||||
return vscode.commands.executeCommand('vscode.open', targetUri);
|
||||
}
|
||||
case 'placeholder': {
|
||||
const title = uri.getName();
|
||||
if (uri.isAbsolute()) {
|
||||
return NoteFactory.createForPlaceholderWikilink(
|
||||
title,
|
||||
URI.file(uri.path)
|
||||
);
|
||||
}
|
||||
const basedir =
|
||||
vscode.workspace.workspaceFolders.length > 0
|
||||
? vscode.workspace.workspaceFolders[0].uri
|
||||
: vscode.window.activeTextEditor?.document.uri
|
||||
? vscode.window.activeTextEditor!.document.uri
|
||||
: undefined;
|
||||
if (basedir === undefined) {
|
||||
return;
|
||||
}
|
||||
const target = fromVsCodeUri(basedir)
|
||||
.resolve(uri, true)
|
||||
.changeExtension('', '.md');
|
||||
await NoteFactory.createForPlaceholderWikilink(title, target);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
vscode.commands.registerCommand(OPEN_COMMAND.command, args => {
|
||||
return openResource(foam.workspace, args);
|
||||
})
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
interface ResourceItem extends vscode.QuickPickItem {
|
||||
label: string;
|
||||
description: string;
|
||||
uri: URI;
|
||||
detail?: string;
|
||||
}
|
||||
|
||||
const createQuickPickItemForResource = (resource: Resource): ResourceItem => {
|
||||
const icon = 'file';
|
||||
const sections = resource.sections
|
||||
.map(s => s.label)
|
||||
.filter(l => l !== resource.title);
|
||||
const detail = sections.length > 0 ? 'Sections: ' + sections.join(', ') : '';
|
||||
return {
|
||||
label: `$(${icon}) ${resource.title}`,
|
||||
description: vscode.workspace.asRelativePath(resource.uri.toFsPath()),
|
||||
uri: resource.uri,
|
||||
detail: detail,
|
||||
};
|
||||
};
|
||||
|
||||
export default feature;
|
||||
|
||||
@@ -14,6 +14,7 @@ import { Range } from '../core/model/range';
|
||||
import { FoamGraph } from '../core/model/graph';
|
||||
import { OPEN_COMMAND } from './commands/open-resource';
|
||||
import { CREATE_NOTE_COMMAND } from './commands/create-note';
|
||||
import { commandAsURI } from '../utils/commands';
|
||||
|
||||
export const CONFIG_KEY = 'links.hover.enable';
|
||||
|
||||
@@ -86,7 +87,7 @@ export class HoverProvider implements vscode.HoverProvider {
|
||||
);
|
||||
|
||||
const links = sources.slice(0, 10).map(ref => {
|
||||
const command = OPEN_COMMAND.asURI(ref);
|
||||
const command = commandAsURI(OPEN_COMMAND.forURI(ref));
|
||||
return `- [${this.workspace.get(ref).title}](${command.toString()})`;
|
||||
});
|
||||
|
||||
@@ -108,27 +109,14 @@ export class HoverProvider implements vscode.HoverProvider {
|
||||
: this.workspace.get(targetUri).title;
|
||||
}
|
||||
|
||||
// If placeholder, offer to create a new note from template (compared to default link provider - not from template)
|
||||
const basedir =
|
||||
vscode.workspace.workspaceFolders.length > 0
|
||||
? vscode.workspace.workspaceFolders[0].uri
|
||||
: vscode.window.activeTextEditor?.document.uri
|
||||
? vscode.window.activeTextEditor!.document.uri
|
||||
: undefined;
|
||||
if (basedir === undefined) {
|
||||
return;
|
||||
}
|
||||
const target = fromVsCodeUri(basedir)
|
||||
.resolve(targetUri, true)
|
||||
.changeExtension('', '.md');
|
||||
const args = {
|
||||
text: target.getName(),
|
||||
notePath: target.path,
|
||||
const command = CREATE_NOTE_COMMAND.forPlaceholder(targetUri.path, {
|
||||
askForTemplate: true,
|
||||
};
|
||||
const command = CREATE_NOTE_COMMAND.asURI(args);
|
||||
onFileExists: 'open',
|
||||
});
|
||||
const newNoteFromTemplate = new vscode.MarkdownString(
|
||||
`[Create note from template for '${targetUri.getName()}'](${command})`
|
||||
`[Create note from template for '${targetUri.getName()}'](${commandAsURI(
|
||||
command
|
||||
).toString()})`
|
||||
);
|
||||
newNoteFromTemplate.isTrusted = true;
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ import { OPEN_COMMAND } from './commands/open-resource';
|
||||
import { toVsCodeUri } from '../utils/vsc-utils';
|
||||
import { createMarkdownParser } from '../core/services/markdown-parser';
|
||||
import { FoamGraph } from '../core/model/graph';
|
||||
import { commandAsURI } from '../utils/commands';
|
||||
import { CREATE_NOTE_COMMAND } from './commands/create-note';
|
||||
|
||||
describe('Document navigation', () => {
|
||||
const parser = createMarkdownParser([]);
|
||||
@@ -82,7 +84,11 @@ describe('Document navigation', () => {
|
||||
|
||||
expect(links.length).toEqual(1);
|
||||
expect(links[0].target).toEqual(
|
||||
OPEN_COMMAND.asURI(URI.placeholder('a placeholder'))
|
||||
commandAsURI(
|
||||
CREATE_NOTE_COMMAND.forPlaceholder('a placeholder', {
|
||||
onFileExists: 'open',
|
||||
})
|
||||
)
|
||||
);
|
||||
expect(links[0].range).toEqual(new vscode.Range(0, 20, 0, 33));
|
||||
});
|
||||
|
||||
@@ -2,7 +2,6 @@ import * as vscode from 'vscode';
|
||||
import { FoamFeature } from '../types';
|
||||
import { mdDocSelector } from '../utils';
|
||||
import { toVsCodeRange, toVsCodeUri, fromVsCodeUri } from '../utils/vsc-utils';
|
||||
import { OPEN_COMMAND } from './commands/open-resource';
|
||||
import { Foam } from '../core/model/foam';
|
||||
import { FoamWorkspace } from '../core/model/workspace';
|
||||
import { Resource, ResourceLink, ResourceParser } from '../core/model/note';
|
||||
@@ -10,6 +9,8 @@ import { URI } from '../core/model/uri';
|
||||
import { Range } from '../core/model/range';
|
||||
import { FoamGraph } from '../core/model/graph';
|
||||
import { Position } from '../core/model/position';
|
||||
import { CREATE_NOTE_COMMAND } from './commands/create-note';
|
||||
import { commandAsURI } from '../utils/commands';
|
||||
|
||||
const feature: FoamFeature = {
|
||||
activate: async (
|
||||
@@ -163,7 +164,9 @@ export class NavigationProvider
|
||||
return targets
|
||||
.filter(o => o.target.isPlaceholder()) // links to resources are managed by the definition provider
|
||||
.map(o => {
|
||||
const command = OPEN_COMMAND.asURI(o.target);
|
||||
const command = CREATE_NOTE_COMMAND.forPlaceholder(o.target.path, {
|
||||
onFileExists: 'open',
|
||||
});
|
||||
|
||||
const documentLink = new vscode.DocumentLink(
|
||||
new vscode.Range(
|
||||
@@ -172,7 +175,7 @@ export class NavigationProvider
|
||||
o.link.range.end.line,
|
||||
o.link.range.end.character - 2
|
||||
),
|
||||
command
|
||||
commandAsURI(command)
|
||||
);
|
||||
documentLink.tooltip = `Create note for '${o.target.path}'`;
|
||||
return documentLink;
|
||||
|
||||
@@ -6,12 +6,15 @@ import {
|
||||
createNote,
|
||||
getUriInWorkspace,
|
||||
} from '../../test/test-utils-vscode';
|
||||
import { BacklinksTreeDataProvider, BacklinkTreeItem } from './backlinks';
|
||||
import { ResourceTreeItem } from '../../utils/grouped-resources-tree-data-provider';
|
||||
import { BacklinksTreeDataProvider } from './backlinks';
|
||||
import { OPEN_COMMAND } from '../commands/open-resource';
|
||||
import { toVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { FoamGraph } from '../../core/model/graph';
|
||||
import { URI } from '../../core/model/uri';
|
||||
import {
|
||||
ResourceRangeTreeItem,
|
||||
ResourceTreeItem,
|
||||
} from '../../utils/tree-view-utils';
|
||||
|
||||
describe('Backlinks panel', () => {
|
||||
beforeAll(async () => {
|
||||
@@ -84,19 +87,19 @@ describe('Backlinks panel', () => {
|
||||
const notes = (await provider.getChildren()) as ResourceTreeItem[];
|
||||
const linksFromB = (await provider.getChildren(
|
||||
notes[0]
|
||||
)) as BacklinkTreeItem[];
|
||||
expect(linksFromB.map(l => l.link)).toEqual(
|
||||
noteB.links.sort(
|
||||
(a, b) => a.range.start.character - b.range.start.character
|
||||
)
|
||||
)) as ResourceRangeTreeItem[];
|
||||
expect(linksFromB.map(l => l.range)).toEqual(
|
||||
noteB.links
|
||||
.map(l => l.range)
|
||||
.sort((a, b) => a.start.character - b.start.character)
|
||||
);
|
||||
});
|
||||
it('navigates to the document if clicking on note', async () => {
|
||||
provider.target = noteA.uri;
|
||||
const notes = (await provider.getChildren()) as ResourceTreeItem[];
|
||||
expect(notes[0].command).toMatchObject({
|
||||
command: OPEN_COMMAND.command,
|
||||
arguments: [expect.objectContaining({ uri: noteB.uri })],
|
||||
command: 'vscode.open',
|
||||
arguments: [expect.objectContaining({ path: noteB.uri.path })],
|
||||
});
|
||||
});
|
||||
it('navigates to document with link selection if clicking on backlink', async () => {
|
||||
@@ -104,11 +107,11 @@ describe('Backlinks panel', () => {
|
||||
const notes = (await provider.getChildren()) as ResourceTreeItem[];
|
||||
const linksFromB = (await provider.getChildren(
|
||||
notes[0]
|
||||
)) as BacklinkTreeItem[];
|
||||
)) as ResourceRangeTreeItem[];
|
||||
expect(linksFromB[0].command).toMatchObject({
|
||||
command: 'vscode.open',
|
||||
arguments: [
|
||||
noteB.uri,
|
||||
expect.objectContaining({ path: noteB.uri.path }),
|
||||
{
|
||||
selection: expect.arrayContaining([]),
|
||||
},
|
||||
|
||||
@@ -1,16 +1,17 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { groupBy } from 'lodash';
|
||||
import { URI } from '../../core/model/uri';
|
||||
|
||||
import { getNoteTooltip, isNone } from '../../utils';
|
||||
import { isNone } from '../../utils';
|
||||
import { FoamFeature } from '../../types';
|
||||
import { ResourceTreeItem } from '../../utils/grouped-resources-tree-data-provider';
|
||||
import { Foam } from '../../core/model/foam';
|
||||
import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { FoamGraph } from '../../core/model/graph';
|
||||
import { Resource, ResourceLink } from '../../core/model/note';
|
||||
import { Range } from '../../core/model/range';
|
||||
import { fromVsCodeUri, toVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { fromVsCodeUri } from '../../utils/vsc-utils';
|
||||
import {
|
||||
ResourceRangeTreeItem,
|
||||
ResourceTreeItem,
|
||||
groupRangesByResource,
|
||||
} from '../../utils/tree-view-utils';
|
||||
|
||||
const feature: FoamFeature = {
|
||||
activate: async (
|
||||
@@ -37,7 +38,7 @@ const feature: FoamFeature = {
|
||||
export default feature;
|
||||
|
||||
export class BacklinksTreeDataProvider
|
||||
implements vscode.TreeDataProvider<BacklinkPanelTreeItem>
|
||||
implements vscode.TreeDataProvider<vscode.TreeItem>
|
||||
{
|
||||
public target?: URI = undefined;
|
||||
// prettier-ignore
|
||||
@@ -54,67 +55,32 @@ export class BacklinksTreeDataProvider
|
||||
return item;
|
||||
}
|
||||
|
||||
getChildren(item?: ResourceTreeItem): Promise<BacklinkPanelTreeItem[]> {
|
||||
getChildren(item?: BacklinkPanelTreeItem): Promise<vscode.TreeItem[]> {
|
||||
const uri = this.target;
|
||||
if (item) {
|
||||
const resource = item.resource;
|
||||
|
||||
const backlinkRefs = Promise.all(
|
||||
resource.links
|
||||
.filter(link =>
|
||||
this.workspace.resolveLink(resource, link).asPlain().isEqual(uri)
|
||||
)
|
||||
.map(async link => {
|
||||
const item = new BacklinkTreeItem(resource, link);
|
||||
const lines = (
|
||||
(await this.workspace.readAsMarkdown(resource.uri)) ?? ''
|
||||
).split('\n');
|
||||
if (link.range.start.line < lines.length) {
|
||||
const line = lines[link.range.start.line];
|
||||
const start = Math.max(0, link.range.start.character - 15);
|
||||
const ellipsis = start === 0 ? '' : '...';
|
||||
|
||||
item.label = `${link.range.start.line}: ${ellipsis}${line.substr(
|
||||
start,
|
||||
300
|
||||
)}`;
|
||||
item.tooltip = getNoteTooltip(line);
|
||||
}
|
||||
return item;
|
||||
})
|
||||
);
|
||||
|
||||
return backlinkRefs;
|
||||
if (item && item instanceof ResourceTreeItem) {
|
||||
return item.getChildren();
|
||||
}
|
||||
|
||||
if (isNone(uri) || isNone(this.workspace.find(uri))) {
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
|
||||
const backlinksByResourcePath = groupBy(
|
||||
this.graph
|
||||
.getConnections(uri)
|
||||
.filter(c => c.target.asPlain().isEqual(uri)),
|
||||
b => b.source.path
|
||||
);
|
||||
const connections = this.graph
|
||||
.getConnections(uri)
|
||||
.filter(c => c.target.asPlain().isEqual(uri));
|
||||
|
||||
const resources = Object.keys(backlinksByResourcePath)
|
||||
.map(res => backlinksByResourcePath[res][0].source)
|
||||
.map(uri => this.workspace.get(uri))
|
||||
.sort(Resource.sortByTitle)
|
||||
.map(note => {
|
||||
const connections = backlinksByResourcePath[note.uri.path].sort(
|
||||
(a, b) => Range.isBefore(a.link.range, b.link.range)
|
||||
);
|
||||
const item = new ResourceTreeItem(
|
||||
note,
|
||||
this.workspace,
|
||||
vscode.TreeItemCollapsibleState.Expanded
|
||||
);
|
||||
item.description = `(${connections.length}) ${item.description}`;
|
||||
return item;
|
||||
});
|
||||
return Promise.resolve(resources);
|
||||
const backlinkItems = connections.map(c =>
|
||||
ResourceRangeTreeItem.createStandardItem(
|
||||
this.workspace,
|
||||
this.workspace.get(c.source),
|
||||
c.link.range
|
||||
)
|
||||
);
|
||||
return groupRangesByResource(
|
||||
this.workspace,
|
||||
backlinkItems,
|
||||
vscode.TreeItemCollapsibleState.Expanded
|
||||
);
|
||||
}
|
||||
|
||||
resolveTreeItem(item: BacklinkPanelTreeItem): Promise<BacklinkPanelTreeItem> {
|
||||
@@ -122,23 +88,4 @@ export class BacklinksTreeDataProvider
|
||||
}
|
||||
}
|
||||
|
||||
export class BacklinkTreeItem extends vscode.TreeItem {
|
||||
constructor(
|
||||
public readonly resource: Resource,
|
||||
public readonly link: ResourceLink
|
||||
) {
|
||||
super(link.rawText, vscode.TreeItemCollapsibleState.None);
|
||||
this.label = `${link.range.start.line}: ${this.label}`;
|
||||
this.command = {
|
||||
command: 'vscode.open',
|
||||
arguments: [toVsCodeUri(resource.uri), { selection: link.range }],
|
||||
title: 'Go to link',
|
||||
};
|
||||
}
|
||||
|
||||
resolveTreeItem(): Promise<BacklinkTreeItem> {
|
||||
return Promise.resolve(this);
|
||||
}
|
||||
}
|
||||
|
||||
type BacklinkPanelTreeItem = ResourceTreeItem | BacklinkTreeItem;
|
||||
type BacklinkPanelTreeItem = ResourceTreeItem | ResourceRangeTreeItem;
|
||||
|
||||
@@ -3,11 +3,8 @@ import { Foam } from '../../core/model/foam';
|
||||
import { createMatcherAndDataStore } from '../../services/editor';
|
||||
import { getOrphansConfig } from '../../settings';
|
||||
import { FoamFeature } from '../../types';
|
||||
import {
|
||||
GroupedResourcesTreeDataProvider,
|
||||
ResourceTreeItem,
|
||||
UriTreeItem,
|
||||
} from '../../utils/grouped-resources-tree-data-provider';
|
||||
import { GroupedResourcesTreeDataProvider } from '../../utils/grouped-resources-tree-data-provider';
|
||||
import { ResourceTreeItem, UriTreeItem } from '../../utils/tree-view-utils';
|
||||
|
||||
const EXCLUDE_TYPES = ['image', 'attachment'];
|
||||
const feature: FoamFeature = {
|
||||
@@ -23,6 +20,8 @@ const feature: FoamFeature = {
|
||||
const provider = new GroupedResourcesTreeDataProvider(
|
||||
'orphans',
|
||||
'orphan',
|
||||
context.globalState,
|
||||
matcher,
|
||||
() =>
|
||||
foam.graph
|
||||
.getAllNodes()
|
||||
@@ -35,21 +34,20 @@ const feature: FoamFeature = {
|
||||
return uri.isPlaceholder()
|
||||
? new UriTreeItem(uri)
|
||||
: new ResourceTreeItem(foam.workspace.find(uri), foam.workspace);
|
||||
},
|
||||
matcher
|
||||
}
|
||||
);
|
||||
provider.setGroupBy(getOrphansConfig().groupBy);
|
||||
|
||||
const treeView = vscode.window.createTreeView('foam-vscode.orphans', {
|
||||
treeDataProvider: provider,
|
||||
showCollapseAll: true,
|
||||
});
|
||||
provider.refresh();
|
||||
const baseTitle = treeView.title;
|
||||
treeView.title = baseTitle + ` (${provider.numElements})`;
|
||||
|
||||
context.subscriptions.push(
|
||||
vscode.window.registerTreeDataProvider('foam-vscode.orphans', provider),
|
||||
...provider.commands,
|
||||
provider,
|
||||
foam.graph.onDidUpdate(() => {
|
||||
provider.refresh();
|
||||
treeView.title = baseTitle + ` (${provider.numElements})`;
|
||||
|
||||
@@ -3,10 +3,16 @@ import { Foam } from '../../core/model/foam';
|
||||
import { createMatcherAndDataStore } from '../../services/editor';
|
||||
import { getPlaceholdersConfig } from '../../settings';
|
||||
import { FoamFeature } from '../../types';
|
||||
import { GroupedResourcesTreeDataProvider } from '../../utils/grouped-resources-tree-data-provider';
|
||||
import {
|
||||
GroupedResourcesTreeDataProvider,
|
||||
ResourceRangeTreeItem,
|
||||
UriTreeItem,
|
||||
} from '../../utils/grouped-resources-tree-data-provider';
|
||||
groupRangesByResource,
|
||||
} from '../../utils/tree-view-utils';
|
||||
import { IMatcher } from '../../core/services/datastore';
|
||||
import { ContextMemento, fromVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { FoamGraph } from '../../core/model/graph';
|
||||
import { URI } from '../../core/model/uri';
|
||||
|
||||
const feature: FoamFeature = {
|
||||
activate: async (
|
||||
@@ -17,16 +23,11 @@ const feature: FoamFeature = {
|
||||
const { matcher } = await createMatcherAndDataStore(
|
||||
getPlaceholdersConfig().exclude
|
||||
);
|
||||
const provider = new GroupedResourcesTreeDataProvider(
|
||||
'placeholders',
|
||||
'placeholder',
|
||||
() => foam.graph.getAllNodes().filter(uri => uri.isPlaceholder()),
|
||||
uri => {
|
||||
return new UriTreeItem(uri);
|
||||
},
|
||||
const provider = new PlaceholderTreeView(
|
||||
context.globalState,
|
||||
foam,
|
||||
matcher
|
||||
);
|
||||
provider.setGroupBy(getPlaceholdersConfig().groupBy);
|
||||
|
||||
const treeView = vscode.window.createTreeView('foam-vscode.placeholders', {
|
||||
treeDataProvider: provider,
|
||||
@@ -34,16 +35,93 @@ const feature: FoamFeature = {
|
||||
});
|
||||
const baseTitle = treeView.title;
|
||||
treeView.title = baseTitle + ` (${provider.numElements})`;
|
||||
provider.refresh();
|
||||
|
||||
context.subscriptions.push(
|
||||
treeView,
|
||||
...provider.commands,
|
||||
provider,
|
||||
foam.graph.onDidUpdate(() => {
|
||||
provider.refresh();
|
||||
}),
|
||||
provider.onDidChangeTreeData(() => {
|
||||
treeView.title = baseTitle + ` (${provider.numElements})`;
|
||||
}),
|
||||
vscode.window.onDidChangeActiveTextEditor(() => {
|
||||
if (provider.show.get() === 'for-current-file') {
|
||||
provider.refresh();
|
||||
}
|
||||
})
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
export class PlaceholderTreeView extends GroupedResourcesTreeDataProvider {
|
||||
private graph: FoamGraph;
|
||||
public show = new ContextMemento<'all' | 'for-current-file'>(
|
||||
this.state,
|
||||
`foam-vscode.views.${this.providerId}.show`,
|
||||
'all'
|
||||
);
|
||||
|
||||
public constructor(state: vscode.Memento, foam: Foam, matcher: IMatcher) {
|
||||
super(
|
||||
'placeholders',
|
||||
'placeholder',
|
||||
state,
|
||||
matcher,
|
||||
() => {
|
||||
// we override computeResources below (as we can't use "this" here)
|
||||
throw new Error('Not implemented');
|
||||
},
|
||||
uri => {
|
||||
return new UriTreeItem(uri, {
|
||||
icon: 'link',
|
||||
getChildren: async () => {
|
||||
return groupRangesByResource(
|
||||
foam.workspace,
|
||||
foam.graph.getBacklinks(uri).map(link => {
|
||||
return ResourceRangeTreeItem.createStandardItem(
|
||||
foam.workspace,
|
||||
foam.workspace.get(link.source),
|
||||
link.link.range
|
||||
);
|
||||
})
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
this.graph = foam.graph;
|
||||
this.disposables.push(
|
||||
vscode.commands.registerCommand(
|
||||
`foam-vscode.views.${this.providerId}.show:all`,
|
||||
() => {
|
||||
this.show.update('all');
|
||||
this.refresh();
|
||||
}
|
||||
),
|
||||
vscode.commands.registerCommand(
|
||||
`foam-vscode.views.${this.providerId}.show:for-current-file`,
|
||||
() => {
|
||||
this.show.update('for-current-file');
|
||||
this.refresh();
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
computeResources = (): URI[] => {
|
||||
if (this.show.get() === 'for-current-file') {
|
||||
const currentFile = vscode.window.activeTextEditor?.document.uri;
|
||||
return currentFile
|
||||
? this.graph
|
||||
.getLinks(fromVsCodeUri(currentFile))
|
||||
.map(link => link.target)
|
||||
.filter(uri => uri.isPlaceholder())
|
||||
: [];
|
||||
}
|
||||
return this.graph.getAllNodes().filter(uri => uri.isPlaceholder());
|
||||
};
|
||||
}
|
||||
|
||||
export default feature;
|
||||
|
||||
@@ -1,65 +1,49 @@
|
||||
import {
|
||||
createTestNote,
|
||||
readFileFromFs,
|
||||
TEST_DATA_DIR,
|
||||
} from '../../test/test-utils';
|
||||
import { createTestNote } from '../../test/test-utils';
|
||||
import { cleanWorkspace, closeEditors } from '../../test/test-utils-vscode';
|
||||
import { TagItem, TagReference, TagsProvider } from './tags-explorer';
|
||||
import { bootstrap, Foam } from '../../core/model/foam';
|
||||
import { MarkdownResourceProvider } from '../../core/services/markdown-provider';
|
||||
import { createMarkdownParser } from '../../core/services/markdown-parser';
|
||||
import { URI } from '../../core/model/uri';
|
||||
import { FileDataStore, Matcher } from '../../test/test-datastore';
|
||||
import { TagItem, TagsProvider } from './tags-explorer';
|
||||
import { FoamTags } from '../../core/model/tags';
|
||||
import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { ResourceTreeItem } from '../../utils/tree-view-utils';
|
||||
|
||||
describe('Tags tree panel', () => {
|
||||
let _foam: Foam;
|
||||
let provider: TagsProvider;
|
||||
|
||||
const dataStore = new FileDataStore(readFileFromFs, TEST_DATA_DIR.toFsPath());
|
||||
const matcher = new Matcher([URI.file(TEST_DATA_DIR.toFsPath())]);
|
||||
const parser = createMarkdownParser();
|
||||
const mdProvider = new MarkdownResourceProvider(dataStore, parser);
|
||||
|
||||
beforeAll(async () => {
|
||||
await cleanWorkspace();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
_foam.dispose();
|
||||
await cleanWorkspace();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
_foam = await bootstrap(matcher, undefined, dataStore, parser, [
|
||||
mdProvider,
|
||||
]);
|
||||
provider = new TagsProvider(_foam, _foam.workspace);
|
||||
await closeEditors();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
_foam.dispose();
|
||||
});
|
||||
|
||||
it('correctly provides a tag from a set of notes', async () => {
|
||||
it('provides a tag from a set of notes', async () => {
|
||||
const noteA = createTestNote({
|
||||
tags: ['test'],
|
||||
uri: './note-a.md',
|
||||
});
|
||||
_foam.workspace.set(noteA);
|
||||
const workspace = new FoamWorkspace().set(noteA);
|
||||
const foamTags = FoamTags.fromWorkspace(workspace);
|
||||
const provider = new TagsProvider(foamTags, workspace);
|
||||
provider.refresh();
|
||||
|
||||
const treeItems = (await provider.getChildren()) as TagItem[];
|
||||
|
||||
treeItems.forEach(item => expect(item.tag).toContain('test'));
|
||||
expect(treeItems).toHaveLength(1);
|
||||
expect(treeItems[0].label).toEqual('test');
|
||||
expect(treeItems[0].tag).toEqual('test');
|
||||
expect(treeItems[0].nResourcesInSubtree).toEqual(1);
|
||||
});
|
||||
|
||||
it('correctly handles a parent and child tag', async () => {
|
||||
it('handles a simple parent and child tag', async () => {
|
||||
const noteA = createTestNote({
|
||||
tags: ['parent/child'],
|
||||
uri: './note-a.md',
|
||||
});
|
||||
_foam.workspace.set(noteA);
|
||||
const workspace = new FoamWorkspace().set(noteA);
|
||||
const foamTags = FoamTags.fromWorkspace(workspace);
|
||||
const provider = new TagsProvider(foamTags, workspace);
|
||||
provider.refresh();
|
||||
|
||||
const parentTreeItems = (await provider.getChildren()) as TagItem[];
|
||||
@@ -78,17 +62,18 @@ describe('Tags tree panel', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('correctly handles a single parent and multiple child tag', async () => {
|
||||
it('handles a single parent and multiple child tag', async () => {
|
||||
const noteA = createTestNote({
|
||||
tags: ['parent/child'],
|
||||
uri: './note-a.md',
|
||||
});
|
||||
_foam.workspace.set(noteA);
|
||||
const noteB = createTestNote({
|
||||
tags: ['parent/subchild'],
|
||||
uri: './note-b.md',
|
||||
});
|
||||
_foam.workspace.set(noteB);
|
||||
const workspace = new FoamWorkspace().set(noteA).set(noteB);
|
||||
const foamTags = FoamTags.fromWorkspace(workspace);
|
||||
const provider = new TagsProvider(foamTags, workspace);
|
||||
provider.refresh();
|
||||
|
||||
const parentTreeItems = (await provider.getChildren()) as TagItem[];
|
||||
@@ -114,14 +99,15 @@ describe('Tags tree panel', () => {
|
||||
expect(childTreeItems).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('correctly handles a single parent and child tag in the same note', async () => {
|
||||
it('handles a parent and child tag in the same note', async () => {
|
||||
const noteC = createTestNote({
|
||||
tags: ['main', 'main/subtopic'],
|
||||
title: 'Test note',
|
||||
uri: './note-c.md',
|
||||
});
|
||||
|
||||
_foam.workspace.set(noteC);
|
||||
const workspace = new FoamWorkspace().set(noteC);
|
||||
const foamTags = FoamTags.fromWorkspace(workspace);
|
||||
const provider = new TagsProvider(foamTags, workspace);
|
||||
|
||||
provider.refresh();
|
||||
|
||||
@@ -137,9 +123,9 @@ describe('Tags tree panel', () => {
|
||||
)) as TagItem[];
|
||||
|
||||
childTreeItems
|
||||
.filter(item => item instanceof TagReference)
|
||||
.filter(item => item instanceof ResourceTreeItem)
|
||||
.forEach(item => {
|
||||
expect(item.title).toEqual('Test note');
|
||||
expect(item.label).toEqual('Test note');
|
||||
});
|
||||
|
||||
childTreeItems
|
||||
@@ -151,4 +137,36 @@ describe('Tags tree panel', () => {
|
||||
|
||||
expect(childTreeItems).toHaveLength(3);
|
||||
});
|
||||
|
||||
it('handles a tag with multiple levels of hierarchy - #1134', async () => {
|
||||
const noteA = createTestNote({
|
||||
tags: ['parent/child/second'],
|
||||
uri: './note-a.md',
|
||||
});
|
||||
const workspace = new FoamWorkspace().set(noteA);
|
||||
const foamTags = FoamTags.fromWorkspace(workspace);
|
||||
const provider = new TagsProvider(foamTags, workspace);
|
||||
|
||||
provider.refresh();
|
||||
|
||||
const parentTreeItems = (await provider.getChildren()) as TagItem[];
|
||||
const parentTagItem = parentTreeItems.pop();
|
||||
expect(parentTagItem.title).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');
|
||||
|
||||
const grandchildTreeItems = (await provider.getChildren(
|
||||
childTreeItems[1]
|
||||
)) as TagItem[];
|
||||
|
||||
expect(grandchildTreeItems).toHaveLength(2);
|
||||
expect(grandchildTreeItems[0].label).toMatch(/^Search.*/);
|
||||
expect(grandchildTreeItems[1].label).toEqual('second');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,6 +6,12 @@ import { toVsCodeRange, toVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { Foam } from '../../core/model/foam';
|
||||
import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { Resource, Tag } from '../../core/model/note';
|
||||
import { FoamTags } from '../../core/model/tags';
|
||||
import {
|
||||
ResourceRangeTreeItem,
|
||||
ResourceTreeItem,
|
||||
groupRangesByResource,
|
||||
} from '../../utils/tree-view-utils';
|
||||
|
||||
const TAG_SEPARATOR = '/';
|
||||
const feature: FoamFeature = {
|
||||
@@ -14,7 +20,7 @@ const feature: FoamFeature = {
|
||||
foamPromise: Promise<Foam>
|
||||
) => {
|
||||
const foam = await foamPromise;
|
||||
const provider = new TagsProvider(foam, foam.workspace);
|
||||
const provider = new TagsProvider(foam.tags, foam.workspace);
|
||||
const treeView = vscode.window.createTreeView('foam-vscode.tags-explorer', {
|
||||
treeDataProvider: provider,
|
||||
showCollapseAll: true,
|
||||
@@ -48,7 +54,10 @@ export class TagsProvider implements vscode.TreeDataProvider<TagTreeItem> {
|
||||
notes: URI[];
|
||||
}[];
|
||||
|
||||
constructor(private foam: Foam, private workspace: FoamWorkspace) {
|
||||
private foamTags: FoamTags;
|
||||
|
||||
constructor(tags: FoamTags, private workspace: FoamWorkspace) {
|
||||
this.foamTags = tags;
|
||||
this.computeTags();
|
||||
}
|
||||
|
||||
@@ -58,7 +67,7 @@ export class TagsProvider implements vscode.TreeDataProvider<TagTreeItem> {
|
||||
}
|
||||
|
||||
private computeTags() {
|
||||
this.tags = [...this.foam.tags.tags]
|
||||
this.tags = [...this.foamTags.tags]
|
||||
.map(([tag, notes]) => ({ tag, notes }))
|
||||
.sort((a, b) => a.tag.localeCompare(b.tag));
|
||||
}
|
||||
@@ -67,78 +76,92 @@ export class TagsProvider implements vscode.TreeDataProvider<TagTreeItem> {
|
||||
return element;
|
||||
}
|
||||
|
||||
getChildren(element?: TagItem): Promise<TagTreeItem[]> {
|
||||
if (element) {
|
||||
const nestedTagItems: TagTreeItem[] = this.tags
|
||||
.filter(item => item.tag.indexOf(element.title + TAG_SEPARATOR) > -1)
|
||||
.map(
|
||||
item =>
|
||||
new TagItem(
|
||||
item.tag,
|
||||
item.tag.substring(item.tag.indexOf(TAG_SEPARATOR) + 1),
|
||||
item.notes
|
||||
)
|
||||
)
|
||||
.sort((a, b) => a.title.localeCompare(b.title));
|
||||
|
||||
const references: TagTreeItem[] = element.notes
|
||||
.map(uri => this.foam.workspace.get(uri))
|
||||
.reduce((acc, note) => {
|
||||
const tags = note.tags.filter(t => t.label === element.tag);
|
||||
return [
|
||||
...acc,
|
||||
...tags.slice(0, 1).map(t => new TagReference(t, note)),
|
||||
];
|
||||
}, [])
|
||||
.sort((a, b) => a.title.localeCompare(b.title));
|
||||
|
||||
return Promise.resolve([
|
||||
new TagSearch(element.tag),
|
||||
...nestedTagItems,
|
||||
...references,
|
||||
]);
|
||||
async getChildren(element?: TagItem): Promise<TagTreeItem[]> {
|
||||
if ((element as any)?.getChildren) {
|
||||
const children = await (element as any).getChildren();
|
||||
return children;
|
||||
}
|
||||
if (!element) {
|
||||
const tags: TagItem[] = this.tags
|
||||
.map(({ tag, notes }) => {
|
||||
const parentTag =
|
||||
tag.indexOf(TAG_SEPARATOR) > 0
|
||||
? tag.substring(0, tag.indexOf(TAG_SEPARATOR))
|
||||
: tag;
|
||||
const parentTag = element ? element.tag : '';
|
||||
const parentPrefix = element ? parentTag + TAG_SEPARATOR : '';
|
||||
|
||||
return new TagItem(parentTag, parentTag, notes);
|
||||
})
|
||||
.filter(
|
||||
(value, index, array) =>
|
||||
array.findIndex(tag => tag.title === value.title) === index
|
||||
const tagsAtThisLevel = this.tags
|
||||
.filter(({ tag }) => tag.startsWith(parentPrefix))
|
||||
.map(({ tag }) => {
|
||||
const nextSeparator = tag.indexOf(TAG_SEPARATOR, parentPrefix.length);
|
||||
const label =
|
||||
nextSeparator > -1
|
||||
? tag.substring(parentPrefix.length, nextSeparator)
|
||||
: tag.substring(parentPrefix.length);
|
||||
const tagId = parentPrefix + label;
|
||||
return { label, tagId, tag };
|
||||
})
|
||||
.reduce((acc, { label, tagId, tag }) => {
|
||||
const existing = acc.has(label);
|
||||
const nResources = this.foamTags.tags.get(tag).length ?? 0;
|
||||
if (!existing) {
|
||||
acc.set(label, { label, tagId, nResources: 0 });
|
||||
}
|
||||
acc.get(label).nResources += nResources;
|
||||
return acc;
|
||||
}, new Map() as Map<string, { label: string; tagId: string; nResources: number }>);
|
||||
|
||||
const subtags = Array.from(tagsAtThisLevel.values())
|
||||
.map(({ label, tagId, nResources }) => {
|
||||
const resources = this.foamTags.tags.get(tagId) ?? [];
|
||||
return new TagItem(tagId, label, nResources, resources);
|
||||
})
|
||||
.sort((a, b) => a.title.localeCompare(b.title));
|
||||
|
||||
const resourceTags: ResourceRangeTreeItem[] = (element?.notes ?? [])
|
||||
.map(uri => this.workspace.get(uri))
|
||||
.reduce((acc, note) => {
|
||||
const tags = note.tags.filter(t => t.label === element.tag);
|
||||
const items = tags.map(t =>
|
||||
ResourceRangeTreeItem.createStandardItem(
|
||||
this.workspace,
|
||||
note,
|
||||
t.range
|
||||
)
|
||||
);
|
||||
return [...acc, ...items];
|
||||
}, []);
|
||||
|
||||
return Promise.resolve(tags.sort((a, b) => a.tag.localeCompare(b.tag)));
|
||||
}
|
||||
const resources = await groupRangesByResource(this.workspace, resourceTags);
|
||||
|
||||
return Promise.resolve(
|
||||
[element && new TagSearch(element.tag), ...subtags, ...resources].filter(
|
||||
Boolean
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
async resolveTreeItem(item: TagTreeItem): Promise<TagTreeItem> {
|
||||
if (item instanceof TagReference) {
|
||||
const content = await this.workspace.readAsMarkdown(item.note.uri);
|
||||
if (isSome(content)) {
|
||||
item.tooltip = getNoteTooltip(content);
|
||||
}
|
||||
if (
|
||||
item instanceof ResourceTreeItem ||
|
||||
item instanceof ResourceRangeTreeItem
|
||||
) {
|
||||
return item.resolveTreeItem();
|
||||
}
|
||||
return item;
|
||||
return Promise.resolve(item);
|
||||
}
|
||||
}
|
||||
|
||||
type TagTreeItem = TagItem | TagReference | TagSearch;
|
||||
type TagTreeItem =
|
||||
| TagItem
|
||||
| TagSearch
|
||||
| ResourceTreeItem
|
||||
| ResourceRangeTreeItem;
|
||||
|
||||
export class TagItem extends vscode.TreeItem {
|
||||
constructor(
|
||||
public readonly tag: string,
|
||||
public readonly title: string,
|
||||
public readonly nResourcesInSubtree: number,
|
||||
public readonly notes: URI[]
|
||||
) {
|
||||
super(title, vscode.TreeItemCollapsibleState.Collapsed);
|
||||
this.description = `${this.notes.length} reference${
|
||||
this.notes.length !== 1 ? 's' : ''
|
||||
this.description = `${nResourcesInSubtree} reference${
|
||||
nResourcesInSubtree !== 1 ? 's' : ''
|
||||
}`;
|
||||
}
|
||||
|
||||
@@ -169,28 +192,3 @@ export class TagSearch extends vscode.TreeItem {
|
||||
iconPath = new vscode.ThemeIcon('search');
|
||||
contextValue = 'tag-search';
|
||||
}
|
||||
|
||||
export class TagReference extends vscode.TreeItem {
|
||||
public readonly title: string;
|
||||
constructor(public readonly tag: Tag, public readonly note: Resource) {
|
||||
super(note.title, vscode.TreeItemCollapsibleState.None);
|
||||
const uri = toVsCodeUri(note.uri);
|
||||
this.title = note.title;
|
||||
this.description = vscode.workspace.asRelativePath(uri);
|
||||
this.tooltip = undefined;
|
||||
this.command = {
|
||||
command: 'vscode.open',
|
||||
arguments: [
|
||||
uri,
|
||||
{
|
||||
preview: true,
|
||||
selection: toVsCodeRange(tag.range),
|
||||
},
|
||||
],
|
||||
title: 'Open File',
|
||||
};
|
||||
}
|
||||
|
||||
iconPath = new vscode.ThemeIcon('note');
|
||||
contextValue = 'reference';
|
||||
}
|
||||
|
||||
@@ -22,7 +22,11 @@ const feature: FoamFeature = {
|
||||
markdownItFoamTags,
|
||||
markdownItWikilinkNavigation,
|
||||
markdownItRemoveLinkReferences,
|
||||
].reduce((acc, extension) => extension(acc, foam.workspace), md);
|
||||
].reduce(
|
||||
(acc, extension) =>
|
||||
extension(acc, foam.workspace, foam.services.parser),
|
||||
md
|
||||
);
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
@@ -24,7 +24,7 @@ describe('Displaying included notes in preview', () => {
|
||||
CONFIG_EMBED_NOTE_IN_CONTAINER,
|
||||
false,
|
||||
() => {
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws);
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
|
||||
|
||||
expect(
|
||||
md.render(`This is the root node.
|
||||
@@ -51,7 +51,7 @@ describe('Displaying included notes in preview', () => {
|
||||
CONFIG_EMBED_NOTE_IN_CONTAINER,
|
||||
true,
|
||||
() => {
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws);
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
|
||||
|
||||
const res = md.render(`This is the root node. ![[note-a]]`);
|
||||
expect(res).toContain('This is the root node');
|
||||
@@ -68,19 +68,19 @@ describe('Displaying included notes in preview', () => {
|
||||
const note = await createFile(
|
||||
`
|
||||
# Section 1
|
||||
This is the first section of note D
|
||||
This is the first section of note E
|
||||
|
||||
# Section 2
|
||||
This is the second section of note D
|
||||
This is the second section of note E
|
||||
|
||||
# Section 3
|
||||
This is the third section of note D
|
||||
This is the third section of note E
|
||||
`,
|
||||
['note-e.md']
|
||||
);
|
||||
const parser = createMarkdownParser([]);
|
||||
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws);
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
|
||||
|
||||
await withModifiedFoamConfiguration(
|
||||
CONFIG_EMBED_NOTE_IN_CONTAINER,
|
||||
@@ -93,7 +93,7 @@ This is the third section of note D
|
||||
).toMatch(
|
||||
`<p>This is the root node.</p>
|
||||
<p><h1>Section 2</h1>
|
||||
<p>This is the second section of note D</p>
|
||||
<p>This is the second section of note E</p>
|
||||
</p>`
|
||||
);
|
||||
}
|
||||
@@ -102,8 +102,48 @@ This is the third section of note D
|
||||
await deleteFile(note);
|
||||
});
|
||||
|
||||
it('should render an included section in container mode', async () => {
|
||||
const note = await createFile(
|
||||
`
|
||||
# Section 1
|
||||
This is the first section of note E
|
||||
|
||||
# Section 2
|
||||
This is the second section of note E
|
||||
|
||||
# Section 3
|
||||
This is the third section of note E
|
||||
`,
|
||||
['note-e-container.md']
|
||||
);
|
||||
const parser = createMarkdownParser([]);
|
||||
const ws = new FoamWorkspace().set(parser.parse(note.uri, note.content));
|
||||
|
||||
await withModifiedFoamConfiguration(
|
||||
CONFIG_EMBED_NOTE_IN_CONTAINER,
|
||||
true,
|
||||
() => {
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
|
||||
|
||||
const res = md.render(
|
||||
`This is the root node. ![[note-e-container#Section 3]]`
|
||||
);
|
||||
expect(res).toContain('This is the root node');
|
||||
expect(res).toContain('embed-container-note');
|
||||
expect(res).toContain('Section 3');
|
||||
expect(res).toContain('This is the third section of note E');
|
||||
}
|
||||
);
|
||||
|
||||
await deleteFile(note);
|
||||
});
|
||||
|
||||
it('should fallback to the bare text when the note is not found', () => {
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), new FoamWorkspace());
|
||||
const md = markdownItWikilinkEmbed(
|
||||
MarkdownIt(),
|
||||
new FoamWorkspace(),
|
||||
parser
|
||||
);
|
||||
|
||||
expect(md.render(`This is the root node. ![[non-existing-note]]`)).toMatch(
|
||||
`<p>This is the root node. ![[non-existing-note]]</p>`
|
||||
@@ -122,12 +162,12 @@ This is the third section of note D
|
||||
const ws = new FoamWorkspace()
|
||||
.set(parser.parse(noteA.uri, noteA.content))
|
||||
.set(parser.parse(noteB.uri, noteB.content));
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws);
|
||||
const md = markdownItWikilinkEmbed(MarkdownIt(), ws, parser);
|
||||
const res = md.render(noteBText);
|
||||
|
||||
expect(res).toContain('This is the text of note B which includes');
|
||||
expect(res).toContain('This is the text of note A which includes');
|
||||
expect(res).toContain('Cyclic link detected for wikilink: note-a');
|
||||
expect(res).toContain('Cyclic link detected for wikilink');
|
||||
|
||||
deleteFile(noteA);
|
||||
deleteFile(noteB);
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
/*global markdownit:readonly*/
|
||||
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { readFileSync } from 'fs';
|
||||
import { workspace as vsWorkspace } from 'vscode';
|
||||
import markdownItRegex from 'markdown-it-regex';
|
||||
import { isSome } from '../../utils';
|
||||
import { FoamWorkspace } from '../../core/model/workspace';
|
||||
import { Logger } from '../../core/utils/log';
|
||||
import { Resource } from '../../core/model/note';
|
||||
import { Resource, ResourceParser } from '../../core/model/note';
|
||||
import { getFoamVsCodeConfig } from '../../services/config';
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { readFileSync } from 'fs';
|
||||
import { fromVsCodeUri, toVsCodeUri } from '../../utils/vsc-utils';
|
||||
import { MarkdownLink } from '../../core/services/markdown-link';
|
||||
import { Position } from '../../core/model/position';
|
||||
import { TextEdit } from '../../core/services/text-edit';
|
||||
|
||||
export const CONFIG_EMBED_NOTE_IN_CONTAINER = 'preview.embedNoteInContainer';
|
||||
const refsStack: string[] = [];
|
||||
|
||||
export const markdownItWikilinkEmbed = (
|
||||
md: markdownit,
|
||||
workspace: FoamWorkspace
|
||||
workspace: FoamWorkspace,
|
||||
parser: ResourceParser
|
||||
) => {
|
||||
return md.use(markdownItRegex, {
|
||||
name: 'embed-wikilinks',
|
||||
@@ -41,9 +47,22 @@ export const markdownItWikilinkEmbed = (
|
||||
let content = `Embed for [[${wikilink}]]`;
|
||||
switch (includedNote.type) {
|
||||
case 'note': {
|
||||
const noteText = readFileSync(
|
||||
includedNote.uri.toFsPath()
|
||||
).toString();
|
||||
let noteText = readFileSync(includedNote.uri.toFsPath()).toString();
|
||||
const section = Resource.findSection(
|
||||
includedNote,
|
||||
includedNote.uri.fragment
|
||||
);
|
||||
if (isSome(section)) {
|
||||
const rows = noteText.split('\n');
|
||||
noteText = rows
|
||||
.slice(section.range.start.line, section.range.end.line)
|
||||
.join('\n');
|
||||
}
|
||||
noteText = withLinksRelativeToWorkspaceRoot(
|
||||
noteText,
|
||||
parser,
|
||||
workspace
|
||||
);
|
||||
content = getFoamVsCodeConfig(CONFIG_EMBED_NOTE_IN_CONTAINER)
|
||||
? `<div class="embed-container-note">${md.render(noteText)}</div>`
|
||||
: noteText;
|
||||
@@ -62,16 +81,6 @@ Embed for attachments is not supported
|
||||
)}</div>`;
|
||||
break;
|
||||
}
|
||||
const section = Resource.findSection(
|
||||
includedNote,
|
||||
includedNote.uri.fragment
|
||||
);
|
||||
if (isSome(section)) {
|
||||
const rows = content.split('\n');
|
||||
content = rows
|
||||
.slice(section.range.start.line, section.range.end.line)
|
||||
.join('\n');
|
||||
}
|
||||
const html = md.render(content);
|
||||
refsStack.pop();
|
||||
return html;
|
||||
@@ -86,4 +95,32 @@ Embed for attachments is not supported
|
||||
});
|
||||
};
|
||||
|
||||
function withLinksRelativeToWorkspaceRoot(
|
||||
noteText: string,
|
||||
parser: ResourceParser,
|
||||
workspace: FoamWorkspace
|
||||
) {
|
||||
const note = parser.parse(
|
||||
fromVsCodeUri(vsWorkspace.workspaceFolders[0].uri),
|
||||
noteText
|
||||
);
|
||||
const edits = note.links
|
||||
.map(link => {
|
||||
const info = MarkdownLink.analyzeLink(link);
|
||||
const resource = workspace.find(info.target);
|
||||
const pathFromRoot = vsWorkspace.asRelativePath(
|
||||
toVsCodeUri(resource.uri)
|
||||
);
|
||||
return MarkdownLink.createUpdateLinkEdit(link, {
|
||||
target: pathFromRoot,
|
||||
});
|
||||
})
|
||||
.sort((a, b) => Position.compareTo(b.range.start, a.range.start));
|
||||
const text = edits.reduce(
|
||||
(text, edit) => TextEdit.apply(text, edit),
|
||||
noteText
|
||||
);
|
||||
return text;
|
||||
}
|
||||
|
||||
export default markdownItWikilinkEmbed;
|
||||
|
||||
@@ -23,6 +23,7 @@ export const markdownItWikilinkNavigation = (
|
||||
rawText: '[[' + wikilink + ']]',
|
||||
type: 'wikilink',
|
||||
range: Range.create(0, 0),
|
||||
isEmbed: false,
|
||||
});
|
||||
const formattedSection = section ? `#${section}` : '';
|
||||
const label = isEmpty(alias) ? `${target}${formattedSection}` : alias;
|
||||
|
||||
@@ -45,7 +45,7 @@ const feature: FoamFeature = {
|
||||
);
|
||||
renameEdits.replace(
|
||||
toVsCodeUri(connection.source),
|
||||
toVsCodeRange(edit.selection),
|
||||
toVsCodeRange(edit.range),
|
||||
edit.newText
|
||||
);
|
||||
break;
|
||||
@@ -62,7 +62,7 @@ const feature: FoamFeature = {
|
||||
);
|
||||
renameEdits.replace(
|
||||
toVsCodeUri(connection.source),
|
||||
toVsCodeRange(edit.selection),
|
||||
toVsCodeRange(edit.range),
|
||||
edit.newText
|
||||
);
|
||||
break;
|
||||
|
||||
@@ -93,4 +93,124 @@ describe('Tag Completion', () => {
|
||||
expect(foamTags.tags.get('primary')).toBeTruthy();
|
||||
expect(tags).toBeNull();
|
||||
});
|
||||
|
||||
it('should not provide suggestions when inside a markdown heading #1182', async () => {
|
||||
const { uri } = await createFile('# primary');
|
||||
const { doc } = await showInEditor(uri);
|
||||
const provider = new TagCompletionProvider(foamTags);
|
||||
|
||||
const tags = await provider.provideCompletionItems(
|
||||
doc,
|
||||
new vscode.Position(0, 7)
|
||||
);
|
||||
|
||||
expect(foamTags.tags.get('primary')).toBeTruthy();
|
||||
expect(tags).toBeNull();
|
||||
});
|
||||
|
||||
describe('has robust triggering #1189', () => {
|
||||
it('should provide multiple suggestions when typing #', async () => {
|
||||
const { uri } = await createFile(`# Title
|
||||
|
||||
#`);
|
||||
const { doc } = await showInEditor(uri);
|
||||
const provider = new TagCompletionProvider(foamTags);
|
||||
|
||||
const tags = await provider.provideCompletionItems(
|
||||
doc,
|
||||
new vscode.Position(2, 1)
|
||||
);
|
||||
expect(tags.items.length).toEqual(3);
|
||||
});
|
||||
|
||||
it('should provide multiple suggestions when typing # on line with match', async () => {
|
||||
const { uri } = await createFile('Here is #my-tag and #');
|
||||
const { doc } = await showInEditor(uri);
|
||||
const provider = new TagCompletionProvider(foamTags);
|
||||
|
||||
const tags = await provider.provideCompletionItems(
|
||||
doc,
|
||||
new vscode.Position(0, 21)
|
||||
);
|
||||
expect(tags.items.length).toEqual(3);
|
||||
});
|
||||
|
||||
it('should provide multiple suggestions when typing # at EOL', async () => {
|
||||
const { uri } = await createFile(`# Title
|
||||
|
||||
#
|
||||
more text
|
||||
`);
|
||||
const { doc } = await showInEditor(uri);
|
||||
const provider = new TagCompletionProvider(foamTags);
|
||||
|
||||
const tags = await provider.provideCompletionItems(
|
||||
doc,
|
||||
new vscode.Position(2, 1)
|
||||
);
|
||||
expect(tags.items.length).toEqual(3);
|
||||
});
|
||||
|
||||
it('should not provide a suggestion when typing `# `', async () => {
|
||||
const { uri } = await createFile(`# Title
|
||||
|
||||
# `);
|
||||
const { doc } = await showInEditor(uri);
|
||||
const provider = new TagCompletionProvider(foamTags);
|
||||
|
||||
const tags = await provider.provideCompletionItems(
|
||||
doc,
|
||||
new vscode.Position(2, 2)
|
||||
);
|
||||
|
||||
expect(foamTags.tags.get('primary')).toBeTruthy();
|
||||
expect(tags).toBeNull();
|
||||
});
|
||||
|
||||
it('should not provide a suggestion when typing `#{non-match}`', async () => {
|
||||
const { uri } = await createFile(`# Title
|
||||
|
||||
#$`);
|
||||
const { doc } = await showInEditor(uri);
|
||||
const provider = new TagCompletionProvider(foamTags);
|
||||
|
||||
const tags = await provider.provideCompletionItems(
|
||||
doc,
|
||||
new vscode.Position(2, 2)
|
||||
);
|
||||
|
||||
expect(foamTags.tags.get('primary')).toBeTruthy();
|
||||
expect(tags).toBeNull();
|
||||
});
|
||||
|
||||
it('should not provide a suggestion when typing `##`', async () => {
|
||||
const { uri } = await createFile(`# Title
|
||||
|
||||
##`);
|
||||
const { doc } = await showInEditor(uri);
|
||||
const provider = new TagCompletionProvider(foamTags);
|
||||
|
||||
const tags = await provider.provideCompletionItems(
|
||||
doc,
|
||||
new vscode.Position(2, 2)
|
||||
);
|
||||
|
||||
expect(foamTags.tags.get('primary')).toBeTruthy();
|
||||
expect(tags).toBeNull();
|
||||
});
|
||||
|
||||
it('should not provide a suggestion when typing `# ` in a line that already matched', async () => {
|
||||
const { uri } = await createFile('here is #primary and now # ');
|
||||
const { doc } = await showInEditor(uri);
|
||||
const provider = new TagCompletionProvider(foamTags);
|
||||
|
||||
const tags = await provider.provideCompletionItems(
|
||||
doc,
|
||||
new vscode.Position(0, 29)
|
||||
);
|
||||
|
||||
expect(foamTags.tags.get('primary')).toBeTruthy();
|
||||
expect(tags).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,9 +3,11 @@ import { Foam } from '../core/model/foam';
|
||||
import { FoamTags } from '../core/model/tags';
|
||||
import { FoamFeature } from '../types';
|
||||
import { mdDocSelector } from '../utils';
|
||||
import { SECTION_REGEX } from './link-completion';
|
||||
|
||||
export const TAG_REGEX = /#(.*)/;
|
||||
// this regex is different from HASHTAG_REGEX in that it does not look for a
|
||||
// #+character. It uses a negative look-ahead for `# `
|
||||
const TAG_REGEX =
|
||||
/(?<=^|\s)#(?![ \t#])([0-9]*[\p{L}\p{Emoji_Presentation}\p{N}/_-]*)/dgu;
|
||||
|
||||
const feature: FoamFeature = {
|
||||
activate: async (
|
||||
@@ -36,13 +38,23 @@ export class TagCompletionProvider
|
||||
.lineAt(position)
|
||||
.text.substr(0, position.character);
|
||||
|
||||
const requiresAutocomplete =
|
||||
cursorPrefix.match(TAG_REGEX) && !cursorPrefix.match(SECTION_REGEX);
|
||||
|
||||
const requiresAutocomplete = cursorPrefix.match(TAG_REGEX);
|
||||
if (!requiresAutocomplete) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// check the match group length.
|
||||
// find the last match group, and ensure the end of that group is
|
||||
// at the cursor position.
|
||||
// This excludes both `#%` and also `here is #my-app1 and now # ` with
|
||||
// trailing space
|
||||
const matches = Array.from(cursorPrefix.matchAll(TAG_REGEX));
|
||||
const lastMatch = matches[matches.length - 1];
|
||||
const lastMatchEndIndex = lastMatch[0].length + lastMatch.index;
|
||||
if (lastMatchEndIndex !== position.character) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const completionTags = [];
|
||||
[...this.foamTags.tags].forEach(([tag]) => {
|
||||
const item = new vscode.CompletionItem(
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Selection, ViewColumn, window } from 'vscode';
|
||||
import { Selection, Uri, ViewColumn, window, workspace } from 'vscode';
|
||||
import { fromVsCodeUri } from '../utils/vsc-utils';
|
||||
import { NoteFactory } from '../services/templates';
|
||||
import {
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
deleteFile,
|
||||
getUriInWorkspace,
|
||||
showInEditor,
|
||||
withModifiedFoamConfiguration,
|
||||
} from '../test/test-utils-vscode';
|
||||
import { Resolver } from './variable-resolver';
|
||||
import { fileExists } from './editor';
|
||||
@@ -19,6 +20,56 @@ describe('Create note from template', () => {
|
||||
});
|
||||
|
||||
describe('User flow', () => {
|
||||
it('should resolve the path using the config when path is derived from note title', async () => {
|
||||
const templateA = await createFile('Template A', [
|
||||
'.foam',
|
||||
'templates',
|
||||
'template-a.md',
|
||||
]);
|
||||
jest
|
||||
.spyOn(window, 'showInputBox')
|
||||
.mockImplementation(jest.fn(() => Promise.resolve('Title of note')));
|
||||
|
||||
const noteA = await createFile('Note A', [
|
||||
'path',
|
||||
'of-new-note',
|
||||
'note-a.md',
|
||||
]);
|
||||
await showInEditor(noteA.uri);
|
||||
await withModifiedFoamConfiguration(
|
||||
'files.newNotePath',
|
||||
'currentDir',
|
||||
async () => {
|
||||
const result = await NoteFactory.createFromTemplate(
|
||||
templateA.uri,
|
||||
new Resolver(new Map(), new Date())
|
||||
);
|
||||
expect(result.uri.path).toEqual(
|
||||
noteA.uri.getDirectory().joinPath('Title of note.md').path
|
||||
);
|
||||
await deleteFile(result.uri);
|
||||
}
|
||||
);
|
||||
await withModifiedFoamConfiguration(
|
||||
'files.newNotePath',
|
||||
'root',
|
||||
async () => {
|
||||
const result = await NoteFactory.createFromTemplate(
|
||||
templateA.uri,
|
||||
new Resolver(new Map(), new Date())
|
||||
);
|
||||
expect(result.uri.path).toEqual(
|
||||
Uri.joinPath(workspace.workspaceFolders[0].uri, 'Title of note.md')
|
||||
.path
|
||||
);
|
||||
await deleteFile(result.uri);
|
||||
}
|
||||
);
|
||||
|
||||
await deleteFile(noteA);
|
||||
await deleteFile(templateA);
|
||||
});
|
||||
|
||||
it('should ask a user to confirm the path if note already exists', async () => {
|
||||
const templateA = await createFile('Template A', [
|
||||
'.foam',
|
||||
|
||||
@@ -349,13 +349,15 @@ export const NoteFactory = {
|
||||
resolver
|
||||
);
|
||||
|
||||
const newFilePath = asAbsoluteWorkspaceUri(
|
||||
template.metadata.has('filepath')
|
||||
? URI.file(template.metadata.get('filepath'))
|
||||
: isSome(filepathFallbackURI)
|
||||
? filepathFallbackURI
|
||||
: await getPathFromTitle(resolver)
|
||||
);
|
||||
let newFilePath = template.metadata.has('filepath')
|
||||
? URI.file(template.metadata.get('filepath'))
|
||||
: filepathFallbackURI;
|
||||
|
||||
if (isNone(newFilePath)) {
|
||||
newFilePath = await getPathFromTitle(resolver);
|
||||
} else if (!newFilePath.path.startsWith('./')) {
|
||||
newFilePath = asAbsoluteWorkspaceUri(newFilePath);
|
||||
}
|
||||
|
||||
return NoteFactory.createNote(
|
||||
newFilePath,
|
||||
|
||||
@@ -19,6 +19,7 @@ export function getWikilinkDefinitionSetting(): LinkReferenceDefinitionsSetting
|
||||
/** Retrieve the list of file ignoring globs. */
|
||||
export function getIgnoredFilesSetting(): GlobPattern[] {
|
||||
return [
|
||||
'**/.foam/**',
|
||||
...workspace.getConfiguration().get('foam.files.ignore', []),
|
||||
...Object.keys(workspace.getConfiguration().get('files.exclude', {})),
|
||||
];
|
||||
@@ -42,24 +43,16 @@ export function getFoamLoggerLevel(): LogLevel {
|
||||
export function getOrphansConfig(): GroupedResourcesConfig {
|
||||
const orphansConfig = workspace.getConfiguration('foam.orphans');
|
||||
const exclude: string[] = orphansConfig.get('exclude');
|
||||
const groupBy: GroupedResoucesConfigGroupBy = orphansConfig.get('groupBy');
|
||||
return { exclude, groupBy };
|
||||
return { exclude };
|
||||
}
|
||||
|
||||
/** Retrieve the placeholders configuration */
|
||||
export function getPlaceholdersConfig(): GroupedResourcesConfig {
|
||||
const placeholderCfg = workspace.getConfiguration('foam.placeholders');
|
||||
const exclude: string[] = placeholderCfg.get('exclude');
|
||||
const groupBy: GroupedResoucesConfigGroupBy = placeholderCfg.get('groupBy');
|
||||
return { exclude, groupBy };
|
||||
return { exclude };
|
||||
}
|
||||
|
||||
export interface GroupedResourcesConfig {
|
||||
exclude: string[];
|
||||
groupBy: GroupedResoucesConfigGroupBy;
|
||||
}
|
||||
|
||||
export enum GroupedResoucesConfigGroupBy {
|
||||
Folder = 'folder',
|
||||
Off = 'off',
|
||||
}
|
||||
|
||||
@@ -89,11 +89,13 @@ export const createTestNote = (params: {
|
||||
type: 'wikilink',
|
||||
range: range,
|
||||
rawText: `[[${link.slug}]]`,
|
||||
isEmbed: false,
|
||||
}
|
||||
: {
|
||||
type: 'link',
|
||||
range: range,
|
||||
rawText: `[link text](${link.to})`,
|
||||
isEmbed: false,
|
||||
};
|
||||
})
|
||||
: [],
|
||||
|
||||
20
packages/foam-vscode/src/utils/commands.ts
Normal file
20
packages/foam-vscode/src/utils/commands.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Uri } from 'vscode';
|
||||
import { merge } from 'lodash';
|
||||
|
||||
export interface CommandDescriptor<T> {
|
||||
name: string;
|
||||
params: T;
|
||||
}
|
||||
|
||||
export function describeCommand<T>(
|
||||
base: CommandDescriptor<T>,
|
||||
...extra: Partial<T>[]
|
||||
) {
|
||||
return merge(base, ...extra.map(e => ({ params: e })));
|
||||
}
|
||||
|
||||
export function commandAsURI<T>(command: CommandDescriptor<T>) {
|
||||
return Uri.parse(`command:${command.name}`).with({
|
||||
query: encodeURIComponent(JSON.stringify(command.params)),
|
||||
});
|
||||
}
|
||||
@@ -3,14 +3,14 @@ import {
|
||||
AlwaysIncludeMatcher,
|
||||
SubstringExcludeMatcher,
|
||||
} from '../core/services/datastore';
|
||||
import { OPEN_COMMAND } from '../features/commands/open-resource';
|
||||
import { GroupedResoucesConfigGroupBy } from '../settings';
|
||||
import { createTestNote } from '../test/test-utils';
|
||||
import {
|
||||
DirectoryTreeItem,
|
||||
GroupedResourcesTreeDataProvider,
|
||||
UriTreeItem,
|
||||
} from './grouped-resources-tree-data-provider';
|
||||
import { ResourceTreeItem, UriTreeItem } from './tree-view-utils';
|
||||
import { randomString } from '../test/test-utils';
|
||||
import { MapBasedMemento } from './vsc-utils';
|
||||
|
||||
const testMatcher = new SubstringExcludeMatcher('path-exclude');
|
||||
|
||||
@@ -37,17 +37,19 @@ describe('GroupedResourcesTreeDataProvider', () => {
|
||||
|
||||
it('should return the grouped resources as a folder tree', async () => {
|
||||
const provider = new GroupedResourcesTreeDataProvider(
|
||||
'length3',
|
||||
randomString(),
|
||||
'note',
|
||||
new MapBasedMemento(),
|
||||
testMatcher,
|
||||
() =>
|
||||
workspace
|
||||
.list()
|
||||
.filter(r => r.title.length === 3)
|
||||
.map(r => r.uri),
|
||||
uri => new UriTreeItem(uri),
|
||||
testMatcher
|
||||
uri => new UriTreeItem(uri)
|
||||
);
|
||||
provider.setGroupBy(GroupedResoucesConfigGroupBy.Folder);
|
||||
provider.groupBy.update('folder');
|
||||
provider.refresh();
|
||||
const result = await provider.getChildren();
|
||||
expect(result).toMatchObject([
|
||||
{
|
||||
@@ -67,21 +69,23 @@ describe('GroupedResourcesTreeDataProvider', () => {
|
||||
|
||||
it('should return the grouped resources in a directory', async () => {
|
||||
const provider = new GroupedResourcesTreeDataProvider(
|
||||
'length3',
|
||||
randomString(),
|
||||
'note',
|
||||
new MapBasedMemento(),
|
||||
testMatcher,
|
||||
() =>
|
||||
workspace
|
||||
.list()
|
||||
.filter(r => r.title.length === 3)
|
||||
.map(r => r.uri),
|
||||
uri => new UriTreeItem(uri),
|
||||
testMatcher
|
||||
uri => new ResourceTreeItem(workspace.get(uri), workspace)
|
||||
);
|
||||
provider.setGroupBy(GroupedResoucesConfigGroupBy.Folder);
|
||||
provider.groupBy.update('folder');
|
||||
provider.refresh();
|
||||
|
||||
const directory = new DirectoryTreeItem(
|
||||
'/path',
|
||||
[new UriTreeItem(matchingNote1.uri)],
|
||||
[new ResourceTreeItem(matchingNote1, workspace)],
|
||||
'note'
|
||||
);
|
||||
const result = await provider.getChildren(directory);
|
||||
@@ -90,24 +94,26 @@ describe('GroupedResourcesTreeDataProvider', () => {
|
||||
collapsibleState: 0,
|
||||
label: 'ABC',
|
||||
description: '/path/ABC.md',
|
||||
command: { command: OPEN_COMMAND.command },
|
||||
command: { command: 'vscode.open' },
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should return the flattened resources', async () => {
|
||||
const provider = new GroupedResourcesTreeDataProvider(
|
||||
'length3',
|
||||
randomString(),
|
||||
'note',
|
||||
new MapBasedMemento(),
|
||||
testMatcher,
|
||||
() =>
|
||||
workspace
|
||||
.list()
|
||||
.filter(r => r.title.length === 3)
|
||||
.map(r => r.uri),
|
||||
uri => new UriTreeItem(uri),
|
||||
testMatcher
|
||||
uri => new ResourceTreeItem(workspace.get(uri), workspace)
|
||||
);
|
||||
provider.setGroupBy(GroupedResoucesConfigGroupBy.Off);
|
||||
provider.groupBy.update('off');
|
||||
provider.refresh();
|
||||
|
||||
const result = await provider.getChildren();
|
||||
expect(result).toMatchObject([
|
||||
@@ -115,30 +121,32 @@ describe('GroupedResourcesTreeDataProvider', () => {
|
||||
collapsibleState: 0,
|
||||
label: matchingNote1.title,
|
||||
description: '/path/ABC.md',
|
||||
command: { command: OPEN_COMMAND.command },
|
||||
command: { command: 'vscode.open' },
|
||||
},
|
||||
{
|
||||
collapsibleState: 0,
|
||||
label: matchingNote2.title,
|
||||
description: '/path-bis/XYZ.md',
|
||||
command: { command: OPEN_COMMAND.command },
|
||||
command: { command: 'vscode.open' },
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('should return the grouped resources without exclusion', async () => {
|
||||
const provider = new GroupedResourcesTreeDataProvider(
|
||||
'length3',
|
||||
randomString(),
|
||||
'note',
|
||||
new MapBasedMemento(),
|
||||
new AlwaysIncludeMatcher(),
|
||||
() =>
|
||||
workspace
|
||||
.list()
|
||||
.filter(r => r.title.length === 3)
|
||||
.map(r => r.uri),
|
||||
uri => new UriTreeItem(uri),
|
||||
new AlwaysIncludeMatcher()
|
||||
uri => new UriTreeItem(uri)
|
||||
);
|
||||
provider.setGroupBy(GroupedResoucesConfigGroupBy.Folder);
|
||||
provider.groupBy.update('folder');
|
||||
provider.refresh();
|
||||
|
||||
const result = await provider.getChildren();
|
||||
expect(result).toMatchObject([
|
||||
@@ -156,17 +164,19 @@ describe('GroupedResourcesTreeDataProvider', () => {
|
||||
it('should dynamically set the description', async () => {
|
||||
const description = 'test description';
|
||||
const provider = new GroupedResourcesTreeDataProvider(
|
||||
'length3',
|
||||
randomString(),
|
||||
description,
|
||||
new MapBasedMemento(),
|
||||
testMatcher,
|
||||
() =>
|
||||
workspace
|
||||
.list()
|
||||
.filter(r => r.title.length === 3)
|
||||
.map(r => r.uri),
|
||||
uri => new UriTreeItem(uri),
|
||||
testMatcher
|
||||
uri => new UriTreeItem(uri)
|
||||
);
|
||||
provider.setGroupBy(GroupedResoucesConfigGroupBy.Folder);
|
||||
provider.groupBy.update('folder');
|
||||
provider.refresh();
|
||||
const result = await provider.getChildren();
|
||||
expect(result).toMatchObject([
|
||||
{
|
||||
|
||||
@@ -1,13 +1,10 @@
|
||||
import * as path from 'path';
|
||||
import * as vscode from 'vscode';
|
||||
import { GroupedResoucesConfigGroupBy } from '../settings';
|
||||
import { getContainsTooltip, getNoteTooltip, isSome } from '../utils';
|
||||
import { OPEN_COMMAND } from '../features/commands/open-resource';
|
||||
import { toVsCodeUri } from './vsc-utils';
|
||||
import { getContainsTooltip, isSome } from '../utils';
|
||||
import { URI } from '../core/model/uri';
|
||||
import { Resource } from '../core/model/note';
|
||||
import { FoamWorkspace } from '../core/model/workspace';
|
||||
import { IMatcher } from '../core/services/datastore';
|
||||
import { UriTreeItem } from './tree-view-utils';
|
||||
import { ContextMemento } from './vsc-utils';
|
||||
|
||||
/**
|
||||
* Provides the ability to expose a TreeDataExplorerView in VSCode. This class will
|
||||
@@ -16,8 +13,8 @@ import { IMatcher } from '../core/services/datastore';
|
||||
*
|
||||
* **NOTE**: In order for this provider to correctly function, you must define the following command in the package.json file:
|
||||
* ```
|
||||
* foam-vscode.group-${providerId}-by-folder
|
||||
* foam-vscode.group-${providerId}-off
|
||||
* foam-vscode.views.${providerId}.group-by-folder
|
||||
* foam-vscode.views.${providerId}.group-off
|
||||
* ```
|
||||
* Where `providerId` is the same string provided to the constructor. You must also register the commands in your context subscriptions as follows:
|
||||
* ```
|
||||
@@ -37,24 +34,29 @@ import { IMatcher } from '../core/services/datastore';
|
||||
* @implements {vscode.TreeDataProvider<GroupedResourceTreeItem>}
|
||||
*/
|
||||
export class GroupedResourcesTreeDataProvider
|
||||
implements vscode.TreeDataProvider<GroupedResourceTreeItem>
|
||||
implements
|
||||
vscode.TreeDataProvider<GroupedResourceTreeItem>,
|
||||
vscode.Disposable
|
||||
{
|
||||
// prettier-ignore
|
||||
private _onDidChangeTreeData: vscode.EventEmitter<GroupedResourceTreeItem | undefined | void> = new vscode.EventEmitter<GroupedResourceTreeItem | undefined | void>();
|
||||
// prettier-ignore
|
||||
readonly onDidChangeTreeData: vscode.Event<GroupedResourceTreeItem | undefined | void> = this._onDidChangeTreeData.event;
|
||||
// prettier-ignore
|
||||
private groupBy: GroupedResoucesConfigGroupBy = GroupedResoucesConfigGroupBy.Folder;
|
||||
private exclude: string[] = [];
|
||||
private flatUris: Array<URI> = [];
|
||||
private root = vscode.workspace.workspaceFolders[0].uri.path;
|
||||
public groupBy = new ContextMemento<'off' | 'folder'>(
|
||||
this.state,
|
||||
`foam-vscode.views.${this.providerId}.group-by`,
|
||||
'folder'
|
||||
);
|
||||
protected disposables: vscode.Disposable[] = [];
|
||||
|
||||
/**
|
||||
* Creates an instance of GroupedResourcesTreeDataProvider.
|
||||
* **NOTE**: In order for this provider to correctly function, you must define the following command in the package.json file:
|
||||
* ```
|
||||
* foam-vscode.group-${providerId}-by-folder
|
||||
* foam-vscode.group-${providerId}-off
|
||||
* foam-vscode.views.${this.providerId}.group-by-folder
|
||||
* foam-vscode.views.${this.providerId}.group-by-off
|
||||
* ```
|
||||
* Where `providerId` is the same string provided to this constructor. You must also register the commands in your context subscriptions as follows:
|
||||
* ```
|
||||
@@ -78,47 +80,39 @@ export class GroupedResourcesTreeDataProvider
|
||||
* @memberof GroupedResourcesTreeDataProvider
|
||||
*/
|
||||
constructor(
|
||||
private providerId: string,
|
||||
protected providerId: string,
|
||||
private resourceName: string,
|
||||
private computeResources: () => Array<URI>,
|
||||
private createTreeItem: (item: URI) => GroupedResourceTreeItem,
|
||||
private matcher: IMatcher
|
||||
protected state: vscode.Memento,
|
||||
private matcher: IMatcher,
|
||||
protected computeResources: () => Array<URI>,
|
||||
private createTreeItem: (item: URI) => GroupedResourceTreeItem
|
||||
) {
|
||||
this.setContext();
|
||||
this.doComputeResources();
|
||||
this.disposables.push(
|
||||
vscode.commands.registerCommand(
|
||||
`foam-vscode.views.${this.providerId}.group-by:folder`,
|
||||
() => {
|
||||
this.groupBy.update('folder');
|
||||
this.refresh();
|
||||
}
|
||||
),
|
||||
vscode.commands.registerCommand(
|
||||
`foam-vscode.views.${this.providerId}.group-by:off`,
|
||||
() => {
|
||||
this.groupBy.update('off');
|
||||
this.refresh();
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public get commands() {
|
||||
return [
|
||||
vscode.commands.registerCommand(
|
||||
`foam-vscode.group-${this.providerId}-by-folder`,
|
||||
() => this.setGroupBy(GroupedResoucesConfigGroupBy.Folder)
|
||||
),
|
||||
vscode.commands.registerCommand(
|
||||
`foam-vscode.group-${this.providerId}-off`,
|
||||
() => this.setGroupBy(GroupedResoucesConfigGroupBy.Off)
|
||||
),
|
||||
];
|
||||
dispose() {
|
||||
this.disposables.forEach(d => d.dispose());
|
||||
}
|
||||
|
||||
public get numElements() {
|
||||
return this.flatUris.length;
|
||||
}
|
||||
|
||||
setGroupBy(groupBy: GroupedResoucesConfigGroupBy): void {
|
||||
this.groupBy = groupBy;
|
||||
this.setContext();
|
||||
this.refresh();
|
||||
}
|
||||
|
||||
private setContext(): void {
|
||||
vscode.commands.executeCommand(
|
||||
'setContext',
|
||||
`foam-vscode.${this.providerId}-grouped-by-folder`,
|
||||
this.groupBy === GroupedResoucesConfigGroupBy.Folder
|
||||
);
|
||||
}
|
||||
|
||||
refresh(): void {
|
||||
this.doComputeResources();
|
||||
this._onDidChangeTreeData.fire();
|
||||
@@ -128,13 +122,13 @@ export class GroupedResourcesTreeDataProvider
|
||||
return item;
|
||||
}
|
||||
|
||||
getChildren(
|
||||
directory?: DirectoryTreeItem
|
||||
async getChildren(
|
||||
item?: GroupedResourceTreeItem
|
||||
): Promise<GroupedResourceTreeItem[]> {
|
||||
if (this.groupBy === GroupedResoucesConfigGroupBy.Folder) {
|
||||
if (isSome(directory)) {
|
||||
return Promise.resolve(directory.children.sort(sortByTreeItemLabel));
|
||||
}
|
||||
if ((item as any)?.getChildren) {
|
||||
return (item as any).getChildren();
|
||||
}
|
||||
if (this.groupBy.get() === 'folder') {
|
||||
const directories = Object.entries(this.getUrisByDirectory())
|
||||
.sort(([dir1], [dir2]) => sortByString(dir1, dir2))
|
||||
.map(
|
||||
@@ -186,63 +180,6 @@ type UrisByDirectory = { [key: string]: Array<URI> };
|
||||
|
||||
type GroupedResourceTreeItem = UriTreeItem | DirectoryTreeItem;
|
||||
|
||||
export class UriTreeItem extends vscode.TreeItem {
|
||||
constructor(
|
||||
public readonly uri: URI,
|
||||
options: {
|
||||
collapsibleState?: vscode.TreeItemCollapsibleState;
|
||||
icon?: string;
|
||||
title?: string;
|
||||
} = {}
|
||||
) {
|
||||
super(options?.title ?? uri.getName(), options.collapsibleState);
|
||||
this.description = uri.path.replace(
|
||||
vscode.workspace.getWorkspaceFolder(toVsCodeUri(uri))?.uri.path,
|
||||
''
|
||||
);
|
||||
this.tooltip = undefined;
|
||||
this.command = {
|
||||
command: OPEN_COMMAND.command,
|
||||
title: OPEN_COMMAND.title,
|
||||
arguments: [
|
||||
{
|
||||
uri: uri,
|
||||
},
|
||||
],
|
||||
};
|
||||
this.iconPath = new vscode.ThemeIcon(options.icon ?? 'new-file');
|
||||
}
|
||||
|
||||
resolveTreeItem(): Promise<GroupedResourceTreeItem> {
|
||||
return Promise.resolve(this);
|
||||
}
|
||||
}
|
||||
|
||||
export class ResourceTreeItem extends UriTreeItem {
|
||||
constructor(
|
||||
public readonly resource: Resource,
|
||||
private readonly workspace: FoamWorkspace,
|
||||
collapsibleState = vscode.TreeItemCollapsibleState.None
|
||||
) {
|
||||
super(resource.uri, {
|
||||
title: resource.title,
|
||||
icon: 'note',
|
||||
collapsibleState,
|
||||
});
|
||||
this.contextValue = 'resource';
|
||||
}
|
||||
|
||||
async resolveTreeItem(): Promise<ResourceTreeItem> {
|
||||
if (this instanceof ResourceTreeItem) {
|
||||
const content = await this.workspace.readAsMarkdown(this.resource.uri);
|
||||
this.tooltip = isSome(content)
|
||||
? getNoteTooltip(content)
|
||||
: this.resource.title;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export class DirectoryTreeItem extends vscode.TreeItem {
|
||||
constructor(
|
||||
public readonly dir: string,
|
||||
@@ -264,6 +201,10 @@ export class DirectoryTreeItem extends vscode.TreeItem {
|
||||
this.tooltip = getContainsTooltip(titles);
|
||||
return Promise.resolve(this);
|
||||
}
|
||||
|
||||
getChildren(): Promise<GroupedResourceTreeItem[]> {
|
||||
return Promise.resolve(this.children);
|
||||
}
|
||||
}
|
||||
|
||||
const sortByTreeItemLabel = (a: vscode.TreeItem, b: vscode.TreeItem) =>
|
||||
|
||||
178
packages/foam-vscode/src/utils/tree-view-utils.ts
Normal file
178
packages/foam-vscode/src/utils/tree-view-utils.ts
Normal file
@@ -0,0 +1,178 @@
|
||||
import * as vscode from 'vscode';
|
||||
import { Resource } from '../core/model/note';
|
||||
import { toVsCodeUri } from './vsc-utils';
|
||||
import { Range } from '../core/model/range';
|
||||
import { OPEN_COMMAND } from '../features/commands/open-resource';
|
||||
import { URI } from '../core/model/uri';
|
||||
import { FoamWorkspace } from '../core/model/workspace';
|
||||
import { getNoteTooltip } from '../utils';
|
||||
import { isSome } from '../core/utils';
|
||||
import { groupBy } from 'lodash';
|
||||
import { getBlockFor } from '../core/services/markdown-parser';
|
||||
|
||||
export class UriTreeItem extends vscode.TreeItem {
|
||||
private doGetChildren: () => Promise<vscode.TreeItem[]>;
|
||||
|
||||
constructor(
|
||||
public readonly uri: URI,
|
||||
options: {
|
||||
collapsibleState?: vscode.TreeItemCollapsibleState;
|
||||
icon?: string;
|
||||
title?: string;
|
||||
getChildren?: () => Promise<vscode.TreeItem[]>;
|
||||
} = {}
|
||||
) {
|
||||
super(
|
||||
options?.title ?? uri.getName(),
|
||||
options.collapsibleState
|
||||
? options.collapsibleState
|
||||
: options.getChildren
|
||||
? vscode.TreeItemCollapsibleState.Collapsed
|
||||
: vscode.TreeItemCollapsibleState.None
|
||||
);
|
||||
this.doGetChildren = options.getChildren;
|
||||
this.description = uri.path.replace(
|
||||
vscode.workspace.getWorkspaceFolder(toVsCodeUri(uri))?.uri.path,
|
||||
''
|
||||
);
|
||||
this.tooltip = undefined;
|
||||
this.iconPath = new vscode.ThemeIcon(options.icon ?? 'new-file');
|
||||
}
|
||||
|
||||
resolveTreeItem(): Promise<UriTreeItem> {
|
||||
return Promise.resolve(this);
|
||||
}
|
||||
|
||||
getChildren(): Promise<vscode.TreeItem[]> {
|
||||
return isSome(this.doGetChildren)
|
||||
? this.doGetChildren()
|
||||
: Promise.resolve([]);
|
||||
}
|
||||
}
|
||||
|
||||
export class ResourceTreeItem extends UriTreeItem {
|
||||
constructor(
|
||||
public readonly resource: Resource,
|
||||
private readonly workspace: FoamWorkspace,
|
||||
options: {
|
||||
collapsibleState?: vscode.TreeItemCollapsibleState;
|
||||
getChildren?: () => Promise<vscode.TreeItem[]>;
|
||||
} = {}
|
||||
) {
|
||||
super(resource.uri, {
|
||||
title: resource.title,
|
||||
icon: 'note',
|
||||
collapsibleState: options.collapsibleState,
|
||||
getChildren: options.getChildren,
|
||||
});
|
||||
this.command = {
|
||||
command: 'vscode.open',
|
||||
arguments: [toVsCodeUri(resource.uri)],
|
||||
title: 'Go to location',
|
||||
};
|
||||
|
||||
this.contextValue = 'resource';
|
||||
}
|
||||
|
||||
async resolveTreeItem(): Promise<ResourceTreeItem> {
|
||||
if (this instanceof ResourceTreeItem) {
|
||||
const content = await this.workspace.readAsMarkdown(this.resource.uri);
|
||||
this.tooltip = isSome(content)
|
||||
? getNoteTooltip(content)
|
||||
: this.resource.title;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
export class ResourceRangeTreeItem extends vscode.TreeItem {
|
||||
constructor(
|
||||
public label: string,
|
||||
public readonly resource: Resource,
|
||||
public readonly range: Range,
|
||||
private resolveFn?: (
|
||||
item: ResourceRangeTreeItem
|
||||
) => Promise<ResourceRangeTreeItem>
|
||||
) {
|
||||
super(label, vscode.TreeItemCollapsibleState.None);
|
||||
this.label = `${range.start.line}: ${this.label}`;
|
||||
this.command = {
|
||||
command: 'vscode.open',
|
||||
arguments: [toVsCodeUri(resource.uri), { selection: range }],
|
||||
title: 'Go to location',
|
||||
};
|
||||
}
|
||||
|
||||
resolveTreeItem(): Promise<ResourceRangeTreeItem> {
|
||||
return this.resolveFn ? this.resolveFn(this) : Promise.resolve(this);
|
||||
}
|
||||
|
||||
static async createStandardItem(
|
||||
workspace: FoamWorkspace,
|
||||
resource: Resource,
|
||||
range: Range
|
||||
): Promise<ResourceRangeTreeItem> {
|
||||
const markdown = (await workspace.readAsMarkdown(resource.uri)) ?? '';
|
||||
const lines = markdown.split('\n');
|
||||
|
||||
const line = lines[range.start.line];
|
||||
const start = Math.max(0, range.start.character - 15);
|
||||
const ellipsis = start === 0 ? '' : '...';
|
||||
|
||||
const label = line
|
||||
? `${range.start.line}: ${ellipsis}${line.slice(start, start + 300)}`
|
||||
: Range.toString(range);
|
||||
|
||||
const resolveFn = (item: ResourceRangeTreeItem) => {
|
||||
let { block, nLines } = getBlockFor(markdown, range.start);
|
||||
// Long blocks need to be interrupted or they won't display in hover preview
|
||||
// We keep the extra lines so that the count in the preview is correct
|
||||
if (nLines > 15) {
|
||||
let tmp = block.split('\n');
|
||||
tmp.splice(15, 1, '\n'); // replace a line with a blank line to interrupt the block
|
||||
block = tmp.join('\n');
|
||||
}
|
||||
const tooltip = getNoteTooltip(block ?? line ?? '');
|
||||
item.tooltip = tooltip;
|
||||
return Promise.resolve(item);
|
||||
};
|
||||
|
||||
const item = new ResourceRangeTreeItem(label, resource, range, resolveFn);
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
export const groupRangesByResource = async (
|
||||
workspace: FoamWorkspace,
|
||||
items:
|
||||
| ResourceRangeTreeItem[]
|
||||
| Promise<ResourceRangeTreeItem[]>
|
||||
| Promise<ResourceRangeTreeItem>[],
|
||||
collapsibleState = vscode.TreeItemCollapsibleState.Collapsed
|
||||
) => {
|
||||
let itemsArray = [] as ResourceRangeTreeItem[];
|
||||
if (items instanceof Promise) {
|
||||
itemsArray = await items;
|
||||
}
|
||||
if (items instanceof Array && items[0] instanceof Promise) {
|
||||
itemsArray = await Promise.all(items);
|
||||
}
|
||||
if (items instanceof Array && items[0] instanceof ResourceRangeTreeItem) {
|
||||
itemsArray = items as any;
|
||||
}
|
||||
const byResource = groupBy(itemsArray, item => item.resource.uri.path);
|
||||
const resourceItems = Object.values(byResource).map(items => {
|
||||
const resourceItem = new ResourceTreeItem(items[0].resource, workspace, {
|
||||
collapsibleState,
|
||||
getChildren: () => {
|
||||
return Promise.resolve(
|
||||
items.sort((a, b) => Range.isBefore(a.range, b.range))
|
||||
);
|
||||
},
|
||||
});
|
||||
resourceItem.description = `(${items.length}) ${resourceItem.description}`;
|
||||
return resourceItem;
|
||||
});
|
||||
resourceItems.sort((a, b) => Resource.sortByTitle(a.resource, b.resource));
|
||||
return resourceItems;
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Position, Range, Uri } from 'vscode';
|
||||
import { Memento, Position, Range, Uri, commands } from 'vscode';
|
||||
import { Position as FoamPosition } from '../core/model/position';
|
||||
import { Range as FoamRange } from '../core/model/range';
|
||||
import { URI as FoamURI } from '../core/model/uri';
|
||||
@@ -12,3 +12,37 @@ export const toVsCodeRange = (r: FoamRange): Range =>
|
||||
export const toVsCodeUri = (u: FoamURI): Uri => Uri.parse(u.toString());
|
||||
|
||||
export const fromVsCodeUri = (u: Uri): FoamURI => FoamURI.parse(u.toString());
|
||||
|
||||
/**
|
||||
* A class that wraps context value, syncs it via setContext, and provides a typed interface to it.
|
||||
*/
|
||||
export class ContextMemento<T> {
|
||||
constructor(private data: Memento, private key: string, defaultValue: T) {
|
||||
const value = data.get(key) ?? defaultValue;
|
||||
commands.executeCommand('setContext', this.key, value);
|
||||
}
|
||||
public get(): T {
|
||||
return this.data.get(this.key);
|
||||
}
|
||||
public async update(value: T): Promise<void> {
|
||||
this.data.update(this.key, value);
|
||||
await commands.executeCommand('setContext', this.key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of the Memento interface that uses a Map as backend
|
||||
*/
|
||||
export class MapBasedMemento implements Memento {
|
||||
get<T>(key: unknown, defaultValue?: unknown | T): T | T {
|
||||
return (this.map.get(key as string) as T) || (defaultValue as T);
|
||||
}
|
||||
private map: Map<string, string> = new Map();
|
||||
keys(): readonly string[] {
|
||||
return Array.from(this.map.keys());
|
||||
}
|
||||
update(key: string, value: any): Promise<void> {
|
||||
this.map.set(key, value);
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,7 @@
|
||||
👀*This is an early stage project under rapid development. For updates join the [Foam community Discord](https://foambubble.github.io/join-discord/g)! 💬*
|
||||
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||
|
||||
[](#contributors-)
|
||||
|
||||
[](#contributors-)
|
||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||
|
||||
[](https://marketplace.visualstudio.com/items?itemName=foam.foam-vscode)
|
||||
@@ -341,6 +339,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://jonathanpberger.com/"><img src="https://avatars.githubusercontent.com/u/41085?v=4?s=60" width="60px;" alt="jonathan berger"/><br /><sub><b>jonathan berger</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=jonathanpberger" title="Documentation">📖</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="https://github.com/badsketch"><img src="https://avatars.githubusercontent.com/u/8953212?v=4?s=60" width="60px;" alt="Daniel Wang"/><br /><sub><b>Daniel Wang</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=badsketch" title="Code">💻</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://yongliangliu.com"><img src="https://avatars.githubusercontent.com/u/41845017?v=4?s=60" width="60px;" alt="Liu YongLiang"/><br /><sub><b>Liu YongLiang</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=tlylt" title="Documentation">📖</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://scottakerman.com"><img src="https://avatars.githubusercontent.com/u/15224439?v=4?s=60" width="60px;" alt="Scott Akerman"/><br /><sub><b>Scott Akerman</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=Skakerman" title="Code">💻</a></td>
|
||||
<td align="center" valign="top" width="14.28%"><a href="http://www.jim-graham.net/"><img src="https://avatars.githubusercontent.com/u/430293?v=4?s=60" width="60px;" alt="Jim Graham"/><br /><sub><b>Jim Graham</b></sub></a><br /><a href="https://github.com/foambubble/foam/commits?author=jimgraham" title="Code">💻</a></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
Reference in New Issue
Block a user