rupert / pyls-black

Black plugin for the Python Language Server
MIT License
72 stars 8 forks source link

Prefer black over other formatter plugins #5

Closed auscompgeek closed 6 years ago

auscompgeek commented 6 years ago

This adds the tryfirst=True hookimpl argument, similarly to the builtin autopep8 plugin to (try to) ensure black is attempted before other formatters.

rupert commented 6 years ago

Thanks for the PR! I did a bit of research into tryfirst and this looks like a good approach.

Definition of hooks in python-language-server: https://github.com/palantir/python-language-server/blob/d294fe14b632ca14f333552b572650dda8327a2a/pyls/hookspecs.py#L70 https://github.com/palantir/python-language-server/blob/d294fe14b632ca14f333552b572650dda8327a2a/pyls/hookspecs.py#L75

Documentation of firstresult flag: https://github.com/pytest-dev/pluggy/blob/76232fa360f76992c55f04165bb5ff3119b98eff/pluggy/hooks.py#L20

Documentation of tryfirst flag: https://github.com/pytest-dev/pluggy/blob/76232fa360f76992c55f04165bb5ff3119b98eff/pluggy/hooks.py#L58

>>> import logging
>>> logging.basicConfig(level=logging.INFO)
>>> from pyls.config.config import Config
>>> Config('.', {})
WARNING:pyls.config.config:Failed to load pyls entry point 'autopep8': No module named 'autopep8'
WARNING:pyls.config.config:Failed to load pyls entry point 'mccabe': No module named 'mccabe'
WARNING:pyls.config.config:Failed to load pyls entry point 'pycodestyle': No module named 'pycodestyle'
WARNING:pyls.config.config:Failed to load pyls entry point 'pydocstyle': No module named 'pydocstyle'
WARNING:pyls.config.config:Failed to load pyls entry point 'pyflakes': No module named 'pyflakes'
WARNING:pyls.config.config:Failed to load pyls entry point 'rope_completion': No module named 'rope'
WARNING:pyls.config.config:Failed to load pyls entry point 'rope_rename': No module named 'rope'
WARNING:pyls.config.config:Failed to load pyls entry point 'yapf': No module named 'yapf'
INFO:pyls.config.config:Loaded pyls plugin jedi_completion from <module 'pyls.plugins.jedi_completion' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls/plugins/jedi_completion.py'>
INFO:pyls.config.config:Loaded pyls plugin jedi_definition from <module 'pyls.plugins.definition' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls/plugins/definition.py'>
INFO:pyls.config.config:Loaded pyls plugin jedi_highlight from <module 'pyls.plugins.highlight' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls/plugins/highlight.py'>
INFO:pyls.config.config:Loaded pyls plugin jedi_hover from <module 'pyls.plugins.hover' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls/plugins/hover.py'>
INFO:pyls.config.config:Loaded pyls plugin jedi_references from <module 'pyls.plugins.references' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls/plugins/references.py'>
INFO:pyls.config.config:Loaded pyls plugin jedi_signature_help from <module 'pyls.plugins.signature' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls/plugins/signature.py'>
INFO:pyls.config.config:Loaded pyls plugin jedi_symbols from <module 'pyls.plugins.symbols' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls/plugins/symbols.py'>
INFO:pyls.config.config:Loaded pyls plugin pyls_mypy from <module 'pyls_mypy.plugin' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls_mypy/plugin.py'>
INFO:pyls.config.config:Loaded pyls plugin pyls_isort from <module 'pyls_isort.plugin' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls_isort/plugin.py'>
INFO:pyls.config.config:Loaded pyls plugin pyls_black from <module 'pyls_black.plugin' from '/Users/rupert/.virtualenvs/pyls/lib/python3.6/site-packages/pyls_black/plugin.py'>
INFO:pyls.config.config:Disabled plugins: []
<pyls.config.config.Config object at 0x10e9835f8>

Since autopep8 is alphabetically before pyls_black, I think autopep8 will take priority if they are both installed (as you mentioned autopep8 also sets the tryfirst flag).

rupert commented 6 years ago

@auscompgeek released in https://github.com/rupert/pyls-black/releases/tag/v0.2.1

auscompgeek commented 6 years ago

That log looks like pyls_black gets loaded last (which may be completely coincidental). If I'm understanding the documentation correctly, that means the hooks that are created here would come first before the others.

Anyway, thanks for merging!

rupert commented 6 years ago

which may be completely coincidental

Yes that seems to be the case. I hadn't noticed that the plugins weren't quite alphabetically sorted (from pyls_mypy onwards in the logs).

Entry points are yielded from the active distributions in the order that the distributions appear on sys.path. (Within entry points for a particular distribution, however, there is no particular ordering.)

https://setuptools.readthedocs.io/en/latest/pkg_resources.html#convenience-api (see iter_entry_points)

🤷‍♂️