Skip to content

Conversation

@idbrii
Copy link
Contributor

@idbrii idbrii commented Jan 6, 2025

Fix #1041: iterating over snip.buffer fails in neovim.

Use iter() to defer iteration of a buffer to the underlying object.

Test

  • Run this code in neovim and vim: import vim import UltiSnips.vim_helper as helper
    def dump(b):
        for line in b:
            print(line.upper())

    # This works fine in both nvim and vim.
    dump(vim.current.buffer)

    # In nvim, the iteration previously failed with "pynvim.api.common.NvimError: Index out of bounds"
    dump(helper.VimBuffer())

@idbrii
Copy link
Contributor Author

idbrii commented Jan 6, 2025

Draft because I don't know how to write or run tests. (I'm on Windows.)

Fix SirVer#1041: iterating over snip.buffer fails in neovim.

Use __iter__() to defer iteration of a buffer to the underlying object.

Test
* Run this code in neovim and vim:
    import vim
    import UltiSnips.vim_helper as helper

    def dump(b):
        for line in b:
            print(line.upper())

    # This works fine in both nvim and vim.
    dump(vim.current.buffer)

    # In nvim, the iteration previously failed with "pynvim.api.common.NvimError: Index out of bounds"
    dump(helper.VimBuffer())
@idbrii
Copy link
Contributor Author

idbrii commented Jan 8, 2025

I couldn't figure out the tests, so I removed it from my PR. Here's my proposed test (just guessing how to specify the contents of the buffer):

class PythonCode_IterateBuffer(_VimTest):
    snippets = ("test", """Start `!p
        snip.rv = ""
        for line in snip.buffer:
            snip.rv += "x"
        ` End""")
    buffer = """a
        b
        c
        d
        """
    keys = "test" + EX
    wanted = "Start xxxx End"

The change is simple enough that it may not warrant the test.

@idbrii idbrii marked this pull request as ready for review January 8, 2025 05:47
@SirVer
Copy link
Owner

SirVer commented Jan 11, 2025

I think the change is fine without a test, but please add a comment explaining why it is necessary. Once done I'll happily merge.

@SirVer SirVer merged commit dbc458e into SirVer:master Mar 9, 2025
11 checks passed
@SirVer
Copy link
Owner

SirVer commented Mar 9, 2025

@idbrii Finally got around to cleaning this up and merging it. Thanks for your contribution! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pynvim.api.nvim.NvimError: b'Index out of bounds'

2 participants