Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add language-agnostic snippets #13253

Merged
merged 7 commits into from
Jun 19, 2024
Merged

Add language-agnostic snippets #13253

merged 7 commits into from
Jun 19, 2024

Conversation

osiewicz
Copy link
Contributor

@osiewicz osiewicz commented Jun 19, 2024

Note that right now we can't attach a language server to arbitrary buffer, which is why I've listed a bunch of languages verbatim.
See https://github.com/zed-industries/simple-completion-language-server/tree/main for docs on how to define your snippets. They should be placed in ~/.config/zed/snippets ; snippets.(toml|json) file can be used to define language-agnostic snippets, and any other name (e.g. python.toml) will apply only to buffers of that particular type.

There's https://github.com/rafamadriz/friendly-snippets you can use as a repository of snippets, for your convenience.

Fixes #4611

Release Notes:

  • Added support for snippets via simple-completion-language-server

@cla-bot cla-bot bot added the cla-signed The user has signed the Contributor License Agreement label Jun 19, 2024
@zed-industries-bot
Copy link

zed-industries-bot commented Jun 19, 2024

Warnings
⚠️

This PR is missing release notes.

Please add a "Release Notes" section that describes the change:

Release Notes:

- (Added|Fixed|Improved) ... ([#<public_issue_number_if_exists>](https://github.com/zed-industries/zed/issues/<public_issue_number_if_exists>)).

If your change is not user-facing, you can use "N/A" for the entry:

Release Notes:

- N/A

Generated by 🚫 dangerJS against 758fe26

@d1y
Copy link
Contributor

d1y commented Jun 19, 2024

The official support now is really great.
I also made this extension before
And I would like to offer some suggestions and ideas:

  1. wonder if it's possible to change its service name to scls, which would be much easier to remember?
  2. How about adding a command to obtain rafamadriz/friendly? like Zed:: FetchFriendlySnippets?(d1y/scls@d0d1daf)
  3. The completion sorting of scls should be able to sort (Multiple LSP-server completion result support sort #12028)
  4. Always highlight code blocks (d1y/scls/8)

@osiewicz
Copy link
Contributor Author

Hey @d1y,
Oh, I didn't know that we've had an extension based on scls already.
On one hand yeah, scls is easier to remember, but on the other how are you supposed to know what it means? I think it makes sense to spell out snippets explicitly in the name to make it a bit easier on the users.
For the other ideas, I'd just wait for users feedback to make a call. I'm a bit skeptical of adding a zed action that's specific to an extension.
As for sorting, that sounds like a good idea; though we can probably implement in on the side of language server (setting it's sortText to something low-ish).
As for code highlighting, can we reliably assume that the snippet description contains just code?

@osiewicz
Copy link
Contributor Author

Nevermind, you're right about highlighting; since we're using the body as doc, it should be valid code. It may contain unsubstituted variables tho, which is a bit of a bummer.

@osiewicz osiewicz merged commit d665f28 into main Jun 19, 2024
9 checks passed
@osiewicz osiewicz deleted the snippets-lsp-server branch June 19, 2024 12:03
@eproxus
Copy link

eproxus commented Jun 19, 2024

@osiewicz I'm trying to use this according to these instructions and nothing shows up for me. I've installed the extension and created the following snippet files:

~/.config/zed/snippets/snippets.json:

{
  "my snippet": {
    "prefix": "my-snippet",
    "body": [
      "console.log('Hello World')"
    ],
    "description": "Log Hello World to the console"
  }
}

~/.config/zed/snippets/erlang.json:

{
    "horizontal line": {
        "prefix": "foo",
        "body": [
            "%--- Internal ------------------------------------------------------------------"
        ],
        "description": "horizontal comment line"
    }
}

However, typing my-snippet or foo anywhere (either in any syntax or in an Erlang file) doesn't actually show any completions. I even restarted Zed after setting everything up, but still nothing. Is there anything else one has to do to get it to work?

@osiewicz
Copy link
Contributor Author

@eproxus I think that's because Erlang is not in a list of languages for which this language server is enabled (see this line). That can be fixed. You should see completion from snippets.json in any other file (I think we don't do plain text right now); can you try it in e.g. go file?

@eproxus
Copy link

eproxus commented Jun 19, 2024

@osiewicz Yes, please support more languages! ❤️

I tried in a file with Shell syntax but the snippet above from snippets.json still doesn't show up...

@osiewicz
Copy link
Contributor Author

Oh, it looks like there's a typo in languages list; "Shell" should be a "Shell Script" syntax. TL;dr: try other file. I'll fix the shell association in a sec.

@eproxus
Copy link

eproxus commented Jun 19, 2024

Alright, it works in a Python file. However I noticed it does not work in any supported syntax if the file is not saved. Is that supposed to work?

@osiewicz
Copy link
Contributor Author

No, it's not supposed to work (unrelated to this PR); we don't support LSPs in unsaved files right now.

@eproxus
Copy link

eproxus commented Jun 19, 2024

Do you want a PR to add more languages?

@osiewicz
Copy link
Contributor Author

osiewicz commented Jun 19, 2024

I'm on it, but thanks anyways. =)

osiewicz added a commit that referenced this pull request Jun 19, 2024
@osiewicz
Copy link
Contributor Author

0.0.3 is out now. Give it a go =)

@eproxus
Copy link

eproxus commented Jun 19, 2024

@osiewicz Works! Thanks for the quick work! 😍

@danremollino
Copy link

You mention that you should be able to use a snippets.toml file. Have not been able to get this to work. Do I have something formatted wrong?

[snippet]
prefix = "log"
body = ["console.log(\"$1\")"]
description = "Expand `log` to `console.log()`"

@osiewicz
Copy link
Contributor Author

@danremollino yeah, I think the first line should be [[snippets]] and not [snippet].

@eproxus
Copy link

eproxus commented Jun 20, 2024

Here's an example erlang.toml that I use:

[[snippets]]
prefix = "hr"
scope = ["erlang"]
body = '%--- ${1:Internal} ------------------------------------------------------------------'

[[snippets]]
prefix = "iof"
scope = ["erlang"]
body = 'io:format("--> ${1}: ~p~n", [${1}])'

@osiewicz Is there any chance advanced text replacements will be added? I'm thinking like filter than VS Code or Sublime uses, with support for regex replacements etc. In the above example I'd like the first ${1} instance in the second snippet to be upper case but I couldn't figure out a way to do it...

@osiewicz
Copy link
Contributor Author

@eproxus I guess it might be added at some point, yeah - it's in the spec, so I don't see why not; our snippet support is relatively scarce right now, so please create a new issue for it.

@eproxus
Copy link

eproxus commented Jun 20, 2024

@osiewicz Great! I'll file an issue. Do you know if those variables are supported already, like TM_FILENAME_BASE?

@osiewicz
Copy link
Contributor Author

Hehe, nope: #13255

@tepavcevic
Copy link

@osiewicz I have an issue with starting snippets language server on Ubuntu.
My installation process went as follows:

  • installed the extention
  • added a sample console.log snippet to ~./config/zed/snippets/snippets.json
  • no snippets show up

In my zed logs I've found errors related to starting the snippets server:
[ERROR] failed to start language server "snippet-completion-server": no asset found matching "simple-completion-language-server-x86_64-unknown-linux-musl.tar.gz"

Can you please help me decode the issue here, since my best guess was that the extention is not installed correctly and I've tried reinstalling multiple times with no result?

@osiewicz
Copy link
Contributor Author

@tepavcevic Thankfully this is relatively straightforward - I've screwed up the name of a platform for Linux. None of this is your fault and I'm sorry for it being so confusing.
I'll release a new version which should fix it shortly.

osiewicz added a commit that referenced this pull request Jun 23, 2024
Fix invalid platform name on Linux.
Related to:
#13253 (comment)
Release Notes:

- N/A
@osiewicz
Copy link
Contributor Author

osiewicz commented Jun 23, 2024

@tepavcevic Can you give 0.0.5 snippets extension a try?

@tepavcevic
Copy link

@tepavcevic Can you give 0.0.5 snippets extension a try?

Just tried out 0.0.5, working as intended. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Language server agnostic snippets
6 participants