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
62.61k stars 4.73k forks source link

snippets: Linked tabstops don't use the same placeholder/choice #28646

Open loczek opened 3 months ago

loczek commented 3 months ago

Summary

Currently linked tabstops don't use the same placeholder, instead they need a placeholder for each linked tabstop.

Instead of providing each tabstop with a placeholder they probably should use the first one that is encountered. I don't think there is a case where we would want to have a linked tabstop that uses different values, also this change would bring the behavior closer to what VSCode does.

This also applies to choices like ${1|one,two,three|}

Steps to reproduce:

  1. Paste the snippet file listed below
  2. Type "for" and insert snippet
{
  "for statement": {
    "prefix": "for",
    "body": ["for ${1:i} := ${2:0}; $1 < ${3:count}; $1${4:++} {", "\t$0", "}"],
  },
}

Expected Behavior:

for i := 0; i < count; i++ {

}

Actual Behavior:

for i := 0;  < count; ++ {

}

Zed Version and System Specs

Zed: v0.182.0 (Zed Dev b864a9b0ae633006a44c02b723fe6fad07e84b93) OS: Windows 10.0.26200 Memory: 63.9 GiB Architecture: x86_64 GPU: NVIDIA GeForce GTX 1070 || NVIDIA || 566.36

notpeter commented 3 months ago

I can reproduce the behavior you're observing. So the request is that when a default value is applied to a variable (e.g. ${1:i}) that any subsequent references to $1 without a default specified should implicitly inherit from the initial default. Doesn't seem too crazy.

As a workaround you can specify the default for all instances of $1:

{
  "for statement": {
    "prefix": "for",
    "body": ["for ${1:i} := ${2:0}; ${1:i} < ${3:count}; ${1:i}${4:++} {", "\t$0", "}"]
  }
}

Zed's snippets are powered by zed-industries/simple-completion-language-server a fork of estin/simple-completion-language-server, likely this would need to be implemented upstream.