SirVer / ultisnips

UltiSnips - The ultimate snippet solution for Vim. Send pull requests to SirVer/ultisnips!
GNU General Public License v3.0
7.55k stars 691 forks source link

Regex snippet starting with `\b(?i)` used to work but doesn't anymore. #1531

Closed iago-lito closed 1 year ago

iago-lito commented 1 year ago

The following regex snippet, triggered in-word with \b and ignoring case with (?i) used to work:

snippet "\b(?i)regex" "not-inword-regex-snippet" r
not-even-expanded
endsnippet

But it makes Ultisnips crash on first insertion with neovim now, with the following error:

An error occured. This is either a bug in UltiSnips or a bug in a
snippet definition. If you think this is a bug, please report it to
https://github.com/SirVer/ultisnips/issues/new
Please read and follow:
https://github.com/SirVer/ultisnips/blob/master/CONTRIBUTING.md#reproducing-bugs

Following is the full stack trace:
Traceback (most recent call last):
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/definition/base.py", line 334, in matches
    match = self._re_match(before)
            ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/definition/base.py", line 143, in _re_match
    for match in re.finditer(self._trigger, trigger):
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/__init__.py", line 223, in finditer
    return _compile(pattern, flags).finditer(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/__init__.py", line 294, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_compiler.py", line 743, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 980, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 455, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/re/_parser.py", line 841, in _parse
    raise source.error('global flags not at the start '
re.error: global flags not at the start of the expression at position 2

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/err_to_scratch_buffer.py", line 47, in wrapper
    return func(self, *args, **kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet_manager.py", line 945, in _track_change
    self._try_expand(autotrigger_only=True)
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet_manager.py", line 802, in _try_expand
    before, snippets = self._can_expand(autotrigger_only)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet_manager.py", line 798, in _can_expand
    return before, self._snips(before, False, autotrigger_only)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet_manager.py", line 679, in _snips
    source.ensure(filetypes)
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/source/file/base.py", line 32, in ensure
    self._load_snippets_for(ft)
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/source/file/base.py", line 54, in _load_snippets_for
    self._parse_snippets(ft, fn)
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/source/file/base.py", line 65, in _parse_snippets
    for event, data in self._parse_snippet_file(file_data, filename):
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/source/file/ulti_snips.py", line 221, in _parse_snippet_file
    for event, data in _parse_snippets_file(filedata, filename):
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/source/file/ulti_snips.py", line 173, in _parse_snippets_file
    snippet = _handle_snippet_or_global(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/source/file/ulti_snips.py", line 139, in _handle_snippet_or_global
    definition = UltiSnipsSnippetDefinition(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/definition/base.py", line 115, in __init__
    self.matches(self._trigger)
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/definition/base.py", line 336, in matches
    self._make_debug_exception(e)
  File "/home/.vim/plugged/Ultisnips/pythonx/UltiSnips/snippet/definition/base.py", line 265, in _make_debug_exception
    self._actions["pre_expand"] if "pre_expand" in self._actions else "<none>",
                                                   ^^^^^^^^^^^^^
AttributeError: 'UltiSnipsSnippetDefinition' object has no attribute '_actions'

Here is how to reproduce with a DockerFile recipe:

# syntax=docker/dockerfile:1.3-labs
FROM archlinux

# Configure.
RUN <<EOF
    echo 'Server = https://mirrors.kernel.org/archlinux/$repo/os/$arch' \
         > /etc/pacman.d/mirrorlist
    pacman -Syu --noconfirm
EOF

# Dependencies.
RUN pacman -Sy --noconfirm neovim git python-pynvim

# Install Ultisnips.
RUN <<EOF
    curl -fLo /usr/share/nvim/runtime/autoload/plug.vim --create-dirs \
         https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
EOF
COPY <<-"EOF" /root/.config/nvim/init.vim
    call plug#begin('/home/.vim/plugged')
        Plug 'SirVer/Ultisnips'
    call plug#end()
    PlugUpdate
EOF

# The faulty snippet.
COPY <<-"EOF" /root/.config/nvim/UltiSnips/all.snippets
snippet "\b(?i)regex" "not-inword-regex-snippet" r
not-even-expanded
endsnippet
EOF

WORKDIR /home

ENTRYPOINT ["nvim", "empty_file"] 

Steps to reproduce:

$ docker buildx build -t ultisnips . # From the folder with the above `Dockerfile`.
$ docker run --rm -it ultisnips
<Ultisnips is being installed by the plugin manager, press `q` when it's done>
<Insert new line and first char with `o;`>
SirVer commented 1 year ago

I believe this is due to an update of Python in your setup:

re.error: global flags not at the start of the expression at position 2

Try putting the (?i) before the \b in your regex?

iago-lito commented 1 year ago

Indeed, archlinux has just upgraded to python 3.11.. and moving the (?i) before the \b does fix the problem. Thank you :) I'm closing this because I guess it's not exactly a bug in Ultisnips.

Thank you for this amazing piece of software btw <3