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

Add pylsp as the python LSP #857

Merged
merged 17 commits into from
Mar 3, 2024
Merged

Add pylsp as the python LSP #857

merged 17 commits into from
Mar 3, 2024

Conversation

janvhs
Copy link
Contributor

@janvhs janvhs commented Dec 26, 2023

This PR, lays the foundation to introduce an LSP for the python language.
Pylsp is packaged as a flatpak module and included in the build tree for the Devel version of Workbench.

Currently, pylsp responds to file input changing, but doesn't publish any diagnostics and I can not figure out why.
It seems to support the same capabilities as the JS client, yet only responds with empty diagnostics on the notification::textDocument/publishDiagnostics event.

Another thing, that raised my attention, is the usage of booth python and python3, when referencing to the language ID throughout the codebase. I don't think it is part of the problem, tho.

Can someone take a look at this and help to figure out, why it does not work?

janvhs and others added 15 commits December 17, 2023 21:09
Generated the module via `flatpak-pip-generator` on my host with python 3.12.
When building with this module, it complains about the package `maturin`
missing. I suspect, that it is missing due to the difference in python versions.
It is not working as intendet and we decided to use pylsp instead.
LSPs, who exit early, trigger a lot of errors, because they do not send any headers.
This patch fixes the access of undefined headers to prevent runtime exceptions.

Furthermore, it adds a timeout to not overwhelm the garbage collection sweeping phase of GTK and adds debug output for non-JSON LSP responses.
To not slow down the diagnoses,
the LSPClient, should only wait for GC, if it doesn't do any message processing.
As decided, this commit changes the executed LSP to pyslp and adds the missing code to it up.
Furthermore, it adds a missing return in the LSP's setup function, which doesn't seem to have triggered any issues, yet.

Although, a locally installed version of pylsp, initializes, it doesn't yet work, because it quit's after sending it's firsts message.
This change packages pylsp as a flatpak module, so it can be included in the flatpak.
I just forgot this before.
Run eslint on python.js and fix formatting
Copy link
Contributor

@sonnyp sonnyp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great start thanks!

I rebased and pushed - makes sure to pull my changes.

I'm observing the same result as you - no diagnostics.
From the logs it looks like modules are missing and that is why the plugins are disabled ?

2024-02-13 21:32:51,864 CET - INFO - pylsp.python_lsp - Starting PythonLSPServer IO language server
2024-02-13 21:32:51,877 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'autopep8': No module named 'pycodestyle'
2024-02-13 21:32:51,888 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'mccabe': No module named 'mccabe'
2024-02-13 21:32:51,889 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'pycodestyle': No module named 'pycodestyle'
2024-02-13 21:32:51,890 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'pydocstyle': No module named 'pydocstyle'
2024-02-13 21:32:51,891 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'pyflakes': No module named 'pyflakes'
2024-02-13 21:32:51,893 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'rope_autoimport': No module named 'rope'
2024-02-13 21:32:51,894 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'rope_completion': No module named 'rope'
2024-02-13 21:32:51,895 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'rope_rename': No module named 'rope'
2024-02-13 21:32:51,895 CET - INFO - pylsp.config.config - Failed to load pylsp entry point 'yapf': No module named 'yapf'
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin flake8 from <module 'pylsp.plugins.flake8_lint' from '/app/lib/python3.11/site-packages/pylsp/plugins/flake8_lint.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin folding from <module 'pylsp.plugins.folding' from '/app/lib/python3.11/site-packages/pylsp/plugins/folding.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin jedi_completion from <module 'pylsp.plugins.jedi_completion' from '/app/lib/python3.11/site-packages/pylsp/plugins/jedi_completion.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin jedi_definition from <module 'pylsp.plugins.definition' from '/app/lib/python3.11/site-packages/pylsp/plugins/definition.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin jedi_highlight from <module 'pylsp.plugins.highlight' from '/app/lib/python3.11/site-packages/pylsp/plugins/highlight.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin jedi_hover from <module 'pylsp.plugins.hover' from '/app/lib/python3.11/site-packages/pylsp/plugins/hover.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin jedi_references from <module 'pylsp.plugins.references' from '/app/lib/python3.11/site-packages/pylsp/plugins/references.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin jedi_rename from <module 'pylsp.plugins.jedi_rename' from '/app/lib/python3.11/site-packages/pylsp/plugins/jedi_rename.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin jedi_signature_help from <module 'pylsp.plugins.signature' from '/app/lib/python3.11/site-packages/pylsp/plugins/signature.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin jedi_symbols from <module 'pylsp.plugins.symbols' from '/app/lib/python3.11/site-packages/pylsp/plugins/symbols.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin preload from <module 'pylsp.plugins.preload_imports' from '/app/lib/python3.11/site-packages/pylsp/plugins/preload_imports.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Loaded pylsp plugin pylint from <module 'pylsp.plugins.pylint_lint' from '/app/lib/python3.11/site-packages/pylsp/plugins/pylint_lint.py'>
2024-02-13 21:32:51,898 CET - INFO - pylsp.config.config - Disabled plugins: [<module 'pylsp.plugins.flake8_lint' from '/app/lib/python3.11/site-packages/pylsp/plugins/flake8_lint.py'>, <module 'pylsp.plugins.pylint_lint' from '/app/lib/python3.11/site-packages/pylsp/plugins/pylint_lint.py'>]
2024-02-13 21:32:51,902 CET - INFO - pylsp.python_lsp - Server capabilities: {'codeActionProvider': True, 'codeLensProvider': {'resolveProvider': False}, 'completionProvider': {'resolveProvider': True, 'triggerCharacters': ['.']}, 'documentFormattingProvider': True, 'documentHighlightProvider': True, 'documentRangeFormattingProvider': True, 'documentSymbolProvider': True, 'definitionProvider': True, 'executeCommandProvider': {'commands': []}, 'hoverProvider': True, 'referencesProvider': True, 'renameProvider': True, 'foldingRangeProvider': True, 'signatureHelpProvider': {'triggerCharacters': ['(', ',', '=']}, 'textDocumentSync': {'change': 2, 'save': {'includeText': True}, 'openClose': True}, 'notebookDocumentSync': {'notebookSelector': [{'cells': [{'language': 'python'}]}]}, 'workspace': {'workspaceFolders': {'supported': True, 'changeNotifications': True}}, 'experimental': {}}

@theCapypara
Copy link
Contributor

theCapypara commented Mar 3, 2024

Which capabilities do we want?

From the README:

Rope for Completions and renaming
Pyflakes linter to detect various errors
McCabe linter for complexity checking
pycodestyle linter for style checking
pydocstyle linter for docstring style checking (disabled by default)
autopep8 for code formatting
YAPF for code formatting (preferred over autopep8)
flake8 for error checking (disabled by default)
pylint for code linting (disabled by default)

My concern is that this seems to use other tools for code formatting than we currently use.
We considered using Ruff in #804, Ruff has it's own LSP:
https://github.com/astral-sh/ruff-lsp

I think the idea is to combine it with python-lsp:

ruff-lsp supports surfacing Ruff diagnostics and providing Code Actions to fix them, but is intended to be used alongside another Python LSP in order to support features like navigation and autocompletion.

So should we for now only install Rope for completions and add Ruff to replace Black in a new PR where we would also add ruff-lsp?
Is it even possible to run multiple LSPs on the same code?

@theCapypara
Copy link
Contributor

I guess Workbench currently doesn't use completions. I'll still add Rope in case we want that in the future.
For now I'll also add Pyflakes for error checking, but we may want to replace that part with Ruff later.

@theCapypara theCapypara marked this pull request as ready for review March 3, 2024 17:17
@theCapypara theCapypara self-requested a review as a code owner March 3, 2024 17:17
Copy link
Contributor

@theCapypara theCapypara left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me this looks good for now! See my comments above for questions on how to extend this.

@theCapypara
Copy link
Contributor

Since I had time, I started creating a possible follow-up PR that would add Ruff: #902

@theCapypara
Copy link
Contributor

I removed rope again for now, since we don't support completions anyway yet.

Copy link
Contributor

@sonnyp sonnyp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome team work!

@janvhs thanks for doing the heavy lifting, feel free to add yourself to contributors in about.js

const lspc = createLSPClient({
lang: getLanguage("python"),
root_uri: file.get_parent().get_uri(),
quiet: false,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
quiet: false,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theCapypara if you debug while working on LSP, make sure to use this flag to see stdout

@sonnyp sonnyp changed the title Add pylsp as the python LSP. Add pylsp as the python LSP Mar 3, 2024
@sonnyp sonnyp merged commit eb3a1e0 into workbenchdev:main Mar 3, 2024
1 check passed
@janvhs
Copy link
Contributor Author

janvhs commented Mar 4, 2024

@theCapypara thanks for moving it forward! ^^

@sonnyp I'm glad to do so :)

sonnyp pushed a commit that referenced this pull request Mar 4, 2024
This commit adds my name to the contributor list for #857 

Thanks so much for the patience and help I received from you @sonnyp
@theCapypara
Hofer-Julian pushed a commit that referenced this pull request Mar 24, 2024
Co-authored-by: Sonny Piers <[email protected]>
Co-authored-by: Marco Capypara Köpcke <[email protected]>
Hofer-Julian pushed a commit that referenced this pull request Mar 24, 2024
This commit adds my name to the contributor list for #857 

Thanks so much for the patience and help I received from you @sonnyp
@theCapypara
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.

3 participants