microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
150 stars 35 forks source link

Formatting extensions not working #420

Closed benjaminphalane closed 8 months ago

benjaminphalane commented 8 months ago

Type: Bug

Download Black Formatter extension, set it as the default formatter and select a .py file and dilberately mess with the formatting and attempt to use Black Formatter to fix formatting in the file. (It didn't work when I tried)

VS Code version: Code 1.85.2 (8b3775030ed1a69b13e4f4c628c612102e30a681, 2024-01-18T06:40:10.514Z) OS version: Windows_NT x64 10.0.22621 Modes://

System Info |Item|Value| |---|---| |CPUs|Intel(R) Celeron(R) N4020 CPU @ 1.10GHz (2 x 1094)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: disabled_off
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|undefined| |Memory (System)|3.82GB (0.20GB free)| |Process Argv|--crash-reporter-id e42f6d51-5a61-4ca9-bf98-69f920fa12cc| |Screen Reader|no| |VM|0%|
Extensions (5) Extension|Author (truncated)|Version ---|---|--- prettier-vscode|esb|10.1.0 black-formatter|ms-|2023.6.0 debugpy|ms-|2023.3.13341008 python|ms-|2023.22.1 vscode-pylance|ms-|2023.12.1
A/B Experiments ``` vsliv368:30146709 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscoreces:30445986 vscod805cf:30301675 binariesv615:30325510 vsaa593:30376534 py29gd2263:30899288 c4g48928:30535728 azure-dev_surveyone:30548225 a9j8j154:30646983 962ge761:30933248 pythongtdpath:30769146 welcomedialogc:30910334 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pyreplss1:30897532 pythonmypyd1:30879173 pythoncet0:30885854 pythontbext0:30879054 accentitlementsc:30887149 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 3ef8e399:30949928 ```
karthiknadig commented 8 months ago

@benjaminphalane How are you triggering formatting? You can also see Output > Black Formatter logs to see why it was not formatted.

benjaminphalane commented 8 months ago

I triggered formating by using both the "shift+alt+f "shortcut as well as the "format document with" option and nothing happened. I also checked the log according to it everything seems to be in order : Screenshot (257)

karthiknadig commented 8 months ago

The screen shot shows the error. According to Python parser there is syntax error because there is an indent on line 4 that is not valid in Python.

benjaminphalane commented 8 months ago

I created that error on purpose to test the formatting extension.

karthiknadig commented 8 months ago

You can try the following to see if formatting works: Before:

x=[1,1]

After:

x = [1, 1]

We block code from formatting if there are syntax errors. This is to preserve the current experience with notebooks. It will get better when we implement support to handle notebook cells with cell magic (like %%time).

benjaminphalane commented 8 months ago

It works, thanks. I was surprised because the tutorial I'm following on YouTube had this working like a charm.