zed icon indicating copy to clipboard operation
zed copied to clipboard

Language server agnostic snippets

Open failable opened this issue 2 years ago • 6 comments

Check for existing issues

  • [X] Completed

Describe the feature

Other editors have the ability to define snippet without setting up in the LSP. For example, one can still define snippets for editing plain text files and that does not require user to setup initialization options of a LSP. (And we don't generally have a LSP for plain text files.)

Also some snippets might shared across different file types/LSPs.

If applicable, add mockups / screenshots to help present your vision of the feature

output

failable avatar Jul 21 '23 00:07 failable

One example use-case is authoring HTML embedded in a string/template within another language. By default, Sublime maps Ctrl+W to an 'insert tag' snippet which makes this very easy and provides a consistent workflow across languages.

xtian avatar Jan 24 '24 18:01 xtian

Hi, when will this happen?

miknyko avatar Mar 13 '24 04:03 miknyko

Is there any ongoing work on this feature? Would be happy to join/support here

thoreg avatar Apr 06 '24 05:04 thoreg

is this implement? how to add custom snippets in zed like we in vscode javascript.json?

auspy avatar Apr 19 '24 15:04 auspy

+1 for requesting this feature. It's a huge time saver!

sevignator avatar Apr 26 '24 15:04 sevignator

A very important feature for me is luasnip's auto snippets. Snippets marked as "auto" will trigger the moment the prefix is typed out, without having to select the snippet in the autocomplete menu and press enter. While this might sound useless, it's super handy when having to type LaTeX quickly (i.e.: when taking live notes during a lecture)

prescientmoon avatar May 01 '24 19:05 prescientmoon

I want to switch fully to Zed for Go projects and this feature prevents me from switching. Would be great to have it.

TuralAsgar avatar Jun 16 '24 12:06 TuralAsgar

Snippet support is now available as "Snippets" extension in the store. Give it a go; it should work with both Stable and Preview versions of Zed. You can now store your snippets in ~/.config/zed/snippets subdirectory. Language-agnostic snippets should reside in snippets.json file; you can also have snippets that apply only to a specific language, in which case the file should be named after that language (e.g. python.json). I'd recommend checking out friendly-snippets for starters.

Under the hood we use our own fork of simple-completion-language-server.

osiewicz avatar Jun 19 '24 12:06 osiewicz

I have added the zed/snippets/snippets.json - and when I restarted Zed the snippet is showing with typing the prefix in a hovering selector (awesome!)- but when I press enter or click the option it is not populating into the buffer - is there a specific command for populating the snippet from the options?

cosmic-bytes avatar Jun 19 '24 13:06 cosmic-bytes

Can you share your snippets file? It should work just like normal completions, so you shouldn't have to do anything special.

osiewicz avatar Jun 19 '24 13:06 osiewicz

Can you share your snippets file? It should work just like normal completions, so you shouldn't have to do anything special.

{
  "getVars": {
    "prefix": "var",
    "body": "${getVars({ name : '' })}"
  }
}

cosmic-bytes avatar Jun 19 '24 13:06 cosmic-bytes

I'm not sure if that's a valid syntax; does it work for you in other editors, e.g. VSC? Edit: I've checked it myself - it is inserted verbatim in VSC. I think you might want to escape the leading $

osiewicz avatar Jun 19 '24 13:06 osiewicz

I think it is bad syntax on my part (${} is used for arguments for snippets it looks like)- thanks!

cosmic-bytes avatar Jun 19 '24 13:06 cosmic-bytes

@osiewicz Thanks for the work! I tested and it works perfectly for Python and Markdown but Dockerfile. Is the snippet file name dockerfile.json correct for Dockerfile ?

failable avatar Jun 19 '24 15:06 failable

Hey @failable, Currently we can only launch a language server for a known subset of languages (we don't have an ability to launch a language server for any file); we've simply missed Dockerfile in a list. I'll try to figure out if we could lift that limitation somehow by the next weeks release; I realize the current approach is not really scalable.

osiewicz avatar Jun 19 '24 15:06 osiewicz

I copied my snippets over from VS code and they did not show up at first either. But then I validated the json of the file and it had errors. When I had fixed those errors and I restarted Zed, the snippets appeared.

Thanks for adding this feature.

ottob avatar Jun 20 '24 12:06 ottob

How do we trigger snippets? For instance, here’s one I have:

{ "Horizontal Rule": { "prefix": "---", "body": ["- - - "], "description": "Add horizontal rule" } }

fuzenco avatar Jun 21 '24 17:06 fuzenco

I've put the following code in ~/.config/zed/snippets/markdown.json, but it doesn't work in 123.md file

{
"Create new md file": {
    "prefix": "md",
    "body": [
      "+++",
      "title = '$1'",
      "categories = ['LearnOn${2:line}']",
      "tags = ['${3:go}']",
      "date = 2024-$4-$5",
      "draft = true",
      "+++"
    ],
    "description": "Create new md file"
  }
  }

huhu415 avatar Jun 22 '24 01:06 huhu415

@fuzenco It seems the current implementation only works if the prefix is provided in the completion menu while prefixs like --- are not provided in the menu.

@osiewicz It would also nice to have customizable key binding to trigger snippets manually.

failable avatar Jun 22 '24 13:06 failable

@fuzenco It seems the current implementation only works if the prefix is provided in the completion menu while prefixs like --- are not provided in the menu.

@failable thanks but where can I find the completion menu?

fuzenco avatar Jun 22 '24 14:06 fuzenco

@fuzenco It seems the current implementation only works if the prefix is provided in the completion menu while prefixs like --- are not provided in the menu.

@failable thanks but where can I find the completion menu?

@failable Disregard. I’d been working on newly opened files which I didn’t have language formatting applied to. Once I saved my file as .md, the snippet appeared. Works very nicely too. Great job Zed team!

UPDATE: '---' does work to invoke the snippet. Again, my problem was not correctly setting the document type.

fuzenco avatar Jun 22 '24 14:06 fuzenco

I'm encountering an issue with Zed snippets in LaTeX files. The same snippet works fine in Markdown but fails to trigger when writing LaTeX files. Here's an example: I've added the following snippet to both markdown.json and latex.json:

{ "Horizontal Rule": { "prefix": "---", "body": ["- - - "], "description": "Add horizontal rule" } }

When writing Markdown files, the snippet triggers as expected. However, when writing LaTeX files, no snippet is triggered at all. Has anyone else experienced this issue or know how to resolve it? Any help would be appreciated.

ivansigmund avatar Jun 25 '24 09:06 ivansigmund

I had to restart Zed before the snippet in the added markdown.json file would work…

sci-phi avatar Jun 25 '24 20:06 sci-phi

I had to restart Zed before the snippet in the added markdown.json file would work…

this works for mardown, but not for latex for me. also i am using the latex extension, maybe there is some clash?

ivansigmund avatar Jun 26 '24 09:06 ivansigmund

@failable there's a editor: show completions action. @ivansigmund see https://github.com/zed-industries/zed/issues/4611#issuecomment-2179009910, LaTeX has to be added to the list manually for now.

osiewicz avatar Jun 26 '24 09:06 osiewicz

@osiewicz Hello! I mean trigger expansion directly when prefix is inserted instead of showing the completion menu.

failable avatar Jun 26 '24 09:06 failable

@failable there's a editor: show completions action. @ivansigmund see #4611 (comment), LaTeX has to be added to the list manually for now.

thanks!

ivansigmund avatar Jun 26 '24 11:06 ivansigmund

Issue: Backslashes are removed in snippet expansion

When using the following snippet in Zed:

{
  "equation": {
    "prefix": "BEQ",
    "body": "\\begin{equation}\n\t$0\n\\end{equation}",
    "description": "equation environment"
  }
}

Expected behavior: Typing "BEQ" should expand to:

\begin{equation}

\end{equation}

Actual behavior: Typing "BEQ" expands to:

begin{equation}

end{equation}

The backslashes are missing in the expanded snippet. Steps to reproduce:

Add the above snippet to Zed In a new file, type "BEQ" and trigger snippet expansion

Possible cause: The backslashes might be getting escaped or removed during snippet processing.

ivansigmund avatar Jun 26 '24 12:06 ivansigmund

@osiewicz Thank you for this! Some things I think would be really useful:

  1. The docs should mention the required nomenclature somewhere:

Hey @failable, Currently we can only launch a language server for a known subset of languages (we don't have an ability to launch a language server for any file); we've simply missed Dockerfile in a list. I'll try to figure out if we could lift that limitation somehow by the next weeks release; I realize the current approach is not really scalable.

Took me like 30 minutes of fumbling around locally/searching the web to find your comment and discover how to properly set things up (.tsx snippets needing to go in typescriptreact.json in particular).

  1. Would be great for QoL if servers for superset languages automatically included the snippets of subset languages — for example, I'd expect my snippets/javascript.json file to apply to .ts files, instead of having to duplicate everything into my snippets/typescript.json file. Likewise with jstsx and tstsx.

  2. Noticed that comments do invalidate JSON here. Not always the case these days so maybe worth calling out

alecmarcus avatar Jun 27 '24 21:06 alecmarcus

Is it possible to use variables like in VSCode, like TM_FILENAME_BASE? My attempts failed.

brittof avatar Jul 01 '24 01:07 brittof