zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
46.61k stars 2.64k forks source link

Ability to trigger snippets by TAB or other key on text #4616

Open failable opened 1 year ago

failable commented 1 year ago

Check for existing issues

Describe the feature

In some situations, LSP will not provide any completions (also there's no plain words completion in Zed currently). Thus one can not trigger snippets without the completion menu. Adding the ability to trigger snippets by TAB or other key without selecting from the completion menu can be helpful.

For example,

#[derive(thiserror::Error, Debug)]
pub enum Error {
    | <---cursor    
}

RA will provide no candidates here as it's expecting user input enum names. But one might have snippets like

    Io {
        #[from]
        source: io::Error,
        backtrace: Backtrace,
    }$0

As the completion menu is not popped up, the snippet can not be (selected and) triggered.

(Well, this might now be a good example, but LSP like RA usually provides no completions when expecting names.)

If the feature is added, it works like

#[derive(thiserror::Error, Debug)]
pub enum Error {
    io|  <------ TAB here to trigger snippet, "io" is the prefix
}

then results in

#[derive(thiserror::Error, Debug)]
pub enum Error {
    Io {
        #[from]
        source: io::Error,
        backtrace: Backtrace,
    }|
}

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

No response

peterbe commented 1 year ago

Yes please!! The thing I miss the most coming from VS Code is the ability to set up snippets. In VS Code, I can type log[TAB] and it turns that into console.log(CURSORHERE), for example.

gianpaj commented 7 months ago

Fantastic IDE!

same here. I use almost everyday cl for console.log

image
{
  // Place your snippets for typescriptreact here. Each snippet is defined under a snippet name and has a prefix, body and
  // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
  // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
  // same ids are connected.
  // Example:
  // "Print to console": {
  //    "prefix": "log",
  //    "body": [
  //        "console.log('$1');",
  //        "$2"
  //    ],
  //    "description": "Log output to console"
  // }
  "console.log('var', var)": {
    "prefix": "clv",
    "body": "console.log('$1', $1)$0"
  }
}