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

isort range formatting deletes lines #123

Closed
stevearc opened this issue Oct 9, 2023 · 1 comment
Closed

isort range formatting deletes lines #123

stevearc opened this issue Oct 9, 2023 · 1 comment

Comments

@stevearc
Copy link
Owner

stevearc commented Oct 9, 2023

          I'm having an issue with lines being removed due to `isort`, probably related.

Test file:

import numpy as np

from A import B
import C

Output if I use conform.format() (works as expected):

import C
import numpy as np
from A import B

Output if I use conform.format({range = {start={1, 0}, ["end"]={4,8}}}) (whole file as range):

import C

I tracked it down to apply_format, in particular indices_in_range here is evaluating to false, so it does the deletion but not the insertion, shown in the lack of a second text edit for the call with a range. If I bypass that line (just saying if true for this test), it works exactly as expected.

trace for no range (conform.format()):

15:06:37[DEBUG] Running formatters on ~/test.py: { "isort" }
15:06:37[INFO] Run isort on ~/test.py
15:06:37[TRACE] Input lines: { "import numpy as np", "", "from A import B", "import C" }
15:06:37[DEBUG] Run command: { "isort", "--stdout", "--filename", "~/test.py", "-" }
15:06:38[DEBUG] isort exited with code 0
15:06:38[TRACE] Output lines: { "import C", "import numpy as np", "from A import B" }
15:06:38[TRACE] Applying formatting to ~/test.py
15:06:38[TRACE] Comparing lines { "import numpy as np", "", "from A import B", "import C" } and { "import C", "import numpy as np", "from A import B" }
15:06:38[TRACE] Diff indices { { 1, 3, 0, 0 }, { 4, 0, 2, 2 } }
15:06:38[TRACE] Applying text edits: { {
    newText = "",
    range = {
      ["end"] = {
        character = 0,
        line = 3
      },
      start = {
        character = 0,
        line = 0
      }
    }
  }, {
    newText = "import numpy as np\nfrom A import B",
    range = {
      ["end"] = {
        character = 0,
        line = 4
      },
      start = {
        character = 0,
        line = 4
      }
    }
  } }
15:06:38[TRACE] Done formatting ~/test.py

trace with a range (conform.format({range = {start={1, 0}, ["end"]={4,8}}})):

15:06:33[DEBUG] Running formatters on ~/test.py: { "isort" }
15:06:33[INFO] Run isort on ~/test.py
15:06:33[TRACE] Input lines: { "import numpy as np", "", "from A import B", "import C" }
15:06:33[DEBUG] Run command: { "isort", "--stdout", "--filename", "~/test.py", "-" }
15:06:33[DEBUG] isort exited with code 0
15:06:33[TRACE] Output lines: { "import C", "import numpy as np", "from A import B" }
15:06:33[TRACE] Applying formatting to ~/test.py
15:06:33[TRACE] Comparing lines { "import numpy as np", "", "from A import B", "import C" } and { "import C", "import numpy as np", "from A import B" }
15:06:33[TRACE] Diff indices { { 1, 3, 0, 0 }, { 4, 0, 2, 2 } }
15:06:33[TRACE] Applying text edits: { {
    newText = "",
    range = {
      ["end"] = {
        character = 0,
        line = 3
      },
      start = {
        character = 0,
        line = 0
      }
    }
  } }
15:06:33[TRACE] Done formatting ~/test.py

Originally posted by @milisims in #98 (comment)

@stevearc
Copy link
Owner Author

stevearc commented Oct 9, 2023

This is likely not related to the other issue, so I've moved it to a new issue.

I added some docs under advanced topics that explain the range formatting a little more. As a general rule, the "aftermarket" range formatting that conform offers when a formatter doesn't support it natively is a "best effort" operation and will never be perfect. In this particular case I was able to find a small change that improves the formatting results, but just as a warning this sort of thing can still happen in the future.

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

1 participant