A new type of snippets plugin

(tangent warning) I have yet to – in any editor – really used snippets because there is always some ergonomic snafu that stops me. Sometimes I wonder if post-fix style snippets would work best…

x > 15
console.log("It sure is greater than 15"); exit
console.log("what a tiny less than 15 number"); continue

highlight 3 lines and do your “if” wrapper.

if (x > 15) {
    console.log("It sure is greater than 15")
    exit
} else {
    console.log("what a tiny less than 15 number")
    continue
}
4 Likes

There’s something like this in UltiSnips, called visual placeholder. What it esentially does, is, this:

if ($1) {
    ${VISUAL}
}

You select text you want to go into VISUAL, and press tab. Text disappears, and when you expand a snippet that has a visual placeholder, this text will be used. This doesn’t work more than one time in a row. And because we have R supporting this seems unreasonable.

But I really like your idea of line-based wrapping into snippet. Reminds me of theorem style programming, where you simply specify stuff on lines.

Yeah, I actually think I am going to make this, because it is something I want a lot, and all I need to do is find sane intra-line delimiter, semi-colon probably isn’t it as that will conflict with lots of languages.

I am trying to decide if this is a in conflict with the idea of objects / verb. The core of that thing is content, the action is wrapping it in some syntax?

I actually don’t mind if this conflicts with oject/verb because it makes you more productive in the end.

Kinda would end up being like emmet for code…

What if you’ll use selections? It may be not very fast thing to do though. What I mean is:

[x > 15]
[console.log("It sure is greater than 15")] [exit]
[console.log("what a tiny less than 15 number"] [continue]

It is still line based, but inline selections will split into separate lines in the result:

if ([x > 15]) {
    [console.log("It sure is greater than 15")]
    [exit]
} else {
    [console.log("what a tiny less than 15 number")]
    [continue]
}

([] obviously represent selections)

Interesting idea, but since the number of statements per line is variable, selections would always be annoying… I think something like using Go and building a context object out of input and using the Go builtin template system would make this both easy and practical.

Some food for thought Robert.

Postfix completion on the dot . operator then keyword is working well for me as it keeps a continuous workflow / momentum. The pictures below tell the story for your JavaScript. Perhaps a mix with Andrey’s suggestion would get it working snippet<–register<–postfix.

IntelliJ panel

if-else block

intellij_posfix-completion_2

Cya :wave:


JetBrains GoLand Blog: Increase productivity with Custom Postfix Completion templates.

@duncan that never even crossed my mind, but what a cool idea… I want it.

@andreyorst how about your Mapping sequence of keys in insert mode to trigger kak’s register → execute-keys → snippets.

With snippets being the work of the below.

Would this work Andrey??

Mate, I had no idea. Thank you for the info.
So here is where I’m at and currently stuck. As my kakoune register skills need improvement.

The sequence of events are:

  1. activate snippet trigger on .keyword
  2. escape to normal mode
  3. extend to previous . inclusive
  4. delete
  5. select to previous word start
  6. save to register
  7. delete selected text
  8. do snippet
  9. paste saved register into first tabstop position
  10. if next tabstop then advance
  11. else advance to end of line
  12. enter insert mode
  13. end sequence
Main.class<esc><a-f>.db"0y
do snippet
<c-r>0  paste saved register into first slot
<tab>   advance onto next tab position

where *.class snippets is:

class ${1:Name} {
    ${0:// body}
}

If I can get one going then I’m off and running to postfix glory :smile:. Grateful for any help guys. Bye :wave:


I’m thinking that wrapping the snippets function will allow a way to define a subset of snippets as a trigger and or alternate sequence of execute-keys on a subset of those snippets.

define-command postfix-snippets-wrap %{
  execute-keys '<a-t>."syHdb"kyc'
  execute-keys '<a-;>: snippets-expand-or-jump %val{main_reg_s}'
  execute-keys '<a-;>d<c-r>k'
  execute-keys '<tab><a-;>d'
}

To be continued…

How are your travels going with the tangent @robertmeta?

this could work, but kakoune-snippets already feature automatic expansion of trigger in insert mode without needing to press anything. Maybe I misunderstood your proposal though.