Open iilyak opened 2 years ago
JFYI the frontmatter based approach is implemented in Foam extension for markdown templates https://foambubble.github.io/foam/features/note-templates.
Hey! Thank you so much for reporting this issue!
First of all, it would be possible soon to define snippets at workspace level (in .vscode/settings.json
).
However, your idea would be extremely useful in cases when snippets are large or you want to track them in individual files.
Do we have some cross-IDE standard? Any extensions from other IDEs that implements something like this?
I think to implement this setting with the following default: ['.better-snippets']
(just like foam do). Ideas about default folder name are welcome!
Also about frontmatter. It is okay to use it in Markdown files, however, for other languages it would be much better to define metadata in comments at the start of file. For example we can have file .better-snippets/cdb-debug.erl
:
% .snippet
% locations: fileStart
% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% ...
Empty line is required as it used to denote end of metadata.
Also we can skip definining name
and languages
as they both can be derived from the file name.
It is much better to define metadata in comments, so we don't break:
Also, you can expect web support!
"betterSnippets.customSnippets": [
{
"name": "foo",
"snippetFile": "..."
This case won't be supported as this would make sense only in workspace-level configuration.
In case above and in betterSnippets.snippetsDir
paths should not be absolute as in this case you lose:
That's why global snippets should always be defined in VSCode's settings.json
. I'll make a sidebar panel for convenient snippet editing (each snippet you could edit in individual tab).
Do we have some cross-IDE standard? Any extensions from other IDEs that implements something like this?
I am not aware of cross-IDE standards.
Also about frontmatter. It is okay to use it in Markdown files, however, for other languages it would be much better to define metadata in comments at the start of file.
I thought the better-snippets
extension would read a file. Split the file in two parts: metadata from frontmatter and snippet body. It would use snippet body and inject it into the currently opened file. The expanded result wouldn't have frontmatter and therefore language independent.
However I do agree the syntax highlighting would be broken.
That's why global snippets should always be defined in VSCode's settings.json. I'll make a sidebar panel for convenient snippet editing (each snippet you could edit in individual tab).
Would it be possible to have a project specific snippets? For example by checking existence of .vscode/.better_snippets
directory? It would be nice if additional directories could be configured:
cat .vscode/settings.json
{
"betterSnippets.snippetsDirs": [
"../snippets/erlang",
"../snippets/elixir",
"../snippets/docs"
]
}
By default the "betterSnippets.snippetsDirs"
would be ["./.better-snippets"]
.
However I do agree the syntax highlighting would be broken.
Yes, that's why metadata will live in comments for now. As you said the file split in two parts: metadata and body.
Metadata is commented.
Body goes as is, but with snippet variables support.
Emptyline will be delimiter.
Look at example with .better-snippets/cdb-debug.erl
from above (https://github.com/zardoy/vscode-better-snippets/issues/4#issuecomment-1043115113)
Would it be possible to have a project specific snippets?
Yes, it would be supported with this setting.
By default the "betterSnippets.snippetsDirs" would be ["./.better-snippets"].
Great! This will be configurable!
Yes, that's why metadata will live in comments for now. As you said the file split in two parts: metadata and body.
There is one problem with using comments. Most IDE use shebang presence to figure out which syntax to enable if file extension is not reliable. Which is the case for shell scripts. The shebang should be in the first line for this detection to work.
Sorry for delayed response. I just started implementing this feature.
The shebang should be in the first line for this detection to work.
I see only two solutions:
#!/usr/bin/env bash
# .snippet
# locations: fileStart
if [[ "$OSTYPE" == "darwin"* ]]; then
# ...
The body of the snippet would look like:
#!/usr/bin/env bash
if [[ "$OSTYPE" == "darwin"* ]]; then
# ...
However, I don't like the ambiguity it introduces. For example snippet can have > 100 lines and since snippet metadata can be placed at any line, programmer can spend some time to find it. It can be just annoying.
.better-snippets/cdb-debug.erl
but with body only:
% ...
% License for the specific language governing permissions and limitations under
% the License.
-module(${TM_FILENAME_BASE}). ...
And second file `.better-snippets/cdb-debug.snippet.json`:
```json
{
"locations": ["fileStart"]
}
Supported formats would be: json
, yaml
, js
. The last one would allow to run aribtrary code and will be disabled in trusted workspaces. I'm also thinking about support for toml
and ts
formats.
I more like the second solution and I'm currently planning to implement only it. WDYT?
second file .better-snippets/cdb-debug.snippet.json
would work for my use case.
The https://github.com/asbjornh/vscode-module-templates extension supports loading templates from files using separate configuration field
module-templates.templateFiles
.This extension could use similar approach as well. For example
However ideally the configuration like follows would be perfect for my use case.
cat ./snippets/cdb-debug.erl
As you can see all configuration is contained in the frontmatter of the snippet file.