occivink / kakoune-snippets

Snippet support for kakoune
The Unlicense
48 stars 6 forks source link

Kakoune hangs when trying to add snippets #11

Closed andreyorst closed 5 years ago

andreyorst commented 5 years ago

simply executing of :set-option -add window snippets 'trigger' 'snippet' results in endless waiting for shell to finish.

*debug* buffer shows insanely huge log:

...
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
sh: line 5: shift: 3: shift count out of range
>>>
error running hook WinSetOption(snippets='trigger' 'snippet')/: 2:6: 'set' regex parse error: regex parsed to more than 32767 ast nodes at '\A(snippet|snippet|snippet|snippet|snippet|snippet|snippet|snippet|snippet|snippet|snippet|snipp...
occivink commented 5 years ago

I merged the two options as I was hinting at, it now follows this pattern: snippet-name snippet-trigger snippet-command... I've only been testing the happy path where the option is well-formed so there are probably some bugs if it's not, but I cannot reproduce this one. Is the option already set to something when you do this?

Note that I also pushed a fix for another issue, but it doesn't look like the same.

andreyorst commented 5 years ago

Is the option already set to something when you do this?

No, it is empty. Maybe it is somehow related to Perl? My Perl is v5.28.1.

I merged the two options as I was hinting at, it now follows this pattern: snippet-name snippet-trigger snippet-command...

It would be great if somewhere in the repo were an up to date example of proper snippet definition.

andreyorst commented 5 years ago

I've updated my snippet definitions to what you've described:

hook global WinSetOption filetype=(c|cpp) %{
    unset-option window snippets

    set-option -add window snippets 'For loop' 'for' %{
        snippets-insert 'for (int ${1:i}; $1 < $2; $1++) {
                         ${indent}$0
                         }'
    }

    set-option -add window snippets 'Main function' 'main' %{
    snippets-insert %{int main() {
                     ${indent}$0
                     }}
    }
}

hook global WinSetOption filetype=(rust) %{
    unset-option window snippets

    set-option -add window snippets 'Main function' 'main' %{
    snippets-insert 'fn main() {
                     ${indent}$0
                     }'
    }
}

And now it works. However the issue still persists.

occivink commented 5 years ago

This particular setup seems to work on my side, is this with 35b3371ce8b5937ce1ebb616cd03b08c826c287d? I've also got the same perl version.

It would be great if somewhere in the repo were an up to date example of proper snippet definition.

Yeah that's a good idea

andreyorst commented 5 years ago

I've updated Kakoune, and your plugin, and executing :set-option -add window snippets 'trigger' 'snippet' still hangs my editor. I understand that there must be 3 parameters, but error handling should deal with it.

The problem is here: https://github.com/occivink/kakoune-snippets/blob/3edfca8fa44c22d52f37f6e14a66b67203f83c1a/snippets.kak#L14 but I don't see the proper way to ensure that user provided all three args. Maybe use a function that will require three parameters to add snippet? It could replace snippet insert too.

Something like:

snippets-add 'hint' 'trigger' snippet'

This also could be used by other plugins to dynamically add snippets.

occivink commented 5 years ago

Oh I can reproduce it now, it only occurs with bash as the shell, somehow not with dash

occivink commented 5 years ago

Note that I heavily recommend using dash with kakoune (consider using KAKOUNE_POSIX_SHELL if you don't want to modify /bin/sh) especially if you're on linux, the performance gains are substantial (and essentially free).

andreyorst commented 5 years ago

I'm using dash on part of my devices, but I also use bash because I want to ensure that my plug.kak and fzf.kak work fine on both shells.

occivink commented 5 years ago

One thing that surprises me is that this

    snippets-insert 'if ($1) {
                     ${indent}$2
                     } else {
                     ${indent}$0
                     }'

works nicely regarding indentation. With how I implemented it, I would expect this:

if ($1) {
                     ${indent}$2
                     } else {
                     ${indent}$0
                     }

to be inserted.

edit: nevermind, I implemented that it copies the indentation from the first lines to the others so it's actually working as intended.

andreyorst commented 5 years ago

It seems that I can't find any info about KAKOUNE_POSIX_SHELL Is it a shell variable that I can set in my shellrc?

occivink commented 5 years ago

It's an environment variable that was added somewhat recently (see the closing commit), it's not really advertised since it can break things if you don't use a posix shell.