Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer black over other formatter plugins #5

Merged
merged 1 commit into from
Jun 18, 2018

Conversation

auscompgeek
Copy link
Contributor

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

@rupert
Copy link
Owner

rupert commented Jun 18, 2018

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 rupert merged commit 9bff600 into rupert:master Jun 18, 2018
@rupert
Copy link
Owner

rupert commented Jun 18, 2018

@auscompgeek
Copy link
Contributor Author

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
Copy link
Owner

rupert commented Jun 19, 2018

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)

🤷‍♂️

@auscompgeek auscompgeek deleted the tryfirst branch June 19, 2018 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants