Code Monkey home page Code Monkey logo

black.novaextension's Introduction

Black Nova Extension

It’s a stand-alone Nova extension to use Black, the uncompromising Python code formatter.

Requirements

Before using the extension, it’s necessary to install Black itself if you don’t have one already.

Black can be installed simply by running pip install black.

Configuration

The extension supports both global and workspace configurations. A workspace configuration always overrides a global one.

Options

There are three options available to configure: executable path, command arguments, and format on save. By default, the executable path is /usr/local/bin/black, with no additional arguments, and formatting on saving is on.

You could alter the executable path if Black installed in a different place or if /usr/bin/env usage is desirable.

In the case of /usr/bin/env, it becomes the executable path, and black becomes the first argument.

pyproject.toml

Also, the extension respects pyproject.toml in a project directory. So, there’s no need to specify the --config argument explicitly.

black.novaextension's People

Contributors

aeron avatar dcwatson avatar guillaumealgis avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

black.novaextension's Issues

Format on save causing Nova 6.2 to jump to start of file

In Nova 6.2, saving a Python file with "Format a file on save" checked causes the editor to jump to the top of the file once Black has done formatting.

When working with moderately long files, you then lose your place and need to scroll back down to find your place.

Outside of this, I love having this extension. Thanks so much for providing it!

Conflict execution when multiple format on save is triggered

I have both black and isort install, and both of them have "format on save" feature enabled.

Occasionally, when one of the format is triggered, says black, the modification will trigger isort formating, which will then back trigger black, and the loop goes on until I manually disable one of the extension.
Sometimes, this does not happen, but yield an incorrect code. (duplicate lines with wrong indentation or syntax error)

Maybe there should be a callback to coordinate the priority of the trigger. Although IMHO, this probably should be solved on Nova's side.

Plugin should only reformat Python files

Currently, the extension is only activated when a Python file is opened (which is fine) but once it is activated (and "reformat on save" is enabled) it will try and reformat every file that is saved (regardless of the file content).

This is okay-ish for most files because Black won't know what to do with it, but for (some?) JSON files Black will reformat the content on save, which is unexpected (I wasn't aware Black was able to reformat JSON files, that's probably a side-effect of JSON and Python dict sharing a similar syntax).

I think the extension should check the current buffer syntax (Python vs something else) before reformatting on save.

Broken since last update

Sorry for the vague title but I'm not sure how to get more info. I think it broke with the latest version (1.1.0). Nothing happens if I invoke formatting regardless of whether it's manually or when saving files. No visible error. Nothing has changed on my side and I can successfully run black from the command line by using the same binary path that I've provided in the extension's settings. Tried reinstalling the extension and restarting Nova without luck. Nova version has not changed since it was working. Let me know if I can provide any additional information.

Stubs files (.pyi) are not formatted correctly

Black uses different rules to format stubs (.pyi) files : https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#typing-stub-files

Because the extension is running black by passing the python code on stdin (instead of passing the file's path as argument), the file extension cannot be picked up by Black, and the format used is not the right one.

Black[15:38:15.277000] Running /Users/guillaume/foo/venv/bin/black --quiet -

For example :

Expected formatting
from concurrent.futures import Future
from typing import Any
from _typeshed import Incomplete
from requests import Session
def wrap(self, sup, background_callback, *args_, **kwargs_): ...
PICKLE_ERROR: str
class FuturesSession(Session):
executor: Incomplete
session: Incomplete
def __init__(
    self,
    executor: Incomplete | None = ...,
    max_workers: int = ...,
    session: Session | None = ...,
    adapter_kwargs: Incomplete | None = ...,
    *args,
    **kwargs,
) -> None: ...
def request(self, *args: Any, **kwargs: Any) -> Future:
    pass
def close(self) -> None: ...
def get(self, url, **kwargs): ...
def options(self, url, **kwargs): ...
def head(self, url, **kwargs): ...
def post(
    self,
    url,
    data: Incomplete | None = ...,
    json: Incomplete | None = ...,
    **kwargs,
): ...
def put(self, url, data: Incomplete | None = ..., **kwargs): ...
def patch(self, url, data: Incomplete | None = ..., **kwargs): ...
def delete(self, url, **kwargs): ...

  
Actual Black.novaextension formatting
from concurrent.futures import Future
from typing import Any
from _typeshed import Incomplete
from requests import Session
def wrap(self, sup, background_callback, *args_, **kwargs_):
...
PICKLE_ERROR: str
class FuturesSession(Session):
executor: Incomplete
session: Incomplete
def __init__(
    self,
    executor: Incomplete | None = ...,
    max_workers: int = ...,
    session: Session | None = ...,
    adapter_kwargs: Incomplete | None = ...,
    *args,
    **kwargs,
) -> None:
    ...

def request(self, *args: Any, **kwargs: Any) -> Future:
    pass

def close(self) -> None:
    ...

def get(self, url, **kwargs):
    ...

def options(self, url, **kwargs):
    ...

def head(self, url, **kwargs):
    ...

def post(
    self,
    url,
    data: Incomplete | None = ...,
    json: Incomplete | None = ...,
    **kwargs,
):
    ...

def put(self, url, data: Incomplete | None = ..., **kwargs):
    ...

def patch(self, url, data: Incomplete | None = ..., **kwargs):
    ...

def delete(self, url, **kwargs):
    ...

  

command to format entire workspace

I'd love a small command to run black on the entire workspace directory. I can submit a PR if you think it's something you'd consider adding. Thanks for the nice extension!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.