Skip to content

Commit 62db149

Browse files
committed
Implement Ruff LSP
1 parent 5868709 commit 62db149

File tree

8 files changed

+119
-73
lines changed

8 files changed

+119
-73
lines changed

build-aux/modules/python-pyflakes.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

build-aux/re.sonny.Workbench.Devel.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"modules/GTKCssLanguageServer.json",
4545
"modules/icon-development-kit.json",
4646
"modules/python-rope.json",
47-
"modules/python-pyflakes.json",
4847
"modules/python-python-lsp-server.json",
4948
"modules/python-ruff.json",
5049
"modules/python-ruff-lsp.json",

build-aux/re.sonny.Workbench.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
"modules/GTKCssLanguageServer.json",
4545
"modules/icon-development-kit.json",
4646
"modules/python-rope.json",
47-
"modules/python-pyflakes.json",
4847
"modules/python-python-lsp-server.json",
4948
"modules/python-ruff.json",
5049
"modules/python-ruff-lsp.json",

meson.build

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ project('Workbench', ['vala', 'c', 'rust'],
77
gnome = import('gnome')
88

99
if get_option('profile') == 'development'
10-
app_id = 're.sonny.Workbench.Devel'
10+
app_id = 're.sonny.Workbench.Devel'
1111
vcs_tag = run_command('git', 'rev-parse', '--short', 'HEAD').stdout().strip()
1212
if vcs_tag == ''
1313
version_suffix = '-devel'
@@ -27,6 +27,9 @@ pkgdatadir = join_paths(datadir, app_id)
2727
subdir('data')
2828
subdir('src')
2929

30+
# Needed for the Ruff Python LSP
31+
install_data('ruff.toml', install_dir: pkgdatadir)
32+
3033
gnome.post_install(
3134
glib_compile_schemas: true,
3235
gtk_update_icon_cache: true,

src/common.js

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const languages = [
1919
types: [],
2020
document: null,
2121
default_file: "main.blp",
22-
language_server: ["blueprint-compiler", "lsp"],
22+
language_server: [["blueprint-compiler", "lsp"]],
2323
formatting_options: {
2424
...formatting_options,
2525
tabSize: 2,
@@ -44,12 +44,14 @@ export const languages = [
4444
default_file: "main.js",
4545
index: 0,
4646
language_server: [
47-
"biome",
48-
"lsp-proxy",
49-
// src/meson.build installs biome.json there
50-
GLib.getenv("FLATPAK_ID")
51-
? `--config-path=${pkg.pkgdatadir}`
52-
: `--config-path=src/langs/javascript`,
47+
[
48+
"biome",
49+
"lsp-proxy",
50+
// src/meson.build installs biome.json there
51+
GLib.getenv("FLATPAK_ID")
52+
? `--config-path=${pkg.pkgdatadir}`
53+
: `--config-path=src/langs/javascript`,
54+
],
5355
],
5456
formatting_options: {
5557
...formatting_options,
@@ -64,7 +66,7 @@ export const languages = [
6466
types: ["text/css"],
6567
document: null,
6668
default_file: "main.css",
67-
language_server: ["gtkcsslanguageserver"],
69+
language_server: [["gtkcsslanguageserver"]],
6870
formatting_options: {
6971
...formatting_options,
7072
tabSize: 2,
@@ -79,7 +81,7 @@ export const languages = [
7981
document: null,
8082
default_file: "main.vala",
8183
index: 1,
82-
language_server: ["vala-language-server"],
84+
language_server: [["vala-language-server"]],
8385
formatting_options: {
8486
...formatting_options,
8587
tabSize: 4,
@@ -94,7 +96,7 @@ export const languages = [
9496
document: null,
9597
default_file: "code.rs",
9698
index: 2,
97-
language_server: ["rust-analyzer"],
99+
language_server: [["rust-analyzer"]],
98100
formatting_options: {
99101
...formatting_options,
100102
tabSize: 4,
@@ -109,7 +111,17 @@ export const languages = [
109111
document: null,
110112
default_file: "main.py",
111113
index: 3,
112-
language_server: ["pylsp", "-v"],
114+
language_server: [
115+
["pylsp", "-v"],
116+
{
117+
argv: ["ruff-lsp"],
118+
initializationOptions: {
119+
settings: {
120+
args: ["--config", `${pkg.pkgdatadir}/ruff.toml`],
121+
},
122+
},
123+
},
124+
],
113125
formatting_options: {
114126
...formatting_options,
115127
tabSize: 4,
@@ -123,27 +135,52 @@ export function getLanguage(id) {
123135
);
124136
}
125137

138+
/**
139+
* Returns a LSPClient for the given langauge.
140+
* If multiple are defined, returns an array of LSPClients instead.
141+
*/
126142
export function createLSPClient({ lang, root_uri, quiet = true }) {
127143
const language_id = lang.id;
128144

129-
const lspc = new LSPClient(lang.language_server, {
130-
rootUri: root_uri,
131-
languageId: language_id,
132-
quiet,
133-
});
134-
lspc.connect("exit", () => {
135-
console.debug(`${language_id} language server exit`);
136-
});
137-
lspc.connect("output", (_self, message) => {
138-
console.debug(
139-
`${language_id} language server OUT:\n${JSON.stringify(message)}`,
140-
);
141-
});
142-
lspc.connect("input", (_self, message) => {
143-
console.debug(
144-
`${language_id} language server IN:\n${JSON.stringify(message)}`,
145-
);
146-
});
145+
const lspcs = [];
146+
147+
for (const language_server of lang.language_server) {
148+
// language_server is either an array (then it's just argv) or an object with more info.
149+
let argv,
150+
initializationOptions = undefined;
151+
152+
if (Array.isArray(language_server)) {
153+
argv = language_server;
154+
} else {
155+
argv = language_server.argv;
156+
initializationOptions = language_server.initializationOptions;
157+
}
158+
159+
const lspc = new LSPClient(argv, {
160+
rootUri: root_uri,
161+
languageId: language_id,
162+
quiet,
163+
initializationOptions,
164+
});
165+
lspc.connect("exit", () => {
166+
console.debug(`${language_id} language server exit`);
167+
});
168+
lspc.connect("output", (_self, message) => {
169+
console.debug(
170+
`${language_id} language server OUT:\n${JSON.stringify(message)}`,
171+
);
172+
});
173+
lspc.connect("input", (_self, message) => {
174+
console.debug(
175+
`${language_id} language server IN:\n${JSON.stringify(message)}`,
176+
);
177+
});
178+
lspcs.push(lspc);
179+
}
147180

148-
return lspc;
181+
// If the language has only one language server, return it
182+
if (lspcs.length === 1) {
183+
return lspcs[0];
184+
}
185+
return lspcs;
149186
}

src/langs/python/PythonDocument.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class PythonDocument extends Document {
77
constructor(...args) {
88
super(...args);
99

10-
this.lspc = setup({ document: this });
10+
this.lspcs = setup({ document: this });
1111
}
1212

1313
async format() {
@@ -17,18 +17,18 @@ export class PythonDocument extends Document {
1717
}
1818

1919
function formatPythonCode(text) {
20-
const blackLauncher = Gio.SubprocessLauncher.new(
20+
const ruffLauncher = Gio.SubprocessLauncher.new(
2121
Gio.SubprocessFlags.STDIN_PIPE |
2222
Gio.SubprocessFlags.STDOUT_PIPE |
2323
Gio.SubprocessFlags.STDERR_PIPE,
2424
);
2525

26-
const blackProcess = blackLauncher.spawnv(["black", "--quiet", "-"]);
26+
const ruffProcess = ruffLauncher.spawnv(["ruff", "format", "--quiet", "-"]);
2727

28-
const [success, stdout, stderr] = blackProcess.communicate_utf8(text, null);
28+
const [success, stdout, stderr] = ruffProcess.communicate_utf8(text, null);
2929

3030
if (!success || stderr !== "") {
31-
console.error(`Error running black: ${stderr}`);
31+
console.error(`Error running ruff format: ${stderr}`);
3232
return text;
3333
}
3434

src/langs/python/python.js

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,41 @@ import { getLanguage } from "../../util.js";
44
export function setup({ document }) {
55
const { file, buffer, code_view } = document;
66

7-
const lspc = createLSPClient({
7+
const lspcs = createLSPClient({
88
lang: getLanguage("python"),
99
root_uri: file.get_parent().get_uri(),
1010
quiet: false,
1111
});
12-
lspc.buffer = buffer;
13-
lspc.uri = file.get_uri();
14-
lspc.connect(
15-
"notification::textDocument/publishDiagnostics",
16-
(_self, params) => {
17-
if (params.uri !== file.get_uri()) {
18-
return;
19-
}
20-
code_view.handleDiagnostics(params.diagnostics);
21-
},
22-
);
23-
24-
lspc.start().catch(console.error);
25-
26-
buffer.connect("modified-changed", () => {
27-
if (!buffer.get_modified()) return;
28-
lspc.didChange().catch(console.error);
29-
});
3012

31-
return lspc;
13+
const combinedDiagnostics = {};
14+
15+
for (const lspc of lspcs) {
16+
lspc.buffer = buffer;
17+
lspc.uri = file.get_uri();
18+
lspc.connect(
19+
"notification::textDocument/publishDiagnostics",
20+
(self, params) => {
21+
if (params.uri !== file.get_uri()) {
22+
return;
23+
}
24+
combinedDiagnostics[self.argv[0]] = params.diagnostics;
25+
26+
let totalDiagnostics = [];
27+
for (const d of Object.values(combinedDiagnostics)) {
28+
totalDiagnostics = totalDiagnostics.concat(...d);
29+
}
30+
31+
code_view.handleDiagnostics(totalDiagnostics);
32+
},
33+
);
34+
35+
lspc.start().catch(console.error);
36+
37+
buffer.connect("modified-changed", () => {
38+
if (!buffer.get_modified()) return;
39+
lspc.didChange().catch(console.error);
40+
});
41+
}
42+
43+
return lspcs;
3244
}

src/lsp/LSPClient.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@ const clientInfo = {
2222
export default class LSPClient {
2323
constructor(
2424
argv,
25-
{ rootUri, uri, languageId, buffer, env = {}, quiet = true },
25+
{
26+
rootUri,
27+
uri,
28+
languageId,
29+
buffer,
30+
env = {},
31+
quiet = true,
32+
initializationOptions = undefined,
33+
},
2634
) {
2735
this.ready = false;
2836
this.started = false;
@@ -36,6 +44,7 @@ export default class LSPClient {
3644
this.buffer = buffer;
3745
this.env = env;
3846
this.quiet = quiet;
47+
this.initializationOptions = initializationOptions;
3948

4049
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#clientCapabilities
4150
this.capabilities = {
@@ -75,6 +84,7 @@ export default class LSPClient {
7584
capabilities,
7685
rootUri,
7786
locale: "en",
87+
initializationOptions: this.initializationOptions,
7888
});
7989

8090
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialized

0 commit comments

Comments
 (0)