quangnguyen30192 / cmp-nvim-ultisnips

nvim-cmp source for ultisnips
Apache License 2.0
144 stars 19 forks source link

Refactor: extract python code into separate file #52

Closed smjonas closed 3 months ago

smjonas commented 2 years ago

I really want to move Python related code into their own files, unfortunately this doesn't work yet at the moment. I get the following error:

Error detected while processing TextChangedI Autocommands for "*":
E5108: Error executing lua ...t/cmp-nvim-ultisnips/lua/cmp_nvim_ultisnips/snippets.lua:12: Vim(return):Err
or invoking 'python_execute' on channel 3 (python3-script-host):
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/jonas/.local/share/nvim/site/pack/packer/opt/cmp-nvim-ultisnips/python3/ultisnips_utils.py",
 line 22, in fetch_snippets
    vim.command(
  File "/usr/lib/python3/dist-packages/pynvim/api/nvim.py", line 287, in command
    return self.request('nvim_command', string, **kwargs)
  File "/usr/lib/python3/dist-packages/pynvim/api/nvim.py", line 182, in request
    res = self._session.request(name, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/pynvim/msgpack_rpc/session.py", line 104, in request
    raise self.error_wrapper(err)
pynvim.api.common.NvimError: Vim(return):Error invoking 'python_eval' on channel 3 (python3-script-host):
error caught in request handler 'python_eval ['str(snippet._trigger)']':
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pynvim/plugin/script_host.py", line 169, in python_eval
    return eval(expr, self.module.__dict__)
  File "<string>", line 1, in <module>
NameError: name 'snippet' is not defined
stack traceback:
        [C]: in function 'cmd'
        ...t/cmp-nvim-ultisnips/lua/cmp_nvim_ultisnips/snippets.lua:12: in function 'load_snippets'
        ...opt/cmp-nvim-ultisnips/lua/cmp_nvim_ultisnips/source.lua:22: in function 'complete'
        .../nvim/site/pack/packer/start/nvim-cmp/lua/cmp/source.lua:290: in function 'complete'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/core.lua:255: in function 'complete'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/core.lua:166: in function 'callback'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/core.lua:216: in function 'autoindent'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/core.lua:158: in function 'on_change'
        ...re/nvim/site/pack/packer/start/nvim-cmp/lua/cmp/init.lua:301: in function 'callback'
        ...ite/pack/packer/start/nvim-cmp/lua/cmp/utils/autocmd.lua:31: in function 'emit'
        [string ":lua"]:1: in main chunk

I did not change anything in the code but somehow the local variable snippet is not available within py3eval anymore. I created an issue on the pynvim repo here: https://github.com/neovim/pynvim/issues/501

@pianohacker Since you helped us last time with the pyxeval code and submitted a PR, do you have any idea what the problem might be here? Why does this code only work when used inside a vimscript function?

smjonas commented 8 months ago

Update: the aim of this PR was to separate the python code into separate files for easier debugging / LSP support. The presumed blocker of this PR has been resolved in the issue linked above:

x=1 here has a local scope, so there is no way for external expressions (py3eval) to look at the local variable. This is as designed and you should therefore use a global (or nonlocal) variable.

Anyone who wants to finish this PR is greatly encouraged to do so :)

smjonas commented 3 months ago

Superseded by #93.