zed icon indicating copy to clipboard operation
zed copied to clipboard

formatting python code with custom argument isn't working

Open WindSoilder opened this issue 3 years ago • 7 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

I have the following configuration about python formatting:

{
    "buffer_font_size": 16,
    "buffer_font_family": "BerkeleyMono Nerd Font",
    "language_overrides": {
        "Python": {
            "format_on_save": "off",
            "formatter": {
                "external": {
                    "command": "black",
                    "arguments": [
                        "--line-length",
                        "110"
                    ]
                }
            }
        }
    }
}

And given following python code:

# a.py
def a(b, c, d, e):
    pass
    
a("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", "cccccccccccccccccccccccccc", "dddddddddddddddddddddddd", "eeeeeeeeeeeeeeeeeeeeee")

Trying to format python code, nothing is happened.

But the following configuration is ok:

    "language_overrides": {
        "Python": {
            "format_on_save": "off",
            "formatter": {
                "external": {
                    "command": "black",
                    "arguments": [
                        "-"
                    ]
                }
            }
        }
    }

Expected behavior

I'd expect python code is formatted.

Environment

Zed 0.62.8 – /Applications/Zed.app \nmacOS 13.0 \narchitecture x86_64

If applicable, add mockups / screenshots to help explain present your vision of the feature

No response

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue

Here is relative log about formatting:

08:39:58 [ERROR] failed to format via external command "black"

Caused by:
    command failed with exit code Some(1):
    stdout:
    stderr: Usage: black [OPTIONS] SRC ...

    One of 'SRC' or 'code' is required.

WindSoilder avatar Nov 10 '22 08:11 WindSoilder

Just curious, is Black installed in your virtual environment, or globally? I feel like we have a current restriction that only allows the to work if black is installed globally.

JosephTLyons avatar Nov 14 '22 20:11 JosephTLyons

Black is installed globally

WindSoilder avatar Nov 15 '22 07:11 WindSoilder

This works for me fyi,

{
    "language_overrides": {
        "Python": {
            "format_on_save": "on",
            "formatter": {
                "external": {
                    "command": "black",
                    "arguments": [
                        "-"
                    ]
                }
            }
        }
    }
}

Liam-Breen avatar Dec 20 '22 23:12 Liam-Breen

Hi, can't make black formatter work with language_overrides setting, but everything works fine if I add a Python section under languages:

"languages": {
    "Plain Text": {
        "soft_wrap": "preferred_line_length"
    },
    "C": {
        "tab_size": 2
    },
    "C++": {
        "tab_size": 2
    },
    "Elixir": {
        "tab_size": 2
    },
    "Go": {
        "tab_size": 4,
        "hard_tabs": true
    },
    "Markdown": {
        "soft_wrap": "preferred_line_length"
    },
    "Rust": {
        "tab_size": 4
    },
    "JavaScript": {
        "tab_size": 2
    },
    "TypeScript": {
        "tab_size": 2
    },
    "TSX": {
        "tab_size": 2
    },
    "Yaml": {
        "tab_size": 2
    },
    "Python": {
        "format_on_save": {
            "external": {
                "command": "black",
                "arguments": [
                    "-"
                ]
            }
        }
    }

ludoviconemus avatar Feb 18 '23 17:02 ludoviconemus

Hi, can't make black formatter work with language_overrides setting, but everything works fine if I add a Python section under languages:

"languages": {
    "Plain Text": {
        "soft_wrap": "preferred_line_length"
    },
    "C": {
        "tab_size": 2
    },
    "C++": {
        "tab_size": 2
    },
    "Elixir": {
        "tab_size": 2
    },
    "Go": {
        "tab_size": 4,
        "hard_tabs": true
    },
    "Markdown": {
        "soft_wrap": "preferred_line_length"
    },
    "Rust": {
        "tab_size": 4
    },
    "JavaScript": {
        "tab_size": 2
    },
    "TypeScript": {
        "tab_size": 2
    },
    "TSX": {
        "tab_size": 2
    },
    "Yaml": {
        "tab_size": 2
    },
    "Python": {
        "format_on_save": {
            "external": {
                "command": "black",
                "arguments": [
                    "-"
                ]
            }
        }
    }

This scheme worked for me!

yeruoforever avatar Jul 06 '23 03:07 yeruoforever

Hmm... formatting used to work, but for whatever reason it stopped. Maybe I am missing something like python association?

I tried both blocks of code:

  "language_overrides": {
    "Python": {
      "format_on_save": "on",
      "formatter": {
        "external": {
          "command": "black",
          "arguments": [
            "--line-length",
            "89",
            "-"
          ]
        }
      }
    }
    // "Lua": {
    //     "format_on_save": "off"
    // }
  },

or this one which used to work originally:

 "language_overrides": {
    "Python": {
      "format_on_save": "on",
      "formatter": {
        "external": {
          "command": "black",
          "arguments": [
            "--line-length",
            "89",
            "-"
          ]
        }
      }
    }

I am 99% positive it is something with python association I have with ZED, just not sure now how to fix it 🤔

Also note log does not show any error related to black or python itself, so ¯_(ツ)_/¯

Also when launching terminal within ZED I can execute the manual black command and it works fine, so this is not the env setup.

krstp avatar Jul 28 '23 13:07 krstp

Using languages instead of language_overrides worked for me too, I'm not sure why but it sounds like a bug.

BafS avatar Feb 04 '24 00:02 BafS

It works fine on newest version with following config, I think the issue is resolved

  "language_overrides": {
    "Python": {
      "format_on_save": "off",
      "formatter": {
        "external": {
          "command": "black",
          "arguments": ["--line-length", "110", "-"]
        }
      }
    }
  }

WindSoilder avatar May 09 '24 02:05 WindSoilder