SirVer / ultisnips

UltiSnips - The ultimate snippet solution for Vim. Send pull requests to SirVer/ultisnips!
GNU General Public License v3.0
7.55k stars 691 forks source link

Use tabstop exclusively in an interpolation #1511

Open alexandersokolow opened 1 year ago

alexandersokolow commented 1 year ago

Hi, I wanted to create a snippet for react hooks, which usually go by the naming convention of "useName".

It's easy to do if I have use the uncapitalized version somewhere else, but if I only want to have the capitalized version of my input, it won't prompt me for the first tabstop & just complete the snippet with an empty input.

I.e., look at this snippet (where capitalize_first is a function doing exactly what the name says haha):

use`!p snip.rv=capitalize_first(t[1])`

It will complete immediately without prompting me for text & just return "use"

The following works:

$1
use`!p snip.rv=capitalize_first(t[1])`

But it will give me: name useName

instead of just: useName

How can I make it just give me the useName?

Btw, if you wondering whether this simple snippet isn't pretty useless, I actually want to generate the following:

const useName = () => {
  return {};
};

export { useName };
SirVer commented 1 year ago

how should the snippet know which Name you want to use?

alexandersokolow commented 1 year ago

I imagined the following: I type my snippet shortcut (rh for react hook or something like that), click tab to get the boilerplate code without the name & when I start typing, it will take the text I type and capitalize it. Kind of like a normal snippet with just 1 tabstop, but the text from this tabstop should instantly get capitalized. I.e., once I type the first letter "n" (for name) it should give me an N instead of an n.

So, it would still have the tabstop, but that tabstop would only be found "inside" of the interpolation.

SirVer commented 1 year ago

Ah, this is difficult to do, since UltiSnips cannot control the text that Vim types into the buffer. And it would confuse Vim a lot to modify text "under it" so to speak, so unfortunately this will not be easy to implement.

alexandersokolow commented 1 year ago

Hmm, guess there's no simple built-in solution for this then, maybe I'll have a try at implementing it at some point.

Could you elaborate on what you mean by "it cannot control the text that Vim types into the buffer"? Isn't it removing (e.g. "uh") and inserting (e.g. "useHook") text from / into the buffer every time I expand a snippet?

SirVer commented 1 year ago

@alexandersokolow Patch would be very welcome!

What I mean is that if you type a character, Vim is interpreting your input and puts the char in the buffer. Only after, UltiSnips gets a chance to modify the buffer. If it overwrites what Vim just put into the buffer, vim gets really confused.