Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic config leads to python unexpected indent #348

Closed
damca opened this issue Jul 20, 2023 · 7 comments
Closed

basic config leads to python unexpected indent #348

damca opened this issue Jul 20, 2023 · 7 comments

Comments

@damca
Copy link

damca commented Jul 20, 2023

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
Copy link
Author

damca commented Jul 21, 2023

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

This comment was marked as outdated.

@RyanGreenup
Copy link

I have the same issue.

  • Setting ignore_blank_lines = false in any of the following doesn't change the behaviour:
    • init.lua
    • site/pack/packer/start/iron.nvim/lua/iron/core.lua

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
def split_into_tokens(html_text: str):
    tokens = []
    pattern = r"(?:<(h[1-6])>([^<]+)</\1>|([^<]+))"
    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
Copy link

gnsiva commented Jan 31, 2024

@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
Copy link

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 🤷 .

@nickeisenberg
Copy link
Contributor

nickeisenberg commented Apr 19, 2024

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants