microsoft / vscode-isort

Import sorting for python using the isort library.
https://marketplace.visualstudio.com/items?itemName=ms-python.isort
MIT License
86 stars 20 forks source link

When formatting with black, the editor windows scrolls up if the "Format on save" is enable and import lines are too big #245

Closed sarmadgulzar closed 1 year ago

sarmadgulzar commented 1 year ago

Type: Bug

Steps to reproduce:

  1. Make sure the isort extension is installed
  2. Create a Python file which has 50+ lines of code
  3. Make sure to add a import statement at the top which has many imports e.g.
    from itertools import accumulate, chain, combinations, cycle, groupby, permutations, product
  4. Make sure the "Format on save" is enabled
  5. Make sure the default fomatter is black and it is installed
  6. When you save the file, the editor scrolls up automatically Here's a screencast: https://user-images.githubusercontent.com/26636177/220889556-2a65b05f-34b0-4926-8597-33272b7c3638.mov

I think it happens because when the file is saved, first isort tries to sort the imports and reformats even the long imports to a single line, and once it's done, black reformats the long import lines and the editor experiences this flickering behavior.

Extension version: 2022.8.0 VS Code version: Code 1.75.1 (441438abd1ac652551dbe4d408dfcec8a499b8bf, 2023-02-08T21:34:01.965Z) OS version: Darwin arm64 22.3.0 Modes: Sandboxed: Yes

System Info |Item|Value| |---|---| |CPUs|Apple M1 Pro (10 x 24)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
metal: disabled_off
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off| |Load (avg)|3, 3, 3| |Memory (System)|16.00GB (0.25GB free)| |Process Argv|--crash-reporter-id 53daa4f9-d978-448b-9540-86dc99dced0a| |Screen Reader|no| |VM|0%|
A/B Experiments ``` vsliv368cf:30146710 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 vswsl492:30256859 vslsvsres303:30308271 pythonvspyl392:30443607 vserr242cf:30382550 pythontb:30283811 vsjup518:30340749 pythonptprofiler:30281270 vshan820:30294714 vstes263:30335439 vscoreces:30445986 pythondataviewer:30285071 vscod805:30301674 binariesv615:30325510 bridge0708:30335490 bridge0723:30353136 cmake_vspar411:30581797 vsaa593cf:30376535 pythonvs932:30410667 cppdebug:30492333 vsclangdf:30486550 c4g48928:30535728 dsvsc012:30540252 azure-dev_surveyone:30548225 pyindex848:30662994 nodejswelcome1:30587005 282f8724:30602487 pyind779cf:30662993 f6dab269:30613381 pythonsymbol12cf:30657549 a9j8j154:30646983 vsctsb:30662444 ```
sarmadgulzar commented 1 year ago

Adding this code in the settings.json file seems to fix the problem:

"python.sortImports.args": [
    "--multi-line=3",
    "--trailing-comma",
    "--force-grid-wrap=0",
    "--use-parentheses",
    "--line-width=88",
]
karthiknadig commented 1 year ago

@sarmadgulzar When using black it is recommended by isort to use --profile black. This does not seem like an issue with the extension, there is no indication for isort on where to place the cursor after making the changes. As you discovered that configuring isort addresses this, that is the right thing to do in this scenario.