cursorless-dev / cursorless

Don't let the cursor slow you down
https://www.cursorless.org/
MIT License
1.13k stars 79 forks source link

neovim: use vim.fn.getregionpos() to replace buffer_get_selection() #2406

Open fidgetingbits opened 3 months ago

fidgetingbits commented 3 months ago

There is recently a new function in neovim called getregionpos which I believe will be better to use than the existing buffer_get_selection(). Also has the benefit of existing tests in neovim, etc. One thing I'm not sure about will be the 'reverse' part, but I suspect it will still be easier to just use vim.fn.getregionpos() and work out the reverse flag.

See https://github.com/vim/vim/pull/14617

We will have to wait at least for 0.11 release (October 31, 2024) for it to be available though.

saidelike commented 3 months ago

Maybe i misread https://github.com/vim/vim/pull/14617/files#diff-ef8946d89919ad628b9ac65c49d3067e4e017b0980cac70eb25d832a59ef677dR4332 but is it the same?

getregionpos({pos1}, {pos2} [, {opts}])            *getregionpos()*
        Same as |getregion()|, but returns a list of positions
        describing the buffer text segments bound by {pos1} and
        {pos2}.
        The segments are a pair of positions for every line:
                    [[{pos_start}, {pos_end}], ...]

        The position is a |List| with four numbers:
            [bufnum, lnum, col, off]
        "bufnum" is the buffer number.
        "lnum" and "col" are the position in the buffer.  The first
        column is 1.
        The "off" number is zero, unless 'virtualedit' is used.  Then
        it is the offset in screen columns from the start of the
        character.  E.g., a position within a <Tab> or after the last
        character.

        Can also be used as a |method|: >
            getpos('.')->getregionpos(getpos("'a"))
<
fidgetingbits commented 3 months ago

It's not identical. My thinking is that you would call it with something like vim.fn.getpos("'>") and vim.fn.getpos("'>") as the visual selection start/end positions to get back most of the coordinates you are currently trying to compute yourself. Probably the majority of buffer_get_selection()'s logic would be replaced by a call to vim.fn.getregionpos(). But ya, would need to test to make sure it can be used.

fidgetingbits commented 3 months ago

I didn't even try to use getregionpos() in the end, as it was already simple enough without:

https://github.com/saidelike/cursorless/pull/9

Once that PR is merged can close this issue imo.