astral-sh / ruff-vscode

A Visual Studio Code extension with support for the Ruff linter.
Other
946 stars 45 forks source link

ruff formatter cant fix this unwanted indentation? #455

Closed githubfanster closed 2 months ago

githubfanster commented 2 months ago

Hi!

i'm following this freecodecamp tutorial. right before the end of Chapter 2 (https://youtu.be/qwAFL1597eM?t=1462), the instructor was demo'ing auto format fixes on save using the simple example below:

line01 = "********************"
line02 = "*                  *"
line03 = "*     WELCOME!     *"
    line04 = "*****"

print("")
print(line01)
print(line02)
print(line03)
print(line02)
print(line01)

at the time the video was made. vs code fixed the unwanted indentation of line04.

i was trying to find out if the ruff extension for vs code would fix the unwanted indentation problem, but it wasnt fixing it.

ruff fixes the indentation problem in the if ... else statement below:

line01 = "********************"
line02 = "*                  *"
line03 = "*     WELCOME!     *"
line04 = "*****"

print("")
print(line01)
print(line02)
print(line03)
print(line02)
print(line01)

if True: print("true")
else: print("false")

but not if the code included the unwanted indentation of line04:

line01 = "********************"
line02 = "*                  *"
line03 = "*     WELCOME!     *"
    line04 = "*****"

print("")
print(line01)
print(line02)
print(line03)
print(line02)
print(line01)

if True: print("true")
else: print("false")

My OS: Windows 11 Pro Python: version 3.12.3 My VS Code info: Version: 1.88.1 (user setup) Commit: e170252f762678dec6ca2cc69aba1570769a5d39 Date: 2024-04-10T17:41:02.734Z Electron: 28.2.8 ElectronBuildId: 27744544 Chromium: 120.0.6099.291 Node.js: 18.18.2 V8: 12.0.267.19-electron.0 OS: Windows_NT x64 10.0.22631

settings.json:

{
    "[python]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.fixAll": "explicit",
            "source.organizeImports": "explicit"
        },
        "editor.defaultFormatter": "charliermarsh.ruff",
    },
    "editor.fontWeight": "normal",
}

i didnt think i had to do a pip install ruff into my venv (a folder inside lesson02 folder), but i did anyways:

(.venv) E:\pythonproj-vscode\lesson02>pip install ruff
Collecting ruff
  Downloading ruff-0.4.1-py3-none-win_amd64.whl.metadata (24 kB)
Downloading ruff-0.4.1-py3-none-win_amd64.whl (8.5 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.5/8.5 MB 1.5 MB/s eta 0:00:00
Installing collected packages: ruff
Successfully installed ruff-0.4.1

my question is: do i need to do anything else so ruff can fix the unwanted indentation of line04 in the above code snippets?

(also, am i correct in assuming i dont have to additionally pip install ruff into my venv and that the ruff vs code extension would work by itself?)

thanks for any help! and sorry i dont know much about these code formatting tools :-)

MichaReiser commented 2 months ago

Hi

No, Ruff's formatter currently does not format code that contains syntax errors (programs that are not valid Python code). We may want to support this in the future, or at least if there are only specific errors in the source, but it's not something we have the infrastructure for.

You can use the default python formatter (by pylance) if you want this functionality. The video that you linked explains how to configure that formatter.