Skip to content

Commit e79b06a

Browse files
sonnypHofer-Julian
authored andcommitted
cli: Add proper Python support (#903)
1 parent 07ab7ca commit e79b06a

File tree

9 files changed

+84
-25
lines changed

9 files changed

+84
-25
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ test: unit lint
5353
./build-aux/fun workbench-cli ci demos/demos/Welcome
5454

5555
ci: setup test
56-
./build-aux/fun workbench-cli ci demos/demos/**
56+
./build-aux/fun workbench-cli ci demos/demos/*
5757

5858
# Note that if you have Sdk extensions installed they will be used
5959
# make sure to test without the sdk extensions installed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"*.{json,md,yaml,yml}": "prettier --write",
2828
"*.{js,cjs,mjs}": "eslint --fix",
2929
"*.css": "./build-aux/fun workbench-cli format css",
30-
"*.py": "./build-aux/fun ruff format --config=src/langs/python/ruff.toml",
30+
"*.py": "./build-aux/fun workbench-cli format python",
3131
"*.rs": "./build-aux/fun rustfmt --edition 2021",
3232
"*.blp": "./build-aux/fun workbench-cli format blueprint",
3333
"*.vala": "./build-aux/fun workbench-cli format vala"

src/Previewer/Internal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export default function Internal({
219219
}
220220

221221
css_provider = new Gtk.CssProvider();
222-
css_provider.load_from_data(style, -1);
222+
css_provider.load_from_string(style);
223223
Gtk.StyleContext.add_provider_for_display(
224224
output.get_display(),
225225
css_provider,

src/Previewer/previewer.vala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ namespace Workbench {
118118
var end = section.get_end_location ();
119119
this.css_parser_error (error.message, (int) start.lines, (int) start.line_chars, (int) end.lines, (int) end.line_chars);
120120
});
121-
this.css.load_from_data (content.data);
121+
this.css.load_from_string (content);
122122
Gtk.StyleContext.add_provider_for_display (
123123
Gdk.Display.get_default (),
124124
this.css,

src/cli/main.js

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import Gio from "gi://Gio";
88
import Gtk from "gi://Gtk";
99
import Adw from "gi://Adw";
1010

11-
import { createLSPClient, languages, getLanguage } from "../common.js";
11+
import {
12+
createLSPClient,
13+
languages,
14+
getLanguage,
15+
PYTHON_LSP_CONFIG,
16+
} from "../common.js";
1217
import lint, { waitForDiagnostics } from "./lint.js";
1318
import format, { formatting } from "./format.js";
1419

@@ -51,6 +56,12 @@ export async function main([action, ...args]) {
5156
lspc._start_process();
5257
await lspc._initialize();
5358

59+
if (lang.id === "python") {
60+
await lspc._request("workspace/didChangeConfiguration", {
61+
settings: PYTHON_LSP_CONFIG,
62+
});
63+
}
64+
5465
let success = false;
5566

5667
if (action === "lint") {
@@ -78,7 +89,7 @@ const window = new Adw.ApplicationWindow();
7889

7990
function createLSPClients({ root_uri }) {
8091
return Object.fromEntries(
81-
["javascript", "blueprint", "css", "vala", "rust"].map((id) => {
92+
["javascript", "blueprint", "css", "vala", "rust", "python"].map((id) => {
8293
const lang = languages.find((language) => language.id === id);
8394
const lspc = createLSPClient({
8495
lang,
@@ -363,7 +374,6 @@ async function ci({ filenames, current_dir }) {
363374
uri,
364375
lspc: lsp_clients.vala,
365376
});
366-
367377
// FIXME: deprecated features, no replacement?
368378
if (demo_dir.get_basename() === "Text Fields") {
369379
const ignore_for_text_fields = [
@@ -397,6 +407,55 @@ async function ci({ filenames, current_dir }) {
397407
});
398408
}
399409

410+
const file_python = demo_dir.get_child("main.py");
411+
if (file_python.query_exists(null)) {
412+
print(` ${file_python.get_path()}`);
413+
414+
const uri = file_python.get_uri();
415+
const languageId = "python";
416+
let version = 0;
417+
418+
const [contents] = await file_python.load_contents_async(null);
419+
const text = new TextDecoder().decode(contents);
420+
421+
await lsp_clients.python._request("workspace/didChangeConfiguration", {
422+
settings: PYTHON_LSP_CONFIG,
423+
});
424+
425+
await lsp_clients.python._notify("textDocument/didOpen", {
426+
textDocument: {
427+
uri,
428+
languageId,
429+
version: version++,
430+
text,
431+
},
432+
});
433+
434+
const diagnostics = await waitForDiagnostics({
435+
uri,
436+
lspc: lsp_clients.python,
437+
});
438+
if (diagnostics.length > 0) {
439+
printerr(serializeDiagnostics({ diagnostics }));
440+
return false;
441+
}
442+
print(` ✅ lints`);
443+
444+
const checks = await checkFile({
445+
lspc: lsp_clients.python,
446+
file: file_python,
447+
lang: getLanguage("python"),
448+
uri,
449+
});
450+
if (!checks) return false;
451+
452+
await lsp_clients.python._notify("textDocument/didClose", {
453+
textDocument: {
454+
uri,
455+
},
456+
});
457+
}
458+
400459
const file_rust = demo_dir.get_child("code.rs");
401460
if (file_rust.query_exists(null)) {
402461
print(` ${file_rust.get_path()}`);

src/common.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,17 @@ export function createLSPClient({ lang, root_uri, quiet = true }) {
147147

148148
return lspc;
149149
}
150+
151+
export const PYTHON_LSP_CONFIG = {
152+
pylsp: {
153+
configurationSources: ["ruff"],
154+
plugins: {
155+
ruff: {
156+
enabled: true,
157+
formatEnabled: true,
158+
executable: `${pkg.prefix}/bin/ruff`,
159+
config: `${pkg.pkgdatadir}/ruff.toml`,
160+
},
161+
},
162+
},
163+
};

src/langs/python/python-previewer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def update_css(self, content: str):
137137
)
138138
self.css = Gtk.CssProvider()
139139
self.css.connect("parsing-error", self.on_css_parsing_error)
140-
self.css.load_from_data(content, len(content))
140+
self.css.load_from_string(content)
141141
Gtk.StyleContext.add_provider_for_display(
142142
Gdk.Display.get_default(), self.css, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
143143
)

src/langs/python/python.js

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
import { createLSPClient } from "../../common.js";
1+
import { PYTHON_LSP_CONFIG, createLSPClient } from "../../common.js";
22
import { getLanguage } from "../../util.js";
33

4-
const LSP_CONFIG = {
5-
pylsp: {
6-
configurationSources: ["ruff"],
7-
plugins: {
8-
ruff: {
9-
enabled: true,
10-
formatEnabled: true,
11-
executable: `${pkg.prefix}/bin/ruff`,
12-
config: `${pkg.pkgdatadir}/ruff.toml`,
13-
},
14-
},
15-
},
16-
};
17-
184
export function setup({ document }) {
195
const { file, buffer, code_view } = document;
206

@@ -24,7 +10,7 @@ export function setup({ document }) {
2410
});
2511

2612
lspc.request("workspace/didChangeConfiguration", {
27-
settings: LSP_CONFIG,
13+
settings: PYTHON_LSP_CONFIG,
2814
});
2915

3016
lspc.buffer = buffer;

0 commit comments

Comments
 (0)