microsoft / vscode-autopep8

Formatting support for python using autopep8.
https://marketplace.visualstudio.com/items?itemName=ms-python.autopep8
MIT License
20 stars 13 forks source link

Unable to format with Keyboard Shortcut #151

Closed frankli0324 closed 1 year ago

frankli0324 commented 1 year ago

When I format a file with Cmd K + Cmd F, the following message appears:

Extension 'autopep8' is configured as formatter but it cannot format 'Python'-files

While using Format With command using Cmd+Shift+P, the file is successfully formatted.

python | ms- | 2023.14.0
vscode-pylance | ms- | 2023.8.50
autopep8 | ms- | 2023.6.0
karthiknadig commented 1 year ago

@frankli0324 This can happen if you have selected some text and running formatting. Please unselect, and run the command. Autopep8 extension doesn't yet support formatting on range.

frankli0324 commented 1 year ago

This can happen if you have selected some text and running formatting.

The same happens without selecting any text 20230906234622_rec_

frankli0324 commented 1 year ago

I'm surprised that this isn't found in testing stage, even a simple self-test would trigger this obvious bug if I remember correctly, the old python extension did support partial formatting, why is a extension "split" doesn't cover all previous features? also why is vscode pushing users to switch to unfinished extensions?

karthiknadig commented 1 year ago

@frankli0324 I am not able to repro this locally. Can you provide your settings? This looks like an issue with VS Code itself and not this extension specifically. That command is contributed by VS Code and it picks up the formatter based on settings. So please share both your user and workspace settings.

if I remember correctly, the old python extension did support partial formatting

The old extension does not support partial formatting. What it did was format the entire file, even when partial formatting was requested.

why is a extension "split" doesn't cover all previous features? why is vscode pushing users to switch to unfinished extensions?

As I mentioned. The old formatter did not support it, it formatted the whole file. I agree that the missing selection formatting needs to be handled better.

frankli0324 commented 1 year ago

my workspace settings used for the screenshot is generated by the ms-python extension

{
    "[python]": {
        "editor.defaultFormatter": "ms-python.autopep8"
    },
    "python.formatting.provider": "none"
}

while my python related user settings are

"[python]": {
        "editor.defaultFormatter": "ms-python.python",
        "editor.formatOnType": true,        
    },
    "python.languageServer": "Pylance",
    "python.defaultInterpreterPath": "/opt/homebrew/bin/python3",
    "python.venvFolders": [
        "envs",
        ".pyenv",
        ".direnv",
        ".env"
    ],
    "pylint.args": [
        "--disable=all",
        "--disable=E1111",
        "--enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode",
        "--max-line-length=90"
    ],
    "autopep8.args": [
        "--ignore=E731",
        "--max-line-length=95"
    ],
frankli0324 commented 1 year ago

I seem to understood what's happening here... this extension declared hasDocumentSelectionFormattingProvider but not implemented it yet right? so when I use cmd+k cmd+f, which is bound to editor.action.formatSelection triggered at editorHasDocumentSelectionFormattingProvider && editorTextFocus && !editorReadonly (which is the default configuration), it actually tries to format with selection formatter

this image is to show that I didn't override the cmd+k cmd+f settings image

frankli0324 commented 1 year ago

if I rebind cmd+k cmd+f to editor.action.formatDocument (and, of course, change the when expression), it's working as expected (formatting the entire file).