Vigemus / iron.nvim

Interactive Repl Over Neovim
BSD 3-Clause "New" or "Revised" License
978 stars 81 forks source link

basic config leads to python unexpected indent #348

Closed damca closed 4 months ago

damca commented 1 year ago

Behavior

Sending the following with visual send command (<space>sc):

def test():
    print(1)

def test2():
    print(2)

image

Leads to the following error:

Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def test():
...     print(1)
... def test2():
  File "<stdin>", line 3
    def test2():
    ^^^
SyntaxError: invalid syntax
>>>     print(2)
  File "<stdin>", line 1
    print(2)
IndentationError: unexpected indent
>>> 
>>> 

This error persists even if ignore_blank_lines = false

Setup

Using NVIM v0.9.1

I placed the iron directory in the runtime path ~/.config/nvim/lua, for simple testing.

nvim -u NORC

:lua require('mytest')

-- ~/.config/nvim/lua/mytest.lua

require("iron.core").setup {
  config = {
    -- Whether a repl should be discarded or not
    scratch_repl = true,
    -- Your repl definitions come here
    repl_definition = {
      sh = {
        -- Can be a table or a function that
        -- returns a table (see below)
        command = { "zsh" },
      },
    },
    -- How the repl window will be displayed
    -- See below for more information
    repl_open_cmd = require("iron.view").split.vertical('50%')
  },
  -- Iron doesn't set keymaps by default anymore.
  -- You can set them here or manually add keymaps to the functions in iron.core
  keymaps = {
    send_motion = "<space>sc",
    visual_send = "<space>sc",
    send_file = "<space>sf",
    send_line = "<space>sl",
    send_until_cursor = "<space>su",
    send_mark = "<space>sm",
    mark_motion = "<space>mc",
    mark_visual = "<space>mc",
    remove_mark = "<space>md",
    cr = "<space>s<cr>",
    interrupt = "<space>s<space>",
    exit = "<space>sq",
    clear = "<space>cl",
  },
  -- If the highlight is on, you can change how it looks
  -- For the available options, check nvim_set_hl
  highlight = {
    italic = true,
  },
  ignore_blank_lines = true, -- ignore blank lines when sending visual select lines
}
damca commented 1 year ago

Update

A few things. First, it seems the ignore_blank_lines from the config is being ignored during setup, if I set the default to be 'false' in the source core.lua, then the blank lines are included, and the pasting into the python repl works since the new lines are interpreted literally.

More importantly, things work fine with ipython.

I did some reading, and it seems that there is a lot of discussion about whether the default python repl should include "bracketed paste" as a feature:

I don't know exactly what to make of all this. But perhaps the specific version of python (either built or downloaded) may or may not include bracketed paste.

If there is a simple solution I am not seeing please let me know.

amsesk commented 1 year ago

@damca I was having this same issue. By using some of your links to bug reports as jumping off points, I was able to find this question (question is about Mac, but I'm on linux) which implicates newer bash readline versions with having bracketed paste on by default.

I was able to fix the issue in my Iron REPL (python v3.10.12) by adding set enable-bracketed-paste off to my .inputrc and restarting the REPL. I hope this helps fix your problem!

RyanGreenup commented 7 months ago

I have the same issue.

For example this code:


def my_func():
    matches = [1, 2, 3]
    for match in matches:
        print(match)

Gives the following output:

In [1]:

In [1]:

In [1]: def my_func():
   ...:         matches = [1, 2, 3]
   ...:             for match in matches:
   ...:                         print(match)
  Cell In[1], line 3
    for match in matches:
    ^
IndentationError: unexpected indent

In [2]:

On my other laptop (Void Linux), this works with iPython not Python, on this machine (SUSE Aeon) neither works.

The following is an example that doesn't work on either machine regardless of ipython:

Click Me ```python def split_into_tokens(html_text: str): tokens = [] pattern = r"(?:<(h[1-6])>([^<]+)|([^<]+))" matches = re.findall(pattern, html_text) current_token = {} for match in matches: heading_level = match[0] heading_text = match[1].strip() other_text = match[2].strip() if heading_level: if current_token: tokens.append(current_token) current_token = {heading_level: heading_text, "p": ""} elif other_text: if current_token.get("p"): current_token["p"] += "\n" current_token["p"] += other_text if current_token: tokens.append(current_token) return tokens ```

I haven't been able to figure this out, for the moment, I'm using Fterm and Tmux / Zellij.

gnsiva commented 7 months ago

@RyanGreenup I had the same issue, got the answer from this post

So the config starts with this. The key difference being the format = require("iron.fts.common").bracketed_paste. That groups together the whole selected block and pastes it in in one go instead of line by line.

iron.setup {
  config = {
    ignore_blank_lines = true, -- ignore blank lines when sending visual select lines
    -- Whether a repl should be discarded or not
    scratch_repl = true,
    -- Your repl definitions come here
    repl_definition = {
      python = {
        command = "ipython",
        format = require("iron.fts.common").bracketed_paste,
      }
    },
    -- How the repl window will be displayed
    -- See below for more information
    repl_open_cmd = require('iron.view').bottom(40),
  },
RyanGreenup commented 7 months ago

I tried slime in the interim but I was having a similar issue.

I tried the solution by @gnsiva and that seems to have solved it, although when I comment it back out the problem is entirely solved so :shrug: .

nickeisenberg commented 4 months ago

hey @damca and @RyanGreenup , I believe this issue is fixed now. Could you try updating the plugin on your end to use the latest commit to master. I was having the same issue and the last PR for this plugin addressed it. This problem no longer exists on my end.

nickeisenberg commented 4 months ago

I believe this is solved now, if this problem continues, please make another issue with an example that causes the same error.