kakoune-snippets icon indicating copy to clipboard operation
kakoune-snippets copied to clipboard

[Feature Request] Discard default text on typing

Open andreyorst opened this issue 6 years ago • 9 comments

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

andreyorst avatar Jan 17 '19 13:01 andreyorst

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 avatar Jan 22 '19 05:01 andreyorst

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 avatar Jan 22 '19 06:01 andreyorst

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 } 

andreyorst avatar Jan 22 '19 06:01 andreyorst

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 }
}

occivink avatar Jan 22 '19 08:01 occivink

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.

andreyorst avatar Jan 22 '19 08:01 andreyorst

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

occivink avatar Jan 22 '19 09:01 occivink

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 }

andreyorst avatar Jan 22 '19 13:01 andreyorst

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

aidam38 avatar Mar 12 '19 19:03 aidam38

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.

andreyorst avatar Apr 22 '19 10:04 andreyorst