Sublime Forum

Create snippets of user defined functions on the fly

#1

Hi everyone,

I was wondering if it is possible to create snippets of user-defined functions on the fly.
For example, let’s say I am writing some C code and I have a function
void foo(type1 arg1, type2 arg2)
Would it be possible to create a snippet for that function with tab stops for each argument automatically? Of course this should work with other languages as well.

I’m not asking that to be implemented in vanilla ST. I’m just wondering if that is possible.
I don’t think I could find the answer myself since I don’t have much time these days (still a student) and since I’m a noob when it comes to plugins :sweat_smile:

Anyway, i wish you all a nice day.

0 Likes

#2

I don’t write C, but are you talking about something like this?

  • you write the .h line
  • you get a snippet to write your function

Yes it is possible, but I’m not sure if there’s a plugin that already does that… Have you had a look on package control?

Matt

0 Likes

#3

Yes, that would be awesome.
Yes, I have thoroughly searched for something similar on package control and even came across someone suggesting the same thing on a forum, but I wasn’t able to find anything regarding C/C++ that had that. However it could already exist for other languages, I don’t know.

Thanks for the quick response!

0 Likes

#4

If you want to define your own snippets, you can do so like this.

Basically, you need to go on tools -> developer -> new snippet.

<snippet>
    <content><![CDATA[void foo(${1:type} ${5:arg1}, $10)
{
  $0
}
]]></content>
    <!-- Optional: Tab trigger to activate the snippet -->
    <tabTrigger>fn</tabTrigger>
    <!-- Optional: Scope the tab trigger will be active in -->
    <scope>source.c</scope>
</snippet>

Next time you only need to type fn then press tab and the whole thing will unfurl as wanted.

Hints:

  1. You should use tabs for indentation on snippets (even if you’re using spaces in your code);
  • don’t use subseqents stops (e.g. $1, $2, $3 etc); leaving a range between them will allow you to add an extra tabstop in the future without the need to chaneg everything.
0 Likes

#5

I would like the snippets to be automatically created for all functions that I define.
Doing this for each function would be ridiculously long and boring.

1 Like

#6

AH! You actually need some kind of intellisense, that detects the functions in current project.

If that’s the case, you have several options:

Also, a similar discussion.

1 Like

#7

Ah thank you, this looks promising! I’m quite busy right now so I’ll see your suggestions later (and give feedback of course).

0 Likes