Skip to content

Commit

Permalink
Add pylsp as the python LSP (#857)
Browse files Browse the repository at this point in the history
Co-authored-by: Sonny Piers <[email protected]>
Co-authored-by: Marco Capypara Köpcke <[email protected]>
  • Loading branch information
3 people authored and Hofer-Julian committed Mar 24, 2024
1 parent 74c3204 commit b879513
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 7 deletions.
18 changes: 11 additions & 7 deletions build-aux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,28 @@ cd gst-plugin-gtk4-0.11.1/
# cp generated-sources.json to gst-plugin-gtk4-sources.json
```

### Python Black Dependency
### Python Modules

`modules/python-black.json` contains the Flatpak modules to install [https://github.com/psf/black](black), the
uncompromising Python code formatter.
The `modules/python-*.json` files contain Flatpak modules to install Python dependencies.

This file is partially auto-generated. Here is how to generate it:
These files are (sometimes partially, see below) auto-generated. Here is how to generate it:

1. Obtain an up-to-date copy of [flatpak-builder-tools](https://github.com/flatpak/flatpak-builder-tools).
2. Make sure you have a Python virtualenv activated with the dependencies for the `pip` generator from the repo above.
3. Run `python3 <...>/flatpak-builder-tools/pip/flatpak-pip-generator black -o modules/python-black --build-isolation`
3. Run `python3 <...>/flatpak-builder-tools/pip/flatpak-pip-generator <package> -o modules/python-<package> --build-isolation`

You will notice that Workbench will now not build. This is due to these issues:
You will notice that Workbench will not build after auto-generating these files as described above.
This is due to these issues:

- https://github.com/flatpak/flatpak-builder-tools/issues/380
- https://github.com/pypa/pip/issues/7863

This means that the generated JSON file now needs its build dependencies manually added. Check the build dependencies
of black and their dependencies and add them to the JSON as well. Brute-forcing the build to obtain missing packages
of the package and their dependencies and add them to the JSON as well. Brute-forcing the build to obtain missing packages
may help as does referencing old commits of the file. You can also use the generator command to generate dependencies
for them and then merge it into the file by hand, but note that you will also need to manually collect their build
dependencies too.

In some cases you may also need to manually remove some packages the generator adds (such as `packaging` with `rope`,
as the SDK and Runtime already contain a version of these packages that can not be replaced. Make sure the package
you want to install is actually compatible with the version of that dependency in the SDK/Runtime.
14 changes: 14 additions & 0 deletions build-aux/modules/python-pyflakes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "python3-pyflakes",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"pyflakes\""
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/d4/d7/f1b7db88d8e4417c5d47adad627a93547f44bdc9028372dbd2313f34a855/pyflakes-3.2.0-py2.py3-none-any.whl",
"sha256": "84b5be138a2dfbb40689ca07e2152deb896a65c3a3e24c251c5c62489568074a"
}
]
}
69 changes: 69 additions & 0 deletions build-aux/modules/python-python-lsp-server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "python3-python-lsp-server",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"python-lsp-server\""
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/be/4f/1b6eb5a6d92ae52f3798e9fdffd225f1bcd017172bb732743315390a9025/docstring_to_markdown-0.13-py3-none-any.whl",
"sha256": "aa487059d0883e70e54da25c7b230e918d9e4d40f23d6dfaa2b73e4225b2d7dd"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/20/9f/bc63f0f0737ad7a60800bfd472a4836661adae21f9c2535f3957b1e54ceb/jedi-0.19.1-py2.py3-none-any.whl",
"sha256": "e983c654fe5c02867aef4cdfce5a2fbb4a50adc0af145f70504238f18ef5e7e0"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/05/63/8011bd08a4111858f79d2b09aad86638490d62fbf881c44e434a6dfca87b/parso-0.8.3-py2.py3-none-any.whl",
"sha256": "c001d4636cd3aecdaf33cbb40aebb59b094be2a74c556778ef5576c175e19e75"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/05/b8/42ed91898d4784546c5f06c60506400548db3f7a4b3fb441cba4e5c17952/pluggy-1.3.0-py3-none-any.whl",
"sha256": "d89c696a773f8bd377d18e5ecda92b7a3793cbe66c87060a6fb58c7b6e1061f7"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/cb/d9/656659d5b5d5f402b2b174cd0ba9bc827e07ce3c0bf88da65424baf64af8/python_lsp_jsonrpc-1.1.2-py3-none-any.whl",
"sha256": "7339c2e9630ae98903fdaea1ace8c47fba0484983794d6aafd0bd8989be2b03c"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/83/49/355783384945708c604f4ad4c773d4ec93bc609b691698e5d8ecfc51d01c/python_lsp_server-1.9.0-py3-none-any.whl",
"sha256": "6b947cf9dc33d7bed9abc936bb173140fcf606b6eb50cf02e27d4cb09f10d3fb"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/6e/54/6f2bdac7117e89a47de4511c9f01732a283457ab1bf856e1e51aa861619e/ujson-5.9.0.tar.gz",
"sha256": "89cc92e73d5501b8a7f48575eeb14ad27156ad092c2e9fc7e3cf949f07e75532"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/ec/1a/610693ac4ee14fcdf2d9bf3c493370e4f2ef7ae2e19217d7a237ff42367d/packaging-23.2-py3-none-any.whl",
"sha256": "8c491190033a9af7e1d931d0b5dacc2ef47509b34dd0de67ed209b5203fc88c7"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/0e/a3/b9a8b0adfe672bf0df5901707aa929d30a97ee390ba651910186776746d2/setuptools_scm-8.0.4-py3-none-any.whl",
"sha256": "b47844cd2a84b83b3187a5782c71128c28b4c94cad8bfb871da2784a5cb54c4f"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/b7/f4/6a90020cd2d93349b442bfcb657d0dc91eee65491600b2cb1d388bc98e6b/typing_extensions-4.9.0-py3-none-any.whl",
"sha256": "af72aea155e91adfc61c3ae9e0e342dbc0cba726d6cba4b6c72c1f34e47291cd"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/bb/e1/ed2dd0850446b8697ad28d118df885ad04140c64ace06c4bd559f7c8a94f/setuptools-69.0.2-py3-none-any.whl",
"sha256": "1e8fdff6797d3865f37397be788a4e3cba233608e9b509382a2777d25ebde7f2"
},
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/c7/c3/55076fc728723ef927521abaa1955213d094933dc36d4a2008d5101e1af5/wheel-0.42.0-py3-none-any.whl",
"sha256": "177f9c9b0d45c47873b619f5b650346d632cdc35fb5e4d25058e09c9e581433d"
}
]
}
2 changes: 2 additions & 0 deletions build-aux/re.sonny.Workbench.Devel.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"modules/libspelling.json",
"modules/GTKCssLanguageServer.json",
"modules/icon-development-kit.json",
"modules/python-pyflakes.json",
"modules/python-python-lsp-server.json",
{
"name": "Workbench",
"buildsystem": "meson",
Expand Down
2 changes: 2 additions & 0 deletions build-aux/re.sonny.Workbench.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
"modules/libspelling.json",
"modules/GTKCssLanguageServer.json",
"modules/icon-development-kit.json",
"modules/python-pyflakes.json",
"modules/python-python-lsp-server.json",
{
"name": "Workbench",
"buildsystem": "meson",
Expand Down
5 changes: 5 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export const languages = [
document: null,
default_file: "main.py",
index: 3,
language_server: ["pylsp", "-v"],
formatting_options: {
...formatting_options,
tabSize: 4,
},
},
];

Expand Down
7 changes: 7 additions & 0 deletions src/langs/python/PythonDocument.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import Gio from "gi://Gio";

import Document from "../../Document.js";
import { setup } from "./python.js";

export class PythonDocument extends Document {
constructor(...args) {
super(...args);

this.lspc = setup({ document: this });
}

async format() {
const code = await formatPythonCode(this.buffer.text);
this.code_view.replaceText(code, true);
Expand Down
32 changes: 32 additions & 0 deletions src/langs/python/python.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createLSPClient } from "../../common.js";
import { getLanguage } from "../../util.js";

export function setup({ document }) {
const { file, buffer, code_view } = document;

const lspc = createLSPClient({
lang: getLanguage("python"),
root_uri: file.get_parent().get_uri(),
quiet: false,
});
lspc.buffer = buffer;
lspc.uri = file.get_uri();
lspc.connect(
"notification::textDocument/publishDiagnostics",
(_self, params) => {
if (params.uri !== file.get_uri()) {
return;
}
code_view.handleDiagnostics(params.diagnostics);
},
);

lspc.start().catch(console.error);

buffer.connect("modified-changed", () => {
if (!buffer.get_modified()) return;
lspc.didChange().catch(console.error);
});

return lspc;
}

0 comments on commit b879513

Please sign in to comment.