jalvesaq / vimcmdline

Send code to command line interpreter
GNU General Public License v2.0
223 stars 38 forks source link

Sending multiple lines freezes IPython with "%cpaste -q" #77

Open DaanJG opened 5 years ago

DaanJG commented 5 years ago

When I send multiple lines of Python (e.g. a paragraph or function) to IPython running in a Neovim terminal buffer, "%cpaste -q" appears at the console and nothing happens. If I then switch to the console and hit CTRL-C I get a long traceback related to the keyboard interrupt and then one by one the sent lines are evaluated at the normal IPython prompt (not the one expected to be invoked by %cpaste) and also "--" is evaluated raising an error.

This happens most of the time but not always. The expected behaviour also occurs.

How can I fix this?

I'm using NVIM v0.2.2, Python 3.6.8 and IPython 7.8.0 all running in tmux in Windows Subsystem for Linux with Ubuntu 18.04.

jalvesaq commented 5 years ago

It seems that the plugin is not compatible with IPython.

sheriferson commented 5 years ago

For what it's worth, I've been using vimcmdline with IPython for a while with almost no issues.

It works most of the time, but every once in a while I'll try to send a highlighted function to IPython and get an "invalid syntax" error from code that I know does not have syntax issues.

The way I know there isn't an actual syntax issue is that running %run myfile.py and using the functions defined within the file works just fine. So it seems like sometimes highlighting a function and sending it to the REPL doesn't escape or indent everything properly.

DaanJG commented 5 years ago

For what it's worth, I've been using vimcmdline with IPython for a while with almost no issues.

It works most of the time, but every once in a while I'll try to send a highlighted function to IPython and get an "invalid syntax" error from code that I know does not have syntax issues.

The way I know there isn't an actual syntax issue is that running %run myfile.py and using the functions defined within the file works just fine. So it seems like sometimes highlighting a function and sending it to the REPL doesn't escape or indent everything properly.

Hi sherifson, maybe try toggling %autoindent in IPython.

Can you give some details as to what you are using exactly? Vim or Neovim? Terminal buffer or Tmux pane? Which OS?

DaanJG commented 5 years ago

It seems that the plugin is not compatible with IPython.

jalvesaq, do you have any ideas about what is causing my issue? What would be a work around?

I have noticed that typing manually at the IPython prompt seems to introduce the problem. Before I do that sending multiple lines works, after that, no.

jalvesaq commented 5 years ago

I'm sorry, but I don't know. I'm not the author of the code that tries to fix the issues with IPython. I use the plugins jedi-vim and ncm2 to help me writing Python code.

sheriferson commented 5 years ago

@DaanJG Thanks for the tip, unfortunately it didn't work. I still get invalid syntax error when sending some visual blocks to the REPL.

Using Neovim, no Tmux, just a Python file and vimcmdline. Tested in iPython 7.8 and 7.9. Python 3.7.4. I use Shift + V to highlight a function block, then send the function to the REPL.

Looking at the code sent to the REPL, the issue doesn't look like it's indentation, but that the code gets mangled and repeated, causing errors.

For example, one line in the function:

            logger.info('Ending addition - reached limit {}'.format(limit))

appears to be sent as

            logger.info('            logger.info('            logger.info('            logger.info('            eid)

which unsurprisingly causes the syntax error:

  File "<ipython-input-8-03e147747bfe>", line 32
    logger.info('            logger.info('            logger.info('            logger.info('            eid)
                                                           ^
SyntaxError: invalid syntax
sheriferson commented 5 years ago

This issue actually seems separate from the interpreter and might warrant its own GitHub issue. It still happens with the same code even with

let cmdline_app['python'] = 'ptipython'

and also happens if I remove the configuration entirely and use the default Python REPL.

DaanJG commented 5 years ago

I think I have found a satisfying workaround for my issue. I now run IPython in a Tmux pane in stead of the default, which is in a Neovim terminal buffer, by having

let cmdline_in_buffer = 0

in my configuration file (~/.config/nvim/init.vim on my system). Works great so far!

@sheriferson, maybe this works for you? If you don't use Tmux you can open IPython in another terminal window I think.

sheriferson commented 5 years ago

I'm not sure it can work without Tmux. I tried setting cmdline_in_buffer = 0 and I get "Cannot start interpreter because not inside a Tmux session"

I'll try to experiment with settings (e.g., let cmdline_external_term_cmd = "gnome-terminal -e '%s'") to see if I can make it happen without Tmux. I can't see any documentation that indicates whether that's possible.

@jalvesaq Is it possible to not use the Neovim terminal and not use Tmux either? Just have a REPL in a different terminal window?

jalvesaq commented 5 years ago

Tmux is required to run a REPL in another terminal. I will put this information in the README.

Soulthym commented 5 years ago

I don't know if any of that code is portable, but I find this implementation of ipython through nvim quite handy. I use this in my vimrc:

Plugin 'bfredl/nvim-ipy'
"Doc : https://github.com/bfredl/nvim-ipy
let g:nvim_ipy_perform_mappings = 0
let g:ipy_celldef = '^##'
"
autocmd Filetype python map <silent> <F3> <Esc>:IPython<CR>
autocmd Filetype python map <silent> <F4> <Plug>(IPy-Run)
autocmd Filetype python map <silent> <F5> <Plug>(IPy-RunCell)
autocmd Filetype python map <silent> <F6> <Plug>(IPy-RunAll)
autocmd Filetype python imap <silent> <c-n> <Plug>(IPy-Complete)
autocmd Filetype python map <silent> <F2> <Plug>(IPy-WordObjInfo)
autocmd Filetype python map <silent> <F9> <Plug>(IPy-Interrupt)
autocmd Filetype python map <silent> <F10> <Plug>(IPy-Terminate)