occivink / kakoune-snippets

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

[Feature Request] Discard default text on typing #8

Closed andreyorst closed 2 years ago

andreyorst commented 5 years ago

After you jump to the placeholder the text is selected. Typing arbitrary text right after jump could perform c automatically for us.

andreyorst commented 5 years ago

this can be solved by adding this hook to the very end of snippets-select-next-placeholders command:

hook window -once InsertChar .* %{ execute-keys -draft "di%val{hook_param}" }
andreyorst commented 5 years ago

hm, but we need to discard placeholders that do not have default text from such hook. And discard this hook if user leaves insert mode for some reason

andreyorst commented 5 years ago

for some reason the hook, mentioned above works fine for selections with 2+ characters, but results in duplicated input on single selected characters. From my point of view there should be no differencies between single cell selection and a multiple cell selection

This hooks works when added manually, but not from your function:

hook window -once InsertChar .* %{ execute-keys -draft d } 
occivink commented 5 years ago

hm, but we need to discard placeholders that do not have default textfrom such hook. Anddiscard this hook if user leaves insertmode for somereason

You could do

try %{
    # abort if there is no default text
    exec -draft '<a-K>\A.\z<ret>'
    hook window -group auto-discard InsertChar .* %{
        exec-keys -draft "di%val{hook_param}"
        rmhooks auto-discard
    }
    hook window -group auto-discard InsertEnd '' %{ rmhooks auto-discard }
    hook window -group auto-discard InsertMove '' %{ rmhooks auto-discard }
}
andreyorst commented 5 years ago

This is bit more difficult.

First we need different behavior for selections with single character and more than single character:

try %{
    # abort if there default text is less than two chars
    execute-keys -draft '<a-k>\A[^\h]{2,}\z<ret>'
    hook window -once -group auto-discard InsertChar [^\h\n\t]+ %{ execute-keys -draft "c%val{hook_param}" }
    hook window -group auto-discard InsertEnd '' %{ remove-hooks window auto-discard }
    hook window -group auto-discard InsertMove '' %{ remove-hooks window auto-discard }
} catch %{
    # abort if there is no default text
    execute-keys -draft '<a-k>\A[^\h]\z<ret>'
    hook window -once -group auto-discard InsertChar [^\h\n\t]+ %{ execute-keys -draft d }
    hook window -group auto-discard InsertEnd '' %{ remove-hooks window auto-discard }
    hook window -group auto-discard InsertMove '' %{ remove-hooks window auto-discard }
}

But there's another problem. For example in case of this snippet:

for (int ${1:i}; $1 < $2; $1++) {
${indent}$0
}

The second placeholder doesn't have default text. But the selection will be placed on the ; symbol so this try block will successfully evaluate, and when we will start to type we will replace ;.

So the check should be performed when calculating selections.

occivink commented 5 years ago

Mh yeah that's unfortunate, we can't distinguish an empty placeholder from a 1-character wide one after they've been inserted. I was kinda hoping that I could get away with leaving this auto-discard feature as a simple user configuration, but it looks like it might not be so easy

andreyorst commented 5 years ago

Also I've figured out what's causing that we need two different hooks.Since we start in append mode, after selections are added, inserted text gets deleted by the d. If placeholder was selected in insert mode instead we could use only one hook here - the bottom one.

hook window -once -group auto-discard InsertChar [^\h\n\t]+ %{ execute-keys -draft "c%val{hook_param}" }
hook window -once -group auto-discard InsertChar [^\h\n\t]+ %{ execute-keys -draft d }
akrivka commented 5 years ago

Is there any progress on this? I would like to know...

andreyorst commented 5 years ago

I guess I can make this feature real, but I need to improve my Perl skills first. Anyway this is in my TODO list, since I really need this feature to use snippets comfortably.