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

Feature request/idea: shrink?

Open dpc opened this issue 6 years ago • 12 comments

,<a-e> could do a reverse operation?

dpc avatar Nov 24 '18 08:11 dpc

I considered "reverse expand" but it's not as easy as expanding, and I'm not sure what would be the correct semantics. If it's to simply reverse previous expand commands I think solving this issue would be a better direction. If it's to shrink arbitrary selections then the problem is that there is not necessarily just one object that is a subset current, and then we somehow have to chose which. Let's say your selection is this, what should it shrink to?

[
   [a,b,c],
   [d,e,f],
]

occivink avatar Nov 25 '18 21:11 occivink

Mh having written this I realize that we could just select both. Let me try to think about this

occivink avatar Nov 25 '18 21:11 occivink

Ok I started working on this feature, I found an algorithm I'm satisfied with. If you check the shrink branch you should be able to try the new shrink command. It's configured with an option, similarly to expand_commands. The default value is

declare-option str-list shrink_commands \
    %{ exec <a-s> } \
    %{ exec 's\{.*?\}<ret>' } \
    %{ exec 's\[.*?\]<ret>' } \
    %{ exec 's<lt>.*?><ret>' } \
    %{ exec '1S\n(\n+)<ret>' } \
    %{ exec 's\w+<ret>' } \
    %{ exec '<a-X>' } \

but I'll probably be tweaking this some. If you have some feedback that'd be cool

occivink avatar Mar 24 '19 11:03 occivink

I'll be doing some refactoring (possibly to merge the expand and shrink implementations) before I merge but I don't expect any drastic changes

occivink avatar Mar 24 '19 11:03 occivink

I like how selection expands keeping single cusor. It matches how word-movement.kak acts when I move with capital W and B. But when I shrink (maybe reduce is a more appropriate name?) the selection, there are new cursors added, which involves additional steps to remove unneeded cursor with Alt+Space, which is somewhat different from what I've expected I can get used to it, but generic undo selection would be nicer. Can't you store new selection parameters on each expand in stack like option to restore previous state?

I'm using the very same package for Emacs: expand-region.el (consider trying it out), and I like how it works for different languages with different semantics. Is something like this possible with your plugin?

Also, it would be cool if it could be possible to select current word first, then current WORD, then from first non-whitespace character to last non-whitespace, then full line, and after that expand over regions. This is how Emacs package does it.

andreyorst avatar Apr 05 '19 19:04 andreyorst

I’ve been watching the video but it does not show how reduce would work with double <div> or when it has multiple children.

https://youtu.be/_RvHz3vJ3kA?t=76

alexherbo2 avatar Apr 05 '19 19:04 alexherbo2

In my opinion the shrink feature is a nop.

alexherbo2 avatar Apr 06 '19 00:04 alexherbo2

It hangs Kakoune if I expand selection to whole file of C code which is 3500 lines long and will try to shrink the selection. Perhaps too many multiple cursors are produced by shrinking

andreyorst avatar Apr 08 '19 09:04 andreyorst

You're right, in that case s\w+ produces a lot of selections which explodes the computation time. I also think that the fact that the shrinking result is independent for each selection makes the result really hard to predict when you chain multiple calls to shrink, it's not really usable currently

occivink avatar Apr 08 '19 10:04 occivink

I’m currently working on a split object feature. Maybe it could help.

alexherbo2 avatar Apr 08 '19 11:04 alexherbo2

I also think that the fact that the shrinking result is independent for each selection makes the result really hard to predict when you chain multiple calls to shrink, it's not really usable currently

That's why I feel that going to previous selection would be more appropriate way to shrink, but as you've said it is impossible to do without https://github.com/mawww/kakoune/issues/898 being fixed. Unless you could store selections by script. I've tried to do it, but couldn't find a proper way to extract ranges from stack

andreyorst avatar Apr 08 '19 11:04 andreyorst

POC implementation of expand-reduce-selection command.

# a variable to store selection stack
declare-option str selection_stack ''

# this command adds current selection to the beginning of the stack
define-command expand-store-selection %{ evaluate-commands %sh{
    eval "set -- ${kak_opt_selection_stack}"
    printf "%s " "set-option global selection_stack %{$kak_selection_desc $@}"
}}

# this command takes first selection out of stack and selects it
define-command expand-select-previous %{ evaluate-commands %sh{
    eval "set -- ${kak_opt_selection_stack}"
    [ -n "$1" ] && printf "%s\n" "select $1"
    shift
    printf "%s " "set-option global selection_stack %{$@}"
}}

Modifications to expand command:

def expand -docstring '
Expand the current selections up to their next semantic block
' %{
    expand-store-selection # store current selection before expanding.
    expand-shrink-impl expand %opt{expand_commands}
}

Now we can reduce selection to previous one without generating new selections in case we've expanded too far. I think we need to update current selection if it was modified before expanding.

andreyorst avatar Apr 09 '19 19:04 andreyorst