hanschen / vim-ipython-cell

Seamlessly run Python code in IPython from Vim
GNU General Public License v3.0
331 stars 18 forks source link

Mangled Indentation with `IPythonCellExecuteCellVerboseJump` #37

Closed Edenhofer closed 2 years ago

Edenhofer commented 2 years ago

The following snippet produces a syntax error even though the code is valid. This is to say, neither ipython nor python complain about the syntax if run as is. However, if I let vim-ipython paste the snippet into an interactive ipython session using IPythonCellExecuteCellVerboseJump, it fails to execute the cell correctly and instead prints SyntaxError: 'return' outside function for the last return. Unfortunately, I was unable to strip down the reproducing example further. If I remove any line in the cell, it starts to work again. Likewise, the error does not reproduce if I use plain cpaste in ipython.

 # %%
def timeit(stmt, setup=lambda: None, number=None):
    import timeit

    setup()
    t = timeit.timeit(stmt, number=number) / number
    return Timed(time=t, number=number)

def _matern_kernel(distance, scale, cutoff, dof):
    from jax.scipy.special import gammaln

    reg_dist = jnp.sqrt(2 * dof) * distance / cutoff
    return scale**2 * 2**(1 - dof) / jnp.exp(
        gammaln(dof)
    ) * (reg_dist)**dof * mod_bessel2(dof, reg_dist)

def matern_kernel(distance, scale, cutoff, dof):
    from jax.scipy.special import gammaln

    reg_dist = jnp.sqrt(2 * dof) * distance / cutoff
    dof, reg_dist = jnp.broadcast_arrays(dof, reg_dist)

    # Never produce NaNs (https://github.com/google/jax/issues/1052)
    corr = 2**(1 - dof)
    return scale**2 * corr

scale, cutoff, dof = 1., 80., 3 / 2
# %%

My slime config is as follows:

" This line should not be removed as it ensures that various options are
" properly set to work with the Vim-related packages.
runtime! archlinux.vim
" Move around code blocks in e.g. Julia
runtime macros/matchit.vim

call plug#begin('~/.vim/plugged')
Plug 'jpalardy/vim-slime', { 'branch': 'main', 'for': 'python' }
Plug 'hanschen/vim-ipython-cell', { 'for': 'python' }
call plug#end()

let g:slime_target = "tmux"
if exists('$TMUX')
    " By default the last active pane is chosen within the window
    let g:slime_default_config = {"socket_name": get(split($TMUX, ','), 0), "target_pane": "repl-0:"}
else
    let g:slime_default_config = {"socket_name": "default", "target_pane": "repl-0:"}
endif
let g:slime_dont_ask_default = 1
let g:slime_paste_file = "$HOME/.cache/slime_paste"
let g:slime_no_mappings = 1
hanschen commented 2 years ago

Can you try to add

let g:slime_python_ipython = 1

to your config and try again? If it works, I'll add it to the README. See this page for more information: https://github.com/jpalardy/vim-slime/blob/a522fed677e50175f52efc5848cc35209af33216/ftplugin/python/README.md

Edenhofer commented 2 years ago

With g:slime_python_ipython = 1 the above snippet works. However, I can't paste blocks which have a global indentation then. Plain old %cpaste still works though.

EDIT: Unfortunately, this again does not reproduce on a simple example. The above code with everything intended however consistently errors out for me.

hanschen commented 2 years ago

I've added a workaround for the problem with global indentations, can you update the plugin and let me know if it works as expected now?

Edenhofer commented 2 years ago

The new line trick seems to work like a charm! Many thanks :)

hanschen commented 2 years ago

Awesome! :)